@idlebox/common 1.3.6 → 1.3.7
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/lib/string/concatType.generated.cjs +1 -1
- package/lib/string/concatType.generated.cjs.map +1 -1
- package/lib/string/concatType.generated.js +1 -1
- package/lib/string/concatType.generated.js.map +1 -1
- package/package.json +1 -1
- package/.rush/temp/8ffc60c66c29712a4b0392d78f8ceb08fd99cd43.log +0 -10
- package/.rush/temp/shrinkwrap-deps.json +0 -671
- package/Gulpfile.js +0 -4
- package/build-script.json +0 -55
- package/common.build.log +0 -42
- package/config/rush-project.json +0 -4
- package/docs/common.api.json +0 -21006
- package/docs/common.api.md +0 -1125
- package/docs/tsdoc-metadata.json +0 -11
- package/idlebox-common-v1.3.2.tgz +0 -0
- package/idlebox-common-v1.3.3.tgz +0 -0
- package/src/array/arrayDiff.ts +0 -31
- package/src/array/arraySame.ts +0 -15
- package/src/array/arrayUnique.ts +0 -50
- package/src/array/normalizeArray.ts +0 -13
- package/src/array/sortAlpha.ts +0 -15
- package/src/date/consts.ts +0 -5
- package/src/date/isInvalid.ts +0 -6
- package/src/date/sibling.ts +0 -28
- package/src/date/timeString.ts +0 -150
- package/src/date/unix.ts +0 -13
- package/src/debugging/tryInspect.ts +0 -37
- package/src/error/convertUnknown.ts +0 -10
- package/src/error/getFrame.ts +0 -13
- package/src/function/asyncCallbackList.ts +0 -75
- package/src/function/callbackList.ts +0 -84
- package/src/function/delayCallbackList.ts +0 -45
- package/src/function/functionName.ts +0 -39
- package/src/lifecycle/dispose/bridges/rxjs.ts +0 -6
- package/src/lifecycle/dispose/disposableEvent.ts +0 -117
- package/src/lifecycle/dispose/disposedError.ts +0 -16
- package/src/lifecycle/dispose/lifecycle.async.ts +0 -61
- package/src/lifecycle/dispose/lifecycle.global.ts +0 -61
- package/src/lifecycle/dispose/lifecycle.sync.ts +0 -79
- package/src/lifecycle/dispose/lifecycle.ts +0 -28
- package/src/lifecycle/event/event.ts +0 -63
- package/src/lifecycle/promise/cancel.ts +0 -16
- package/src/lifecycle/promise/cancellationToken/driver.browser.ts +0 -55
- package/src/lifecycle/promise/cancellationToken/driver.common.ts +0 -43
- package/src/lifecycle/promise/cancellationToken/source.ts +0 -48
- package/src/lifecycle/promise/deferredPromise.ts +0 -102
- package/src/lifecycle/timeout/timeout.ts +0 -48
- package/src/lifecycle/timeout/timeoutError.ts +0 -16
- package/src/log/logger.ts +0 -148
- package/src/mapSet/customSet.ts +0 -91
- package/src/mapSet/extendMap.ts +0 -40
- package/src/misc/assertNotNull.ts +0 -21
- package/src/object/definePublicConstant.ts +0 -10
- package/src/object/initOnRead.ts +0 -27
- package/src/object/objectPath.ts +0 -10
- package/src/object/objectSame.ts +0 -52
- package/src/path/isAbsolute.ts +0 -11
- package/src/path/normalizePath.ts +0 -8
- package/src/path/pathArray.ts +0 -42
- package/src/platform/globalObject.ts +0 -18
- package/src/platform/globalSingleton.ts +0 -82
- package/src/platform/globalSymbol.ts +0 -36
- package/src/platform/os.ts +0 -45
- package/src/promise/awaitIterator.ts +0 -19
- package/src/promise/finishAllPromise.ts +0 -50
- package/src/promise/promiseBool.ts +0 -10
- package/src/promise/promisePool.ts +0 -40
- package/src/promise/timeoutPromisePool.ts +0 -22
- package/src/reflection/classes/hookClass.ts +0 -47
- package/src/reflection/classes/singleton.ts +0 -33
- package/src/reflection/methods/bind.ts +0 -30
- package/src/reflection/methods/initOnRead.ts +0 -11
- package/src/reflection/methods/memorize.ts +0 -33
- package/src/string/castCase.ts +0 -44
- package/src/string/concatType.generated.ts +0 -255
- package/src/string/concatType.generator.ts +0 -31
- package/src/string/escapeRegexp.ts +0 -4
- package/src/string/pad2.ts +0 -11
- package/src/string/sizeString.ts +0 -52
- package/src/tsconfig.json +0 -13
package/docs/tsdoc-metadata.json
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
-
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
-
{
|
|
4
|
-
"tsdocVersion": "0.12",
|
|
5
|
-
"toolPackages": [
|
|
6
|
-
{
|
|
7
|
-
"packageName": "@microsoft/api-extractor",
|
|
8
|
-
"packageVersion": "7.19.5"
|
|
9
|
-
}
|
|
10
|
-
]
|
|
11
|
-
}
|
|
Binary file
|
|
Binary file
|
package/src/array/arrayDiff.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @public
|
|
3
|
-
*/
|
|
4
|
-
export interface IArrayUpdate<T> {
|
|
5
|
-
add: T[];
|
|
6
|
-
del: T[];
|
|
7
|
-
same: T[];
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Compare two array, returns the difference from `before` to `after`
|
|
12
|
-
* @public
|
|
13
|
-
*/
|
|
14
|
-
export function arrayDiff<T>(before: T[], after: T[]) {
|
|
15
|
-
before = before.slice().sort();
|
|
16
|
-
const add: T[] = after.slice().sort();
|
|
17
|
-
const del: T[] = [];
|
|
18
|
-
const same: T[] = [];
|
|
19
|
-
next: while (before.length) {
|
|
20
|
-
const item = before.pop()!;
|
|
21
|
-
for (let j = add.length - 1; j >= 0; j--) {
|
|
22
|
-
if (item === add[j]) {
|
|
23
|
-
same.push(item);
|
|
24
|
-
add.splice(j, 1);
|
|
25
|
-
continue next;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
del.push(item);
|
|
29
|
-
}
|
|
30
|
-
return { add, del, same };
|
|
31
|
-
}
|
package/src/array/arraySame.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* is the two array EXACTLY same
|
|
3
|
-
* @public
|
|
4
|
-
*/
|
|
5
|
-
export function isArraySame<T>(a1: T[], a2: T[]): boolean {
|
|
6
|
-
if (a1.length !== a2.length) {
|
|
7
|
-
return false;
|
|
8
|
-
}
|
|
9
|
-
for (let i = a1.length - 1; i >= 0; i--) {
|
|
10
|
-
if (a1[i] !== a2[i]) {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return true;
|
|
15
|
-
}
|
package/src/array/arrayUnique.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns a new array without duplicate values
|
|
3
|
-
* @public
|
|
4
|
-
*/
|
|
5
|
-
export function arrayUnique<T>(arr: T[]): T[] {
|
|
6
|
-
return arr.filter((item, index) => {
|
|
7
|
-
return arr.lastIndexOf(item) === index;
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Removes duplicate values from an array
|
|
13
|
-
* @public
|
|
14
|
-
*/
|
|
15
|
-
export function arrayUniqueReference(arr: any[]): void {
|
|
16
|
-
for (let index = arr.length - 1; index >= 0; index--) {
|
|
17
|
-
if (arr.lastIndexOf(arr[index]) !== index) {
|
|
18
|
-
arr.splice(index, 1);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface IUniqueIdFactory<T> {
|
|
24
|
-
(item: T): string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function defaultFactory(t: any) {
|
|
28
|
-
return t as string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Returns a function to be used in <arr>.filter()
|
|
33
|
-
*
|
|
34
|
-
* the returned function can use multiple times, it will remember all values inter multiple arrays
|
|
35
|
-
*
|
|
36
|
-
* @param {IUniqueIdFactory} idFactory function takes an array element, return it's id to be compare with each other
|
|
37
|
-
* @public
|
|
38
|
-
*/
|
|
39
|
-
export function uniqueFilter<T>(idFactory: IUniqueIdFactory<T> = defaultFactory) {
|
|
40
|
-
const seen: { [id: string]: true } = {};
|
|
41
|
-
return function uniqueFilterInner(item: T): boolean {
|
|
42
|
-
const id = idFactory(item);
|
|
43
|
-
if (seen[id]) {
|
|
44
|
-
return false;
|
|
45
|
-
} else {
|
|
46
|
-
seen[id] = true;
|
|
47
|
-
return true;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ensure a value is an array
|
|
3
|
-
* @public
|
|
4
|
-
*/
|
|
5
|
-
export function normalizeArray<T>(input: T | T[]): T[] {
|
|
6
|
-
if (input && Array.isArray(input)) {
|
|
7
|
-
return input;
|
|
8
|
-
} else if (typeof input !== undefined) {
|
|
9
|
-
return [input];
|
|
10
|
-
} else {
|
|
11
|
-
return [];
|
|
12
|
-
}
|
|
13
|
-
}
|
package/src/array/sortAlpha.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sort string array alphabet order
|
|
3
|
-
*
|
|
4
|
-
* to be used in <arr>.sort()
|
|
5
|
-
* @public
|
|
6
|
-
*/
|
|
7
|
-
export function sortByString(a: string, b: string): number {
|
|
8
|
-
if (a === b) {
|
|
9
|
-
return 0;
|
|
10
|
-
} else if (a > b) {
|
|
11
|
-
return 1;
|
|
12
|
-
} else {
|
|
13
|
-
return -1;
|
|
14
|
-
}
|
|
15
|
-
}
|
package/src/date/consts.ts
DELETED
package/src/date/isInvalid.ts
DELETED
package/src/date/sibling.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export function nextSecond(d: Date, n = 1) {
|
|
2
|
-
d.setSeconds(d.getSeconds() + n);
|
|
3
|
-
return d;
|
|
4
|
-
}
|
|
5
|
-
export function nextMinute(d: Date, n = 1) {
|
|
6
|
-
d.setMinutes(d.getMinutes() + n);
|
|
7
|
-
return d;
|
|
8
|
-
}
|
|
9
|
-
export function nextHour(d: Date, n = 1) {
|
|
10
|
-
d.setHours(d.getHours() + n);
|
|
11
|
-
return d;
|
|
12
|
-
}
|
|
13
|
-
export function nextDay(d: Date, n = 1) {
|
|
14
|
-
d.setDate(d.getDate() + n);
|
|
15
|
-
return d;
|
|
16
|
-
}
|
|
17
|
-
export function nextWeek(d: Date, n = 1) {
|
|
18
|
-
d.setDate(d.getDate() + n * 7);
|
|
19
|
-
return d;
|
|
20
|
-
}
|
|
21
|
-
export function nextMonth(d: Date, n = 1) {
|
|
22
|
-
d.setMonth(d.getMonth() + n);
|
|
23
|
-
return d;
|
|
24
|
-
}
|
|
25
|
-
export function nextYear(d: Date, n = 1) {
|
|
26
|
-
d.setFullYear(d.getFullYear() + n);
|
|
27
|
-
return d;
|
|
28
|
-
}
|
package/src/date/timeString.ts
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import { pad2 } from '../string/pad2';
|
|
2
|
-
|
|
3
|
-
export namespace humanDate {
|
|
4
|
-
/**
|
|
5
|
-
* Format: HH:mm:ss
|
|
6
|
-
*/
|
|
7
|
-
export function time(date: Date | string | number) {
|
|
8
|
-
if (typeof date === 'string') {
|
|
9
|
-
date = parseInt(date);
|
|
10
|
-
}
|
|
11
|
-
date = new Date(date as number);
|
|
12
|
-
return `${pad2(date.getHours())}:${pad2(date.getMinutes())}:${pad2(date.getSeconds())}`;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Format: YYYY-MM-dd
|
|
17
|
-
*
|
|
18
|
-
* separator can change
|
|
19
|
-
*/
|
|
20
|
-
export function date(date: Date | string | number, sp = '-') {
|
|
21
|
-
if (typeof date === 'string') {
|
|
22
|
-
date = parseInt(date);
|
|
23
|
-
}
|
|
24
|
-
date = new Date(date as number);
|
|
25
|
-
return `${date.getFullYear()}${sp}${pad2(date.getMonth() + 1)}${sp}${pad2(date.getDate())}`;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Format: YYYY-MM-dd HH:mm:ss
|
|
30
|
-
*/
|
|
31
|
-
export function datetime(date: Date | string | number) {
|
|
32
|
-
if (typeof date === 'string') {
|
|
33
|
-
date = parseInt(date);
|
|
34
|
-
}
|
|
35
|
-
date = new Date(date as number);
|
|
36
|
-
return (
|
|
37
|
-
`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}` +
|
|
38
|
-
' ' +
|
|
39
|
-
`${pad2(date.getHours())}:${pad2(date.getMinutes())}:${pad2(date.getSeconds())}`
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export interface ITimeFormatter {
|
|
44
|
-
(s: number): string;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface IFormatters {
|
|
48
|
-
s: ITimeFormatter;
|
|
49
|
-
m: ITimeFormatter;
|
|
50
|
-
h: ITimeFormatter;
|
|
51
|
-
d: ITimeFormatter;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const formatters: IFormatters = {
|
|
55
|
-
s(v: number) {
|
|
56
|
-
return v + 's';
|
|
57
|
-
},
|
|
58
|
-
m(v: number) {
|
|
59
|
-
return v + 'm';
|
|
60
|
-
},
|
|
61
|
-
h(v: number) {
|
|
62
|
-
return v + 'h';
|
|
63
|
-
},
|
|
64
|
-
d(v: number) {
|
|
65
|
-
return v + 'd';
|
|
66
|
-
},
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* set format for time delta
|
|
71
|
-
*/
|
|
72
|
-
export function setLocaleFormatter(formatter: Partial<IFormatters>) {
|
|
73
|
-
Object.assign(formatters, formatter);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* format time delta (in ms) to string, like: '1d'
|
|
78
|
-
* when ms<=0, returns '0s'
|
|
79
|
-
*
|
|
80
|
-
* format can set by `setLocaleFormatter`
|
|
81
|
-
* day is the largest unit
|
|
82
|
-
*/
|
|
83
|
-
export function deltaTiny(ms: number) {
|
|
84
|
-
if (ms <= 0) {
|
|
85
|
-
return '0s';
|
|
86
|
-
}
|
|
87
|
-
if (ms > 86400000) {
|
|
88
|
-
return formatters.d(Math.floor(ms / 86400000));
|
|
89
|
-
}
|
|
90
|
-
if (ms > 3600000) {
|
|
91
|
-
return formatters.h(Math.floor(ms / 3600000));
|
|
92
|
-
}
|
|
93
|
-
if (ms > 60000) {
|
|
94
|
-
return formatters.m(Math.floor(ms / 60000));
|
|
95
|
-
}
|
|
96
|
-
return formatters.s(Math.floor(ms / 1000));
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* format time delta (in ms) to string, like: '1d10m42s'
|
|
101
|
-
* when ms<=0, returns '0s'
|
|
102
|
-
*
|
|
103
|
-
* format can set by `setLocaleFormatter`
|
|
104
|
-
* day is the largest unit
|
|
105
|
-
*/
|
|
106
|
-
export function delta(ms: number) {
|
|
107
|
-
let ret = '';
|
|
108
|
-
let val = Math.ceil(ms / 1000);
|
|
109
|
-
|
|
110
|
-
if (val <= 0) {
|
|
111
|
-
return '0s';
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// sec
|
|
115
|
-
const s = val % 60;
|
|
116
|
-
val = Math.floor(val / 60);
|
|
117
|
-
if (s > 0) {
|
|
118
|
-
ret = formatters.s(s);
|
|
119
|
-
}
|
|
120
|
-
if (val === 0) {
|
|
121
|
-
return ret;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// min
|
|
125
|
-
const m = val % 60;
|
|
126
|
-
val = Math.floor(val / 60);
|
|
127
|
-
if (m > 0) {
|
|
128
|
-
ret = formatters.m(m) + ret;
|
|
129
|
-
}
|
|
130
|
-
if (val === 0) {
|
|
131
|
-
return ret;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// hour
|
|
135
|
-
const h = val % 24;
|
|
136
|
-
val = Math.floor(val / 24);
|
|
137
|
-
if (h > 0) {
|
|
138
|
-
ret = formatters.h(h) + ret;
|
|
139
|
-
}
|
|
140
|
-
if (val === 0) {
|
|
141
|
-
return ret;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// day
|
|
145
|
-
if (val > 0) {
|
|
146
|
-
ret = formatters.d(h) + ret;
|
|
147
|
-
}
|
|
148
|
-
return ret;
|
|
149
|
-
}
|
|
150
|
-
}
|
package/src/date/unix.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
const inspect = Symbol.for('nodejs.util.inspect.custom'); // high version node
|
|
2
|
-
const inspectOld = tryGetSymbol();
|
|
3
|
-
|
|
4
|
-
declare const global: any;
|
|
5
|
-
|
|
6
|
-
function tryGetSymbol() {
|
|
7
|
-
try {
|
|
8
|
-
const r = global.require;
|
|
9
|
-
return r('util').inspect.custom;
|
|
10
|
-
} catch {
|
|
11
|
-
return undefined;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* try to call `inspect` method of an object, if not exists, call `toString`.
|
|
17
|
-
* @returns {string}
|
|
18
|
-
*/
|
|
19
|
-
export function tryInspect(object: any) {
|
|
20
|
-
if (!object || typeof object !== 'object') {
|
|
21
|
-
return JSON.stringify(object);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (object[inspect]) {
|
|
25
|
-
return object[inspect]();
|
|
26
|
-
} else if (inspectOld && object[inspectOld]) {
|
|
27
|
-
return object[inspectOld]();
|
|
28
|
-
} else if (object['inspect']) {
|
|
29
|
-
return object['inspect']();
|
|
30
|
-
} else if (object[Symbol.toStringTag]) {
|
|
31
|
-
return object[Symbol.toStringTag]();
|
|
32
|
-
} else if (object.toJSON) {
|
|
33
|
-
return object.toJSON();
|
|
34
|
-
} else {
|
|
35
|
-
return '' + object;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { getErrorFrame } from './getFrame';
|
|
2
|
-
|
|
3
|
-
export function convertCatchedError(e: unknown): Error {
|
|
4
|
-
if (e instanceof Error) {
|
|
5
|
-
return e;
|
|
6
|
-
} else {
|
|
7
|
-
console.error('Catched invalid error at %s, type %s, value %s.', getErrorFrame(new Error(), 1), typeof e, e);
|
|
8
|
-
return new Error('Invalid: ' + e);
|
|
9
|
-
}
|
|
10
|
-
}
|
package/src/error/getFrame.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Get nth line of Error.stack
|
|
3
|
-
* @returns {string} if frame greater than max, return ''
|
|
4
|
-
*/
|
|
5
|
-
export function getErrorFrame(e: Error, frame: number): string {
|
|
6
|
-
if (e && e.stack) {
|
|
7
|
-
const stackArr = e.stack.split('\n');
|
|
8
|
-
if (stackArr.length > frame + 1) {
|
|
9
|
-
return stackArr[frame + 1].trim();
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
return '';
|
|
13
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { nameFunction } from './functionName';
|
|
2
|
-
|
|
3
|
-
export interface MyAsyncCallback<Argument extends unknown[]> {
|
|
4
|
-
displayName?: string;
|
|
5
|
-
|
|
6
|
-
(...param: Argument): Promise<void | undefined | boolean> | void | undefined | boolean;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* like CallbackList, but async
|
|
11
|
-
*/
|
|
12
|
-
export class AsyncCallbackList<Argument extends unknown[]> {
|
|
13
|
-
protected list: MyAsyncCallback<Argument>[] = [];
|
|
14
|
-
protected running: boolean = false;
|
|
15
|
-
|
|
16
|
-
constructor() {
|
|
17
|
-
this.run = (this.run as any).bind(this);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
count() {
|
|
21
|
-
return this.list.length;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
reset() {
|
|
25
|
-
if (this.running) {
|
|
26
|
-
throw new Error("Can not reset when it's running.");
|
|
27
|
-
}
|
|
28
|
-
this.list.length = 0;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @param name optional name of `item` (will assign displayName to `item`)
|
|
33
|
-
* @returns function list length
|
|
34
|
-
*/
|
|
35
|
-
add(item: MyAsyncCallback<Argument>, name?: string): number {
|
|
36
|
-
if (this.running) {
|
|
37
|
-
throw new Error("Can not add callback when it's running.");
|
|
38
|
-
}
|
|
39
|
-
if (name) {
|
|
40
|
-
nameFunction(name, item);
|
|
41
|
-
}
|
|
42
|
-
return this.list.push(item);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @returns if removed: return `item`; if did not exists: return null
|
|
47
|
-
*/
|
|
48
|
-
remove(item: MyAsyncCallback<Argument>): null | MyAsyncCallback<Argument> {
|
|
49
|
-
if (this.running) {
|
|
50
|
-
throw new Error("Can not remove callback when it's running.");
|
|
51
|
-
}
|
|
52
|
-
const found = this.list.indexOf(item);
|
|
53
|
-
if (found !== -1) {
|
|
54
|
-
return this.list.splice(found, 1)[0];
|
|
55
|
-
}
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Stop run if one callback return `true`
|
|
61
|
-
* @returns {boolean} true if one callback return true
|
|
62
|
-
*/
|
|
63
|
-
async run(...argument: Argument): Promise<boolean> {
|
|
64
|
-
this.running = true;
|
|
65
|
-
let ret: boolean | undefined | void;
|
|
66
|
-
for (const cb of this.list) {
|
|
67
|
-
ret = await cb(...argument);
|
|
68
|
-
if (ret === true) {
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
this.running = false;
|
|
73
|
-
return ret || false;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { nameFunction } from './functionName';
|
|
2
|
-
|
|
3
|
-
export interface MyCallback<Argument extends unknown[]> {
|
|
4
|
-
displayName?: string;
|
|
5
|
-
|
|
6
|
-
(...param: Argument): any;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Manage a list of callback
|
|
11
|
-
*/
|
|
12
|
-
export class CallbackList<Argument extends unknown[]> {
|
|
13
|
-
protected list: MyCallback<Argument>[] = [];
|
|
14
|
-
protected running: boolean = false;
|
|
15
|
-
protected stop: boolean = false;
|
|
16
|
-
|
|
17
|
-
constructor() {
|
|
18
|
-
this.run = (this.run as any).bind(this);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
count() {
|
|
22
|
-
return this.list.length;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
reset() {
|
|
26
|
-
if (this.running) {
|
|
27
|
-
throw new Error("Can not reset when it's running.");
|
|
28
|
-
}
|
|
29
|
-
this.list.length = 0;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @param name optional name of `item` (will assign displayName to `item`)
|
|
34
|
-
* @returns function list length
|
|
35
|
-
*/
|
|
36
|
-
add(item: MyCallback<Argument>, name?: string): number {
|
|
37
|
-
if (this.running) {
|
|
38
|
-
throw new Error("Can not add callback when it's running.");
|
|
39
|
-
}
|
|
40
|
-
if (name) {
|
|
41
|
-
nameFunction(name, item);
|
|
42
|
-
}
|
|
43
|
-
return this.list.push(item);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @returns if removed: return `item`; if did not exists: return null
|
|
48
|
-
*/
|
|
49
|
-
remove(item: MyCallback<Argument>): null | MyCallback<Argument> {
|
|
50
|
-
if (this.running) {
|
|
51
|
-
throw new Error("Can not remove callback when it's running.");
|
|
52
|
-
}
|
|
53
|
-
const found = this.list.indexOf(item);
|
|
54
|
-
if (found !== -1) {
|
|
55
|
-
return this.list.splice(found, 1)[0];
|
|
56
|
-
}
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* @returns {boolean} true if every callback called, false if stop in middle
|
|
62
|
-
*/
|
|
63
|
-
run(...argument: Argument): boolean {
|
|
64
|
-
if (this.running) {
|
|
65
|
-
throw new Error("can not run CallbackList in it's callback.");
|
|
66
|
-
}
|
|
67
|
-
this.stop = false;
|
|
68
|
-
|
|
69
|
-
this.running = true;
|
|
70
|
-
for (const cb of this.list) {
|
|
71
|
-
if (this.stop) break;
|
|
72
|
-
cb(...argument);
|
|
73
|
-
}
|
|
74
|
-
this.running = false;
|
|
75
|
-
|
|
76
|
-
const ret = !this.stop;
|
|
77
|
-
this.stop = false;
|
|
78
|
-
return ret;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
stopRun() {
|
|
82
|
-
this.stop = true;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { nameFunction } from './functionName';
|
|
2
|
-
|
|
3
|
-
export interface MyDelayCallback<Argument extends unknown[]> {
|
|
4
|
-
displayName?: string;
|
|
5
|
-
|
|
6
|
-
(...param: Argument): void;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* remember arguments after run
|
|
11
|
-
* run all later added function with memorized argument
|
|
12
|
-
*/
|
|
13
|
-
export class DelayCallbackList<Argument extends unknown[]> {
|
|
14
|
-
private delayArgument?: Argument;
|
|
15
|
-
private delayComplete: boolean = false;
|
|
16
|
-
|
|
17
|
-
protected list?: MyDelayCallback<Argument>[] = [];
|
|
18
|
-
|
|
19
|
-
count() {
|
|
20
|
-
return this.list?.length ?? 0;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
add(item: MyDelayCallback<Argument>, name?: string) {
|
|
24
|
-
if (name) {
|
|
25
|
-
nameFunction(name, item);
|
|
26
|
-
}
|
|
27
|
-
if (this.delayComplete) {
|
|
28
|
-
item(...this.delayArgument!);
|
|
29
|
-
} else {
|
|
30
|
-
this.list!.push(item);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
run(argument: Argument) {
|
|
35
|
-
if (this.delayComplete) {
|
|
36
|
-
throw new Error('call to delay callback twice!');
|
|
37
|
-
}
|
|
38
|
-
this.delayComplete = true;
|
|
39
|
-
this.delayArgument = argument;
|
|
40
|
-
this.list!.forEach((cb) => {
|
|
41
|
-
cb(...argument);
|
|
42
|
-
});
|
|
43
|
-
delete this.list;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Function with displayName
|
|
3
|
-
*/
|
|
4
|
-
export interface NamedFunction extends Function {
|
|
5
|
-
displayName: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Get displayName/name of a function
|
|
10
|
-
*/
|
|
11
|
-
export function functionName(func: Function) {
|
|
12
|
-
return (func as NamedFunction).displayName || func.name;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Set displayName of a function
|
|
17
|
-
*/
|
|
18
|
-
export function nameFunction<T extends Function>(name: string, func: T): T & NamedFunction {
|
|
19
|
-
return Object.assign(func, {
|
|
20
|
-
displayName: name,
|
|
21
|
-
inspect() {
|
|
22
|
-
return `[Function: ${name}]`;
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface MaybeNamedFunction extends Function {
|
|
28
|
-
displayName?: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Assert function must have oneof displayName/name property
|
|
33
|
-
*/
|
|
34
|
-
export function assertFunctionHasName(func: MaybeNamedFunction) {
|
|
35
|
-
if (!func.displayName && !func.name) {
|
|
36
|
-
console.error(func);
|
|
37
|
-
throw new TypeError('function must have name!');
|
|
38
|
-
}
|
|
39
|
-
}
|