@latticexyz/utils 0.9.0 → 0.11.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 (59) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/CoordMap.d.ts +26 -0
  3. package/dist/area.d.ts +3 -0
  4. package/{src/arrays.ts → dist/arrays.d.ts} +2 -8
  5. package/dist/console.d.ts +52 -0
  6. package/dist/cubic.d.ts +32 -0
  7. package/dist/deferred.d.ts +5 -0
  8. package/dist/distance.d.ts +8 -0
  9. package/dist/enums.d.ts +5 -0
  10. package/dist/eth.d.ts +22 -0
  11. package/dist/guards.d.ts +3 -0
  12. package/dist/hash.d.ts +6 -0
  13. package/{src/index.ts → dist/index.d.ts} +3 -13
  14. package/dist/index.js +802 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/iterable.d.ts +10 -0
  17. package/dist/math.d.ts +8 -0
  18. package/dist/mobx.d.ts +6 -0
  19. package/dist/objects.d.ts +11 -0
  20. package/dist/pack.d.ts +16 -0
  21. package/dist/promise.d.ts +4 -0
  22. package/dist/proxy.d.ts +8 -0
  23. package/{src/random.ts → dist/random.d.ts} +2 -7
  24. package/dist/rx.d.ts +30 -0
  25. package/dist/sleep.d.ts +1 -0
  26. package/dist/types.d.ts +40 -0
  27. package/dist/uuid.d.ts +19 -0
  28. package/dist/worker.d.ts +6 -0
  29. package/package.json +9 -5
  30. package/jest.config.js +0 -5
  31. package/rollup.config.js +0 -16
  32. package/src/CoordMap.spec.ts +0 -57
  33. package/src/CoordMap.ts +0 -106
  34. package/src/area.ts +0 -15
  35. package/src/console.ts +0 -62
  36. package/src/cubic.spec.ts +0 -14
  37. package/src/cubic.ts +0 -109
  38. package/src/deferred.ts +0 -14
  39. package/src/enums.ts +0 -13
  40. package/src/eth.ts +0 -42
  41. package/src/guards.ts +0 -10
  42. package/src/hash.ts +0 -11
  43. package/src/iterable.spec.ts +0 -56
  44. package/src/iterable.ts +0 -59
  45. package/src/mobx.ts +0 -26
  46. package/src/objects.ts +0 -16
  47. package/src/pack.spec.ts +0 -37
  48. package/src/pack.ts +0 -61
  49. package/src/promise.ts +0 -45
  50. package/src/proxy.spec.ts +0 -186
  51. package/src/proxy.ts +0 -101
  52. package/src/rx.spec.ts +0 -45
  53. package/src/rx.ts +0 -124
  54. package/src/sleep.ts +0 -3
  55. package/src/types.ts +0 -48
  56. package/src/uuid.ts +0 -70
  57. package/src/worker.ts +0 -16
  58. package/tsconfig.json +0 -102
  59. package/typedoc.json +0 -9
