Create a gist now

Instantly share code, notes, and snippets.

ES2017/ES2015/ES5 examples after expansion via babel
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// ES2017
var StreamFile = function () {
function StreamFile() {
_classCallCheck(this, StreamFile);
}
_createClass(StreamFile, [{
key: 'buffer',
/**
* Wait until the given number of bytes are available to read, or end of file.
* @param {number} nbytes - max bytes to wait for
* @param {Object?} cancelToken - optional cancellation token
*/
value: function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(nbytes, cancelToken) {
var end, readable, remainder;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (this.loaded) {
_context.next = 4;
break;
}
throw new Error('cannot buffer when not loaded');
case 4:
if (!this.buffering) {
_context.next = 8;
break;
}
throw new Error('cannot buffer while buffering');
case 8:
if (!this.seeking) {
_context.next = 12;
break;
}
throw new Error('cannot buffer while seeking');
case 12:
if (!(nbytes !== (nbytes | 0))) {
_context.next = 16;
break;
}
throw new Error('read with invalid byte count');
case 16:
if (!(nbytes <= 0)) {
_context.next = 18;
break;
}
throw new Error('read with negative or 0 bytes');
case 18:
end = this._clampToLength(this.offset + nbytes);
readable = end - this.offset;
if (!(this.bytesAvailable(readable) >= readable)) {
_context.next = 23;
break;
}
_context.next = 34;
break;
case 23:
this.buffering = true;
_context.prev = 24;
_context.next = 27;
return this._triggerDownload(cancelToken);
case 27:
remainder = end - this._cache.writeOffset;
if (!(remainder > 0)) {
_context.next = 31;
break;
}
_context.next = 31;
return this._backend.buffer(remainder, cancelToken);
case 31:
_context.prev = 31;
this.buffering = false;
return _context.finish(31);
case 34:
case 'end':
return _context.stop();
}
}
}, _callee, this, [[24,, 31, 34]]);
}));
function buffer(_x, _x2) {
return _ref.apply(this, arguments);
}
return buffer;
}()
}]);
return StreamFile;
}();
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// ES2015
var StreamFile = function () {
function StreamFile() {
_classCallCheck(this, StreamFile);
}
_createClass(StreamFile, [{
key: 'buffer',
/**
* Wait until the given number of bytes are available to read, or end of file.
* @param {number} nbytes - max bytes to wait for
* @param {Object?} cancelToken - optional cancellation token
*/
value: function buffer(nbytes, cancelToken) {
var _this = this;
return new Promise(function (resolve, reject) {
if (!_this.loaded) {
throw new Error('cannot buffer when not loaded');
} else if (_this.buffering) {
throw new Error('cannot buffer while buffering');
} else if (_this.seeking) {
throw new Error('cannot buffer while seeking');
} else if (nbytes !== (nbytes | 0)) {
throw new Error('read with invalid byte count');
} else if (nbytes <= 0) {
throw new Error('read with negative or 0 bytes');
}
var end = _this._clampToLength(_this.offset + nbytes);
var readable = end - _this.offset;
if (_this.bytesAvailable(readable) >= readable) {
// Requested data is immediately available.
} else {
_this.buffering = true;
// If we don't already have a backend open, start downloading.
_this._triggerDownload(cancelToken).then(function () {
var remainder = end - _this._cache.writeOffset;
if (remainder > 0) {
return _this._backend.buffer(remainder, cancelToken);
}
}).then(function () {
_this.buffering = false;
resolve();
}).catch(function (err) {
_this.buffering = false;
reject(err);
});
}
});
}
}]);
return StreamFile;
}();
'use strict';
// ES5 with polyfill for Promise
function StreamFile() {}
/**
* Wait until the given number of bytes are available to read, or end of file.
* @param {number} nbytes - max bytes to wait for
* @param {Object?} cancelToken - optional cancellation token
*/
StreamFile.prototype.buffer = function (nbytes, cancelToken) {
var self = this;
return new Promise(function (resolve, reject) {
if (!self.loaded) {
throw new Error('cannot buffer when not loaded');
} else if (self.buffering) {
throw new Error('cannot buffer while buffering');
} else if (self.seeking) {
throw new Error('cannot buffer while seeking');
} else if (nbytes !== (nbytes | 0)) {
throw new Error('read with invalid byte count');
} else if (nbytes <= 0) {
throw new Error('read with negative or 0 bytes');
}
var end = self._clampToLength(self.offset + nbytes);
var readable = end - self.offset;
if (self.bytesAvailable(readable) >= readable) {
// Requested data is immediately available.
} else {
self.buffering = true;
// If we don't already have a backend open, start downloading.
self._triggerDownload(cancelToken).then(function () {
var remainder = end - self._cache.writeOffset;
if (remainder > 0) {
return self._backend.buffer(remainder, cancelToken);
}
}).then(function () {
self.buffering = false;
resolve();
}).catch(function (err) {
self.buffering = false;
reject(err);
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment