@latticexyz/utils 2.0.12-account-kit-27d878b7 → 2.0.12-account-kit-9160f5e1a

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/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/random.ts DELETED
@@ -1,17 +0,0 @@
1
- /**
2
- *
3
- * @param to Upper bound (included)
4
- * @param from Lower bound (included). Default 0.
5
- * @returns A random integer between from and to.
6
- */
7
- export function random(to: number, from = 0): number {
8
- return Math.floor(Math.random() * (to - from + 1)) + from;
9
- }
10
-
11
- /**
12
- * @param array Array to pick a random element from.
13
- * @returns Random element from the given array.
14
- */
15
- export function pickRandom<T>(array: [T, ...T[]]): T {
16
- return array[random(array.length - 1)];
17
- }
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
- };
@@ -1,3 +0,0 @@
1
- // TODO: migrate to viem's toHex()
2
- export const arrayToHex = (array: Uint8Array | ArrayBuffer): `0x${string}` =>
3
- `0x${[...new Uint8Array(array)].map((x) => x.toString(16).padStart(2, "0")).join("")}`;
@@ -1 +0,0 @@
1
- export const bytesToString = (bytes: Uint8Array): string => [...bytes].map((x) => String.fromCharCode(x)).join("");
@@ -1,12 +0,0 @@
1
- import { isHex } from "./isHex";
2
-
3
- // TODO: migrate to viem's toBytes(hex)
4
- export const hexToArray = (hex: string): Uint8Array => {
5
- if (!isHex(hex)) {
6
- console.error("Invalid hex string", hex);
7
- throw new Error("Invalid hex string");
8
- }
9
- const bytes = hex.match(/[\da-f]{2}/gi);
10
- if (!bytes) return new Uint8Array([]);
11
- return new Uint8Array(bytes.map((byte) => parseInt(byte, 16)));
12
- };
package/src/v2/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from "./arrayToHex";
2
- export * from "./bytesToString";
3
- export * from "./hexToArray";
4
- export * from "./isHex";
5
- export * from "./stringToBytes";
package/src/v2/isHex.ts DELETED
@@ -1,6 +0,0 @@
1
- // TODO: migrate to viem's isHex()
2
- // Note that this assumes hex pairs, but viem does not. We'll need to be careful migrating.
3
- // Padding an odd-length hex sounds scary (based on how Solidity left/right aligns numbers vs bytes/strings).
4
- export function isHex(hex: string): boolean {
5
- return /^(0x)?([\da-f]{2})*$/i.test(hex);
6
- }
@@ -1,9 +0,0 @@
1
- export const stringToBytes16 = (str: string): Uint8Array => {
2
- if (str.length > 16) throw new Error("string too long");
3
- return new Uint8Array(16).map((v, i) => str.charCodeAt(i));
4
- };
5
-
6
- export const stringToBytes32 = (str: string): Uint8Array => {
7
- if (str.length > 32) throw new Error("string too long");
8
- return new Uint8Array(32).map((v, i) => str.charCodeAt(i));
9
- };
package/src/worker.ts DELETED
@@ -1,16 +0,0 @@
1
- import { fromEvent, map, Observable } from "rxjs";
2
-
3
- export interface DoWork<In, Out> {
4
- work(input$: Observable<In>): Observable<Out>;
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
- }