@loaders.gl/loader-utils 3.0.9 → 3.0.13

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.
Files changed (76) hide show
  1. package/dist/es5/index.js +69 -53
  2. package/dist/es5/index.js.map +1 -1
  3. package/dist/es5/json-loader.js +5 -33
  4. package/dist/es5/json-loader.js.map +1 -1
  5. package/dist/es5/lib/binary-utils/array-buffer-utils.js +33 -66
  6. package/dist/es5/lib/binary-utils/array-buffer-utils.js.map +1 -1
  7. package/dist/es5/lib/binary-utils/binary-copy-utils.js +7 -7
  8. package/dist/es5/lib/binary-utils/binary-copy-utils.js.map +1 -1
  9. package/dist/es5/lib/binary-utils/buffer-utils.js +4 -10
  10. package/dist/es5/lib/binary-utils/buffer-utils.js.map +1 -1
  11. package/dist/es5/lib/binary-utils/encode-utils.js +7 -7
  12. package/dist/es5/lib/binary-utils/encode-utils.js.map +1 -1
  13. package/dist/es5/lib/binary-utils/get-first-characters.js +5 -7
  14. package/dist/es5/lib/binary-utils/get-first-characters.js.map +1 -1
  15. package/dist/es5/lib/binary-utils/memory-copy-utils.js +6 -7
  16. package/dist/es5/lib/binary-utils/memory-copy-utils.js.map +1 -1
  17. package/dist/es5/lib/env-utils/globals.js +9 -14
  18. package/dist/es5/lib/env-utils/globals.js.map +1 -1
  19. package/dist/es5/lib/filesystems/node-filesystem.js +74 -0
  20. package/dist/es5/lib/filesystems/node-filesystem.js.map +1 -0
  21. package/dist/es5/lib/iterators/async-iteration.js +27 -242
  22. package/dist/es5/lib/iterators/async-iteration.js.map +1 -1
  23. package/dist/es5/lib/iterators/text-iterators.js +35 -410
  24. package/dist/es5/lib/iterators/text-iterators.js.map +1 -1
  25. package/dist/es5/lib/node/{buffer-utils.node.js → buffer.js} +5 -5
  26. package/dist/es5/lib/node/buffer.js.map +1 -0
  27. package/dist/es5/lib/node/fs.js +30 -61
  28. package/dist/es5/lib/node/fs.js.map +1 -1
  29. package/dist/es5/lib/node/util.js +16 -0
  30. package/dist/es5/lib/node/util.js.map +1 -0
  31. package/dist/es5/lib/path-utils/file-aliases.js +4 -4
  32. package/dist/es5/lib/path-utils/file-aliases.js.map +1 -1
  33. package/dist/es5/lib/path-utils/path.js +4 -8
  34. package/dist/es5/lib/path-utils/path.js.map +1 -1
  35. package/dist/es5/lib/request-utils/request-scheduler.js +96 -124
  36. package/dist/es5/lib/request-utils/request-scheduler.js.map +1 -1
  37. package/dist/es5/lib/worker-loader-utils/create-loader-worker.js +63 -132
  38. package/dist/es5/lib/worker-loader-utils/create-loader-worker.js.map +1 -1
  39. package/dist/es5/lib/worker-loader-utils/parse-with-worker.js +51 -111
  40. package/dist/es5/lib/worker-loader-utils/parse-with-worker.js.map +1 -1
  41. package/dist/esm/index.js +8 -4
  42. package/dist/esm/index.js.map +1 -1
  43. package/dist/esm/json-loader.js +1 -1
  44. package/dist/esm/json-loader.js.map +1 -1
  45. package/dist/esm/lib/binary-utils/array-buffer-utils.js +8 -4
  46. package/dist/esm/lib/binary-utils/array-buffer-utils.js.map +1 -1
  47. package/dist/esm/lib/binary-utils/buffer-utils.js +1 -1
  48. package/dist/esm/lib/binary-utils/buffer-utils.js.map +1 -1
  49. package/dist/esm/lib/filesystems/node-filesystem.js +60 -0
  50. package/dist/esm/lib/filesystems/node-filesystem.js.map +1 -0
  51. package/dist/esm/lib/node/{buffer-utils.node.js → buffer.js} +1 -1
  52. package/dist/esm/lib/node/buffer.js.map +1 -0
  53. package/dist/esm/lib/node/fs.js +12 -14
  54. package/dist/esm/lib/node/fs.js.map +1 -1
  55. package/dist/esm/lib/node/util.js +3 -0
  56. package/dist/esm/lib/node/util.js.map +1 -0
  57. package/dist/esm/lib/parser-utils/parse-json.js +1 -1
  58. package/dist/esm/lib/parser-utils/parse-json.js.map +1 -1
  59. package/dist/esm/lib/path-utils/file-aliases.js +1 -1
  60. package/dist/esm/lib/path-utils/file-aliases.js.map +1 -1
  61. package/dist/esm/lib/path-utils/path.js +2 -2
  62. package/dist/esm/lib/path-utils/path.js.map +1 -1
  63. package/dist/esm/lib/worker-loader-utils/create-loader-worker.js +1 -1
  64. package/dist/esm/lib/worker-loader-utils/create-loader-worker.js.map +1 -1
  65. package/dist/esm/lib/worker-loader-utils/parse-with-worker.js +1 -1
  66. package/dist/esm/lib/worker-loader-utils/parse-with-worker.js.map +1 -1
  67. package/package.json +13 -8
  68. package/src/index.ts +26 -8
  69. package/src/lib/binary-utils/array-buffer-utils.ts +8 -5
  70. package/src/lib/binary-utils/buffer-utils.ts +1 -1
  71. package/src/lib/filesystems/node-filesystem.ts +79 -0
  72. package/src/lib/node/{buffer-utils.node.ts → buffer.ts} +0 -0
  73. package/src/lib/node/fs.ts +29 -13
  74. package/src/lib/node/util.ts +4 -0
  75. package/dist/es5/lib/node/buffer-utils.node.js.map +0 -1
  76. package/dist/esm/lib/node/buffer-utils.node.js.map +0 -1
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.promisify = void 0;
7
+
8
+ var util = _interopRequireWildcard(require("util"));
9
+
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); }
11
+
12
+ 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
+
14
+ const promisify = util.promisify;
15
+ exports.promisify = promisify;
16
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/lib/node/util.ts"],"names":["promisify","util"],"mappings":";;;;;;;AAAA;;;;;;AAGO,MAAMA,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
- var pathPrefix = '';
11
- var fileAliases = {};
10
+ let pathPrefix = '';
11
+ const 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 (var alias in fileAliases) {
26
+ for (const alias in fileAliases) {
27
27
  if (filename.startsWith(alias)) {
28
- var replacement = fileAliases[alias];
28
+ const 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,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"}
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,MAAMC,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,MAAMC,KAAX,IAAoBV,WAApB,EAAiC;AAC/B,QAAIS,QAAQ,CAACE,UAAT,CAAoBD,KAApB,CAAJ,EAAgC;AAC9B,YAAME,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"}
@@ -7,17 +7,13 @@ exports.dirname = dirname;
7
7
  exports.join = join;
8
8
 
9
9
  function dirname(url) {
10
- var slashIndex = url && url.lastIndexOf('/');
10
+ const slashIndex = url && url.lastIndexOf('/');
11
11
  return slashIndex >= 0 ? url.substr(0, slashIndex) : '';
12
12
  }
13
13
 
14
- function join() {
15
- for (var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++) {
16
- parts[_key] = arguments[_key];
17
- }
18
-
19
- var separator = '/';
20
- parts = parts.map(function (part, index) {
14
+ function join(...parts) {
15
+ const separator = '/';
16
+ parts = parts.map((part, index) => {
21
17
  if (index) {
22
18
  part = part.replace(new RegExp("^".concat(separator)), '');
23
19
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/path-utils/path.ts"],"names":["dirname","url","slashIndex","lastIndexOf","substr","join","parts","separator","map","part","index","replace","RegExp","length"],"mappings":";;;;;;;;AAMO,SAASA,OAAT,CAAiBC,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,SAASG,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.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"}
1
+ {"version":3,"sources":["../../../../src/lib/path-utils/path.ts"],"names":["dirname","url","slashIndex","lastIndexOf","substr","join","parts","separator","map","part","index","replace","RegExp","length"],"mappings":";;;;;;;;AAMO,SAASA,OAAT,CAAiBC,GAAjB,EAAsC;AAC3C,QAAMC,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,SAASG,IAAT,CAAc,GAAGC,KAAjB,EAA0C;AAC/C,QAAMC,SAAS,GAAG,GAAlB;AACAD,EAAAA,KAAK,GAAGA,KAAK,CAACE,GAAN,CAAU,CAACC,IAAD,EAAOC,KAAP,KAAiB;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.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,40 +7,32 @@ 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
-
14
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
11
 
16
12
  var _stats = require("@probe.gl/stats");
17
13
 
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 = {
14
+ const STAT_QUEUED_REQUESTS = 'Queued Requests';
15
+ const STAT_ACTIVE_REQUESTS = 'Active Requests';
16
+ const STAT_CANCELLED_REQUESTS = 'Cancelled Requests';
17
+ const STAT_QUEUED_REQUESTS_EVER = 'Queued Requests Ever';
18
+ const STAT_ACTIVE_REQUESTS_EVER = 'Active Requests Ever';
19
+ const DEFAULT_PROPS = {
28
20
  id: 'request-scheduler',
29
21
  throttleRequests: true,
30
22
  maxRequests: 6
31
23
  };
32
24
 
33
- var RequestScheduler = function () {
34
- function RequestScheduler() {
35
- var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
36
- (0, _classCallCheck2.default)(this, RequestScheduler);
25
+ class RequestScheduler {
26
+ constructor(props = {}) {
37
27
  (0, _defineProperty2.default)(this, "props", void 0);
38
28
  (0, _defineProperty2.default)(this, "stats", void 0);
39
29
  (0, _defineProperty2.default)(this, "activeRequestCount", 0);
40
30
  (0, _defineProperty2.default)(this, "requestQueue", []);
41
31
  (0, _defineProperty2.default)(this, "requestMap", new Map());
42
32
  (0, _defineProperty2.default)(this, "deferredUpdate", null);
43
- this.props = _objectSpread(_objectSpread({}, DEFAULT_PROPS), props);
33
+ this.props = { ...DEFAULT_PROPS,
34
+ ...props
35
+ };
44
36
  this.stats = new _stats.Stats({
45
37
  id: this.props.id
46
38
  });
@@ -51,132 +43,112 @@ var RequestScheduler = function () {
51
43
  this.stats.get(STAT_ACTIVE_REQUESTS_EVER);
52
44
  }
53
45
 
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
- }
46
+ scheduleRequest(handle, getPriority = () => 0) {
47
+ if (!this.props.throttleRequests) {
48
+ return Promise.resolve({
49
+ done: () => {}
50
+ });
51
+ }
66
52
 
67
- if (this.requestMap.has(handle)) {
68
- return this.requestMap.get(handle);
69
- }
53
+ if (this.requestMap.has(handle)) {
54
+ return this.requestMap.get(handle);
55
+ }
70
56
 
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);
57
+ const request = {
58
+ handle,
59
+ priority: 0,
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);
82
68
 
83
- this._issueNewRequests();
69
+ this._issueNewRequests();
84
70
 
85
- return promise;
86
- }
87
- }, {
88
- key: "_issueRequest",
89
- value: function _issueRequest(request) {
90
- var _this = this;
71
+ return promise;
72
+ }
91
73
 
92
- var handle = request.handle,
93
- resolve = request.resolve;
94
- var isDone = false;
74
+ _issueRequest(request) {
75
+ const {
76
+ handle,
77
+ resolve
78
+ } = request;
79
+ let isDone = false;
95
80
 
96
- var done = function done() {
97
- if (!isDone) {
98
- isDone = true;
81
+ const done = () => {
82
+ if (!isDone) {
83
+ isDone = true;
84
+ this.requestMap.delete(handle);
85
+ this.activeRequestCount--;
99
86
 
100
- _this.requestMap.delete(handle);
87
+ this._issueNewRequests();
88
+ }
89
+ };
90
+
91
+ this.activeRequestCount++;
92
+ return resolve ? resolve({
93
+ done
94
+ }) : Promise.resolve({
95
+ done
96
+ });
97
+ }
101
98
 
102
- _this.activeRequestCount--;
99
+ _issueNewRequests() {
100
+ if (!this.deferredUpdate) {
101
+ this.deferredUpdate = setTimeout(() => this._issueNewRequestsAsync(), 0);
102
+ }
103
+ }
103
104
 
104
- _this._issueNewRequests();
105
- }
106
- };
105
+ _issueNewRequestsAsync() {
106
+ this.deferredUpdate = null;
107
+ const freeSlots = Math.max(this.props.maxRequests - this.activeRequestCount, 0);
107
108
 
108
- this.activeRequestCount++;
109
- return resolve ? resolve({
110
- done: done
111
- }) : Promise.resolve({
112
- done: done
113
- });
109
+ if (freeSlots === 0) {
110
+ return;
114
111
  }
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);
112
+
113
+ this._updateAllRequests();
114
+
115
+ for (let i = 0; i < freeSlots; ++i) {
116
+ const request = this.requestQueue.shift();
117
+
118
+ if (request) {
119
+ this._issueRequest(request);
124
120
  }
125
121
  }
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
- }
122
+ }
135
123
 
136
- this._updateAllRequests();
124
+ _updateAllRequests() {
125
+ const requestQueue = this.requestQueue;
137
126
 
138
- for (var i = 0; i < freeSlots; ++i) {
139
- var request = this.requestQueue.shift();
127
+ for (let i = 0; i < requestQueue.length; ++i) {
128
+ const request = requestQueue[i];
140
129
 
141
- if (request) {
142
- this._issueRequest(request);
143
- }
130
+ if (!this._updateRequest(request)) {
131
+ requestQueue.splice(i, 1);
132
+ this.requestMap.delete(request.handle);
133
+ i--;
144
134
  }
145
135
  }
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
- }
160
136
 
161
- requestQueue.sort(function (a, b) {
162
- return a.priority - b.priority;
163
- });
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
- }
137
+ requestQueue.sort((a, b) => a.priority - b.priority);
138
+ }
174
139
 
175
- return true;
140
+ _updateRequest(request) {
141
+ request.priority = request.getPriority(request.handle);
142
+
143
+ if (request.priority < 0) {
144
+ request.resolve(null);
145
+ return false;
176
146
  }
177
- }]);
178
- return RequestScheduler;
179
- }();
147
+
148
+ return true;
149
+ }
150
+
151
+ }
180
152
 
181
153
  exports.default = RequestScheduler;
182
154
  //# 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","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;AA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;AAGDd,MAAAA,YAAY,CAACoB,IAAb,CAAkB,UAACC,CAAD,EAAIC,CAAJ;AAAA,eAAUD,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"}
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,MAD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B,QAAF,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,97 +1,59 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
6
  exports.createLoaderWorker = createLoaderWorker;
9
7
 
10
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
-
12
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
-
14
- var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
15
-
16
8
  var _workerUtils = require("@loaders.gl/worker-utils");
17
9
 
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 requestId = 0;
10
+ let requestId = 0;
23
11
 
24
12
  function createLoaderWorker(loader) {
25
13
  if (typeof self === 'undefined') {
26
14
  return;
27
15
  }
28
16
 
29
- _workerUtils.WorkerBody.onmessage = function () {
30
- var _ref = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee(type, payload) {
31
- var input, _payload$options, options, result, message;
32
-
33
- return _regenerator.default.wrap(function _callee$(_context) {
34
- while (1) {
35
- switch (_context.prev = _context.next) {
36
- case 0:
37
- _context.t0 = type;
38
- _context.next = _context.t0 === 'process' ? 3 : 16;
39
- break;
40
-
41
- case 3:
42
- _context.prev = 3;
43
- input = payload.input, _payload$options = payload.options, options = _payload$options === void 0 ? {} : _payload$options;
44
- _context.next = 7;
45
- return parseData({
46
- loader: loader,
47
- arrayBuffer: input,
48
- options: options,
49
- context: {
50
- parse: parseOnMainThread
51
- }
52
- });
53
-
54
- case 7:
55
- result = _context.sent;
56
-
57
- _workerUtils.WorkerBody.postMessage('done', {
58
- result: result
59
- });
60
-
61
- _context.next = 15;
62
- break;
63
-
64
- case 11:
65
- _context.prev = 11;
66
- _context.t1 = _context["catch"](3);
67
- message = _context.t1 instanceof Error ? _context.t1.message : '';
68
-
69
- _workerUtils.WorkerBody.postMessage('error', {
70
- error: message
71
- });
72
-
73
- case 15:
74
- return _context.abrupt("break", 16);
75
-
76
- case 16:
77
- case "end":
78
- return _context.stop();
79
- }
17
+ _workerUtils.WorkerBody.onmessage = async (type, payload) => {
18
+ switch (type) {
19
+ case 'process':
20
+ try {
21
+ const {
22
+ input,
23
+ options = {}
24
+ } = payload;
25
+ const result = await parseData({
26
+ loader,
27
+ arrayBuffer: input,
28
+ options,
29
+ context: {
30
+ parse: parseOnMainThread
31
+ }
32
+ });
33
+
34
+ _workerUtils.WorkerBody.postMessage('done', {
35
+ result
36
+ });
37
+ } catch (error) {
38
+ const message = error instanceof Error ? error.message : '';
39
+
40
+ _workerUtils.WorkerBody.postMessage('error', {
41
+ error: message
42
+ });
80
43
  }
81
- }, _callee, null, [[3, 11]]);
82
- }));
83
44
 
84
- return function (_x, _x2) {
85
- return _ref.apply(this, arguments);
86
- };
87
- }();
45
+ break;
46
+
47
+ default:
48
+ }
49
+ };
88
50
  }
89
51
 
90
52
  function parseOnMainThread(arrayBuffer, options) {
91
- return new Promise(function (resolve, reject) {
92
- var id = requestId++;
53
+ return new Promise((resolve, reject) => {
54
+ const id = requestId++;
93
55
 
94
- var onMessage = function onMessage(type, payload) {
56
+ const onMessage = (type, payload) => {
95
57
  if (payload.id !== id) {
96
58
  return;
97
59
  }
@@ -115,72 +77,41 @@ function parseOnMainThread(arrayBuffer, options) {
115
77
 
116
78
  _workerUtils.WorkerBody.addEventListener(onMessage);
117
79
 
118
- var payload = {
119
- id: id,
80
+ const payload = {
81
+ id,
120
82
  input: arrayBuffer,
121
- options: options
83
+ options
122
84
  };
123
85
 
124
86
  _workerUtils.WorkerBody.postMessage('process', payload);
125
87
  });
126
88
  }
127
89
 
128
- function parseData(_x3) {
129
- return _parseData.apply(this, arguments);
130
- }
131
-
132
- function _parseData() {
133
- _parseData = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee2(_ref2) {
134
- var loader, arrayBuffer, options, context, data, parser, textDecoder;
135
- return _regenerator.default.wrap(function _callee2$(_context2) {
136
- while (1) {
137
- switch (_context2.prev = _context2.next) {
138
- case 0:
139
- loader = _ref2.loader, arrayBuffer = _ref2.arrayBuffer, options = _ref2.options, context = _ref2.context;
140
-
141
- if (!(loader.parseSync || loader.parse)) {
142
- _context2.next = 6;
143
- break;
144
- }
145
-
146
- data = arrayBuffer;
147
- parser = loader.parseSync || loader.parse;
148
- _context2.next = 13;
149
- break;
150
-
151
- case 6:
152
- if (!loader.parseTextSync) {
153
- _context2.next = 12;
154
- break;
155
- }
90
+ async function parseData({
91
+ loader,
92
+ arrayBuffer,
93
+ options,
94
+ context
95
+ }) {
96
+ let data;
97
+ let parser;
98
+
99
+ if (loader.parseSync || loader.parse) {
100
+ data = arrayBuffer;
101
+ parser = loader.parseSync || loader.parse;
102
+ } else if (loader.parseTextSync) {
103
+ const textDecoder = new TextDecoder();
104
+ data = textDecoder.decode(arrayBuffer);
105
+ parser = loader.parseTextSync;
106
+ } else {
107
+ throw new Error("Could not load data with ".concat(loader.name, " loader"));
108
+ }
156
109
 
157
- textDecoder = new TextDecoder();
158
- data = textDecoder.decode(arrayBuffer);
159
- parser = loader.parseTextSync;
160
- _context2.next = 13;
161
- break;
162
-
163
- case 12:
164
- throw new Error("Could not load data with ".concat(loader.name, " loader"));
165
-
166
- case 13:
167
- options = _objectSpread(_objectSpread({}, options), {}, {
168
- modules: loader && loader.options && loader.options.modules || {},
169
- worker: false
170
- });
171
- _context2.next = 16;
172
- return parser(data, _objectSpread({}, options), context, loader);
173
-
174
- case 16:
175
- return _context2.abrupt("return", _context2.sent);
176
-
177
- case 17:
178
- case "end":
179
- return _context2.stop();
180
- }
181
- }
182
- }, _callee2);
183
- }));
184
- return _parseData.apply(this, arguments);
110
+ options = { ...options,
111
+ modules: loader && loader.options && loader.options.modules || {},
112
+ worker: false
113
+ };
114
+ return await parser(data, { ...options
115
+ }, context, loader);
185
116
  }
186
117
  //# sourceMappingURL=create-loader-worker.js.map