@loaders.gl/loader-utils 3.1.0-beta.6 → 3.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es5/index.js +48 -46
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/json-loader.js +33 -5
- package/dist/es5/json-loader.js.map +1 -1
- package/dist/es5/lib/binary-utils/array-buffer-utils.js +58 -23
- package/dist/es5/lib/binary-utils/array-buffer-utils.js.map +1 -1
- package/dist/es5/lib/binary-utils/binary-copy-utils.js +7 -7
- package/dist/es5/lib/binary-utils/binary-copy-utils.js.map +1 -1
- package/dist/es5/lib/binary-utils/buffer-utils.js +10 -4
- package/dist/es5/lib/binary-utils/buffer-utils.js.map +1 -1
- package/dist/es5/lib/binary-utils/encode-utils.js +7 -7
- package/dist/es5/lib/binary-utils/encode-utils.js.map +1 -1
- package/dist/es5/lib/binary-utils/get-first-characters.js +7 -5
- package/dist/es5/lib/binary-utils/get-first-characters.js.map +1 -1
- package/dist/es5/lib/binary-utils/memory-copy-utils.js +7 -6
- package/dist/es5/lib/binary-utils/memory-copy-utils.js.map +1 -1
- package/dist/es5/lib/env-utils/globals.js +14 -9
- package/dist/es5/lib/env-utils/globals.js.map +1 -1
- package/dist/es5/lib/filesystems/node-filesystem.js +243 -50
- package/dist/es5/lib/filesystems/node-filesystem.js.map +1 -1
- package/dist/es5/lib/iterators/async-iteration.js +242 -27
- package/dist/es5/lib/iterators/async-iteration.js.map +1 -1
- package/dist/es5/lib/iterators/text-iterators.js +410 -35
- package/dist/es5/lib/iterators/text-iterators.js.map +1 -1
- package/dist/es5/lib/node/buffer.js +4 -4
- package/dist/es5/lib/node/buffer.js.map +1 -1
- package/dist/es5/lib/node/fs.js +65 -20
- package/dist/es5/lib/node/fs.js.map +1 -1
- package/dist/es5/lib/node/util.js +5 -3
- package/dist/es5/lib/node/util.js.map +1 -1
- package/dist/es5/lib/path-utils/file-aliases.js +4 -4
- package/dist/es5/lib/path-utils/file-aliases.js.map +1 -1
- package/dist/es5/lib/path-utils/path.js +9 -5
- package/dist/es5/lib/path-utils/path.js.map +1 -1
- package/dist/es5/lib/request-utils/request-scheduler.js +124 -96
- package/dist/es5/lib/request-utils/request-scheduler.js.map +1 -1
- package/dist/es5/lib/worker-loader-utils/create-loader-worker.js +132 -63
- package/dist/es5/lib/worker-loader-utils/create-loader-worker.js.map +1 -1
- package/dist/es5/lib/worker-loader-utils/parse-with-worker.js +112 -51
- package/dist/es5/lib/worker-loader-utils/parse-with-worker.js.map +1 -1
- package/dist/esm/json-loader.js +1 -1
- package/dist/esm/json-loader.js.map +1 -1
- package/dist/esm/lib/node/fs.js +26 -11
- package/dist/esm/lib/node/fs.js.map +1 -1
- package/dist/lib/node/fs.d.ts +11 -14
- package/dist/lib/node/fs.d.ts.map +1 -1
- package/dist/lib/node/fs.js +28 -22
- package/package.json +4 -4
- package/src/lib/node/fs.ts +44 -14
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
4
|
+
|
|
3
5
|
Object.defineProperty(exports, "__esModule", {
|
|
4
6
|
value: true
|
|
5
7
|
});
|
|
@@ -7,10 +9,10 @@ exports.promisify = void 0;
|
|
|
7
9
|
|
|
8
10
|
var util = _interopRequireWildcard(require("util"));
|
|
9
11
|
|
|
10
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
13
|
|
|
12
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null ||
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
var promisify = util.promisify;
|
|
15
17
|
exports.promisify = promisify;
|
|
16
18
|
//# sourceMappingURL=util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/lib/node/util.ts"],"names":["promisify","util"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../../../src/lib/node/util.ts"],"names":["promisify","util"],"mappings":";;;;;;;;;AAAA;;;;;;AAGO,IAAMA,SAAS,GAAGC,IAAI,CAACD,SAAvB","sourcesContent":["import * as util from 'util';\n\n/** Wrapper for Node.js promisify */\nexport const promisify = util.promisify;\n"],"file":"util.js"}
|
|
@@ -7,8 +7,8 @@ exports.setPathPrefix = setPathPrefix;
|
|
|
7
7
|
exports.getPathPrefix = getPathPrefix;
|
|
8
8
|
exports.addAliases = addAliases;
|
|
9
9
|
exports.resolvePath = resolvePath;
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
var pathPrefix = '';
|
|
11
|
+
var fileAliases = {};
|
|
12
12
|
|
|
13
13
|
function setPathPrefix(prefix) {
|
|
14
14
|
pathPrefix = prefix;
|
|
@@ -23,9 +23,9 @@ function addAliases(aliases) {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
function resolvePath(filename) {
|
|
26
|
-
for (
|
|
26
|
+
for (var alias in fileAliases) {
|
|
27
27
|
if (filename.startsWith(alias)) {
|
|
28
|
-
|
|
28
|
+
var replacement = fileAliases[alias];
|
|
29
29
|
filename = filename.replace(alias, replacement);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/lib/path-utils/file-aliases.ts"],"names":["pathPrefix","fileAliases","setPathPrefix","prefix","getPathPrefix","addAliases","aliases","Object","assign","resolvePath","filename","alias","startsWith","replacement","replace"],"mappings":";;;;;;;;;AAEA,IAAIA,UAAU,GAAG,EAAjB;AACA,
|
|
1
|
+
{"version":3,"sources":["../../../../src/lib/path-utils/file-aliases.ts"],"names":["pathPrefix","fileAliases","setPathPrefix","prefix","getPathPrefix","addAliases","aliases","Object","assign","resolvePath","filename","alias","startsWith","replacement","replace"],"mappings":";;;;;;;;;AAEA,IAAIA,UAAU,GAAG,EAAjB;AACA,IAAMC,WAA0C,GAAG,EAAnD;;AAKO,SAASC,aAAT,CAAuBC,MAAvB,EAA6C;AAClDH,EAAAA,UAAU,GAAGG,MAAb;AACD;;AAKM,SAASC,aAAT,GAAiC;AACtC,SAAOJ,UAAP;AACD;;AASM,SAASK,UAAT,CAAoBC,OAApB,EAAkE;AACvEC,EAAAA,MAAM,CAACC,MAAP,CAAcP,WAAd,EAA2BK,OAA3B;AACD;;AAKM,SAASG,WAAT,CAAqBC,QAArB,EAA+C;AACpD,OAAK,IAAMC,KAAX,IAAoBV,WAApB,EAAiC;AAC/B,QAAIS,QAAQ,CAACE,UAAT,CAAoBD,KAApB,CAAJ,EAAgC;AAC9B,UAAME,WAAW,GAAGZ,WAAW,CAACU,KAAD,CAA/B;AACAD,MAAAA,QAAQ,GAAGA,QAAQ,CAACI,OAAT,CAAiBH,KAAjB,EAAwBE,WAAxB,CAAX;AACD;AACF;;AACD,MAAI,CAACH,QAAQ,CAACE,UAAT,CAAoB,SAApB,CAAD,IAAmC,CAACF,QAAQ,CAACE,UAAT,CAAoB,UAApB,CAAxC,EAAyE;AACvEF,IAAAA,QAAQ,aAAMV,UAAN,SAAmBU,QAAnB,CAAR;AACD;;AACD,SAAOA,QAAP;AACD","sourcesContent":["// Simple file alias mechanisms for tests.\n\nlet pathPrefix = '';\nconst fileAliases: {[aliasPath: string]: string} = {};\n\n/*\n * Set a relative path prefix\n */\nexport function setPathPrefix(prefix: string): void {\n pathPrefix = prefix;\n}\n\n/*\n * Get the relative path prefix\n */\nexport function getPathPrefix(): string {\n return pathPrefix;\n}\n\n/**\n *\n * @param aliases\n *\n * Note: addAliases are an experimental export, they are only for testing of loaders.gl loaders\n * not intended as a generic aliasing mechanism\n */\nexport function addAliases(aliases: {[aliasPath: string]: string}): void {\n Object.assign(fileAliases, aliases);\n}\n\n/**\n * Resolves aliases and adds path-prefix to paths\n */\nexport function resolvePath(filename: string): string {\n for (const alias in fileAliases) {\n if (filename.startsWith(alias)) {\n const replacement = fileAliases[alias];\n filename = filename.replace(alias, replacement);\n }\n }\n if (!filename.startsWith('http://') && !filename.startsWith('https://')) {\n filename = `${pathPrefix}${filename}`;\n }\n return filename;\n}\n"],"file":"file-aliases.js"}
|
|
@@ -8,18 +8,22 @@ exports.dirname = dirname;
|
|
|
8
8
|
exports.join = join;
|
|
9
9
|
|
|
10
10
|
function filename(url) {
|
|
11
|
-
|
|
11
|
+
var slashIndex = url && url.lastIndexOf('/');
|
|
12
12
|
return slashIndex >= 0 ? url.substr(slashIndex + 1) : '';
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
function dirname(url) {
|
|
16
|
-
|
|
16
|
+
var slashIndex = url && url.lastIndexOf('/');
|
|
17
17
|
return slashIndex >= 0 ? url.substr(0, slashIndex) : '';
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
function join(
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
function join() {
|
|
21
|
+
for (var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
22
|
+
parts[_key] = arguments[_key];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var separator = '/';
|
|
26
|
+
parts = parts.map(function (part, index) {
|
|
23
27
|
if (index) {
|
|
24
28
|
part = part.replace(new RegExp("^".concat(separator)), '');
|
|
25
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/lib/path-utils/path.ts"],"names":["filename","url","slashIndex","lastIndexOf","substr","dirname","join","parts","separator","map","part","index","replace","RegExp","length"],"mappings":";;;;;;;;;AAMO,SAASA,QAAT,CAAkBC,GAAlB,EAAuC;AAC5C,
|
|
1
|
+
{"version":3,"sources":["../../../../src/lib/path-utils/path.ts"],"names":["filename","url","slashIndex","lastIndexOf","substr","dirname","join","parts","separator","map","part","index","replace","RegExp","length"],"mappings":";;;;;;;;;AAMO,SAASA,QAAT,CAAkBC,GAAlB,EAAuC;AAC5C,MAAMC,UAAU,GAAGD,GAAG,IAAIA,GAAG,CAACE,WAAJ,CAAgB,GAAhB,CAA1B;AACA,SAAOD,UAAU,IAAI,CAAd,GAAkBD,GAAG,CAACG,MAAJ,CAAYF,UAAD,GAAyB,CAApC,CAAlB,GAA2D,EAAlE;AACD;;AAMM,SAASG,OAAT,CAAiBJ,GAAjB,EAAsC;AAC3C,MAAMC,UAAU,GAAGD,GAAG,IAAIA,GAAG,CAACE,WAAJ,CAAgB,GAAhB,CAA1B;AACA,SAAOD,UAAU,IAAI,CAAd,GAAkBD,GAAG,CAACG,MAAJ,CAAW,CAAX,EAAcF,UAAd,CAAlB,GAAwD,EAA/D;AACD;;AAMM,SAASI,IAAT,GAA0C;AAAA,oCAAzBC,KAAyB;AAAzBA,IAAAA,KAAyB;AAAA;;AAC/C,MAAMC,SAAS,GAAG,GAAlB;AACAD,EAAAA,KAAK,GAAGA,KAAK,CAACE,GAAN,CAAU,UAACC,IAAD,EAAOC,KAAP,EAAiB;AACjC,QAAIA,KAAJ,EAAW;AACTD,MAAAA,IAAI,GAAGA,IAAI,CAACE,OAAL,CAAa,IAAIC,MAAJ,YAAeL,SAAf,EAAb,EAA0C,EAA1C,CAAP;AACD;;AACD,QAAIG,KAAK,KAAKJ,KAAK,CAACO,MAAN,GAAe,CAA7B,EAAgC;AAC9BJ,MAAAA,IAAI,GAAGA,IAAI,CAACE,OAAL,CAAa,IAAIC,MAAJ,WAAcL,SAAd,OAAb,EAA0C,EAA1C,CAAP;AACD;;AACD,WAAOE,IAAP;AACD,GARO,CAAR;AASA,SAAOH,KAAK,CAACD,IAAN,CAAWE,SAAX,CAAP;AACD","sourcesContent":["// Beginning of a minimal implementation of the Node.js path API, that doesn't pull in big polyfills.\n\n/**\n * Replacement for Node.js path.filename\n * @param url\n */\nexport function filename(url: string): string {\n const slashIndex = url && url.lastIndexOf('/');\n return slashIndex >= 0 ? url.substr((slashIndex as number) + 1) : '';\n}\n\n/**\n * Replacement for Node.js path.dirname\n * @param url\n */\nexport function dirname(url: string): string {\n const slashIndex = url && url.lastIndexOf('/');\n return slashIndex >= 0 ? url.substr(0, slashIndex as number) : '';\n}\n\n/**\n * Replacement for Node.js path.join\n * @param parts\n */\nexport function join(...parts: string[]): string {\n const separator = '/';\n parts = parts.map((part, index) => {\n if (index) {\n part = part.replace(new RegExp(`^${separator}`), '');\n }\n if (index !== parts.length - 1) {\n part = part.replace(new RegExp(`${separator}$`), '');\n }\n return part;\n });\n return parts.join(separator);\n}\n"],"file":"path.js"}
|
|
@@ -7,32 +7,40 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports.default = void 0;
|
|
9
9
|
|
|
10
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
11
|
+
|
|
12
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
13
|
+
|
|
10
14
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
15
|
|
|
12
16
|
var _stats = require("@probe.gl/stats");
|
|
13
17
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
19
|
+
|
|
20
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
21
|
+
|
|
22
|
+
var STAT_QUEUED_REQUESTS = 'Queued Requests';
|
|
23
|
+
var STAT_ACTIVE_REQUESTS = 'Active Requests';
|
|
24
|
+
var STAT_CANCELLED_REQUESTS = 'Cancelled Requests';
|
|
25
|
+
var STAT_QUEUED_REQUESTS_EVER = 'Queued Requests Ever';
|
|
26
|
+
var STAT_ACTIVE_REQUESTS_EVER = 'Active Requests Ever';
|
|
27
|
+
var DEFAULT_PROPS = {
|
|
20
28
|
id: 'request-scheduler',
|
|
21
29
|
throttleRequests: true,
|
|
22
30
|
maxRequests: 6
|
|
23
31
|
};
|
|
24
32
|
|
|
25
|
-
|
|
26
|
-
|
|
33
|
+
var RequestScheduler = function () {
|
|
34
|
+
function RequestScheduler() {
|
|
35
|
+
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
36
|
+
(0, _classCallCheck2.default)(this, RequestScheduler);
|
|
27
37
|
(0, _defineProperty2.default)(this, "props", void 0);
|
|
28
38
|
(0, _defineProperty2.default)(this, "stats", void 0);
|
|
29
39
|
(0, _defineProperty2.default)(this, "activeRequestCount", 0);
|
|
30
40
|
(0, _defineProperty2.default)(this, "requestQueue", []);
|
|
31
41
|
(0, _defineProperty2.default)(this, "requestMap", new Map());
|
|
32
42
|
(0, _defineProperty2.default)(this, "deferredUpdate", null);
|
|
33
|
-
this.props = {
|
|
34
|
-
...props
|
|
35
|
-
};
|
|
43
|
+
this.props = _objectSpread(_objectSpread({}, DEFAULT_PROPS), props);
|
|
36
44
|
this.stats = new _stats.Stats({
|
|
37
45
|
id: this.props.id
|
|
38
46
|
});
|
|
@@ -43,112 +51,132 @@ class RequestScheduler {
|
|
|
43
51
|
this.stats.get(STAT_ACTIVE_REQUESTS_EVER);
|
|
44
52
|
}
|
|
45
53
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
(0, _createClass2.default)(RequestScheduler, [{
|
|
55
|
+
key: "scheduleRequest",
|
|
56
|
+
value: function scheduleRequest(handle) {
|
|
57
|
+
var getPriority = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {
|
|
58
|
+
return 0;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
if (!this.props.throttleRequests) {
|
|
62
|
+
return Promise.resolve({
|
|
63
|
+
done: function done() {}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
56
66
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
getPriority
|
|
61
|
-
};
|
|
62
|
-
const promise = new Promise(resolve => {
|
|
63
|
-
request.resolve = resolve;
|
|
64
|
-
return request;
|
|
65
|
-
});
|
|
66
|
-
this.requestQueue.push(request);
|
|
67
|
-
this.requestMap.set(handle, promise);
|
|
67
|
+
if (this.requestMap.has(handle)) {
|
|
68
|
+
return this.requestMap.get(handle);
|
|
69
|
+
}
|
|
68
70
|
|
|
69
|
-
|
|
71
|
+
var request = {
|
|
72
|
+
handle: handle,
|
|
73
|
+
priority: 0,
|
|
74
|
+
getPriority: getPriority
|
|
75
|
+
};
|
|
76
|
+
var promise = new Promise(function (resolve) {
|
|
77
|
+
request.resolve = resolve;
|
|
78
|
+
return request;
|
|
79
|
+
});
|
|
80
|
+
this.requestQueue.push(request);
|
|
81
|
+
this.requestMap.set(handle, promise);
|
|
70
82
|
|
|
71
|
-
|
|
72
|
-
}
|
|
83
|
+
this._issueNewRequests();
|
|
73
84
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
85
|
+
return promise;
|
|
86
|
+
}
|
|
87
|
+
}, {
|
|
88
|
+
key: "_issueRequest",
|
|
89
|
+
value: function _issueRequest(request) {
|
|
90
|
+
var _this = this;
|
|
80
91
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.requestMap.delete(handle);
|
|
85
|
-
this.activeRequestCount--;
|
|
92
|
+
var handle = request.handle,
|
|
93
|
+
resolve = request.resolve;
|
|
94
|
+
var isDone = false;
|
|
86
95
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
96
|
+
var done = function done() {
|
|
97
|
+
if (!isDone) {
|
|
98
|
+
isDone = true;
|
|
90
99
|
|
|
91
|
-
|
|
92
|
-
return resolve ? resolve({
|
|
93
|
-
done
|
|
94
|
-
}) : Promise.resolve({
|
|
95
|
-
done
|
|
96
|
-
});
|
|
97
|
-
}
|
|
100
|
+
_this.requestMap.delete(handle);
|
|
98
101
|
|
|
99
|
-
|
|
100
|
-
if (!this.deferredUpdate) {
|
|
101
|
-
this.deferredUpdate = setTimeout(() => this._issueNewRequestsAsync(), 0);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
102
|
+
_this.activeRequestCount--;
|
|
104
103
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
104
|
+
_this._issueNewRequests();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
108
107
|
|
|
109
|
-
|
|
110
|
-
return
|
|
108
|
+
this.activeRequestCount++;
|
|
109
|
+
return resolve ? resolve({
|
|
110
|
+
done: done
|
|
111
|
+
}) : Promise.resolve({
|
|
112
|
+
done: done
|
|
113
|
+
});
|
|
111
114
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
115
|
+
}, {
|
|
116
|
+
key: "_issueNewRequests",
|
|
117
|
+
value: function _issueNewRequests() {
|
|
118
|
+
var _this2 = this;
|
|
119
|
+
|
|
120
|
+
if (!this.deferredUpdate) {
|
|
121
|
+
this.deferredUpdate = setTimeout(function () {
|
|
122
|
+
return _this2._issueNewRequestsAsync();
|
|
123
|
+
}, 0);
|
|
120
124
|
}
|
|
121
125
|
}
|
|
122
|
-
}
|
|
126
|
+
}, {
|
|
127
|
+
key: "_issueNewRequestsAsync",
|
|
128
|
+
value: function _issueNewRequestsAsync() {
|
|
129
|
+
this.deferredUpdate = null;
|
|
130
|
+
var freeSlots = Math.max(this.props.maxRequests - this.activeRequestCount, 0);
|
|
131
|
+
|
|
132
|
+
if (freeSlots === 0) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
123
135
|
|
|
124
|
-
|
|
125
|
-
const requestQueue = this.requestQueue;
|
|
136
|
+
this._updateAllRequests();
|
|
126
137
|
|
|
127
|
-
|
|
128
|
-
|
|
138
|
+
for (var i = 0; i < freeSlots; ++i) {
|
|
139
|
+
var request = this.requestQueue.shift();
|
|
129
140
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
i--;
|
|
141
|
+
if (request) {
|
|
142
|
+
this._issueRequest(request);
|
|
143
|
+
}
|
|
134
144
|
}
|
|
135
145
|
}
|
|
146
|
+
}, {
|
|
147
|
+
key: "_updateAllRequests",
|
|
148
|
+
value: function _updateAllRequests() {
|
|
149
|
+
var requestQueue = this.requestQueue;
|
|
150
|
+
|
|
151
|
+
for (var i = 0; i < requestQueue.length; ++i) {
|
|
152
|
+
var request = requestQueue[i];
|
|
153
|
+
|
|
154
|
+
if (!this._updateRequest(request)) {
|
|
155
|
+
requestQueue.splice(i, 1);
|
|
156
|
+
this.requestMap.delete(request.handle);
|
|
157
|
+
i--;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
136
160
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
_updateRequest(request) {
|
|
141
|
-
request.priority = request.getPriority(request.handle);
|
|
142
|
-
|
|
143
|
-
if (request.priority < 0) {
|
|
144
|
-
request.resolve(null);
|
|
145
|
-
return false;
|
|
161
|
+
requestQueue.sort(function (a, b) {
|
|
162
|
+
return a.priority - b.priority;
|
|
163
|
+
});
|
|
146
164
|
}
|
|
165
|
+
}, {
|
|
166
|
+
key: "_updateRequest",
|
|
167
|
+
value: function _updateRequest(request) {
|
|
168
|
+
request.priority = request.getPriority(request.handle);
|
|
169
|
+
|
|
170
|
+
if (request.priority < 0) {
|
|
171
|
+
request.resolve(null);
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
147
174
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
}]);
|
|
178
|
+
return RequestScheduler;
|
|
179
|
+
}();
|
|
152
180
|
|
|
153
181
|
exports.default = RequestScheduler;
|
|
154
182
|
//# sourceMappingURL=request-scheduler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/lib/request-utils/request-scheduler.ts"],"names":["STAT_QUEUED_REQUESTS","STAT_ACTIVE_REQUESTS","STAT_CANCELLED_REQUESTS","STAT_QUEUED_REQUESTS_EVER","STAT_ACTIVE_REQUESTS_EVER","DEFAULT_PROPS","id","throttleRequests","maxRequests","RequestScheduler","constructor","props","Map","stats","Stats","get","scheduleRequest","handle","getPriority","Promise","resolve","done","requestMap","has","request","priority","promise","requestQueue","push","set","_issueNewRequests","_issueRequest","isDone","delete","activeRequestCount","deferredUpdate","setTimeout","_issueNewRequestsAsync","freeSlots","Math","max","_updateAllRequests","i","shift","length","_updateRequest","splice","sort","a","b"],"mappings":";;;;;;;;;;;AAAA;;AAgBA,MAAMA,oBAAoB,GAAG,iBAA7B;AACA,MAAMC,oBAAoB,GAAG,iBAA7B;AACA,MAAMC,uBAAuB,GAAG,oBAAhC;AACA,MAAMC,yBAAyB,GAAG,sBAAlC;AACA,MAAMC,yBAAyB,GAAG,sBAAlC;AAEA,MAAMC,aAA8C,GAAG;AACrDC,EAAAA,EAAE,EAAE,mBADiD;AAGrDC,EAAAA,gBAAgB,EAAE,IAHmC;AAKrDC,EAAAA,WAAW,EAAE;AALwC,CAAvD;;AAoBe,MAAMC,gBAAN,CAAuB;AAUpCC,EAAAA,WAAW,CAACC,KAA4B,GAAG,EAAhC,EAAoC;AAAA;AAAA;AAAA,8DAPlB,CAOkB;AAAA,wDAJb,EAIa;AAAA,sDAHW,IAAIC,GAAJ,EAGX;AAAA,0DAFjB,IAEiB;AAC7C,SAAKD,KAAL,GAAa,EAAC,GAAGN,aAAJ;AAAmB,SAAGM;AAAtB,KAAb;AAGA,SAAKE,KAAL,GAAa,IAAIC,YAAJ,CAAU;AAACR,MAAAA,EAAE,EAAE,KAAKK,KAAL,CAAWL;AAAhB,KAAV,CAAb;AACA,SAAKO,KAAL,CAAWE,GAAX,CAAef,oBAAf;AACA,SAAKa,KAAL,CAAWE,GAAX,CAAed,oBAAf;AACA,SAAKY,KAAL,CAAWE,GAAX,CAAeb,uBAAf;AACA,SAAKW,KAAL,CAAWE,GAAX,CAAeZ,yBAAf;AACA,SAAKU,KAAL,CAAWE,GAAX,CAAeX,yBAAf;AACD;;AAkBDY,EAAAA,eAAe,CACbC,MADa,EAEb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,GAAawB,CAAC,CAACxB,QAA3C;AACD;;AAGDoB,EAAAA,cAAc,CAACrB,OAAD,EAAU;AACtBA,IAAAA,OAAO,CAACC,QAAR,GAAmBD,OAAO,CAACN,WAAR,CAAoBM,OAAO,CAACP,MAA5B,CAAnB;;AAGA,QAAIO,OAAO,CAACC,QAAR,GAAmB,CAAvB,EAA0B;AACxBD,MAAAA,OAAO,CAACJ,OAAR,CAAgB,IAAhB;AACA,aAAO,KAAP;AACD;;AACD,WAAO,IAAP;AACD;;AArJmC","sourcesContent":["import {Stats} from '@probe.gl/stats';\n\ntype Handle = any;\ntype DoneFunction = () => any;\ntype GetPriorityFunction = () => number;\ntype RequestResult = {\n done: DoneFunction;\n} | null;\n\n/** RequestScheduler Options */\nexport type RequestSchedulerProps = {\n id?: string;\n throttleRequests?: boolean;\n maxRequests?: number;\n};\n\nconst STAT_QUEUED_REQUESTS = 'Queued Requests';\nconst STAT_ACTIVE_REQUESTS = 'Active Requests';\nconst STAT_CANCELLED_REQUESTS = 'Cancelled Requests';\nconst STAT_QUEUED_REQUESTS_EVER = 'Queued Requests Ever';\nconst STAT_ACTIVE_REQUESTS_EVER = 'Active Requests Ever';\n\nconst DEFAULT_PROPS: Required<RequestSchedulerProps> = {\n id: 'request-scheduler',\n // Specifies if the request scheduler should throttle incoming requests, mainly for comparative testing\n throttleRequests: true,\n // The maximum number of simultaneous active requests. Un-throttled requests do not observe this limit.\n maxRequests: 6\n};\n\n/** Tracks one request */\ntype Request = {\n handle: Handle;\n priority: number;\n getPriority: GetPriorityFunction;\n resolve?: (value: any) => any;\n};\n\n/**\n * Used to issue a request, without having them \"deeply queued\" by the browser.\n * @todo - Track requests globally, across multiple servers\n */\nexport default class RequestScheduler {\n readonly props: Required<RequestSchedulerProps>;\n readonly stats: Stats;\n activeRequestCount: number = 0;\n\n /** Tracks the number of active requests and prioritizes/cancels queued requests. */\n private requestQueue: Request[] = [];\n private requestMap: Map<Handle, Promise<RequestResult>> = new Map();\n private deferredUpdate: any = null;\n\n constructor(props: RequestSchedulerProps = {}) {\n this.props = {...DEFAULT_PROPS, ...props};\n\n // Returns the statistics used by the request scheduler.\n this.stats = new Stats({id: this.props.id});\n this.stats.get(STAT_QUEUED_REQUESTS);\n this.stats.get(STAT_ACTIVE_REQUESTS);\n this.stats.get(STAT_CANCELLED_REQUESTS);\n this.stats.get(STAT_QUEUED_REQUESTS_EVER);\n this.stats.get(STAT_ACTIVE_REQUESTS_EVER);\n }\n\n /**\n * Called by an application that wants to issue a request, without having it deeply queued by the browser\n *\n * When the returned promise resolved, it is OK for the application to issue a request.\n * The promise resolves to an object that contains a `done` method.\n * When the application's request has completed (or failed), the application must call the `done` function\n *\n * @param handle\n * @param getPriority will be called when request \"slots\" open up,\n * allowing the caller to update priority or cancel the request\n * Highest priority executes first, priority < 0 cancels the request\n * @returns a promise\n * - resolves to a object (with a `done` field) when the request can be issued without queueing,\n * - resolves to `null` if the request has been cancelled (by the callback return < 0).\n * In this case the application should not issue the request\n */\n scheduleRequest(\n handle: Handle,\n getPriority: GetPriorityFunction = () => 0\n ): Promise<RequestResult> {\n // Allows throttling to be disabled\n if (!this.props.throttleRequests) {\n return Promise.resolve({done: () => {}});\n }\n\n // dedupe\n if (this.requestMap.has(handle)) {\n return this.requestMap.get(handle) as Promise<any>;\n }\n\n const request: Request = {handle, priority: 0, getPriority};\n const promise = new Promise<RequestResult>((resolve) => {\n // @ts-ignore\n request.resolve = resolve;\n return request;\n });\n\n this.requestQueue.push(request);\n this.requestMap.set(handle, promise);\n this._issueNewRequests();\n return promise;\n }\n\n // PRIVATE\n\n _issueRequest(request: Request): Promise<any> {\n const {handle, resolve} = request;\n let isDone = false;\n\n const done = () => {\n // can only be called once\n if (!isDone) {\n isDone = true;\n\n // Stop tracking a request - it has completed, failed, cancelled etc\n this.requestMap.delete(handle);\n this.activeRequestCount--;\n // A slot just freed up, see if any queued requests are waiting\n this._issueNewRequests();\n }\n };\n\n // Track this request\n this.activeRequestCount++;\n\n return resolve ? resolve({done}) : Promise.resolve({done});\n }\n\n /** We check requests asynchronously, to prevent multiple updates */\n _issueNewRequests(): void {\n if (!this.deferredUpdate) {\n this.deferredUpdate = setTimeout(() => this._issueNewRequestsAsync(), 0);\n }\n }\n\n /** Refresh all requests */\n _issueNewRequestsAsync() {\n // TODO - shouldn't we clear the timeout?\n this.deferredUpdate = null;\n\n const freeSlots = Math.max(this.props.maxRequests - this.activeRequestCount, 0);\n\n if (freeSlots === 0) {\n return;\n }\n\n this._updateAllRequests();\n\n // Resolve pending promises for the top-priority requests\n for (let i = 0; i < freeSlots; ++i) {\n const request = this.requestQueue.shift();\n if (request) {\n this._issueRequest(request); // eslint-disable-line @typescript-eslint/no-floating-promises\n }\n }\n\n // Uncomment to debug\n // console.log(`${freeSlots} free slots, ${this.requestQueue.length} queued requests`);\n }\n\n /** Ensure all requests have updated priorities, and that no longer valid requests are cancelled */\n _updateAllRequests() {\n const requestQueue = this.requestQueue;\n for (let i = 0; i < requestQueue.length; ++i) {\n const request = requestQueue[i];\n if (!this._updateRequest(request)) {\n // Remove the element and make sure to adjust the counter to account for shortened array\n requestQueue.splice(i, 1);\n this.requestMap.delete(request.handle);\n i--;\n }\n }\n\n // Sort the remaining requests based on priority\n requestQueue.sort((a, b) => a.priority - b.priority);\n }\n\n /** Update a single request by calling the callback */\n _updateRequest(request) {\n request.priority = request.getPriority(request.handle); // eslint-disable-line callback-return\n\n // by returning a negative priority, the callback cancels the request\n if (request.priority < 0) {\n request.resolve(null);\n return false;\n }\n return true;\n }\n}\n"],"file":"request-scheduler.js"}
|
|
1
|
+
{"version":3,"sources":["../../../../src/lib/request-utils/request-scheduler.ts"],"names":["STAT_QUEUED_REQUESTS","STAT_ACTIVE_REQUESTS","STAT_CANCELLED_REQUESTS","STAT_QUEUED_REQUESTS_EVER","STAT_ACTIVE_REQUESTS_EVER","DEFAULT_PROPS","id","throttleRequests","maxRequests","RequestScheduler","props","Map","stats","Stats","get","handle","getPriority","Promise","resolve","done","requestMap","has","request","priority","promise","requestQueue","push","set","_issueNewRequests","isDone","delete","activeRequestCount","deferredUpdate","setTimeout","_issueNewRequestsAsync","freeSlots","Math","max","_updateAllRequests","i","shift","_issueRequest","length","_updateRequest","splice","sort","a","b"],"mappings":";;;;;;;;;;;;;;;AAAA;;;;;;AAgBA,IAAMA,oBAAoB,GAAG,iBAA7B;AACA,IAAMC,oBAAoB,GAAG,iBAA7B;AACA,IAAMC,uBAAuB,GAAG,oBAAhC;AACA,IAAMC,yBAAyB,GAAG,sBAAlC;AACA,IAAMC,yBAAyB,GAAG,sBAAlC;AAEA,IAAMC,aAA8C,GAAG;AACrDC,EAAAA,EAAE,EAAE,mBADiD;AAGrDC,EAAAA,gBAAgB,EAAE,IAHmC;AAKrDC,EAAAA,WAAW,EAAE;AALwC,CAAvD;;IAoBqBC,gB;AAUnB,8BAA+C;AAAA,QAAnCC,KAAmC,uEAAJ,EAAI;AAAA;AAAA;AAAA;AAAA,8DAPlB,CAOkB;AAAA,wDAJb,EAIa;AAAA,sDAHW,IAAIC,GAAJ,EAGX;AAAA,0DAFjB,IAEiB;AAC7C,SAAKD,KAAL,mCAAiBL,aAAjB,GAAmCK,KAAnC;AAGA,SAAKE,KAAL,GAAa,IAAIC,YAAJ,CAAU;AAACP,MAAAA,EAAE,EAAE,KAAKI,KAAL,CAAWJ;AAAhB,KAAV,CAAb;AACA,SAAKM,KAAL,CAAWE,GAAX,CAAed,oBAAf;AACA,SAAKY,KAAL,CAAWE,GAAX,CAAeb,oBAAf;AACA,SAAKW,KAAL,CAAWE,GAAX,CAAeZ,uBAAf;AACA,SAAKU,KAAL,CAAWE,GAAX,CAAeX,yBAAf;AACA,SAAKS,KAAL,CAAWE,GAAX,CAAeV,yBAAf;AACD;;;;WAkBD,yBACEW,MADF,EAG0B;AAAA,UADxBC,WACwB,uEADW;AAAA,eAAM,CAAN;AAAA,OACX;;AAExB,UAAI,CAAC,KAAKN,KAAL,CAAWH,gBAAhB,EAAkC;AAChC,eAAOU,O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UD,CAAC,CAACvB,QAAF,GAAawB,CAAC,CAACxB,QAAzB;AAAA,OAAlB;AACD;;;WAGD,wBAAeD,OAAf,EAAwB;AACtBA,MAAAA,OAAO,CAACC,QAAR,GAAmBD,OAAO,CAACN,WAAR,CAAoBM,OAAO,CAACP,MAA5B,CAAnB;;AAGA,UAAIO,OAAO,CAACC,QAAR,GAAmB,CAAvB,EAA0B;AACxBD,QAAAA,OAAO,CAACJ,OAAR,CAAgB,IAAhB;AACA,eAAO,KAAP;AACD;;AACD,aAAO,IAAP;AACD","sourcesContent":["import {Stats} from '@probe.gl/stats';\n\ntype Handle = any;\ntype DoneFunction = () => any;\ntype GetPriorityFunction = () => number;\ntype RequestResult = {\n done: DoneFunction;\n} | null;\n\n/** RequestScheduler Options */\nexport type RequestSchedulerProps = {\n id?: string;\n throttleRequests?: boolean;\n maxRequests?: number;\n};\n\nconst STAT_QUEUED_REQUESTS = 'Queued Requests';\nconst STAT_ACTIVE_REQUESTS = 'Active Requests';\nconst STAT_CANCELLED_REQUESTS = 'Cancelled Requests';\nconst STAT_QUEUED_REQUESTS_EVER = 'Queued Requests Ever';\nconst STAT_ACTIVE_REQUESTS_EVER = 'Active Requests Ever';\n\nconst DEFAULT_PROPS: Required<RequestSchedulerProps> = {\n id: 'request-scheduler',\n // Specifies if the request scheduler should throttle incoming requests, mainly for comparative testing\n throttleRequests: true,\n // The maximum number of simultaneous active requests. Un-throttled requests do not observe this limit.\n maxRequests: 6\n};\n\n/** Tracks one request */\ntype Request = {\n handle: Handle;\n priority: number;\n getPriority: GetPriorityFunction;\n resolve?: (value: any) => any;\n};\n\n/**\n * Used to issue a request, without having them \"deeply queued\" by the browser.\n * @todo - Track requests globally, across multiple servers\n */\nexport default class RequestScheduler {\n readonly props: Required<RequestSchedulerProps>;\n readonly stats: Stats;\n activeRequestCount: number = 0;\n\n /** Tracks the number of active requests and prioritizes/cancels queued requests. */\n private requestQueue: Request[] = [];\n private requestMap: Map<Handle, Promise<RequestResult>> = new Map();\n private deferredUpdate: any = null;\n\n constructor(props: RequestSchedulerProps = {}) {\n this.props = {...DEFAULT_PROPS, ...props};\n\n // Returns the statistics used by the request scheduler.\n this.stats = new Stats({id: this.props.id});\n this.stats.get(STAT_QUEUED_REQUESTS);\n this.stats.get(STAT_ACTIVE_REQUESTS);\n this.stats.get(STAT_CANCELLED_REQUESTS);\n this.stats.get(STAT_QUEUED_REQUESTS_EVER);\n this.stats.get(STAT_ACTIVE_REQUESTS_EVER);\n }\n\n /**\n * Called by an application that wants to issue a request, without having it deeply queued by the browser\n *\n * When the returned promise resolved, it is OK for the application to issue a request.\n * The promise resolves to an object that contains a `done` method.\n * When the application's request has completed (or failed), the application must call the `done` function\n *\n * @param handle\n * @param getPriority will be called when request \"slots\" open up,\n * allowing the caller to update priority or cancel the request\n * Highest priority executes first, priority < 0 cancels the request\n * @returns a promise\n * - resolves to a object (with a `done` field) when the request can be issued without queueing,\n * - resolves to `null` if the request has been cancelled (by the callback return < 0).\n * In this case the application should not issue the request\n */\n scheduleRequest(\n handle: Handle,\n getPriority: GetPriorityFunction = () => 0\n ): Promise<RequestResult> {\n // Allows throttling to be disabled\n if (!this.props.throttleRequests) {\n return Promise.resolve({done: () => {}});\n }\n\n // dedupe\n if (this.requestMap.has(handle)) {\n return this.requestMap.get(handle) as Promise<any>;\n }\n\n const request: Request = {handle, priority: 0, getPriority};\n const promise = new Promise<RequestResult>((resolve) => {\n // @ts-ignore\n request.resolve = resolve;\n return request;\n });\n\n this.requestQueue.push(request);\n this.requestMap.set(handle, promise);\n this._issueNewRequests();\n return promise;\n }\n\n // PRIVATE\n\n _issueRequest(request: Request): Promise<any> {\n const {handle, resolve} = request;\n let isDone = false;\n\n const done = () => {\n // can only be called once\n if (!isDone) {\n isDone = true;\n\n // Stop tracking a request - it has completed, failed, cancelled etc\n this.requestMap.delete(handle);\n this.activeRequestCount--;\n // A slot just freed up, see if any queued requests are waiting\n this._issueNewRequests();\n }\n };\n\n // Track this request\n this.activeRequestCount++;\n\n return resolve ? resolve({done}) : Promise.resolve({done});\n }\n\n /** We check requests asynchronously, to prevent multiple updates */\n _issueNewRequests(): void {\n if (!this.deferredUpdate) {\n this.deferredUpdate = setTimeout(() => this._issueNewRequestsAsync(), 0);\n }\n }\n\n /** Refresh all requests */\n _issueNewRequestsAsync() {\n // TODO - shouldn't we clear the timeout?\n this.deferredUpdate = null;\n\n const freeSlots = Math.max(this.props.maxRequests - this.activeRequestCount, 0);\n\n if (freeSlots === 0) {\n return;\n }\n\n this._updateAllRequests();\n\n // Resolve pending promises for the top-priority requests\n for (let i = 0; i < freeSlots; ++i) {\n const request = this.requestQueue.shift();\n if (request) {\n this._issueRequest(request); // eslint-disable-line @typescript-eslint/no-floating-promises\n }\n }\n\n // Uncomment to debug\n // console.log(`${freeSlots} free slots, ${this.requestQueue.length} queued requests`);\n }\n\n /** Ensure all requests have updated priorities, and that no longer valid requests are cancelled */\n _updateAllRequests() {\n const requestQueue = this.requestQueue;\n for (let i = 0; i < requestQueue.length; ++i) {\n const request = requestQueue[i];\n if (!this._updateRequest(request)) {\n // Remove the element and make sure to adjust the counter to account for shortened array\n requestQueue.splice(i, 1);\n this.requestMap.delete(request.handle);\n i--;\n }\n }\n\n // Sort the remaining requests based on priority\n requestQueue.sort((a, b) => a.priority - b.priority);\n }\n\n /** Update a single request by calling the callback */\n _updateRequest(request) {\n request.priority = request.getPriority(request.handle); // eslint-disable-line callback-return\n\n // by returning a negative priority, the callback cancels the request\n if (request.priority < 0) {\n request.resolve(null);\n return false;\n }\n return true;\n }\n}\n"],"file":"request-scheduler.js"}
|