@hpcc-js/util 2.46.1 → 2.47.1

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 (79) hide show
  1. package/LICENSE +43 -43
  2. package/dist/index.js +2373 -2281
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.min.js +1 -1
  5. package/dist/index.min.js.map +1 -1
  6. package/lib-es6/__package__.js +3 -3
  7. package/lib-es6/array.js +84 -84
  8. package/lib-es6/cache.js +60 -60
  9. package/lib-es6/debounce.js +85 -85
  10. package/lib-es6/dictionary.js +61 -61
  11. package/lib-es6/dispatch.js +101 -101
  12. package/lib-es6/esp.js +32 -32
  13. package/lib-es6/graph.js +348 -348
  14. package/lib-es6/graph2.js +603 -603
  15. package/lib-es6/hashSum.js +49 -49
  16. package/lib-es6/immutable.js +144 -54
  17. package/lib-es6/immutable.js.map +1 -1
  18. package/lib-es6/index.js +21 -21
  19. package/lib-es6/logging.js +178 -177
  20. package/lib-es6/logging.js.map +1 -1
  21. package/lib-es6/math.js +90 -90
  22. package/lib-es6/object.js +121 -121
  23. package/lib-es6/observer.js +79 -79
  24. package/lib-es6/platform.js +17 -17
  25. package/lib-es6/saxParser.js +125 -125
  26. package/lib-es6/stack.js +41 -41
  27. package/lib-es6/stateful.js +170 -170
  28. package/lib-es6/string.js +22 -22
  29. package/lib-es6/url.js +32 -32
  30. package/package.json +3 -3
  31. package/src/__package__.ts +2 -2
  32. package/src/array.ts +98 -98
  33. package/src/cache.ts +65 -65
  34. package/src/debounce.ts +88 -88
  35. package/src/dictionary.ts +69 -69
  36. package/src/dispatch.ts +119 -119
  37. package/src/esp.ts +32 -32
  38. package/src/graph.ts +353 -353
  39. package/src/graph2.ts +661 -661
  40. package/src/hashSum.ts +55 -55
  41. package/src/immutable.ts +156 -57
  42. package/src/index.ts +21 -21
  43. package/src/logging.ts +212 -211
  44. package/src/math.ts +92 -92
  45. package/src/object.ts +117 -117
  46. package/src/observer.ts +91 -91
  47. package/src/platform.ts +20 -20
  48. package/src/saxParser.ts +135 -135
  49. package/src/stack.ts +41 -41
  50. package/src/stateful.ts +178 -178
  51. package/src/string.ts +21 -21
  52. package/src/url.ts +27 -27
  53. package/types/__package__.d.ts +3 -3
  54. package/types/array.d.ts +13 -13
  55. package/types/cache.d.ts +20 -20
  56. package/types/debounce.d.ts +12 -12
  57. package/types/dictionary.d.ts +20 -20
  58. package/types/dispatch.d.ts +26 -26
  59. package/types/dispatch.d.ts.map +1 -1
  60. package/types/esp.d.ts +1 -1
  61. package/types/graph.d.ts +73 -73
  62. package/types/graph2.d.ts +111 -111
  63. package/types/hashSum.d.ts +1 -1
  64. package/types/immutable.d.ts +3 -2
  65. package/types/immutable.d.ts.map +1 -1
  66. package/types/index.d.ts +21 -21
  67. package/types/logging.d.ts +57 -57
  68. package/types/logging.d.ts.map +1 -1
  69. package/types/math.d.ts +70 -70
  70. package/types/object.d.ts +48 -48
  71. package/types/observer.d.ts +18 -18
  72. package/types/platform.d.ts +5 -5
  73. package/types/saxParser.d.ts +28 -28
  74. package/types/stack.d.ts +28 -28
  75. package/types/stateful.d.ts +33 -33
  76. package/types/string.d.ts +2 -2
  77. package/types/url.d.ts +2 -2
  78. package/types-3.4/__package__.d.ts +2 -2
  79. package/types-3.4/immutable.d.ts +1 -0
package/lib-es6/string.js CHANGED
@@ -1,23 +1,23 @@
1
- export function trim(str, char) {
2
- if (typeof char !== "string")
3
- return str;
4
- if (char.length === 0)
5
- return str;
6
- while (str.indexOf(char) === 0) {
7
- str = str.substring(1);
8
- }
9
- while (endsWith(str, char)) {
10
- str = str.substring(0, str.length - 1);
11
- }
12
- return str;
13
- }
14
- export function endsWith(origString, searchString, position) {
15
- var subjectString = origString.toString();
16
- if (typeof position !== "number" || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
17
- position = subjectString.length;
18
- }
19
- position -= searchString.length;
20
- var lastIndex = subjectString.lastIndexOf(searchString, position);
21
- return lastIndex !== -1 && lastIndex === position;
22
- }
1
+ export function trim(str, char) {
2
+ if (typeof char !== "string")
3
+ return str;
4
+ if (char.length === 0)
5
+ return str;
6
+ while (str.indexOf(char) === 0) {
7
+ str = str.substring(1);
8
+ }
9
+ while (endsWith(str, char)) {
10
+ str = str.substring(0, str.length - 1);
11
+ }
12
+ return str;
13
+ }
14
+ export function endsWith(origString, searchString, position) {
15
+ var subjectString = origString.toString();
16
+ if (typeof position !== "number" || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
17
+ position = subjectString.length;
18
+ }
19
+ position -= searchString.length;
20
+ var lastIndex = subjectString.lastIndexOf(searchString, position);
21
+ return lastIndex !== -1 && lastIndex === position;
22
+ }
23
23
  //# sourceMappingURL=string.js.map
package/lib-es6/url.js CHANGED
@@ -1,33 +1,33 @@
1
- import { __spreadArray } from "tslib";
2
- export function join() {
3
- var segments = [];
4
- for (var _i = 0; _i < arguments.length; _i++) {
5
- segments[_i] = arguments[_i];
6
- }
7
- var parts = segments.reduce(function (parts, segment) {
8
- // Remove leading slashes from non-first part.
9
- if (parts.length > 0) {
10
- segment = segment.replace(/^\//, "");
11
- }
12
- // Remove trailing slashes.
13
- segment = segment.replace(/\/$/, "");
14
- return __spreadArray(__spreadArray([], parts, true), segment.split("/"), true);
15
- }, []);
16
- var resultParts = [];
17
- for (var _a = 0, parts_1 = parts; _a < parts_1.length; _a++) {
18
- var part = parts_1[_a];
19
- if (part === ".") {
20
- continue;
21
- }
22
- if (part === "..") {
23
- resultParts.pop();
24
- continue;
25
- }
26
- resultParts.push(part);
27
- }
28
- return resultParts.join("/");
29
- }
30
- export function dirname(path) {
31
- return join(path, "..");
32
- }
1
+ import { __spreadArray } from "tslib";
2
+ export function join() {
3
+ var segments = [];
4
+ for (var _i = 0; _i < arguments.length; _i++) {
5
+ segments[_i] = arguments[_i];
6
+ }
7
+ var parts = segments.reduce(function (parts, segment) {
8
+ // Remove leading slashes from non-first part.
9
+ if (parts.length > 0) {
10
+ segment = segment.replace(/^\//, "");
11
+ }
12
+ // Remove trailing slashes.
13
+ segment = segment.replace(/\/$/, "");
14
+ return __spreadArray(__spreadArray([], parts, true), segment.split("/"), true);
15
+ }, []);
16
+ var resultParts = [];
17
+ for (var _a = 0, parts_1 = parts; _a < parts_1.length; _a++) {
18
+ var part = parts_1[_a];
19
+ if (part === ".") {
20
+ continue;
21
+ }
22
+ if (part === "..") {
23
+ resultParts.pop();
24
+ continue;
25
+ }
26
+ resultParts.push(part);
27
+ }
28
+ return resultParts.join("/");
29
+ }
30
+ export function dirname(path) {
31
+ return join(path, "..");
32
+ }
33
33
  //# sourceMappingURL=url.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hpcc-js/util",
3
- "version": "2.46.1",
3
+ "version": "2.47.1",
4
4
  "description": "hpcc-js - Utilities",
5
5
  "main": "dist/index.js",
6
6
  "module": "lib-es6/index",
@@ -34,7 +34,7 @@
34
34
  "build": "npm run compile-es6 && npm run bundle",
35
35
  "watch": "npm-run-all compile-es6 -p compile-es6-watch bundle-watch",
36
36
  "stamp": "node ../../node_modules/@hpcc-js/bundle/src/stamp.js",
37
- "lint": "eslint src/**/*.ts",
37
+ "lint": "eslint ./src",
38
38
  "docs": "typedoc --options tdoptions.json .",
39
39
  "update": "npx npm-check-updates -u -t minor"
40
40
  },
@@ -56,5 +56,5 @@
56
56
  "url": "https://github.com/hpcc-systems/Visualization/issues"
57
57
  },
58
58
  "homepage": "https://github.com/hpcc-systems/Visualization",
59
- "gitHead": "bc245d3dccc358e21bc1b2103f6d88bb75b9f999"
59
+ "gitHead": "b7c5d9a853196b53078d665df4368629a1a87903"
60
60
  }
@@ -1,3 +1,3 @@
1
1
  export const PKG_NAME = "@hpcc-js/util";
2
- export const PKG_VERSION = "2.46.1";
3
- export const BUILD_VERSION = "2.102.1";
2
+ export const PKG_VERSION = "2.47.1";
3
+ export const BUILD_VERSION = "2.103.1";
package/src/array.ts CHANGED
@@ -1,98 +1,98 @@
1
- // Based on: https://tc39.github.io/ecma262/#sec-array.prototype.find
2
- export function find<T>(o: ReadonlyArray<T>, predicate: (value: T, index: number) => boolean) {
3
- // 1. Let O be ? ToObject(this value).
4
- if (o == null) {
5
- throw new TypeError('"o" is null or not defined');
6
- }
7
-
8
- // 2. Let len be ? ToLength(? Get(O, "length")).
9
- const len = o.length >>> 0;
10
-
11
- // 3. If IsCallable(predicate) is false, throw a TypeError exception.
12
- if (typeof predicate !== "function") {
13
- throw new TypeError("predicate must be a function");
14
- }
15
-
16
- // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
17
- const thisArg = arguments[1];
18
-
19
- // 5. Let k be 0.
20
- let k = 0;
21
-
22
- // 6. Repeat, while k < len
23
- while (k < len) {
24
- // a. Let Pk be ! ToString(k).
25
- // b. Let kValue be ? Get(O, Pk).
26
- // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
27
- // d. If testResult is true, return kValue.
28
- const kValue = o[k];
29
- if (predicate.call(thisArg, kValue, k, o)) {
30
- return kValue;
31
- }
32
- // e. Increase k by 1.
33
- k++;
34
- }
35
-
36
- // 7. Return undefined.
37
- return undefined;
38
- }
39
-
40
- export interface IDifferences2<T> {
41
- update: T[];
42
- exit: T[];
43
- enter: T[];
44
- }
45
-
46
- export function compare<T>(before: readonly T[], after: readonly T[]): IDifferences2<T> {
47
- const retVal: IDifferences2<T> = {
48
- update: [],
49
- exit: [],
50
- enter: [...after]
51
- };
52
- for (const row of before) {
53
- const otherIdx = retVal.enter.indexOf(row);
54
- if (otherIdx >= 0) {
55
- retVal.update.push(row);
56
- retVal.enter.splice(otherIdx, 1);
57
- } else {
58
- retVal.exit.push(row);
59
- }
60
- }
61
- return retVal;
62
- }
63
-
64
- export interface IDifferences2<T> {
65
- enter: T[];
66
- update: T[];
67
- exit: T[];
68
- }
69
-
70
- export function compare2<T>(before: readonly T[], after: readonly T[], idFunc: (itme: T) => string | number, updateFunc: (before: T, after: T) => T = (before, after) => after): IDifferences2<T> {
71
- const retVal: IDifferences2<T> = {
72
- update: [],
73
- exit: [],
74
- enter: []
75
- };
76
- if (before === after) {
77
- retVal.update = before as T[];
78
- return retVal;
79
- }
80
- const unknownMap: { [key: string]: T } = {};
81
- after.forEach(item => {
82
- unknownMap[idFunc(item)] = item;
83
- });
84
- for (const row of before) {
85
- const id = idFunc(row);
86
- const item = unknownMap[id];
87
- if (item !== undefined) {
88
- delete unknownMap[id];
89
- retVal.update.push(updateFunc(row, item));
90
- } else {
91
- retVal.exit.push(row);
92
- }
93
- }
94
- for (const key in unknownMap) {
95
- retVal.enter.push(unknownMap[key]);
96
- }
97
- return retVal;
98
- }
1
+ // Based on: https://tc39.github.io/ecma262/#sec-array.prototype.find
2
+ export function find<T>(o: ReadonlyArray<T>, predicate: (value: T, index: number) => boolean) {
3
+ // 1. Let O be ? ToObject(this value).
4
+ if (o == null) {
5
+ throw new TypeError('"o" is null or not defined');
6
+ }
7
+
8
+ // 2. Let len be ? ToLength(? Get(O, "length")).
9
+ const len = o.length >>> 0;
10
+
11
+ // 3. If IsCallable(predicate) is false, throw a TypeError exception.
12
+ if (typeof predicate !== "function") {
13
+ throw new TypeError("predicate must be a function");
14
+ }
15
+
16
+ // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
17
+ const thisArg = arguments[1];
18
+
19
+ // 5. Let k be 0.
20
+ let k = 0;
21
+
22
+ // 6. Repeat, while k < len
23
+ while (k < len) {
24
+ // a. Let Pk be ! ToString(k).
25
+ // b. Let kValue be ? Get(O, Pk).
26
+ // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
27
+ // d. If testResult is true, return kValue.
28
+ const kValue = o[k];
29
+ if (predicate.call(thisArg, kValue, k, o)) {
30
+ return kValue;
31
+ }
32
+ // e. Increase k by 1.
33
+ k++;
34
+ }
35
+
36
+ // 7. Return undefined.
37
+ return undefined;
38
+ }
39
+
40
+ export interface IDifferences2<T> {
41
+ update: T[];
42
+ exit: T[];
43
+ enter: T[];
44
+ }
45
+
46
+ export function compare<T>(before: readonly T[], after: readonly T[]): IDifferences2<T> {
47
+ const retVal: IDifferences2<T> = {
48
+ update: [],
49
+ exit: [],
50
+ enter: [...after]
51
+ };
52
+ for (const row of before) {
53
+ const otherIdx = retVal.enter.indexOf(row);
54
+ if (otherIdx >= 0) {
55
+ retVal.update.push(row);
56
+ retVal.enter.splice(otherIdx, 1);
57
+ } else {
58
+ retVal.exit.push(row);
59
+ }
60
+ }
61
+ return retVal;
62
+ }
63
+
64
+ export interface IDifferences2<T> {
65
+ enter: T[];
66
+ update: T[];
67
+ exit: T[];
68
+ }
69
+
70
+ export function compare2<T>(before: readonly T[], after: readonly T[], idFunc: (itme: T) => string | number, updateFunc: (before: T, after: T) => T = (before, after) => after): IDifferences2<T> {
71
+ const retVal: IDifferences2<T> = {
72
+ update: [],
73
+ exit: [],
74
+ enter: []
75
+ };
76
+ if (before === after) {
77
+ retVal.update = before as T[];
78
+ return retVal;
79
+ }
80
+ const unknownMap: { [key: string]: T } = {};
81
+ after.forEach(item => {
82
+ unknownMap[idFunc(item)] = item;
83
+ });
84
+ for (const row of before) {
85
+ const id = idFunc(row);
86
+ const item = unknownMap[id];
87
+ if (item !== undefined) {
88
+ delete unknownMap[id];
89
+ retVal.update.push(updateFunc(row, item));
90
+ } else {
91
+ retVal.exit.push(row);
92
+ }
93
+ }
94
+ for (const key in unknownMap) {
95
+ retVal.enter.push(unknownMap[key]);
96
+ }
97
+ return retVal;
98
+ }
package/src/cache.ts CHANGED
@@ -1,65 +1,65 @@
1
- import { hashSum } from "./hashSum";
2
-
3
- export class Cache<I, C> {
4
- private _cache: { [id: string]: C } = {};
5
- private _calcID: (espObj: I | C) => string;
6
-
7
- static hash(...args: any[]) {
8
- return hashSum({ ...args });
9
- }
10
-
11
- constructor(calcID: (espObj: I | C) => string) {
12
- this._calcID = calcID;
13
- }
14
-
15
- has(espObj: I): boolean {
16
- return this._calcID(espObj) in this._cache;
17
- }
18
-
19
- set(obj: C): C {
20
- this._cache[this._calcID(obj)] = obj;
21
- return obj;
22
- }
23
-
24
- get(espObj: I): C | null;
25
- get(espObj: I, factory: () => C): C;
26
- get(espObj: I, factory?: () => C): C | null {
27
- const retVal = this._cache[this._calcID(espObj)];
28
- if (!retVal) {
29
- return factory ? this.set(factory()) : null;
30
- }
31
- return retVal;
32
- }
33
- }
34
-
35
- export class AsyncCache<I, C> {
36
- private _cache: { [id: string]: Promise<C> } = {};
37
- private _calcID: (espObj: I | C) => string;
38
-
39
- static hash(...args: any[]) {
40
- return hashSum({ ...args });
41
- }
42
-
43
- constructor(calcID: (espObj: I | C) => string) {
44
- this._calcID = calcID;
45
- }
46
-
47
- has(espObj: I): boolean {
48
- return this._calcID(espObj) in this._cache;
49
- }
50
-
51
- set(espObj: I, obj: Promise<C>): Promise<C> {
52
- this._cache[this._calcID(espObj)] = obj;
53
- return obj;
54
- }
55
-
56
- get(espObj: I): Promise<C | null>;
57
- get(espObj: I, factory: () => Promise<C>): Promise<C>;
58
- get(espObj: I, factory?: () => Promise<C>): Promise<C | null> {
59
- const retVal = this._cache[this._calcID(espObj)];
60
- if (!retVal) {
61
- return factory ? this.set(espObj, factory()) : Promise.resolve(null);
62
- }
63
- return retVal;
64
- }
65
- }
1
+ import { hashSum } from "./hashSum";
2
+
3
+ export class Cache<I, C> {
4
+ private _cache: { [id: string]: C } = {};
5
+ private _calcID: (espObj: I | C) => string;
6
+
7
+ static hash(...args: any[]) {
8
+ return hashSum({ ...args });
9
+ }
10
+
11
+ constructor(calcID: (espObj: I | C) => string) {
12
+ this._calcID = calcID;
13
+ }
14
+
15
+ has(espObj: I): boolean {
16
+ return this._calcID(espObj) in this._cache;
17
+ }
18
+
19
+ set(obj: C): C {
20
+ this._cache[this._calcID(obj)] = obj;
21
+ return obj;
22
+ }
23
+
24
+ get(espObj: I): C | null;
25
+ get(espObj: I, factory: () => C): C;
26
+ get(espObj: I, factory?: () => C): C | null {
27
+ const retVal = this._cache[this._calcID(espObj)];
28
+ if (!retVal) {
29
+ return factory ? this.set(factory()) : null;
30
+ }
31
+ return retVal;
32
+ }
33
+ }
34
+
35
+ export class AsyncCache<I, C> {
36
+ private _cache: { [id: string]: Promise<C> } = {};
37
+ private _calcID: (espObj: I | C) => string;
38
+
39
+ static hash(...args: any[]) {
40
+ return hashSum({ ...args });
41
+ }
42
+
43
+ constructor(calcID: (espObj: I | C) => string) {
44
+ this._calcID = calcID;
45
+ }
46
+
47
+ has(espObj: I): boolean {
48
+ return this._calcID(espObj) in this._cache;
49
+ }
50
+
51
+ set(espObj: I, obj: Promise<C>): Promise<C> {
52
+ this._cache[this._calcID(espObj)] = obj;
53
+ return obj;
54
+ }
55
+
56
+ get(espObj: I): Promise<C | null>;
57
+ get(espObj: I, factory: () => Promise<C>): Promise<C>;
58
+ get(espObj: I, factory?: () => Promise<C>): Promise<C | null> {
59
+ const retVal = this._cache[this._calcID(espObj)];
60
+ if (!retVal) {
61
+ return factory ? this.set(espObj, factory()) : Promise.resolve(null);
62
+ }
63
+ return retVal;
64
+ }
65
+ }
package/src/debounce.ts CHANGED
@@ -1,88 +1,88 @@
1
- import { hashSum } from "./hashSum";
2
-
3
- export function debounce<R extends Promise<any>>(fn: () => R, timeout?: number): () => R;
4
- export function debounce<P1, R extends Promise<any>>(fn: (param1: P1) => R, timeout?: number): (param1: P1) => R;
5
- export function debounce<P1, P2, R extends Promise<any>>(fn: (param1: P1, param2: P2) => R, timeout?: number): (param1: P1, param2: P2) => R;
6
- export function debounce<P1, P2, P3, R extends Promise<any>>(fn: (param1: P1, param2: P2, param3: P3) => R, timeout?: number): (param1: P1, param2: P2, param3: P3) => R;
7
- export function debounce<P1, P2, P3, P4, R extends Promise<any>>(fn: (param1: P1, param2: P2, param3: P3, param4: P4) => R, timeout?: number): (param1: P1, param2: P2, param3: P3, param4: P4) => R;
8
- export function debounce<TParam, R extends Promise<any>>(fn: (...params: TParam[]) => R, timeout?: number): (...params: TParam[]) => R {
9
- const promises: { [key: string]: { promise: R, clockStart: number } | null } = {};
10
-
11
- return (...params) => {
12
- const hash = hashSum(params);
13
- if (!promises[hash]) {
14
- promises[hash] = {
15
- clockStart: Date.now(),
16
- promise: fn(...params).then(response => {
17
- if (timeout === undefined) {
18
- promises[hash] = null;
19
- } else {
20
- setTimeout(() => {
21
- promises[hash] = null;
22
- }, Math.max(timeout - (Date.now() - promises[hash]!.clockStart), 0));
23
- }
24
- return response;
25
- }).catch(e => {
26
- promises[hash] = null;
27
- throw e;
28
- }) as R
29
- };
30
- }
31
- return promises[hash]!.promise;
32
- };
33
- }
34
-
35
- export function promiseTimeout<T>(ms: number, promise: Promise<T>): Promise<T> {
36
- let id: number;
37
- const timeout = new Promise((resolve, reject) => {
38
- id = setTimeout(() => {
39
- clearTimeout(id);
40
- reject("Timed out in " + ms + "ms.");
41
- }, ms);
42
- });
43
-
44
- return Promise.race([
45
- promise,
46
- timeout
47
- ]).then((response: T) => {
48
- clearTimeout(id);
49
- return response;
50
- }).catch(e => {
51
- clearTimeout(id);
52
- throw e;
53
- });
54
- }
55
-
56
- export class AsyncOrderedQueue {
57
- private _q: Array<Promise<any>> = [];
58
-
59
- private isTop(p: Promise<any>): boolean {
60
- return this._q[0] === p;
61
- }
62
-
63
- push<T>(p: Promise<T>): Promise<T> {
64
- const retVal = p.then(response => {
65
- if (this.isTop(retVal)) {
66
- this._q.shift();
67
- return response;
68
- }
69
- return new Promise<T>((resolve, reject) => {
70
- const intervalHandler = setInterval(() => {
71
- if (this.isTop(retVal)) {
72
- clearInterval(intervalHandler);
73
- this._q.shift();
74
- resolve(response);
75
- }
76
- }, 20);
77
- });
78
- });
79
- this._q.push(retVal);
80
- return retVal;
81
- }
82
- }
83
-
84
- export function sleep(ms: number): Promise<void> {
85
- return new Promise(resolve => {
86
- setTimeout(() => resolve(), ms);
87
- });
88
- }
1
+ import { hashSum } from "./hashSum";
2
+
3
+ export function debounce<R extends Promise<any>>(fn: () => R, timeout?: number): () => R;
4
+ export function debounce<P1, R extends Promise<any>>(fn: (param1: P1) => R, timeout?: number): (param1: P1) => R;
5
+ export function debounce<P1, P2, R extends Promise<any>>(fn: (param1: P1, param2: P2) => R, timeout?: number): (param1: P1, param2: P2) => R;
6
+ export function debounce<P1, P2, P3, R extends Promise<any>>(fn: (param1: P1, param2: P2, param3: P3) => R, timeout?: number): (param1: P1, param2: P2, param3: P3) => R;
7
+ export function debounce<P1, P2, P3, P4, R extends Promise<any>>(fn: (param1: P1, param2: P2, param3: P3, param4: P4) => R, timeout?: number): (param1: P1, param2: P2, param3: P3, param4: P4) => R;
8
+ export function debounce<TParam, R extends Promise<any>>(fn: (...params: TParam[]) => R, timeout?: number): (...params: TParam[]) => R {
9
+ const promises: { [key: string]: { promise: R, clockStart: number } | null } = {};
10
+
11
+ return (...params) => {
12
+ const hash = hashSum(params);
13
+ if (!promises[hash]) {
14
+ promises[hash] = {
15
+ clockStart: Date.now(),
16
+ promise: fn(...params).then(response => {
17
+ if (timeout === undefined) {
18
+ promises[hash] = null;
19
+ } else {
20
+ setTimeout(() => {
21
+ promises[hash] = null;
22
+ }, Math.max(timeout - (Date.now() - promises[hash]!.clockStart), 0));
23
+ }
24
+ return response;
25
+ }).catch(e => {
26
+ promises[hash] = null;
27
+ throw e;
28
+ }) as R
29
+ };
30
+ }
31
+ return promises[hash]!.promise;
32
+ };
33
+ }
34
+
35
+ export function promiseTimeout<T>(ms: number, promise: Promise<T>): Promise<T> {
36
+ let id: number;
37
+ const timeout = new Promise((resolve, reject) => {
38
+ id = setTimeout(() => {
39
+ clearTimeout(id);
40
+ reject("Timed out in " + ms + "ms.");
41
+ }, ms);
42
+ });
43
+
44
+ return Promise.race([
45
+ promise,
46
+ timeout
47
+ ]).then((response: T) => {
48
+ clearTimeout(id);
49
+ return response;
50
+ }).catch(e => {
51
+ clearTimeout(id);
52
+ throw e;
53
+ });
54
+ }
55
+
56
+ export class AsyncOrderedQueue {
57
+ private _q: Array<Promise<any>> = [];
58
+
59
+ private isTop(p: Promise<any>): boolean {
60
+ return this._q[0] === p;
61
+ }
62
+
63
+ push<T>(p: Promise<T>): Promise<T> {
64
+ const retVal = p.then(response => {
65
+ if (this.isTop(retVal)) {
66
+ this._q.shift();
67
+ return response;
68
+ }
69
+ return new Promise<T>((resolve, reject) => {
70
+ const intervalHandler = setInterval(() => {
71
+ if (this.isTop(retVal)) {
72
+ clearInterval(intervalHandler);
73
+ this._q.shift();
74
+ resolve(response);
75
+ }
76
+ }, 20);
77
+ });
78
+ });
79
+ this._q.push(retVal);
80
+ return retVal;
81
+ }
82
+ }
83
+
84
+ export function sleep(ms: number): Promise<void> {
85
+ return new Promise(resolve => {
86
+ setTimeout(() => resolve(), ms);
87
+ });
88
+ }