@kirill.konshin/utils 0.0.8 → 0.0.9
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/.ctirc +10 -0
- package/.storybook/main.ts +21 -0
- package/.storybook/preview.ts +17 -0
- package/.turbo/turbo-build.log +67 -0
- package/.turbo/turbo-test.log +118 -0
- package/CHANGELOG.md +6 -0
- package/README.md +59 -0
- package/builders/.swcrc +18 -0
- package/builders/build.config.ts +11 -0
- package/builders/bun.build.js +23 -0
- package/builders/package.json +209 -0
- package/builders/perf.mjs +63 -0
- package/builders/rollup.config.mjs +50 -0
- package/builders/rslib.config.ts +26 -0
- package/builders/tsconfig.json +21 -0
- package/builders/tsup.config.ts +20 -0
- package/builders/turbo.json +24 -0
- package/builders/vite.config.ts +37 -0
- package/demo/cache-demo.ts +2 -1
- package/package.json +166 -22
- package/src/bootstrap/adaptiveContainer.tsx +12 -0
- package/src/bootstrap/controls.stories.tsx +62 -0
- package/src/bootstrap/controls.tsx +119 -0
- package/src/bootstrap/error.stories.tsx +34 -0
- package/src/bootstrap/error.tsx +40 -0
- package/src/bootstrap/field.stories.tsx +42 -0
- package/src/bootstrap/field.tsx +35 -0
- package/src/bootstrap/footer.tsx +49 -0
- package/src/bootstrap/globalLoading.stories.tsx +25 -0
- package/src/bootstrap/globalLoading.tsx +17 -0
- package/src/bootstrap/index.ts +12 -0
- package/src/bootstrap/loading.stories.tsx +35 -0
- package/src/bootstrap/loading.tsx +22 -0
- package/src/bootstrap/main.scss +86 -0
- package/src/bootstrap/responsiveHelper.tsx +50 -0
- package/src/bootstrap/screen.stories.tsx +160 -0
- package/src/bootstrap/screen.tsx +144 -0
- package/src/bootstrap/toaster.stories.tsx +32 -0
- package/src/bootstrap/toaster.tsx +38 -0
- package/src/bootstrap/useModal.stories.tsx +51 -0
- package/src/bootstrap/useModal.tsx +88 -0
- package/src/bootstrap/useWrappedForm.ts +40 -0
- package/src/core/cache.test.ts +121 -0
- package/src/electron/README.md +13 -0
- package/src/electron/createWindow.ts +234 -0
- package/src/electron/index.ts +2 -0
- package/src/electron/updater.ts +54 -0
- package/src/electron-builder/builder.ts +150 -0
- package/src/electron-builder/images.ts +33 -0
- package/src/electron-builder/index.ts +2 -0
- package/src/mui/README.md +3 -0
- package/src/mui/formControlFieldset.stories.tsx +71 -0
- package/src/mui/formControlFieldset.tsx +26 -0
- package/src/mui/formLabelLegend.tsx +11 -0
- package/src/mui/genericControl.tsx +20 -0
- package/src/mui/index.ts +4 -0
- package/src/mui/readOnly.tsx +6 -0
- package/src/next/appLink.tsx +39 -0
- package/src/next/index.ts +5 -0
- package/src/next/measure.ts +7 -0
- package/src/next/noSSR.tsx +17 -0
- package/src/next/redirect.tsx +13 -0
- package/src/next/useIsInner.ts +13 -0
- package/src/react/apiCall.ts +25 -0
- package/src/react/form/client.tsx +62 -0
- package/src/react/form/form.tsx +109 -0
- package/src/react/form/index.ts +2 -0
- package/src/react/index.ts +4 -0
- package/src/react/useFetch.ts +29 -0
- package/src/react/useFetcher.ts +49 -0
- package/src/react-native/index.ts +3 -0
- package/src/react-native/share.ts +31 -0
- package/src/react-native/update.tsx +40 -0
- package/src/react-native/useAppState.ts +18 -0
- package/src/tailwind/fullpage.tsx +11 -0
- package/src/tailwind/index.ts +2 -0
- package/src/tailwind/responsiveHelper.tsx +16 -0
- package/src-todo/auth0.tsx +177 -0
- package/tsconfig.json +15 -18
- package/turbo.json +2 -11
- package/vite.config.ts +47 -0
- package/vite.exports.ts +121 -0
- package/build/cache.d.ts +0 -120
- package/build/cache.js +0 -191
- package/build/errors.d.ts +0 -1
- package/build/errors.js +0 -14
- package/build/index.d.ts +0 -5
- package/build/index.js +0 -5
- package/build/measure.d.ts +0 -34
- package/build/measure.js +0 -48
- package/build/mutex.d.ts +0 -5
- package/build/mutex.js +0 -23
- package/build/worker.d.ts +0 -83
- package/build/worker.js +0 -250
- package/tsconfig.tsbuildinfo +0 -1
- package/src/{cache.ts → core/cache.ts} +0 -0
- package/src/{errors.ts → core/errors.ts} +0 -0
- package/src/{index.ts → core/index.ts} +1 -1
- /package/src/{measure.ts → core/measure.ts} +0 -0
- /package/src/{mutex.ts → core/mutex.ts} +0 -0
- /package/src/{worker.ts → core/worker.ts} +0 -0
package/build/cache.d.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
export type MaybePromise<T> = T | Promise<T>;
|
|
2
|
-
export declare const shallowCompare: (prev: any, next: any) => boolean;
|
|
3
|
-
export declare const equal: (prev: any, next: any) => boolean;
|
|
4
|
-
export declare const ANY: unique symbol;
|
|
5
|
-
export declare function createTracker<Dep = any>(whenDifferent: (next: Dep, prev?: Dep) => any, shallow?: boolean): (dependency: Dep) => any;
|
|
6
|
-
/**
|
|
7
|
-
* https://github.com/futpib/deep-weak-map
|
|
8
|
-
* https://github.com/fregante/many-keys-map
|
|
9
|
-
* https://github.com/fregante/many-keys-weakmap
|
|
10
|
-
* https://github.com/sindresorhus/memoize?tab=readme-ov-file#example-multiple-non-serializable-arguments
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* const memoized = memo(
|
|
14
|
-
* (file, options, ...args) => { ... },
|
|
15
|
-
* {
|
|
16
|
-
* key: (file, options, ...args) => [file, JSON.stringify(options), ...args],
|
|
17
|
-
* invalidate: (bitmap) => !bitmap.width,
|
|
18
|
-
* dispose: (bitmap) => bitmap.close(),
|
|
19
|
-
* }
|
|
20
|
-
* );
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export declare function memo<Key extends any[], Val, SerializedKey extends any[] = Key>(fn: (...args: Key) => MaybePromise<Val>, { key, invalidate, dispose, }?: {
|
|
24
|
-
key?: (...key: Key) => SerializedKey;
|
|
25
|
-
invalidate?: (prev: Val, ...key: SerializedKey) => boolean;
|
|
26
|
-
dispose?: (prev: Val, ...key: SerializedKey) => void;
|
|
27
|
-
}): {
|
|
28
|
-
(...args: Key): Promise<{
|
|
29
|
-
value: Val | undefined;
|
|
30
|
-
hit: boolean;
|
|
31
|
-
}>;
|
|
32
|
-
clear: (...condition: Key | any[]) => void;
|
|
33
|
-
size: () => number;
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Allows to memoize values by key and invalidate them based on the previous value.
|
|
37
|
-
*
|
|
38
|
-
* This makes possible to implement various one-off and subsequent transformations.
|
|
39
|
-
*
|
|
40
|
-
* 1. `write`: Before writing to cache, called only once if cache is present and valid
|
|
41
|
-
* 2. `read`: After it's read from cache, always called if cache is valid
|
|
42
|
-
* TODO Better names: once, folloing
|
|
43
|
-
* TODO Better names: memoize, recover
|
|
44
|
-
* TODO Better names: writeTransformer, readTransformer
|
|
45
|
-
* TODO Better names: writeModifier, readModifier
|
|
46
|
-
* TODO Better names: write, read
|
|
47
|
-
*
|
|
48
|
-
* Both should return same type or null.
|
|
49
|
-
*
|
|
50
|
-
* If newValue is null, old is returned, and no cache is set.
|
|
51
|
-
*
|
|
52
|
-
* ```ts
|
|
53
|
-
* class InputCache extends TypedCache<string, ImageBitmap> {
|
|
54
|
-
* dispose(bitmap: ImageBitmap, key: string) {
|
|
55
|
-
* bitmap.close();
|
|
56
|
-
* }
|
|
57
|
-
* }
|
|
58
|
-
*
|
|
59
|
-
* const cache = new InputCache('input');
|
|
60
|
-
*
|
|
61
|
-
* // Cache provided OffscreenCanvas by name and create context once per canvas
|
|
62
|
-
*
|
|
63
|
-
* class CanvasCache extends TypedCache<string, OffscreenCanvas> {
|
|
64
|
-
* protected write(key: string, canvas: OffscreenCanvas): OffscreenCanvas {
|
|
65
|
-
* const canvas = newValue.getContext('2d');
|
|
66
|
-
* canvas.ctx = canvas;
|
|
67
|
-
* return canvas;
|
|
68
|
-
* }
|
|
69
|
-
* }
|
|
70
|
-
*
|
|
71
|
-
* // Transfer control to offscreen canvas and track removal of original
|
|
72
|
-
* // When called again, always return null, because canvas is handed to worker, and never returned to main
|
|
73
|
-
*
|
|
74
|
-
* class CanvasCache extends TypedCache<HTMLCanvasElement, OffscreenCanvas> {
|
|
75
|
-
*
|
|
76
|
-
* protected write(canvas: HTMLCanvasElement): OffscreenCanvas {
|
|
77
|
-
* let l;
|
|
78
|
-
* canvas.addEventListener(
|
|
79
|
-
* 'remove',
|
|
80
|
-
* (l = () => {
|
|
81
|
-
* console.log('Removing canvas from cache', key);
|
|
82
|
-
* this.delete(canvas);
|
|
83
|
-
* key.removeEventListener('remove', l);
|
|
84
|
-
* }),
|
|
85
|
-
* );
|
|
86
|
-
* return canvas.transferControlToOffscreen();
|
|
87
|
-
* }
|
|
88
|
-
*
|
|
89
|
-
* protected read() {
|
|
90
|
-
* return null;
|
|
91
|
-
* }
|
|
92
|
-
* }
|
|
93
|
-
*
|
|
94
|
-
* // Create bitmap from file and check if was used, return null if used
|
|
95
|
-
* // Since bitmaps are also cached in worker, used bitmaps are not sent
|
|
96
|
-
*
|
|
97
|
-
* class BitmapCache extends TypedCache<File, ImageBitmap, File> {
|
|
98
|
-
* protected async write(file: File): Promise<ImageBitmap> {
|
|
99
|
-
* return await createImageBitmap(file);
|
|
100
|
-
* }
|
|
101
|
-
*
|
|
102
|
-
* protected read(bitmap: ImageBitmap): Promise<ImageBitmap> | ImageBitmap {
|
|
103
|
-
* if (!bitmap.width) return null;
|
|
104
|
-
* return bitmap;
|
|
105
|
-
* }
|
|
106
|
-
* }
|
|
107
|
-
*
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
export declare abstract class TransformerMap<Key, Val = Key> extends Map<Key, Val> {
|
|
111
|
-
protected readonly name: string;
|
|
112
|
-
constructor(name: string);
|
|
113
|
-
protected write(key: Key, newValue?: Val, oldValue?: Val): MaybePromise<Val>;
|
|
114
|
-
protected read(oldValue: Val, key: Key): MaybePromise<Val>;
|
|
115
|
-
delete(key: Key): boolean;
|
|
116
|
-
protected dispose(value: Val, key: Key): void;
|
|
117
|
-
clear(): void;
|
|
118
|
-
set(key: Key, value: Val): this;
|
|
119
|
-
memo(key: Key, newValue?: Val): Promise<Val>;
|
|
120
|
-
}
|
package/build/cache.js
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import ManyKeysMap from 'many-keys-map';
|
|
2
|
-
export const shallowCompare = (prev, next) => Array.from(new Set([...Object.keys(prev), ...Object.keys(next)])).every((key) => prev[key] === next[key]);
|
|
3
|
-
export const equal = (prev, next) => prev === next;
|
|
4
|
-
const UNUSED = Symbol('UNUSED');
|
|
5
|
-
export const ANY = Symbol('*');
|
|
6
|
-
export function createTracker(whenDifferent, shallow = false) {
|
|
7
|
-
let lastDependency = UNUSED;
|
|
8
|
-
const comparator = shallow ? shallowCompare : equal;
|
|
9
|
-
return function tracker(dependency) {
|
|
10
|
-
// console.log('Tracker', lastDependency, dependency);
|
|
11
|
-
if (lastDependency !== UNUSED && comparator(lastDependency, dependency)) {
|
|
12
|
-
// console.log('RETAINED cache', { lastDependency, dependency });
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
// console.log('INVALIDATED cache', {lastDependency, dependency});
|
|
16
|
-
const res = whenDifferent(dependency, lastDependency === UNUSED ? undefined : lastDependency) || true;
|
|
17
|
-
lastDependency = dependency;
|
|
18
|
-
return res;
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* https://github.com/futpib/deep-weak-map
|
|
23
|
-
* https://github.com/fregante/many-keys-map
|
|
24
|
-
* https://github.com/fregante/many-keys-weakmap
|
|
25
|
-
* https://github.com/sindresorhus/memoize?tab=readme-ov-file#example-multiple-non-serializable-arguments
|
|
26
|
-
*
|
|
27
|
-
* ```ts
|
|
28
|
-
* const memoized = memo(
|
|
29
|
-
* (file, options, ...args) => { ... },
|
|
30
|
-
* {
|
|
31
|
-
* key: (file, options, ...args) => [file, JSON.stringify(options), ...args],
|
|
32
|
-
* invalidate: (bitmap) => !bitmap.width,
|
|
33
|
-
* dispose: (bitmap) => bitmap.close(),
|
|
34
|
-
* }
|
|
35
|
-
* );
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export function memo(fn, { key = (...k) => k, invalidate, dispose, } = {}) {
|
|
39
|
-
const map = new ManyKeysMap();
|
|
40
|
-
const clear = (...condition) => {
|
|
41
|
-
if (!(condition === null || condition === void 0 ? void 0 : condition.length)) {
|
|
42
|
-
map.forEach((value, serializedKey) => {
|
|
43
|
-
dispose === null || dispose === void 0 ? void 0 : dispose(value, ...serializedKey);
|
|
44
|
-
});
|
|
45
|
-
map.clear();
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const keyToClear = key(...condition);
|
|
49
|
-
map.forEach((value, serializedKey) => {
|
|
50
|
-
if (!serializedKey.every((kk, i) => keyToClear[i] === ANY || kk === keyToClear[i]))
|
|
51
|
-
return;
|
|
52
|
-
dispose === null || dispose === void 0 ? void 0 : dispose(value, ...serializedKey);
|
|
53
|
-
map.delete(serializedKey);
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
const size = () => map.size;
|
|
57
|
-
async function memoized(...args) {
|
|
58
|
-
const k = key(...args);
|
|
59
|
-
const has = map.has(k);
|
|
60
|
-
const old = map.get(k);
|
|
61
|
-
if (has && !(invalidate === null || invalidate === void 0 ? void 0 : invalidate(old, ...k))) {
|
|
62
|
-
return { value: map.get(k), hit: true };
|
|
63
|
-
}
|
|
64
|
-
const value = await fn(...args);
|
|
65
|
-
map.set(k, value);
|
|
66
|
-
return { hit: false, value };
|
|
67
|
-
}
|
|
68
|
-
memoized.clear = clear;
|
|
69
|
-
memoized.size = size;
|
|
70
|
-
//TODO return mimic-function(memorized, fn);
|
|
71
|
-
return memoized;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Allows to memoize values by key and invalidate them based on the previous value.
|
|
75
|
-
*
|
|
76
|
-
* This makes possible to implement various one-off and subsequent transformations.
|
|
77
|
-
*
|
|
78
|
-
* 1. `write`: Before writing to cache, called only once if cache is present and valid
|
|
79
|
-
* 2. `read`: After it's read from cache, always called if cache is valid
|
|
80
|
-
* TODO Better names: once, folloing
|
|
81
|
-
* TODO Better names: memoize, recover
|
|
82
|
-
* TODO Better names: writeTransformer, readTransformer
|
|
83
|
-
* TODO Better names: writeModifier, readModifier
|
|
84
|
-
* TODO Better names: write, read
|
|
85
|
-
*
|
|
86
|
-
* Both should return same type or null.
|
|
87
|
-
*
|
|
88
|
-
* If newValue is null, old is returned, and no cache is set.
|
|
89
|
-
*
|
|
90
|
-
* ```ts
|
|
91
|
-
* class InputCache extends TypedCache<string, ImageBitmap> {
|
|
92
|
-
* dispose(bitmap: ImageBitmap, key: string) {
|
|
93
|
-
* bitmap.close();
|
|
94
|
-
* }
|
|
95
|
-
* }
|
|
96
|
-
*
|
|
97
|
-
* const cache = new InputCache('input');
|
|
98
|
-
*
|
|
99
|
-
* // Cache provided OffscreenCanvas by name and create context once per canvas
|
|
100
|
-
*
|
|
101
|
-
* class CanvasCache extends TypedCache<string, OffscreenCanvas> {
|
|
102
|
-
* protected write(key: string, canvas: OffscreenCanvas): OffscreenCanvas {
|
|
103
|
-
* const canvas = newValue.getContext('2d');
|
|
104
|
-
* canvas.ctx = canvas;
|
|
105
|
-
* return canvas;
|
|
106
|
-
* }
|
|
107
|
-
* }
|
|
108
|
-
*
|
|
109
|
-
* // Transfer control to offscreen canvas and track removal of original
|
|
110
|
-
* // When called again, always return null, because canvas is handed to worker, and never returned to main
|
|
111
|
-
*
|
|
112
|
-
* class CanvasCache extends TypedCache<HTMLCanvasElement, OffscreenCanvas> {
|
|
113
|
-
*
|
|
114
|
-
* protected write(canvas: HTMLCanvasElement): OffscreenCanvas {
|
|
115
|
-
* let l;
|
|
116
|
-
* canvas.addEventListener(
|
|
117
|
-
* 'remove',
|
|
118
|
-
* (l = () => {
|
|
119
|
-
* console.log('Removing canvas from cache', key);
|
|
120
|
-
* this.delete(canvas);
|
|
121
|
-
* key.removeEventListener('remove', l);
|
|
122
|
-
* }),
|
|
123
|
-
* );
|
|
124
|
-
* return canvas.transferControlToOffscreen();
|
|
125
|
-
* }
|
|
126
|
-
*
|
|
127
|
-
* protected read() {
|
|
128
|
-
* return null;
|
|
129
|
-
* }
|
|
130
|
-
* }
|
|
131
|
-
*
|
|
132
|
-
* // Create bitmap from file and check if was used, return null if used
|
|
133
|
-
* // Since bitmaps are also cached in worker, used bitmaps are not sent
|
|
134
|
-
*
|
|
135
|
-
* class BitmapCache extends TypedCache<File, ImageBitmap, File> {
|
|
136
|
-
* protected async write(file: File): Promise<ImageBitmap> {
|
|
137
|
-
* return await createImageBitmap(file);
|
|
138
|
-
* }
|
|
139
|
-
*
|
|
140
|
-
* protected read(bitmap: ImageBitmap): Promise<ImageBitmap> | ImageBitmap {
|
|
141
|
-
* if (!bitmap.width) return null;
|
|
142
|
-
* return bitmap;
|
|
143
|
-
* }
|
|
144
|
-
* }
|
|
145
|
-
*
|
|
146
|
-
* ```
|
|
147
|
-
*/
|
|
148
|
-
export class TransformerMap extends Map {
|
|
149
|
-
constructor(name) {
|
|
150
|
-
console.warn('CREATE cache', name);
|
|
151
|
-
super();
|
|
152
|
-
this.name = name;
|
|
153
|
-
}
|
|
154
|
-
write(key, newValue, oldValue) {
|
|
155
|
-
return newValue;
|
|
156
|
-
}
|
|
157
|
-
read(oldValue, key) {
|
|
158
|
-
return oldValue;
|
|
159
|
-
}
|
|
160
|
-
delete(key) {
|
|
161
|
-
const has = this.has(key);
|
|
162
|
-
if (has)
|
|
163
|
-
this.dispose(this.get(key), key);
|
|
164
|
-
super.delete(key);
|
|
165
|
-
return has;
|
|
166
|
-
}
|
|
167
|
-
// do something with the value and key before removing it
|
|
168
|
-
dispose(value, key) { }
|
|
169
|
-
clear() {
|
|
170
|
-
console.log('CLEAR cache', this.name);
|
|
171
|
-
this.forEach(this.dispose);
|
|
172
|
-
super.clear();
|
|
173
|
-
}
|
|
174
|
-
set(key, value) {
|
|
175
|
-
throw new Error('Use memo() method instead');
|
|
176
|
-
}
|
|
177
|
-
async memo(key, newValue) {
|
|
178
|
-
var _a;
|
|
179
|
-
const oldValue = this.get(key);
|
|
180
|
-
if (!!oldValue && (!newValue || newValue === oldValue)) {
|
|
181
|
-
// console.log('FROM cache', this.name, key, 'old value', oldValue);
|
|
182
|
-
return (_a = this.read) === null || _a === void 0 ? void 0 : _a.call(this, oldValue, key);
|
|
183
|
-
}
|
|
184
|
-
if (this.has(key))
|
|
185
|
-
this.delete(key);
|
|
186
|
-
const value = await this.write(key, newValue, oldValue);
|
|
187
|
-
super.set(key, value);
|
|
188
|
-
// console.log('INVALIDATED KEY cache', this.name, key, 'has', has, 'invalidate', invalidate, 'new value', value);
|
|
189
|
-
return value;
|
|
190
|
-
}
|
|
191
|
-
}
|
package/build/errors.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function catchErrors<T, E extends Error = Error>(promise: Promise<T>, errors?: E[]): Promise<any[] | (T | undefined)[]>;
|
package/build/errors.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// @see https://www.youtube.com/watch?v=AdmGHwvgaVs
|
|
2
|
-
export function catchErrors(promise, errors) {
|
|
3
|
-
return promise
|
|
4
|
-
.then((data) => [undefined, data])
|
|
5
|
-
.catch((error) => {
|
|
6
|
-
if (!(errors === null || errors === void 0 ? void 0 : errors.length)) {
|
|
7
|
-
return [error];
|
|
8
|
-
}
|
|
9
|
-
if (errors.some((e) => e instanceof error)) {
|
|
10
|
-
return [error];
|
|
11
|
-
}
|
|
12
|
-
throw error;
|
|
13
|
-
});
|
|
14
|
-
}
|
package/build/index.d.ts
DELETED
package/build/index.js
DELETED
package/build/measure.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { bold, magenta, cyan, yellow, red, green } from 'colors/safe';
|
|
2
|
-
export declare const colored: {
|
|
3
|
-
important: typeof bold;
|
|
4
|
-
subject: typeof magenta;
|
|
5
|
-
arg: typeof cyan;
|
|
6
|
-
sup: typeof yellow;
|
|
7
|
-
err: typeof red;
|
|
8
|
-
ok: typeof green;
|
|
9
|
-
};
|
|
10
|
-
export declare const uncolored: {
|
|
11
|
-
important: (str: any) => any;
|
|
12
|
-
subject: (str: any) => any;
|
|
13
|
-
arg: (str: any) => any;
|
|
14
|
-
sup: (str: any) => any;
|
|
15
|
-
err: (str: any) => any;
|
|
16
|
-
ok: (str: any) => any;
|
|
17
|
-
};
|
|
18
|
-
export declare function createMeasurer({ colors, prepend, padding }: {
|
|
19
|
-
colors?: boolean | undefined;
|
|
20
|
-
prepend?: string | undefined;
|
|
21
|
-
padding?: number | undefined;
|
|
22
|
-
}): {
|
|
23
|
-
measure: (what: string, step: string, supplemental?: string, ...args: any[]) => {
|
|
24
|
-
done: (result?: string, ...args2: any[]) => void;
|
|
25
|
-
fail: (result?: string, ...args2: any[]) => void;
|
|
26
|
-
log: (...args2: any[]) => void;
|
|
27
|
-
};
|
|
28
|
-
important: (str: any) => any;
|
|
29
|
-
subject: (str: any) => any;
|
|
30
|
-
arg: (str: any) => any;
|
|
31
|
-
sup: (str: any) => any;
|
|
32
|
-
ok: (str: any) => any;
|
|
33
|
-
err: (str: any) => any;
|
|
34
|
-
};
|
package/build/measure.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { bold, magenta, cyan, yellow, red, green } from 'colors/safe';
|
|
2
|
-
export const colored = {
|
|
3
|
-
important: bold,
|
|
4
|
-
subject: magenta,
|
|
5
|
-
arg: cyan,
|
|
6
|
-
sup: yellow,
|
|
7
|
-
err: red,
|
|
8
|
-
ok: green,
|
|
9
|
-
};
|
|
10
|
-
const noColor = (str) => str;
|
|
11
|
-
export const uncolored = {
|
|
12
|
-
important: noColor,
|
|
13
|
-
subject: noColor,
|
|
14
|
-
arg: noColor,
|
|
15
|
-
sup: noColor,
|
|
16
|
-
err: noColor,
|
|
17
|
-
ok: noColor,
|
|
18
|
-
};
|
|
19
|
-
export function createMeasurer({ colors = true, prepend = 'LOG', padding = 10 }) {
|
|
20
|
-
const { important, subject, arg, sup, ok, err } = colors ? colored : uncolored;
|
|
21
|
-
function measure(what, step, supplemental = '', ...args) {
|
|
22
|
-
const time = performance.now();
|
|
23
|
-
const prefix = [
|
|
24
|
-
prepend,
|
|
25
|
-
important(subject(what.substring(0, padding).padStart(padding, ' '))),
|
|
26
|
-
arg(step),
|
|
27
|
-
sup(supplemental),
|
|
28
|
-
...args,
|
|
29
|
-
];
|
|
30
|
-
const done = (result = ok('DONE'), ...args2) => {
|
|
31
|
-
console.log(...[
|
|
32
|
-
...prefix,
|
|
33
|
-
important(result),
|
|
34
|
-
...args2,
|
|
35
|
-
'in',
|
|
36
|
-
important((performance.now() - time).toFixed(0)),
|
|
37
|
-
'ms',
|
|
38
|
-
].filter(Boolean));
|
|
39
|
-
console.groupEnd();
|
|
40
|
-
};
|
|
41
|
-
const fail = (result = err('FAIL'), ...args2) => done(result, ...args2);
|
|
42
|
-
const log = (...args2) => {
|
|
43
|
-
console.log(...prefix, ...args2);
|
|
44
|
-
};
|
|
45
|
-
return { done, fail, log };
|
|
46
|
-
}
|
|
47
|
-
return { measure, important, subject, arg, sup, ok, err };
|
|
48
|
-
}
|
package/build/mutex.d.ts
DELETED
package/build/mutex.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
//TODO Implement queue with ranking & sorting
|
|
2
|
-
//TODO https://github.com/sindresorhus/p-queue
|
|
3
|
-
export class Mutex {
|
|
4
|
-
constructor() {
|
|
5
|
-
this.promise = Promise.resolve();
|
|
6
|
-
}
|
|
7
|
-
exec(fn) {
|
|
8
|
-
return new Promise((resolve, reject) => {
|
|
9
|
-
// wrap in always successful function
|
|
10
|
-
this.promise = this.promise.then(async () => {
|
|
11
|
-
try {
|
|
12
|
-
resolve(await fn());
|
|
13
|
-
}
|
|
14
|
-
catch (e) {
|
|
15
|
-
reject(e);
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
wrap(fn) {
|
|
21
|
-
return (...args) => this.exec(() => fn(...args));
|
|
22
|
-
}
|
|
23
|
-
}
|
package/build/worker.d.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
export declare function isTransferable(obj: any): boolean;
|
|
2
|
-
type WebWorker = Worker | (Window & typeof globalThis) | (WorkerGlobalScope & typeof globalThis);
|
|
3
|
-
type MethodReturn<R, M extends keyof R> = R[M] extends (...args: any[]) => any ? Partial<Awaited<ReturnType<R[M]>>> : never;
|
|
4
|
-
type MethodArg<R, M extends keyof R> = R[M] extends (arg: infer A, ...args: any[]) => any ? Partial<A> : never;
|
|
5
|
-
export type Ctx<R, K extends keyof R> = {
|
|
6
|
-
id: string;
|
|
7
|
-
parentId?: string;
|
|
8
|
-
message: K;
|
|
9
|
-
};
|
|
10
|
-
type Data<R, M extends keyof R> = MethodReturn<R, M> | MethodArg<R, M> | Error;
|
|
11
|
-
type Event<R, M extends keyof R> = MessageEvent<{
|
|
12
|
-
data: Data<R, M>;
|
|
13
|
-
ctx: Ctx<R, M>;
|
|
14
|
-
}>;
|
|
15
|
-
type Listener<R, M extends keyof R, D extends Data<R, M> = Data<R, M>, S extends Context<R, M> = Context<R, M>> = (data: D, self: S, event: Event<R, M>) => void | Promise<void>;
|
|
16
|
-
export declare class WorkerDialog<R extends RespondersBase<any>> {
|
|
17
|
-
worker: WebWorker;
|
|
18
|
-
responders: R;
|
|
19
|
-
name: string;
|
|
20
|
-
readonly contexts: Set<Context<R, keyof R>>;
|
|
21
|
-
protected closed: boolean;
|
|
22
|
-
constructor(worker: WebWorker, responders: R, name: string);
|
|
23
|
-
close(): void;
|
|
24
|
-
withMessage<M extends keyof R>(message: M): RequestContext<R, M>;
|
|
25
|
-
}
|
|
26
|
-
export declare class RespondersBase<R> {
|
|
27
|
-
create<M extends keyof R, F extends (input: any, context: ResponseContext<R, M>) => any>(message: M, responder: F): F;
|
|
28
|
-
}
|
|
29
|
-
declare abstract class Context<R, M extends keyof R> {
|
|
30
|
-
protected readonly dialog: WorkerDialog<any>;
|
|
31
|
-
message: M;
|
|
32
|
-
id: string | undefined;
|
|
33
|
-
parent: Context<R, keyof R> | undefined | null;
|
|
34
|
-
protected readonly unsub: Set<() => void>;
|
|
35
|
-
protected closed: boolean;
|
|
36
|
-
constructor(dialog: WorkerDialog<any>, message: M, id: string | undefined, parent?: Context<R, keyof R> | undefined | null);
|
|
37
|
-
abstract withMessage<M2 extends keyof R>(message: M2, id?: string): Context<R, M2>;
|
|
38
|
-
close(reason?: string): void;
|
|
39
|
-
get info(): string;
|
|
40
|
-
protected postMessage(data?: Data<R, M>): Data<R, M> | undefined;
|
|
41
|
-
send(data?: Data<R, M>): Data<R, M> | undefined;
|
|
42
|
-
/**
|
|
43
|
-
* Does not automatically close, assumes consumer will close
|
|
44
|
-
*
|
|
45
|
-
* ```ts
|
|
46
|
-
* dialog.request('x').fetch((c) => {
|
|
47
|
-
* c.response('x').listen(() => { ... }); // listens to "x", will be closed by upper level fetch
|
|
48
|
-
* c.request('x').fetcH(y, (c) => { ... }); // closes
|
|
49
|
-
* }) // closes
|
|
50
|
-
*
|
|
51
|
-
* const un = dialog.context('x').listen((e) => { ... }); // listens to 'x'
|
|
52
|
-
* un(); // closes
|
|
53
|
-
*
|
|
54
|
-
* const un = dialog.context('x').listen((e) => { ... }); // listens to 'x'
|
|
55
|
-
* un(); // closes
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
listen(callback: Listener<R, M>): () => void;
|
|
59
|
-
}
|
|
60
|
-
declare class RequestContext<R, M extends keyof R> extends Context<R, M> {
|
|
61
|
-
withMessage<M2 extends keyof R>(message: M2, id?: string): RequestContext<R, M2>;
|
|
62
|
-
send(data?: MethodArg<R, M>): MethodArg<R, M>;
|
|
63
|
-
listen(callback: Listener<R, M, MethodReturn<R, M>, RequestContext<R, M>>): () => void;
|
|
64
|
-
/**
|
|
65
|
-
* Returns response
|
|
66
|
-
* If you need to work with ctx, use callback
|
|
67
|
-
* Closed automatically on response
|
|
68
|
-
*
|
|
69
|
-
* dialog.fetch('x', (c) => {
|
|
70
|
-
* c.listen(() => { ... }); // will be closed by upper level fetch
|
|
71
|
-
* c.fetcH(y, (c) => { ... }); // closes c
|
|
72
|
-
* c.context().fetcH(y, (c) => { ... }); // closes sub, not closes c, will be closed by upper level fetch
|
|
73
|
-
* })
|
|
74
|
-
*/
|
|
75
|
-
fetch(data?: MethodArg<R, M>, callback?: (context: RequestContext<R, M>) => void): Promise<MethodReturn<R, M>>;
|
|
76
|
-
protected expect(): Promise<MethodReturn<R, M>>;
|
|
77
|
-
}
|
|
78
|
-
declare class ResponseContext<R, M extends keyof R> extends Context<R, M> {
|
|
79
|
-
withMessage<M2 extends keyof R>(message: M2, id?: string): ResponseContext<R, M2>;
|
|
80
|
-
listen(callback: Listener<R, M, MethodArg<R, M>, ResponseContext<R, M>>): () => void;
|
|
81
|
-
send(data?: MethodReturn<R, M>): MethodReturn<R, M>;
|
|
82
|
-
}
|
|
83
|
-
export {};
|