package/src/proxy.spec.ts DELETED
@@ -1,186 +0,0 @@
1
- import { runInAction } from "mobx";
2
- import { computed, observable } from "mobx";
3
- import { deferred } from "./deferred";
4
- import { cacheUntilReady } from "./proxy";
5
-
6
- describe("cacheUntilReady", () => {
7
- describe("property access", () => {
8
- it("should immediately relay simple property access to the computed target if it is ready", () => {
9
- const target = computed(() => ({ a: 1 }));
10
- const proxy = cacheUntilReady(target);
11
- expect(proxy.a).toBe(1);
12
- expect(proxy.proxied).toBe(false);
13
- });
14
-
15
- it("should immediately relay simple property access to the observable target if it is ready", () => {
16
- const target = observable.box({ a: 1 });
17
- const proxy = cacheUntilReady(target);
18
- expect(proxy.a).toBe(1);
19
- expect(proxy.proxied).toBe(false);
20
- });
21
-
22
- it("should immediately relay deep property access to the computed target if it is ready", () => {
23
- const target = computed(() => ({ a: { b: { c: 2 } } }));
24
- const proxy = cacheUntilReady(target);
25
- expect(proxy.a.b.c).toBe(2);
26
- expect(proxy.proxied).toBe(false);
27
- });
28
-
29
- it("should immediately relay deep property access to the observable target if it is ready", () => {
30
- const target = observable.box({ a: { b: 2 } });
31
- const proxy = cacheUntilReady(target);
32
- expect(proxy.a.b).toBe(2);
33
- expect(proxy.proxied).toBe(false);
34
- });
35
-
36
- it("should return undefined for property access if the target is not ready", () => {
37
- const target = observable.box<{ a: { b: number } } | undefined>(undefined);
38
- const proxy = cacheUntilReady(target);
39
- const result1 = proxy.a;
40
- const result2 = proxy.a.b;
41
-
42
- expect(JSON.stringify(result1)).toBe(JSON.stringify({ proxied: true }));
43
- expect(JSON.stringify(result2)).toBe(JSON.stringify({ proxied: true }));
44
- expect(proxy.proxied).toBe(true);
45
- });
46
- });
47
-
48
- describe("function calls", () => {
49
- it("should immediately relay top level function calls to the computed target it if it ready", () => {
50
- const target = computed(() => ({
51
- a: () => 1,
52
- }));
53
-
54
- const proxy = cacheUntilReady(target);
55
-
56
- expect(proxy.a()).toBe(1);
57
- });
58
-
59
- it("should immediately relay top level function calls to the observable target it if it ready", () => {
60
- const target = observable.box({
61
- a: () => 1,
62
- });
63
-
64
- const proxy = cacheUntilReady(target);
65
-
66
- expect(proxy.a()).toBe(1);
67
- });
68
-
69
- it("should immediately relay deep nested function calls to the computed target it if it ready", () => {
70
- const target = computed(() => ({
71
- a: { b: () => 1 },
72
- }));
73
-
74
- const proxy = cacheUntilReady(target);
75
-
76
- expect(proxy.a.b()).toBe(1);
77
- });
78
-
79
- it("should immediately relay deep nested function calls to the observable target it if it ready", () => {
80
- const target = observable.box({
81
- a: { b: () => 1 },
82
- });
83
-
84
- const proxy = cacheUntilReady(target);
85
-
86
- expect(proxy.a.b()).toBe(1);
87
- });
88
-
89
- it("should chache top level function calls to the target until it is ready", async () => {
90
- const target = observable.box<{ a: (n: number) => number } | undefined>(undefined);
91
-
92
- const proxy = cacheUntilReady(target);
93
-
94
- // Target is not ready yet, but function is called already
95
- const result1 = proxy.a(1);
96
- const result2 = proxy.a(2);
97
-
98
- // Now target is ready
99
- runInAction(() => target.set({ a: (n) => n }));
100
-
101
- // Cached function calls are applied,
102
- expect(await result1).toEqual(1);
103
- expect(await result2).toEqual(2);
104
- });
105
-
106
- it("should chache nested function calls to the target until it is ready", async () => {
107
- const target = observable.box<{ a: { b: (n: number) => number } } | undefined>(undefined);
108
-
109
- const proxy = cacheUntilReady(target);
110
-
111
- // Target is not ready yet, but function is called already
112
- const result1 = proxy.a.b(1);
113
- const result2 = proxy.a.b(2);
114
-
115
- const getResult3 = async () => proxy.a.b((await result1) + (await result2));
116
- const result3 = getResult3();
117
-
118
- // Now target is ready
119
- runInAction(() => target.set({ a: { b: (n) => n + 1 } }));
120
-
121
- // Cached function calls are applied,
122
- expect(await result1).toEqual(2);
123
- expect(await result2).toEqual(3);
124
- expect(await result3).toEqual(6);
125
- });
126
-
127
- it("should chache nested function calls without parameters to the target until it is ready", async () => {
128
- const target = observable.box<{ a: { b: () => number } } | undefined>(undefined);
129
-
130
- const proxy = cacheUntilReady(target);
131
-
132
- // Target is not ready yet, but function is called already
133
- const result1 = proxy.a.b();
134
- const result2 = proxy.proxied && proxy.a.b();
135
-
136
- // Now target is ready
137
- runInAction(() => target.set({ a: { b: () => 1 } }));
138
-
139
- // Cached function calls are applied,
140
- expect(await result1).toEqual(1);
141
- expect(await result2).toEqual(1);
142
- });
143
-
144
- it("should chache nested function calls with variable parameters to the target until it is ready", async () => {
145
- const target = observable.box<{ a: { b: (...args: number[]) => number } } | undefined>(undefined);
146
-
147
- const proxy = cacheUntilReady(target);
148
-
149
- // Target is not ready yet, but function is called already
150
- const result1 = proxy.a.b();
151
- const result2 = proxy.a.b(1);
152
- const result3 = proxy.a.b(1, 2);
153
- const result4 = proxy.a.b(1, 2, 3);
154
-
155
- // Now target is ready
156
- runInAction(() => target.set({ a: { b: (...args) => args.reduce((acc, curr) => acc + curr, 0) } }));
157
-
158
- // Cached function calls are applied,
159
- expect(await result1).toEqual(0);
160
- expect(await result2).toEqual(1);
161
- expect(await result3).toEqual(3);
162
- expect(await result4).toEqual(6);
163
- });
164
-
165
- it("should return a non-nested proxy if the called target function returns a proxy", async () => {
166
- const target = observable.box<{ a: { b: () => Promise<number> } } | undefined>(undefined);
167
-
168
- const proxy = cacheUntilReady(target);
169
-
170
- // Target is not ready yet, but function is called already
171
- const result1 = proxy.a.b();
172
- const result2 = proxy.a.b();
173
-
174
- const [resolve, , promise] = deferred<number>();
175
- // Now target is ready
176
- runInAction(() => target.set({ a: { b: () => promise } }));
177
-
178
- // Now resolve the promise returned by the target function
179
- resolve(1);
180
-
181
- // Cached function calls are applied,
182
- expect(await result1).toEqual(1);
183
- expect(await result2).toEqual(1);
184
- });
185
- });
186
- });
package/src/proxy.ts DELETED
@@ -1,101 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { IComputedValue, IObservableValue, reaction } from "mobx";
3
- import DeepProxy from "proxy-deep";
4
- import { deferred } from "./deferred";
5
- import { isFunction, isObject } from "./guards";
6
- import { Cached } from "./types";
7
-
8
- function deepAccess(target: Record<string, unknown>, path: string[]): any {
9
- if (path.length === 0) return target;
10
- if (path.length === 1) return target[path[0]];
11
- const [next, ...rest] = path;
12
- const nextTarget = target[next];
13
- if (!isObject(nextTarget)) throw new Error("Path does not exist on the target");
14
- return deepAccess(nextTarget, rest);
15
- }
16
-
17
- /**
18
- * Caches any function calls to the target until the target is ready.
19
- * @param target T extends Cachable
20
- * @returns Cached<T>
21
- */
22
- export function cacheUntilReady<T extends Record<string, any>>(
23
- target: IObservableValue<T | undefined> | IComputedValue<T | undefined>
24
- ): Cached<T> {
25
- // The call queue contains the path and arguments of calls to the
26
- // proxiedTarget while the target was not available yet.
27
- // It also contains resolve and reject methods to fulfil the promise
28
- // returned when calling the proxiedTarget once the target becomes available.
29
- const callQueue: {
30
- path: string[];
31
- args?: any[];
32
- resolve: (result: any) => void;
33
- reject: (e: Error) => void;
34
- }[] = [];
35
-
36
- // The proxiedTarget proxies all calls to the target.
37
- // If a function is called on the proxiedTarget while the target is not
38
- // available, a promise is returned and the call will be stored in the callQueue
39
- // until the target becomes available and the promise is fulfilled.
40
- const proxiedTarget = new DeepProxy(
41
- {},
42
- {
43
- get(_t, prop) {
44
- const targetReady = target.get();
45
- if (targetReady) {
46
- // If the target is ready, relay all calls directly to the target
47
- // (Except for the "proxied" key, which indicates whether the object is currently proxied)
48
- if (prop === "proxied") return false;
49
- return Reflect.get(targetReady, prop);
50
- } else {
51
- // Note: if the target is not available, accessing a property returns another proxy,
52
- // not a Promise. It is possible to check whether a value is currently proxied using the proxied key.
53
- if (prop === "proxied") return true;
54
- if (prop === "name") return "ProxiedTarget";
55
- if (prop === "toJSON") return () => ({ proxied: true });
56
- return this.nest(() => void 0);
57
- }
58
- },
59
- apply(_, thisArg, args) {
60
- const targetReady = target.get();
61
- if (targetReady) {
62
- // If the target is ready, relay all calls directly to the target
63
- const targetFunc = deepAccess(targetReady, this.path);
64
- if (!isFunction(targetFunc)) throw new Error("Target is not callable");
65
- return Reflect.apply(targetFunc, thisArg, args);
66
- } else {
67
- // Otherwise store the call and relay it to the target later once it's ready.
68
- // The return value of this call is a promise, that gets resolved once the target is ready.
69
- const [resolve, reject, promise] = deferred();
70
- callQueue.push({ path: this.path, args, resolve, reject });
71
- return promise;
72
- }
73
- },
74
- }
75
- );
76
-
77
- reaction(
78
- () => target.get(),
79
- (targetReady) => {
80
- if (!targetReady) return;
81
- // Move all entries from callQueue to queuedCalls
82
- const queuedCalls = callQueue.splice(0);
83
- for (const { path, args, resolve, reject } of queuedCalls) {
84
- const target = deepAccess(targetReady, path);
85
- if (args && isFunction(target)) {
86
- (async () => {
87
- try {
88
- resolve(await target(...args));
89
- } catch (e: any) {
90
- reject(e);
91
- }
92
- })();
93
- } else {
94
- resolve(target);
95
- }
96
- }
97
- }
98
- );
99
-
100
- return proxiedTarget as Cached<T>;
101
- }
package/src/rx.spec.ts DELETED
@@ -1,45 +0,0 @@
1
- import { Subject } from "rxjs";
2
- import { awaitStreamValue, streamToComputed } from "./rx";
3
- import { sleep } from "./sleep";
4
-
5
- describe("streamToComputed", () => {
6
- it("should return a computed value that corresponds to the last stream value", () => {
7
- const stream = new Subject<number>();
8
- const comp = streamToComputed(stream);
9
- expect(comp.get()).toBeUndefined();
10
-
11
- stream.next(1);
12
- expect(comp.get()).toBe(1);
13
-
14
- stream.next(2);
15
- expect(comp.get()).toBe(2);
16
-
17
- stream.next(3);
18
- stream.next(4);
19
- expect(comp.get()).toBe(4);
20
- });
21
- });
22
-
23
- describe("awaitStreamValue", () => {
24
- it("should return a promise that resolves once the requested stream value is emitted", async () => {
25
- const stream = new Subject<number>();
26
- const promise = awaitStreamValue(stream, (v) => v === 1);
27
-
28
- let isFulfilled = false;
29
- (async () => {
30
- await promise;
31
- isFulfilled = true;
32
- })();
33
-
34
- await sleep(100);
35
- expect(isFulfilled).toBe(false);
36
-
37
- stream.next(2);
38
- await sleep(100);
39
- expect(isFulfilled).toBe(false);
40
-
41
- stream.next(1);
42
- await sleep(100);
43
- expect(isFulfilled).toBe(true);
44
- });
45
- });
package/src/rx.ts DELETED
@@ -1,124 +0,0 @@
1
- import {
2
- concatMap,
3
- delay,
4
- filter,
5
- first,
6
- mergeMap,
7
- Observable,
8
- of,
9
- OperatorFunction,
10
- pipe,
11
- ReplaySubject,
12
- scan,
13
- Timestamp,
14
- timestamp,
15
- } from "rxjs";
16
- import { computed, IComputedValue, IObservableValue, observable, reaction, runInAction, toJS } from "mobx";
17
- import { deferred } from "./deferred";
18
- import { awaitValue } from "./mobx";
19
-
20
- export function filterNullish<T>(): OperatorFunction<T, NonNullable<T>> {
21
- return pipe<Observable<T>, Observable<NonNullable<T>>>(
22
- filter<T>((x: T) => x != null) as OperatorFunction<T, NonNullable<T>>
23
- );
24
- }
25
-
26
- export function awaitPromise<T extends Promise<unknown>>(): OperatorFunction<T, Awaited<T>> {
27
- return pipe(concatMap((x: T) => x)) as OperatorFunction<T, Awaited<T>>;
28
- }
29
-
30
- /**
31
- * RxJS operator to stretch out an event stream by a given delay per event
32
- * @param spacingDelayMs Delay between each event in ms
33
- * @returns stream of events with at least spacingDelayMs spaceing between event
34
- */
35
- export function stretch<T>(spacingDelayMs: number) {
36
- return pipe(
37
- timestamp<T>(),
38
- scan((acc: (Timestamp<T> & { delay: number }) | null, curr: Timestamp<T>) => {
39
- // calculate delay needed to offset next emission
40
- let delay = 0;
41
- if (acc !== null) {
42
- const timeDelta = curr.timestamp - acc.timestamp;
43
- delay = timeDelta > spacingDelayMs ? 0 : spacingDelayMs - timeDelta;
44
- }
45
-
46
- return {
47
- timestamp: curr.timestamp,
48
- delay: delay,
49
- value: curr.value,
50
- };
51
- }, null),
52
- filterNullish(),
53
- mergeMap((i) => of(i.value).pipe(delay(i.delay)), 1)
54
- );
55
- }
56
-
57
- export function observableToComputed<T>(obs: IObservableValue<T>): IComputedValue<T> {
58
- return computed(() => obs.get());
59
- }
60
-
61
- export function computedToStream<T>(comp: IComputedValue<T> | IObservableValue<T>): Observable<T> {
62
- const stream = new ReplaySubject<T>(1);
63
- reaction(
64
- () => comp.get(),
65
- (value) => {
66
- if (value != null) stream.next(value);
67
- },
68
- { fireImmediately: true }
69
- );
70
- return stream;
71
- }
72
-
73
- export function observableToStream<T>(obs: T): Observable<T> {
74
- const stream = new ReplaySubject<T>(1);
75
- reaction(
76
- () => toJS(obs),
77
- (value) => {
78
- if (value != null) stream.next(value);
79
- },
80
- { fireImmediately: true }
81
- );
82
- return stream;
83
- }
84
-
85
- export function streamToComputed<T>(stream$: Observable<T>): IComputedValue<T | undefined> {
86
- const value = observable.box<T | undefined>();
87
- stream$.subscribe((val) => runInAction(() => value.set(val)));
88
- return computed(() => value.get());
89
- }
90
-
91
- export async function streamToDefinedComputed<T>(stream$: Observable<T>): Promise<IComputedValue<T>> {
92
- const value = observable.box<T>();
93
- stream$.subscribe((val) => runInAction(() => value.set(val)));
94
- const computedValue = computed(() => value.get());
95
- await awaitValue(computedValue);
96
- return computedValue as IComputedValue<T>;
97
- }
98
-
99
- /**
100
- *
101
- * @param stream$ RxJS observable to check for the given value
102
- * @param predicate Predicate to check
103
- * @returns A promise that resolves with the requested value once the predicate is true
104
- */
105
- export async function awaitStreamValue<T>(
106
- stream$: Observable<T>,
107
- predicate: (value: T) => boolean = (value) => value != null
108
- ): Promise<T> {
109
- const [resolve, , promise] = deferred<T>();
110
- stream$.pipe(first(predicate)).subscribe(resolve);
111
- return promise;
112
- }
113
-
114
- /**
115
- * Turns a stream into an updating object for easy access outside of rxjs
116
- * @param stream$ Stream to turn into a wrapped value
117
- * @returns Object with `current` key corresponding to last stream value
118
- */
119
- export async function streamToWrappedValue<T>(stream$: Observable<T>): Promise<{ current: T }> {
120
- const value: { current?: T } = {};
121
- stream$.subscribe((v) => (value.current = v));
122
- value.current = await awaitStreamValue(stream$);
123
- return value as { current: T };
124
- }
package/src/sleep.ts DELETED
@@ -1,3 +0,0 @@
1
- export function sleep<T>(timeout: number, returns?: T): Promise<T> {
2
- return new Promise<T>((resolve) => setTimeout(() => resolve(returns as T), timeout));
3
- }
package/src/types.ts DELETED
@@ -1,48 +0,0 @@
1
- import { enableLogger } from "./console";
2
-
3
- /* eslint-disable @typescript-eslint/no-explicit-any */
4
- export type Func<A extends any[], R> = (...args: A) => R;
5
- export type AsyncFunc<A extends any[], R> = R extends Promise<unknown> ? (...args: A) => R : (...args: A) => Promise<R>;
6
-
7
- export type CachedValue<V, Proxied extends boolean> = Proxied extends true
8
- ? V extends Func<infer A, infer B>
9
- ? AsyncFunc<A, B>
10
- : V extends Record<string, any>
11
- ? Cached<V> & { proxied: true }
12
- : { proxied: true }
13
- : V extends Func<infer A, infer B>
14
- ? Func<A, B>
15
- : V extends Record<string, any>
16
- ? V
17
- : V & { proxied: false };
18
-
19
- export type Cached<C> =
20
- | ({ proxied: false } & { [key in keyof C]: CachedValue<C[key], false> })
21
- | ({ proxied: true } & { [key in keyof C]: CachedValue<C[key], true> });
22
-
23
- /**
24
- * @deprecated Use Awaited<T> instead
25
- */
26
- export type PromiseValue<T> = Awaited<T>;
27
-
28
- export type ValueOf<T extends object> = T[keyof T];
29
-
30
- export type Area = {
31
- x: number;
32
- y: number;
33
- width: number;
34
- height: number;
35
- };
36
-
37
- export type Coord = {
38
- x: number;
39
- y: number;
40
- };
41
-
42
- export type VoxelCoord = {
43
- x: number;
44
- y: number;
45
- z: number;
46
- };
47
-
48
- export type Logger = ReturnType<typeof enableLogger>;
package/src/uuid.ts DELETED
@@ -1,70 +0,0 @@
1
- /**
2
- * UUID.core.js - UUID.js for Minimalists
3
- *
4
- * @file
5
- * @author LiosK
6
- * @version v4.2.0
7
- * @license Apache License 2.0: Copyright (c) 2010-2018 LiosK
8
- * @url https://github.com/LiosK/UUID.js/blob/master/src/uuid.core.js
9
- */
10
-
11
- /**
12
- * @class
13
- * @classdesc {@link UUID} object.
14
- * @hideconstructor
15
- */
16
-
17
- // Core Component {{{
18
-
19
- /**
20
- * Generates a version 4 UUID as a hexadecimal string.
21
- * @returns {string} Hexadecimal UUID string.
22
- */
23
- export const uuid = function () {
24
- const rand = _getRandomInt,
25
- hex = _hexAligner;
26
- return (
27
- hex(rand(32), 8) + // time_low
28
- "-" +
29
- hex(rand(16), 4) + // time_mid
30
- "-" +
31
- hex(0x4000 | rand(12), 4) + // time_hi_and_version
32
- "-" +
33
- hex(0x8000 | rand(14), 4) + // clock_seq_hi_and_reserved clock_seq_low
34
- "-" +
35
- hex(rand(48), 12)
36
- ); // node
37
- };
38
-
39
- /**
40
- * Returns an unsigned x-bit random integer.
41
- * @private
42
- * @param {number} x Unsigned integer ranging from 0 to 53, inclusive.
43
- * @returns {number} Unsigned x-bit random integer (0 <= f(x) < 2^x).
44
- */
45
- const _getRandomInt = function (x: number) {
46
- if (x < 0 || x > 53) {
47
- return NaN;
48
- }
49
- const n = 0 | (Math.random() * 0x40000000); // 1 << 30
50
- return x > 30 ? n + (0 | (Math.random() * (1 << (x - 30)))) * 0x40000000 : n >>> (30 - x);
51
- };
52
-
53
- /**
54
- * Converts an integer to a zero-filled hexadecimal string.
55
- * @private
56
- * @param {number} num
57
- * @param {number} length
58
- * @returns {string}
59
- */
60
- const _hexAligner = function (num: number, length: number) {
61
- let str = num.toString(16),
62
- i = length - str.length,
63
- z = "0";
64
- for (; i > 0; i >>>= 1, z += z) {
65
- if (i & 1) {
66
- str = z + str;
67
- }
68
- }
69
- return str;
70
- };
package/src/worker.ts DELETED
@@ -1,16 +0,0 @@
1
- import { fromEvent, map, Observable } from "rxjs";
2
-
3
- export interface DoWork<I, O> {
4
- work(input$: Observable<I>): Observable<O>;
5
- }
6
-
7
- export function fromWorker<I, O>(worker: Worker, input$: Observable<I>): Observable<O> {
8
- input$.subscribe((event) => worker.postMessage(event));
9
- return fromEvent<MessageEvent<O>>(worker, "message").pipe(map((e) => e.data));
10
- }
11
-
12
- export function runWorker<I, O>(worker: DoWork<I, O>) {
13
- const input$ = fromEvent<MessageEvent<I>>(self, "message");
14
- const output$ = worker.work(input$.pipe(map((event) => event.data)));
15
- output$.subscribe((event) => self.postMessage(event));
16
- }