@cgtk/std 0.0.192 → 0.0.194
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/array.d.ts +1 -1
- package/array.js +1 -1
- package/assign.d.ts +1 -1
- package/assign.js +1 -1
- package/async.d.ts +3 -3
- package/async.js +5 -4
- package/basen.js +1 -1
- package/buffer.d.ts +1 -1
- package/buffer.js +1 -1
- package/checks.d.ts +2 -2
- package/checks.js +2 -2
- package/dom.d.ts +2 -2
- package/dom.js +3 -3
- package/fn.d.ts +3 -3
- package/fn.js +3 -3
- package/http.d.ts +6 -7
- package/http.js +11 -15
- package/iterable.d.ts +1 -1
- package/iterable.js +3 -3
- package/json.d.ts +1 -1
- package/json.js +1 -1
- package/map.d.ts +3 -3
- package/map.js +2 -2
- package/math.js +1 -1
- package/npy.d.ts +1 -1
- package/number.js +1 -1
- package/object.d.ts +2 -2
- package/package.json +2 -2
- package/port.d.ts +2 -1
- package/port.js +7 -4
- package/progress.d.ts +1 -1
- package/rect.d.ts +1 -1
- package/rect.js +1 -1
- package/resource.d.ts +10 -8
- package/resource.js +22 -15
- package/schedule.d.ts +1 -1
- package/schedule.js +2 -2
- package/set.d.ts +1 -0
- package/set.js +1 -0
- package/signal.d.ts +18 -5
- package/signal.js +56 -15
- package/src/array.d.ts +14 -0
- package/src/array.js +21 -0
- package/src/assign.d.ts +8 -0
- package/src/assign.js +26 -0
- package/src/async.d.ts +8 -0
- package/src/async.js +31 -0
- package/src/base64.d.ts +1 -0
- package/src/base64.js +7 -0
- package/src/basen.d.ts +12 -0
- package/src/basen.js +31 -0
- package/src/buffer.d.ts +14 -0
- package/src/buffer.js +54 -0
- package/src/checks.d.ts +29 -0
- package/src/checks.js +44 -0
- package/src/constants.d.ts +5 -0
- package/src/constants.js +1 -0
- package/src/dom.d.ts +50 -0
- package/src/dom.js +36 -0
- package/src/fn.d.ts +33 -0
- package/src/fn.js +61 -0
- package/src/http.d.ts +9 -0
- package/src/http.js +33 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/iterable.d.ts +43 -0
- package/src/iterable.js +158 -0
- package/src/json.d.ts +1 -0
- package/src/json.js +1 -0
- package/src/map.d.ts +12 -0
- package/src/map.js +35 -0
- package/src/math.d.ts +34 -0
- package/src/math.js +43 -0
- package/src/npy.d.ts +10 -0
- package/src/npy.js +29 -0
- package/src/number.d.ts +4 -0
- package/src/number.js +13 -0
- package/src/object.d.ts +15 -0
- package/src/object.js +13 -0
- package/src/port.d.ts +30 -0
- package/src/port.js +56 -0
- package/src/progress.d.ts +6 -0
- package/src/progress.js +1 -0
- package/src/rect.d.ts +5 -0
- package/src/rect.js +16 -0
- package/src/resource.d.ts +12 -0
- package/src/resource.js +28 -0
- package/src/schedule.d.ts +10 -0
- package/src/schedule.js +33 -0
- package/src/set.d.ts +2 -0
- package/src/set.js +2 -0
- package/src/signal.d.ts +37 -0
- package/src/signal.js +89 -0
- package/src/stream.d.ts +6 -0
- package/src/stream.js +26 -0
- package/src/string.d.ts +8 -0
- package/src/string.js +29 -0
- package/src/struct.d.ts +42 -0
- package/src/struct.js +58 -0
- package/src/tree.d.ts +11 -0
- package/src/tree.js +25 -0
- package/src/treemap.d.ts +13 -0
- package/src/treemap.js +71 -0
- package/src/typedarray.d.ts +31 -0
- package/src/typedarray.js +39 -0
- package/src/types.d.ts +63 -0
- package/src/types.js +1 -0
- package/src/utils.d.ts +4 -0
- package/src/utils.js +30 -0
- package/src/weak.d.ts +2 -0
- package/src/weak.js +5 -0
- package/stream.d.ts +1 -1
- package/stream.js +2 -2
- package/string.d.ts +2 -1
- package/string.js +3 -2
- package/struct.d.ts +2 -2
- package/struct.js +2 -2
- package/tree.d.ts +3 -3
- package/tree.js +8 -10
- package/treemap.d.ts +12 -12
- package/treemap.js +62 -59
- package/typedarray.d.ts +1 -1
- package/typedarray.js +3 -3
- package/types.d.ts +3 -7
- package/utils.d.ts +1 -1
- package/utils.js +1 -1
- package/weak.d.ts +2 -2
- package/weak.js +1 -1
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Fn, FnV, Indexed, Pred } from "./types.js";
|
|
2
|
+
export declare const next: <T>(xs: Iterator<T>) => T;
|
|
3
|
+
export declare const repeat: (n: number) => <A>(x: A) => Iterator<A>;
|
|
4
|
+
export declare const just: <T>(x: T) => IteratorObject<T>;
|
|
5
|
+
export declare const enumerate: (n?: number) => <T>(x: T) => Indexed<T>;
|
|
6
|
+
export declare function empty(): Generator<never, void, unknown>;
|
|
7
|
+
export declare function concat<T>(...xxs: ReadonlyArray<IteratorObject<T>>): Generator<T, void, unknown>;
|
|
8
|
+
export declare const toArray: <T>(xs: IteratorObject<T>) => T[];
|
|
9
|
+
export declare const map: <T, U = T>(f: Fn<T, U>) => (xs: IteratorObject<T>) => IteratorObject<U, undefined, unknown>;
|
|
10
|
+
export declare const filter: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => IteratorObject<T, undefined, unknown>;
|
|
11
|
+
export declare const drop: (n: number) => <T>(xs: IteratorObject<T>) => IteratorObject<T, undefined, unknown>;
|
|
12
|
+
export declare const flatMap: <T, U = Iterable<T>>(f: Fn<T, Iterator<U> | Iterable<U>>) => (xs: IteratorObject<T>) => IteratorObject<U, undefined, unknown>;
|
|
13
|
+
export declare const forEach: <T>(f?: Fn<T>) => (xs: IteratorObject<T>) => void;
|
|
14
|
+
export declare const some: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => boolean;
|
|
15
|
+
export declare const every: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => boolean;
|
|
16
|
+
export declare const find: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => T | undefined;
|
|
17
|
+
export declare const flat: <T>(xs: IteratorObject<IteratorObject<T>>) => IteratorObject<T, undefined, unknown>;
|
|
18
|
+
export declare const prepend: <A>(ys: IteratorObject<A>) => (xs: IteratorObject<A>) => Generator<A, void, unknown>;
|
|
19
|
+
export declare const append: <A>(ys: IteratorObject<A>) => (xs: IteratorObject<A>) => Generator<A, void, unknown>;
|
|
20
|
+
export declare const first: <T>(xs: IteratorObject<T>) => T | undefined;
|
|
21
|
+
export declare const reduce: <T, S>(f: FnV<[S, T, number], S>, a: S) => (xs: IteratorObject<T>) => S;
|
|
22
|
+
export declare const reduceRec: <T, A, R>(xs: IteratorObject<T>, f: FnV<[A, T, Fn<A>], R>, o: A, result: Fn<A, R>) => R;
|
|
23
|
+
export declare const traverse: <T>(children: Fn<T, Iterable<T>>, f: Fn<T>) => Fn<T>;
|
|
24
|
+
export declare const take: (n: number) => <T>(xs: IteratorObject<T>) => Generator<T>;
|
|
25
|
+
export declare function arange(a: number, b?: number, s?: number): Generator<number, void, unknown>;
|
|
26
|
+
export declare const linspace: (a: number, b: number, n: number) => Generator<number, void, unknown>;
|
|
27
|
+
export declare const steps: (a: number, b: number, s?: number, o?: number, eps?: number) => Generator<number, void, unknown>;
|
|
28
|
+
export declare const strided: (n: number, s: number) => <T>(xs: Iterable<T>) => Generator<T[], void, unknown>;
|
|
29
|
+
export declare const stopAt: <T>(p: Pred<T>) => (xs: IteratorObject<T>) => Generator<T, void, unknown>;
|
|
30
|
+
export declare const until: (p: Pred<void>) => <A>(xs: IteratorObject<A>) => Generator<A, void, unknown>;
|
|
31
|
+
export type Iterators<T extends any[]> = {
|
|
32
|
+
[k in keyof T]: IteratorObject<T[k]>;
|
|
33
|
+
};
|
|
34
|
+
export declare function zip<T extends unknown[]>(...xs: Iterators<T>): Generator<T>;
|
|
35
|
+
export declare const iterA: <T>(xs: Promise<T>[]) => AsyncGenerator<Awaited<T>, void, unknown>;
|
|
36
|
+
export declare const forEachA: <T>(f?: Fn<T>) => (xs: AsyncIterable<T>) => Promise<void>;
|
|
37
|
+
export declare const mapA: <A, B>(f: Fn<A, B>) => Fn<AsyncIterator<A>, AsyncIterableIterator<B>>;
|
|
38
|
+
export declare const flatMapA: <A, B>(f: Fn<A, Iterable<B>>) => (xs: AsyncIterator<A>) => AsyncGenerator<Awaited<B>, void, any>;
|
|
39
|
+
export declare const filterA: <A>(f: Pred<A>) => Fn<AsyncIterator<A>, AsyncIterableIterator<A>>;
|
|
40
|
+
export declare const untilA: (p: Pred<void>) => <A>(xs: AsyncIterator<A>) => AsyncGenerator<Awaited<A>, void, unknown>;
|
|
41
|
+
export declare const reduceA: <T, S>(f: FnV<[S, T], S>, a: S) => (xs: AsyncIterator<T>) => Promise<S>;
|
|
42
|
+
export declare const race: (n: number) => <T>(xs: IteratorObject<Promise<T>>) => AsyncGenerator<Awaited<T>, void, unknown>;
|
|
43
|
+
export declare const zmap: <T extends unknown[]>(f: FnV<T>) => (...xs: Iterators<T>) => void;
|
package/src/iterable.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { assert, isDefined } from "./checks.js";
|
|
2
|
+
import { pipe, comp, scan, apply, noop, identity, constantly } from "./fn.js";
|
|
3
|
+
import { set } from "./map.js";
|
|
4
|
+
export const next = (xs) => {
|
|
5
|
+
const { done, value } = xs.next();
|
|
6
|
+
assert(isDefined(done) && !done, "iterator done");
|
|
7
|
+
return value;
|
|
8
|
+
};
|
|
9
|
+
export const repeat = n => function* (x) { for (let i = 0; i < n; i++)
|
|
10
|
+
yield x; };
|
|
11
|
+
export const just = function* (x) { yield x; };
|
|
12
|
+
export const enumerate = (n = 0) => x => [n++, x];
|
|
13
|
+
export function* empty() { }
|
|
14
|
+
;
|
|
15
|
+
export function* concat(...xxs) { for (const xs of xxs)
|
|
16
|
+
yield* xs; }
|
|
17
|
+
;
|
|
18
|
+
export const toArray = (xs) => xs.toArray();
|
|
19
|
+
export const map = (f) => (xs) => xs.map(f);
|
|
20
|
+
export const filter = (f) => (xs) => xs.filter(f);
|
|
21
|
+
export const drop = (n) => (xs) => xs.drop(n);
|
|
22
|
+
export const flatMap = (f) => (xs) => xs.flatMap(f);
|
|
23
|
+
export const forEach = (f = noop) => (xs) => xs.forEach(f);
|
|
24
|
+
export const some = (f) => (xs) => xs.some(f);
|
|
25
|
+
export const every = (f) => (xs) => xs.every(f);
|
|
26
|
+
export const find = (f) => (xs) => xs.find(f);
|
|
27
|
+
export const flat = (xs) => xs.flatMap(identity);
|
|
28
|
+
export const prepend = (ys) => (xs) => concat(ys, xs);
|
|
29
|
+
export const append = (ys) => (xs) => concat(xs, ys);
|
|
30
|
+
export const first = (xs) => xs.find(constantly(true));
|
|
31
|
+
export const reduce = (f, a) => (xs) => xs.reduce(f, a);
|
|
32
|
+
export const reduceRec = (xs, f, o, result) => {
|
|
33
|
+
const next = (o) => {
|
|
34
|
+
const { done, value } = xs.next();
|
|
35
|
+
if (done)
|
|
36
|
+
return result(o);
|
|
37
|
+
else
|
|
38
|
+
return f(o, value, next);
|
|
39
|
+
};
|
|
40
|
+
return next(o);
|
|
41
|
+
};
|
|
42
|
+
export const traverse = (children, f) => (x) => (f(x), forEach(traverse(children, f))(Iterator.from(children(x))));
|
|
43
|
+
export const take = n => function* (xs) {
|
|
44
|
+
for (let i = 0; i < n; i++) {
|
|
45
|
+
const { done, value } = xs.next();
|
|
46
|
+
if (done)
|
|
47
|
+
break;
|
|
48
|
+
yield value;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
export function* arange(a, b = Number.NaN, s = 1) {
|
|
52
|
+
if (Number.isNaN(b))
|
|
53
|
+
b = a, a = 0;
|
|
54
|
+
for (let i = a; i < b; i += s)
|
|
55
|
+
yield i;
|
|
56
|
+
}
|
|
57
|
+
export const linspace = (a, b, n) => arange(a, b + 1e-6, (b - a) / (n - 1));
|
|
58
|
+
export const steps = (a, b, s = 1, o = 0, eps = 1e-8) => {
|
|
59
|
+
const start = Math.floor((a - o + s - eps) / s) * s + o;
|
|
60
|
+
const end = Math.floor((b - o) / s) * s + o;
|
|
61
|
+
return arange(start, end + s - eps, s);
|
|
62
|
+
};
|
|
63
|
+
export const strided = (n, s) => function* (xs) {
|
|
64
|
+
const cur = [];
|
|
65
|
+
for (const x of xs) {
|
|
66
|
+
cur.push(x);
|
|
67
|
+
if (cur.length == n) {
|
|
68
|
+
yield cur;
|
|
69
|
+
for (let i = 0; i < s; i++)
|
|
70
|
+
cur.shift();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
export const stopAt = (p) => function* (xs) {
|
|
75
|
+
let x = xs.next();
|
|
76
|
+
while (!x.done) {
|
|
77
|
+
yield x.value;
|
|
78
|
+
if (p(x.value))
|
|
79
|
+
return;
|
|
80
|
+
x = xs.next();
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
export const until = (p) => function* (xs) {
|
|
84
|
+
if (p())
|
|
85
|
+
return;
|
|
86
|
+
let x = xs.next();
|
|
87
|
+
while (!x.done) {
|
|
88
|
+
yield x.value;
|
|
89
|
+
if (p())
|
|
90
|
+
return;
|
|
91
|
+
x = xs.next();
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
export function* zip(...xs) {
|
|
95
|
+
let val = xs.map(x => x.next());
|
|
96
|
+
while (val.every(x => !x.done)) {
|
|
97
|
+
yield val.map(x => x.value);
|
|
98
|
+
val = xs.map(x => x.next());
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
;
|
|
102
|
+
export const iterA = async function* (xs) {
|
|
103
|
+
for await (const x of xs)
|
|
104
|
+
yield x;
|
|
105
|
+
};
|
|
106
|
+
export const forEachA = (f = noop) => async (xs) => {
|
|
107
|
+
for await (const x of xs)
|
|
108
|
+
f(x);
|
|
109
|
+
};
|
|
110
|
+
export const mapA = f => async function* (xs) {
|
|
111
|
+
let x = await xs.next();
|
|
112
|
+
while (!x.done)
|
|
113
|
+
yield f(x.value), x = await xs.next();
|
|
114
|
+
};
|
|
115
|
+
export const flatMapA = (f) => async function* (xs) {
|
|
116
|
+
let x = await xs.next();
|
|
117
|
+
while (!x.done) {
|
|
118
|
+
yield* f(x.value);
|
|
119
|
+
x = await xs.next();
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
export const filterA = f => async function* (xs) {
|
|
123
|
+
let x = await xs.next();
|
|
124
|
+
while (!x.done) {
|
|
125
|
+
if (f(x.value))
|
|
126
|
+
yield x.value;
|
|
127
|
+
x = await xs.next();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
export const untilA = (p) => async function* (xs) {
|
|
131
|
+
if (p())
|
|
132
|
+
return;
|
|
133
|
+
let x = await xs.next();
|
|
134
|
+
while (!x.done) {
|
|
135
|
+
yield x.value;
|
|
136
|
+
if (p())
|
|
137
|
+
return;
|
|
138
|
+
x = await xs.next();
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
export const reduceA = (f, a) => async (xs) => {
|
|
142
|
+
const g = scan(f, a);
|
|
143
|
+
await forEachA()(mapA((x) => a = g(x))(xs));
|
|
144
|
+
return a;
|
|
145
|
+
};
|
|
146
|
+
export const race = (n) => async function* (xs) {
|
|
147
|
+
const it = pipe(xs, map(enumerate()), map(([i, x]) => [i, x.then(y => [i, y])]));
|
|
148
|
+
const accum = reduce((set), new Map());
|
|
149
|
+
const tasks = pipe(it, (take(n)), accum);
|
|
150
|
+
const next = comp((take(1)), accum);
|
|
151
|
+
while (tasks.size > 0) {
|
|
152
|
+
const [i, x] = await Promise.race(tasks.values());
|
|
153
|
+
tasks.delete(i);
|
|
154
|
+
yield x;
|
|
155
|
+
next(it);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
export const zmap = (f) => (...xs) => forEach(apply(f))(zip(...xs));
|
package/src/json.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const fromBytes: (data: AllowSharedBufferSource, options?: TextDecodeOptions) => any;
|
package/src/json.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const fromBytes = (data, options) => JSON.parse(new TextDecoder("utf-8").decode(data, options));
|
package/src/map.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FnV, Maybe } from "./types.js";
|
|
2
|
+
export declare const transform: <K, S, V>({ create, update, remove }: {
|
|
3
|
+
create: FnV<[S, K, Map<K, V>], V>;
|
|
4
|
+
update: FnV<[V, S, K, Map<K, V>], V>;
|
|
5
|
+
remove?: FnV<[V, K, Map<K, V>]>;
|
|
6
|
+
}, dst?: Map<K, V>) => (src: Map<K, S>) => Map<K, V>;
|
|
7
|
+
export declare const set: <K, V>(m: Map<K, V>, [i, x]: [K, V]) => Map<K, V>;
|
|
8
|
+
export declare const get: <K, V>(m: Map<K, V>, k: K, d: V) => V;
|
|
9
|
+
export declare const update: <K, V>(m: Map<K, V>, k: K, f: FnV<[V | undefined, K, Map<K, V>], V>) => Map<K, V>;
|
|
10
|
+
export declare const first: <K, V>(m: Map<K, V>) => Maybe<V>;
|
|
11
|
+
export declare const shift: <K, V>(m: Map<K, V>) => Maybe<[K, V]>;
|
|
12
|
+
export declare const reduce: <K, V, T>(m: Map<K, V>, f: FnV<[T, V, K, Map<K, V>], T>, a: T) => T;
|
package/src/map.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { isDefined } from "./checks.js";
|
|
2
|
+
import * as I from "./iterable.js";
|
|
3
|
+
export const transform = ({ create, update, remove }, dst = new Map()) => (src) => {
|
|
4
|
+
for (const k of dst.keys())
|
|
5
|
+
if (!src.has(k)) {
|
|
6
|
+
remove && remove(dst.get(k), k, dst);
|
|
7
|
+
dst.delete(k);
|
|
8
|
+
}
|
|
9
|
+
src.forEach((v, k) => {
|
|
10
|
+
if (!dst.has(k))
|
|
11
|
+
dst.set(k, create(v, k, dst));
|
|
12
|
+
else
|
|
13
|
+
dst.set(k, update(dst.get(k), v, k, dst));
|
|
14
|
+
});
|
|
15
|
+
return dst;
|
|
16
|
+
};
|
|
17
|
+
export const set = (m, [i, x]) => (m.set(i, x), m);
|
|
18
|
+
export const get = (m, k, d) => m.has(k) ? m.get(k) : d;
|
|
19
|
+
export const update = (m, k, f) => set(m, [k, f(m.get(k), k, m)]);
|
|
20
|
+
export const first = (m) => {
|
|
21
|
+
const k = I.first(m.keys());
|
|
22
|
+
if (isDefined(k))
|
|
23
|
+
return m.get(k);
|
|
24
|
+
return undefined;
|
|
25
|
+
};
|
|
26
|
+
export const shift = (m) => {
|
|
27
|
+
const k = I.first(m.keys());
|
|
28
|
+
if (isDefined(k)) {
|
|
29
|
+
const v = m.get(k);
|
|
30
|
+
m.delete(k);
|
|
31
|
+
return [k, v];
|
|
32
|
+
}
|
|
33
|
+
return undefined;
|
|
34
|
+
};
|
|
35
|
+
export const reduce = (m, f, a) => (m.forEach((v, k) => a = f(a, v, k, m)), a);
|
package/src/math.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export declare const PI: number;
|
|
2
|
+
export declare const TAU: number;
|
|
3
|
+
export declare const HALF_PI: number;
|
|
4
|
+
export declare const THIRD_PI: number;
|
|
5
|
+
export declare const QUARTER_PI: number;
|
|
6
|
+
export declare const SIXTH_PI: number;
|
|
7
|
+
export declare const SQRT2: number;
|
|
8
|
+
export declare const GOLDEN: number;
|
|
9
|
+
export declare const FOUR_OVER_PI: number;
|
|
10
|
+
export declare const INV_PI: number;
|
|
11
|
+
export declare const INV_TAU: number;
|
|
12
|
+
export declare const INV_HALF_PI: number;
|
|
13
|
+
export declare const U32_MAX = 4294967295;
|
|
14
|
+
export declare const EPS: number;
|
|
15
|
+
export declare const radians: (x: number) => number;
|
|
16
|
+
export declare const degrees: (x: number) => number;
|
|
17
|
+
export declare const safeSqrt: (x: number) => number;
|
|
18
|
+
export declare const lerp: (a: number, b: number, t: number) => number;
|
|
19
|
+
export declare const clamp: (a: number, b: number, x: number) => number;
|
|
20
|
+
export declare const saturate: (x: number) => number;
|
|
21
|
+
export declare const norm: (a: number, b: number, x: number) => number;
|
|
22
|
+
export declare const fit: (a: number, b: number, c: number, d: number, x: number) => number;
|
|
23
|
+
export declare const efit: (a: number, b: number, c: number, d: number, x: number) => number;
|
|
24
|
+
export declare const fit01: (a: number, b: number, x: number) => number;
|
|
25
|
+
export declare const ewmabc: (beta?: number, t?: number) => (x: number) => number;
|
|
26
|
+
export declare const sinc: (x: number) => number;
|
|
27
|
+
export declare const nextpow2: (x: number) => number;
|
|
28
|
+
export declare const mod: (x: number, m: number) => number;
|
|
29
|
+
export declare const logn: (n: number, x: number) => number;
|
|
30
|
+
export declare const floordiv: (x: number, b: number) => number;
|
|
31
|
+
export declare const zoom: (x: number, s: number, p: number) => number;
|
|
32
|
+
export declare const align: (x: number, b: number) => number;
|
|
33
|
+
export declare const sign11: (x: number) => 1 | -1;
|
|
34
|
+
export declare const nudge: (x: number, v?: number, eps?: number) => number;
|
package/src/math.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { isclose } from "./checks.js";
|
|
2
|
+
export const PI = Math.PI;
|
|
3
|
+
export const TAU = PI * 2;
|
|
4
|
+
export const HALF_PI = PI / 2;
|
|
5
|
+
export const THIRD_PI = PI / 3;
|
|
6
|
+
export const QUARTER_PI = PI / 4;
|
|
7
|
+
export const SIXTH_PI = PI / 6;
|
|
8
|
+
export const SQRT2 = Math.sqrt(2);
|
|
9
|
+
export const GOLDEN = (1 + 5 ** .5) * .5;
|
|
10
|
+
export const FOUR_OVER_PI = 4 / Math.PI;
|
|
11
|
+
export const INV_PI = 1 / PI;
|
|
12
|
+
export const INV_TAU = 1 / TAU;
|
|
13
|
+
export const INV_HALF_PI = 1 / HALF_PI;
|
|
14
|
+
export const U32_MAX = 0xFFFFFFFF;
|
|
15
|
+
export const EPS = Number.EPSILON;
|
|
16
|
+
const DEG2RAD = PI / 180, RAD2DEG = 180 / PI;
|
|
17
|
+
export const radians = (x) => x * DEG2RAD;
|
|
18
|
+
export const degrees = (x) => x * RAD2DEG;
|
|
19
|
+
export const safeSqrt = (x) => Math.sqrt(Math.max(x, 0));
|
|
20
|
+
export const lerp = (a, b, t) => a * (1 - t) + b * t;
|
|
21
|
+
export const clamp = (a, b, x) => Math.max(a, Math.min(b, x));
|
|
22
|
+
export const saturate = (x) => clamp(0, 1, x);
|
|
23
|
+
export const norm = (a, b, x) => (x - a) / ((b - a));
|
|
24
|
+
// https://www.sidefx.com/docs/houdini/expressions/fit.html
|
|
25
|
+
export const fit = (a, b, c, d, x) => lerp(c, d, saturate(norm(a, b, x)));
|
|
26
|
+
// https://www.sidefx.com/docs/houdini/expressions/efit.html
|
|
27
|
+
export const efit = (a, b, c, d, x) => lerp(c, d, norm(a, b, x));
|
|
28
|
+
// https://www.sidefx.com/docs/houdini/expressions/fit01.html
|
|
29
|
+
export const fit01 = (a, b, x) => lerp(a, b, saturate(x));
|
|
30
|
+
export const ewmabc = (beta = .8, t = 1) => {
|
|
31
|
+
let z = 0, bc = beta ** (t - 1);
|
|
32
|
+
return (x) => (z = lerp(x, z, beta)) / (1 - (bc *= beta));
|
|
33
|
+
};
|
|
34
|
+
export const sinc = (x) => x < Number.EPSILON ? 1.0 : Math.sin(x) / x;
|
|
35
|
+
export const nextpow2 = (x) => Math.ceil(Math.log2(Math.abs(x)));
|
|
36
|
+
export const mod = (x, m) => ((x % m) + m) % m;
|
|
37
|
+
export const logn = (n, x) => Math.log(x) / Math.log(n);
|
|
38
|
+
// https://www.desmos.com/calculator/ybbuznxbcc
|
|
39
|
+
export const floordiv = (x, b) => Math.floor(x / b + (x < 0 ? 1 : 0));
|
|
40
|
+
export const zoom = (x, s, p) => (x - p) * s + p;
|
|
41
|
+
export const align = (x, b) => (x + b - 1) & -b;
|
|
42
|
+
export const sign11 = (x) => x >= 0 ? 1 : -1;
|
|
43
|
+
export const nudge = (x, v = 0, eps = EPS) => isclose(x, v, eps) ? sign11(x - v) * eps + v : x;
|
package/src/npy.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { TypedArray, Elements } from "./types.js";
|
|
2
|
+
declare const DTYPES: readonly ["|u1", "<u2", "<u4", "|i1", "<i2", "<i4", "<f4", "<f8"];
|
|
3
|
+
type DType = Elements<typeof DTYPES>;
|
|
4
|
+
export interface NDArray {
|
|
5
|
+
data: TypedArray;
|
|
6
|
+
dtype: DType;
|
|
7
|
+
shape: number[];
|
|
8
|
+
}
|
|
9
|
+
export declare const read: (buf: ArrayBuffer) => NDArray;
|
|
10
|
+
export {};
|
package/src/npy.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const DTYPES = [
|
|
2
|
+
"|u1", "<u2", "<u4",
|
|
3
|
+
"|i1", "<i2", "<i4",
|
|
4
|
+
"<f4", "<f8",
|
|
5
|
+
// "<u8", "<i8",
|
|
6
|
+
];
|
|
7
|
+
const TYPED_ARRAY = {
|
|
8
|
+
"|u1": Uint8Array,
|
|
9
|
+
"<u2": Uint16Array,
|
|
10
|
+
"<u4": Uint32Array,
|
|
11
|
+
"|i1": Int8Array,
|
|
12
|
+
"<i2": Int16Array,
|
|
13
|
+
"<i4": Int32Array,
|
|
14
|
+
"<f4": Float32Array,
|
|
15
|
+
"<f8": Float64Array,
|
|
16
|
+
// "<u8": BigUint64Array,
|
|
17
|
+
// "<i8": BigInt64Array,
|
|
18
|
+
};
|
|
19
|
+
export const read = (buf) => {
|
|
20
|
+
const headerLength = new DataView(buf.slice(8, 10)).getUint8(0);
|
|
21
|
+
const hcontents = new TextDecoder("utf-8").decode(buf.slice(10, 10 + headerLength));
|
|
22
|
+
const { descr, shape } = JSON.parse(hcontents
|
|
23
|
+
.replace(/'/g, '"')
|
|
24
|
+
.replace("False", "false")
|
|
25
|
+
.replace("(", "[")
|
|
26
|
+
.replace(/,*\),*/g, "]"));
|
|
27
|
+
const data = new TYPED_ARRAY[descr](buf, headerLength + 10);
|
|
28
|
+
return { data, shape, dtype: descr };
|
|
29
|
+
};
|
package/src/number.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const add: (a: number, x: number) => number;
|
|
2
|
+
export declare const toFixed: (x: number | bigint, n?: number) => string | bigint;
|
|
3
|
+
export declare const toFixedMax: (x: number, n?: number) => string;
|
|
4
|
+
export declare const roundTo: (x: number, n: number) => number;
|
package/src/number.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { isBigInt } from "./checks.js";
|
|
2
|
+
export const add = (a, x) => a + x;
|
|
3
|
+
export const toFixed = (x, n = 0) => isBigInt(x) ? x : x.toFixed(n);
|
|
4
|
+
export const toFixedMax = (x, n = 0) => {
|
|
5
|
+
const y = Number(x.toFixed(n));
|
|
6
|
+
return `${Math.sign(y) == -1 ? '-' : ''}${Math.abs(y)}`;
|
|
7
|
+
};
|
|
8
|
+
export const roundTo = (x, n) => {
|
|
9
|
+
const inv = 1. / n;
|
|
10
|
+
const result = Math.round(x * inv) / inv;
|
|
11
|
+
const decimals = n.toString().split(".")[1]?.length || 0;
|
|
12
|
+
return parseFloat(result.toFixed(decimals));
|
|
13
|
+
};
|
package/src/object.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { RecordOf, Elements, Maybe, FnV, ValueOf, EntriesTuple, UnionToTuple, Entry } from "./types.js";
|
|
2
|
+
export declare const entries: <T extends RecordOf<any>>(o: T) => EntriesTuple<T>;
|
|
3
|
+
export declare const keys: <T extends RecordOf<any>>(o: T) => UnionToTuple<keyof T>;
|
|
4
|
+
export declare const entry: <K extends PropertyKey, V>(k: K, v: V) => Entry<K, V>;
|
|
5
|
+
export declare const fromEntries: <T extends Entry<PropertyKey, any>[]>(xs: T) => { [K in T[number] as K[0]]: K[1]; };
|
|
6
|
+
export declare const mapEntries: <T extends RecordOf<any>, S>(o: T, f: FnV<[Entry<keyof T, ValueOf<T>>, number], Entry<keyof T, S>>) => { [K in Entry<keyof T, S> as K[0]]: K[1]; };
|
|
7
|
+
export declare const mapValues: <T extends RecordOf<any>, S>(o: T, f: FnV<[ValueOf<T>, keyof T], S>) => { [K in Entry<keyof T, S> as K[0]]: K[1]; };
|
|
8
|
+
export declare const pick: <T extends RecordOf<any>, R extends Array<keyof T>>(obj: T, keys: R) => Pick<T, Elements<R>>;
|
|
9
|
+
export declare const omit: <T extends RecordOf<any>, R extends Array<keyof T>>(obj: T, keys: R) => Omit<T, Elements<R>>;
|
|
10
|
+
export declare const assoc: <T extends RecordOf<any>>(a: T, [k, v]: [keyof T, ValueOf<T>]) => T;
|
|
11
|
+
type Merge<T extends RecordOf<unknown>, S extends RecordOf<unknown>, V> = {
|
|
12
|
+
[K in keyof (T & S)]: V;
|
|
13
|
+
};
|
|
14
|
+
export declare const merge: <T, S = T, V = T>(f: FnV<[Maybe<T>, Maybe<S>], V>) => <X extends RecordOf<T>, Y extends RecordOf<T>>(x: X, y: Y) => Merge<X, Y, V>;
|
|
15
|
+
export {};
|
package/src/object.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const entries = (o) => Object.entries(o);
|
|
2
|
+
export const keys = (o) => Object.keys(o);
|
|
3
|
+
export const entry = (k, v) => [k, v];
|
|
4
|
+
export const fromEntries = (xs) => Object.fromEntries(xs);
|
|
5
|
+
export const mapEntries = (o, f) => fromEntries(entries(o).map(f));
|
|
6
|
+
export const mapValues = (o, f) => mapEntries(o, ([k, v]) => entry(k, f(v, k)));
|
|
7
|
+
export const pick = (obj, keys) => keys.reduce((a, k) => (a[k] = obj[k], a), {});
|
|
8
|
+
export const omit = (obj, keys) => pick(obj, Object.keys(obj).filter(k => !keys.includes(k)));
|
|
9
|
+
export const assoc = (a, [k, v]) => (a[k] = v, a);
|
|
10
|
+
export const merge = (f) => (x, y) => {
|
|
11
|
+
const keys = [...new Set(Object.keys(x).concat(Object.keys(y))).keys()];
|
|
12
|
+
return keys.reduce((a, k) => (a[k] = f(x[k], y[k]), a), {});
|
|
13
|
+
};
|
package/src/port.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Dispose, Fn, Fns, FnV, RecordOf, MaybePromise, Reactive } from "./types.js";
|
|
2
|
+
export interface Message<K, V> {
|
|
3
|
+
type: K;
|
|
4
|
+
payload: V;
|
|
5
|
+
}
|
|
6
|
+
export type Port = {
|
|
7
|
+
postMessage(message: any, options?: StructuredSerializeOptions): void;
|
|
8
|
+
};
|
|
9
|
+
export declare const onMessages: <S extends RecordOf<any>, T = Worker>(target: MessageEventTarget<T>, fs: Fns<S>) => Dispose;
|
|
10
|
+
export declare const onMessage: <K extends string, S, T = Worker>(target: MessageEventTarget<T>, name: K, f: Fn<S>) => Dispose;
|
|
11
|
+
export declare const messageSources: <T extends RecordOf<any>, S = Worker>(worker: MessageEventTarget<S>) => { [K in keyof T]: Reactive<T[K]>; };
|
|
12
|
+
export declare const postMessages: <T extends RecordOf<any>>(port: Port) => { [k in keyof T]: FnV<[T[k], (StructuredSerializeOptions | undefined)?]>; };
|
|
13
|
+
export interface Request {
|
|
14
|
+
id: number;
|
|
15
|
+
name: PropertyKey;
|
|
16
|
+
args: any;
|
|
17
|
+
}
|
|
18
|
+
export interface Response {
|
|
19
|
+
id: number;
|
|
20
|
+
result: any;
|
|
21
|
+
}
|
|
22
|
+
interface TransferableFnV<T extends any[], R> {
|
|
23
|
+
(...args: T): R;
|
|
24
|
+
transfer(ts?: Transferable[]): FnV<T, R>;
|
|
25
|
+
}
|
|
26
|
+
export declare const client: <S extends RecordOf<any>, T = Worker>(target: Port & MessageEventTarget<T>) => { [k in keyof S]: TransferableFnV<Parameters<S[k]>, Promise<ReturnType<S[k]>>>; };
|
|
27
|
+
export declare const server: <S extends RecordOf<any>, T = Worker>(target: Port & MessageEventTarget<T>) => { [k in keyof S]: Fn<FnV<Parameters<S[k]>, MaybePromise<ReturnType<S[k]>>>, Dispose>; };
|
|
28
|
+
export declare const startClient: (worker: Worker, f: Fn<Worker, Dispose | void>) => Promise<Dispose>;
|
|
29
|
+
export declare const startServer: (worker: Worker, f: Fn<Worker, MaybePromise<Dispose>>) => Promise<Dispose>;
|
|
30
|
+
export {};
|
package/src/port.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { forEach, noop, bind } from "./fn.js";
|
|
2
|
+
import { isDefined } from "./checks.js";
|
|
3
|
+
import { on } from "./dom.js";
|
|
4
|
+
import { promise } from "./async.js";
|
|
5
|
+
;
|
|
6
|
+
export const onMessages = (target, fs) => on(target, "message", ({ data: { type, payload } }) => (type in fs) && fs[type](payload));
|
|
7
|
+
export const onMessage = (target, name, f) => on(target, "message", ({ data: { type, payload } }) => type == name && f(payload));
|
|
8
|
+
export const messageSources = (worker) => new Proxy({}, {
|
|
9
|
+
get: (_, k) => bind((onMessage), worker, k)
|
|
10
|
+
});
|
|
11
|
+
export const postMessages = (port) => new Proxy({}, {
|
|
12
|
+
get: (_, type) => (payload, options) => port.postMessage({ type, payload }, options)
|
|
13
|
+
});
|
|
14
|
+
export const client = (target) => {
|
|
15
|
+
const fns = new Map();
|
|
16
|
+
onMessage(target, "rpc", ({ id, result }) => {
|
|
17
|
+
const f = fns.get(id);
|
|
18
|
+
if (isDefined(f))
|
|
19
|
+
f(result), fns.delete(id);
|
|
20
|
+
});
|
|
21
|
+
const { rpc } = postMessages(target);
|
|
22
|
+
let _id = 0;
|
|
23
|
+
return new Proxy({}, {
|
|
24
|
+
get(_, name) {
|
|
25
|
+
const g = (transfer) => (...args) => {
|
|
26
|
+
const id = _id++;
|
|
27
|
+
rpc({ id, name, args }, { transfer });
|
|
28
|
+
return new Promise(resolve => fns.set(id, resolve));
|
|
29
|
+
};
|
|
30
|
+
const f = ((...args) => g()(...args));
|
|
31
|
+
f.transfer = g;
|
|
32
|
+
return f;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
export const server = (target) => {
|
|
37
|
+
const fns = new Map();
|
|
38
|
+
const { rpc } = postMessages(target);
|
|
39
|
+
onMessage(target, "rpc", async ({ id, name, args }) => {
|
|
40
|
+
const f = fns.get(name);
|
|
41
|
+
if (isDefined(f))
|
|
42
|
+
rpc({ id, result: await promise(f(...args)) });
|
|
43
|
+
});
|
|
44
|
+
return new Proxy({}, {
|
|
45
|
+
get: (_, k) => (f) => (fns.set(k, f), () => fns.delete(k))
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
export const startClient = async (worker, f) => {
|
|
49
|
+
const cli = client(worker);
|
|
50
|
+
await cli.start();
|
|
51
|
+
return forEach(f(worker) ?? noop, () => cli.stop().then(() => worker.terminate()));
|
|
52
|
+
};
|
|
53
|
+
export const startServer = async (worker, f) => {
|
|
54
|
+
const ser = server(worker);
|
|
55
|
+
return ser.start(async () => { ser.stop(await promise(f(worker))); });
|
|
56
|
+
};
|
package/src/progress.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/src/rect.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Tuple } from "./types.js";
|
|
2
|
+
export type Rect = Tuple<number, 4>;
|
|
3
|
+
export declare const rect: (x: number, y: number, w: number, h: number) => Rect;
|
|
4
|
+
export declare const vertical: (ratio: number[]) => (i: number) => ([x, _, w, h]: Rect) => Rect;
|
|
5
|
+
export declare const horizontal: (ratio: number[]) => (i: number) => ([_, y, w, h]: Rect) => Rect;
|
package/src/rect.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { add } from "./number.js";
|
|
2
|
+
export const rect = (x, y, w, h) => [x, y, w, h];
|
|
3
|
+
export const vertical = (ratio) => {
|
|
4
|
+
const tot = ratio.reduce(add, 0);
|
|
5
|
+
const cum = [0];
|
|
6
|
+
ratio.forEach(x => cum.push(x / tot + cum[cum.length - 1]));
|
|
7
|
+
cum.pop();
|
|
8
|
+
return (i) => ([x, _, w, h]) => [x, cum[i] * h, w, ratio[i] / tot * h];
|
|
9
|
+
};
|
|
10
|
+
export const horizontal = (ratio) => {
|
|
11
|
+
const tot = ratio.reduce(add, 0);
|
|
12
|
+
const cum = [0];
|
|
13
|
+
ratio.forEach(x => cum.push(x / tot + cum[cum.length - 1]));
|
|
14
|
+
cum.pop();
|
|
15
|
+
return (i) => ([_, y, w, h]) => [cum[i] * w, y, ratio[i] / tot * w, h];
|
|
16
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Fn, Dispose, Structure, Reactive } from "./types.js";
|
|
2
|
+
export type Resource<T> = Reactive<T, Dispose | void> & {
|
|
3
|
+
value: T;
|
|
4
|
+
dispose: Dispose;
|
|
5
|
+
map<S>(f: Fn<T, S>): Resource<S>;
|
|
6
|
+
};
|
|
7
|
+
export declare const resource: <T>(value: T, dispose?: Dispose) => Resource<T>;
|
|
8
|
+
type Unwrapped<T> = T extends Resource<infer U> ? U : {
|
|
9
|
+
[K in keyof T]: Unwrapped<T[K]>;
|
|
10
|
+
};
|
|
11
|
+
export declare const compose: <R extends Structure<Resource<any>>>(rs: R) => Resource<Unwrapped<R>>;
|
|
12
|
+
export {};
|
package/src/resource.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { forEach, noop } from "./fn.js";
|
|
2
|
+
import { isFunction } from "./checks.js";
|
|
3
|
+
export const resource = (value, dispose = noop) => {
|
|
4
|
+
const res = ((f) => forEach(f(value) ?? noop, dispose));
|
|
5
|
+
res.value = value;
|
|
6
|
+
res.dispose = dispose;
|
|
7
|
+
res.map = (f) => resource(f(value), dispose);
|
|
8
|
+
return res;
|
|
9
|
+
};
|
|
10
|
+
const isResource = (r) => isFunction(r)
|
|
11
|
+
&& Object.hasOwn(r, "value")
|
|
12
|
+
&& Object.hasOwn(r, "dispose")
|
|
13
|
+
&& isFunction(r["dispose"]);
|
|
14
|
+
export const compose = (rs) => {
|
|
15
|
+
const disposes = [];
|
|
16
|
+
const rec = (rs) => {
|
|
17
|
+
if (isResource(rs)) {
|
|
18
|
+
disposes.push(rs.dispose);
|
|
19
|
+
return rs.value;
|
|
20
|
+
}
|
|
21
|
+
else if (Array.isArray(rs)) {
|
|
22
|
+
return rs.map(rec);
|
|
23
|
+
}
|
|
24
|
+
return Object.fromEntries(Object.entries(rs).map(([k, v]) => [k, rec(v)]));
|
|
25
|
+
};
|
|
26
|
+
const value = rec(rs);
|
|
27
|
+
return resource(value, forEach(...disposes));
|
|
28
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Dispose, Fn, FnV, Reactive } from './types.js';
|
|
2
|
+
export declare const raf: Reactive<number>;
|
|
3
|
+
export declare const timeout: (ms: number) => Reactive<void>;
|
|
4
|
+
export declare const schedule: <T>(s: Reactive<T>) => <F extends FnV>(f: F) => FnV<Parameters<F>, Dispose>;
|
|
5
|
+
export declare const idempotent: <T>(s: Reactive<T>, d: Fn<Dispose, Dispose>) => <F extends FnV>(f: F) => FnV<Parameters<F>, Dispose>;
|
|
6
|
+
export declare const debounce: <T>(sched?: Reactive<T>) => Reactive<T>;
|
|
7
|
+
export declare const reset: <T>(sched?: Reactive<T>) => Reactive<T>;
|
|
8
|
+
export declare const repeatedly: <T>(f: Fn<void>, sched?: Reactive<T>) => () => any;
|
|
9
|
+
export declare const delayAfter: <F extends FnV<any>>(delta: number, n: number, f: F) => (x: any) => any;
|
|
10
|
+
export declare const micro: <F extends FnV>(f: F) => (...xs: Parameters<F>) => void;
|
package/src/schedule.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { isUndefined, isDefined, counted } from './checks.js';
|
|
2
|
+
import { comp, lazy, bind, apply, call, where, disposing, noop } from './fn.js';
|
|
3
|
+
export const raf = f => bind(cancelAnimationFrame, requestAnimationFrame(f));
|
|
4
|
+
export const timeout = (ms) => f => bind(clearTimeout, setTimeout(f, ms));
|
|
5
|
+
export const schedule = (s) => (f) => call(comp(apply(bind((lazy), f)), s));
|
|
6
|
+
export const idempotent = (s, d) => (f) => disposing()((...xs) => d(s(() => f(...xs))));
|
|
7
|
+
export const debounce = (sched = raf) => {
|
|
8
|
+
let cancel;
|
|
9
|
+
const stop = () => { if (isDefined(cancel))
|
|
10
|
+
cancel(), cancel = undefined; };
|
|
11
|
+
return (f) => {
|
|
12
|
+
if (isUndefined(cancel))
|
|
13
|
+
cancel = sched((x) => (cancel = undefined, f(x)));
|
|
14
|
+
return stop;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export const reset = (sched = raf) => {
|
|
18
|
+
let cancel = noop;
|
|
19
|
+
const stop = () => (cancel(), cancel = noop);
|
|
20
|
+
return (f) => {
|
|
21
|
+
stop();
|
|
22
|
+
cancel = sched(f);
|
|
23
|
+
return stop;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export const repeatedly = (f, sched = raf) => {
|
|
27
|
+
let cancel;
|
|
28
|
+
const g = () => (f(), (cancel = sched(g)));
|
|
29
|
+
cancel = sched(g);
|
|
30
|
+
return () => cancel();
|
|
31
|
+
};
|
|
32
|
+
export const delayAfter = (delta, n, f) => where(counted(n), f, schedule(reset(timeout(delta)))(f));
|
|
33
|
+
export const micro = (f) => (...xs) => queueMicrotask(() => f(...xs));
|
package/src/set.d.ts
ADDED