@latticexyz/utils 2.0.12-account-kit-27d878b7 → 2.0.12-account-kit-72f3555a8
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/package.json +4 -2
- package/CHANGELOG.md +0 -622
- package/src/CoordMap.spec.ts +0 -57
- package/src/CoordMap.ts +0 -106
- package/src/VoxelCoordMap.ts +0 -76
- package/src/area.ts +0 -15
- package/src/arrays.ts +0 -19
- package/src/bytes.spec.ts +0 -77
- package/src/bytes.ts +0 -85
- package/src/console.ts +0 -62
- package/src/cubic.spec.ts +0 -14
- package/src/cubic.ts +0 -109
- package/src/deferred.ts +0 -14
- package/src/distance.ts +0 -11
- package/src/enums.ts +0 -13
- package/src/eth.ts +0 -42
- package/src/guards.ts +0 -10
- package/src/index.ts +0 -38
- package/src/iterable.spec.ts +0 -56
- package/src/iterable.ts +0 -59
- package/src/math.ts +0 -11
- package/src/mobx.ts +0 -26
- package/src/objects.ts +0 -16
- package/src/pack.spec.ts +0 -37
- package/src/pack.ts +0 -61
- package/src/promise.ts +0 -46
- package/src/proxy.spec.ts +0 -186
- package/src/proxy.ts +0 -101
- package/src/random.ts +0 -17
- package/src/rx.spec.ts +0 -45
- package/src/rx.ts +0 -124
- package/src/sleep.ts +0 -3
- package/src/types.ts +0 -48
- package/src/uuid.ts +0 -70
- package/src/v2/arrayToHex.ts +0 -3
- package/src/v2/bytesToString.ts +0 -1
- package/src/v2/hexToArray.ts +0 -12
- package/src/v2/index.ts +0 -5
- package/src/v2/isHex.ts +0 -6
- package/src/v2/stringToBytes.ts +0 -9
- package/src/worker.ts +0 -16
package/src/eth.ts
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Pads start of a hex string with 0 to create a bit string of the given length
|
3
|
-
* @param input Hex string
|
4
|
-
* @param bits Number of bits in the output hex string
|
5
|
-
* @returns Hex string of specified length
|
6
|
-
*/
|
7
|
-
export function padToBitLength(input: string, bits: number) {
|
8
|
-
// Cut off 0x prefix
|
9
|
-
if (input.substring(0, 2) == "0x") input = input.substring(2);
|
10
|
-
// Pad start with 0 to get desired bit length
|
11
|
-
const length = bits / 4;
|
12
|
-
input = input.padStart(length, "0");
|
13
|
-
input = input.substring(input.length - length);
|
14
|
-
// Prefix with 0x
|
15
|
-
return `0x${input}`;
|
16
|
-
}
|
17
|
-
|
18
|
-
/**
|
19
|
-
* Pads start of a hex string with 0 to create a 160 bit hex string
|
20
|
-
* which can be used as an Ethereum address
|
21
|
-
* @param input Hex string
|
22
|
-
* @returns 160 bit hex string
|
23
|
-
*/
|
24
|
-
export function toEthAddress(input: string) {
|
25
|
-
return padToBitLength(input, 160);
|
26
|
-
}
|
27
|
-
|
28
|
-
/**
|
29
|
-
* Pads start of a hex string with 0 to create a 256bit hex string
|
30
|
-
* which can be used as an Ethereum address
|
31
|
-
* @param input Hex string
|
32
|
-
* @returns 256 bit hex string
|
33
|
-
*/
|
34
|
-
export function to256BitString(input: string) {
|
35
|
-
return padToBitLength(input, 256);
|
36
|
-
}
|
37
|
-
|
38
|
-
export function extractEncodedArguments(input: string) {
|
39
|
-
// Cutting off the first 4 bytes, which represent the function selector
|
40
|
-
if (input[0] !== "0" && input[1] !== "x") throw new Error("Invalid hex string");
|
41
|
-
return "0x" + input.substring(10);
|
42
|
-
}
|
package/src/guards.ts
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2
|
-
import { Func } from "./types";
|
3
|
-
|
4
|
-
export function isObject(c: unknown): c is Record<string, any> {
|
5
|
-
return typeof c === "object" && !Array.isArray(c) && c !== null;
|
6
|
-
}
|
7
|
-
|
8
|
-
export function isFunction(c: unknown): c is Func<any, any> {
|
9
|
-
return c instanceof Function;
|
10
|
-
}
|
package/src/index.ts
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
export * from "./arrays";
|
2
|
-
export * from "./deferred";
|
3
|
-
export * from "./mobx";
|
4
|
-
export * from "./guards";
|
5
|
-
export * from "./proxy";
|
6
|
-
export * from "./enums";
|
7
|
-
export * from "./objects";
|
8
|
-
export * from "./random";
|
9
|
-
export * from "./rx";
|
10
|
-
export * from "./uuid";
|
11
|
-
export * from "./promise";
|
12
|
-
export * from "./sleep";
|
13
|
-
export * from "./iterable";
|
14
|
-
export * from "./area";
|
15
|
-
export * from "./worker";
|
16
|
-
export * from "./pack";
|
17
|
-
export * from "./CoordMap";
|
18
|
-
export * from "./VoxelCoordMap";
|
19
|
-
export * from "./eth";
|
20
|
-
export * from "./cubic";
|
21
|
-
export * from "./console";
|
22
|
-
export * from "./distance";
|
23
|
-
export * from "./math";
|
24
|
-
export * from "./bytes";
|
25
|
-
export * from "./v2";
|
26
|
-
|
27
|
-
export type {
|
28
|
-
Cached,
|
29
|
-
CachedValue,
|
30
|
-
Func,
|
31
|
-
AsyncFunc,
|
32
|
-
PromiseValue,
|
33
|
-
ValueOf,
|
34
|
-
Area,
|
35
|
-
Coord,
|
36
|
-
VoxelCoord,
|
37
|
-
Logger,
|
38
|
-
} from "./types";
|
package/src/iterable.spec.ts
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
import { arrayToIterator, mergeIterators } from "./iterable";
|
2
|
-
|
3
|
-
describe("arrayToIterator", () => {
|
4
|
-
it("should return an iterable iterator with the same content as the array", () => {
|
5
|
-
const array = ["a", "b", "c", 1, 2, 3];
|
6
|
-
const iterator = arrayToIterator(array);
|
7
|
-
expect([...iterator]).toEqual(array);
|
8
|
-
});
|
9
|
-
|
10
|
-
it("should not return a next value if the array is empty", () => {
|
11
|
-
const array: string[] = [];
|
12
|
-
const iterator = arrayToIterator(array);
|
13
|
-
expect([...iterator]).toEqual(array);
|
14
|
-
|
15
|
-
const mock = jest.fn();
|
16
|
-
for (const item of iterator) {
|
17
|
-
mock(item);
|
18
|
-
}
|
19
|
-
expect(mock).not.toHaveBeenCalled();
|
20
|
-
});
|
21
|
-
|
22
|
-
it("should not be possible to iterate over an iterator multiple times", () => {
|
23
|
-
const array = ["a", "b", "c", 1, 2, 3];
|
24
|
-
const iterator = arrayToIterator(array);
|
25
|
-
expect([...iterator]).toEqual(array);
|
26
|
-
expect([...iterator]).toEqual([]);
|
27
|
-
});
|
28
|
-
});
|
29
|
-
|
30
|
-
describe("mergeIterators", () => {
|
31
|
-
it("should return a merged iterator", () => {
|
32
|
-
const a = arrayToIterator(["a", "b", "c"]);
|
33
|
-
const b = arrayToIterator([1, 2, 3]);
|
34
|
-
expect([...mergeIterators(a, b)]).toEqual([
|
35
|
-
["a", 1],
|
36
|
-
["b", 2],
|
37
|
-
["c", 3],
|
38
|
-
]);
|
39
|
-
});
|
40
|
-
|
41
|
-
it("should work with iterators of unequal length", () => {
|
42
|
-
const a = arrayToIterator(["a"]);
|
43
|
-
const b = arrayToIterator([1, 2, 3]);
|
44
|
-
expect([...mergeIterators(a, b)]).toEqual([
|
45
|
-
["a", 1],
|
46
|
-
[null, 2],
|
47
|
-
[null, 3],
|
48
|
-
]);
|
49
|
-
});
|
50
|
-
|
51
|
-
it("should return an empty iterator if both inputs are empty", () => {
|
52
|
-
const a = arrayToIterator([]);
|
53
|
-
const b = arrayToIterator([]);
|
54
|
-
expect([...mergeIterators(a, b)]).toEqual([]);
|
55
|
-
});
|
56
|
-
});
|
package/src/iterable.ts
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
export function makeIterable<T>(iterator: Iterator<T>): IterableIterator<T> {
|
2
|
-
const iterable: IterableIterator<T> = {
|
3
|
-
...iterator,
|
4
|
-
[Symbol.iterator]() {
|
5
|
-
return this;
|
6
|
-
},
|
7
|
-
};
|
8
|
-
|
9
|
-
return iterable;
|
10
|
-
}
|
11
|
-
|
12
|
-
export function concatIterators<T>(first: Iterator<T>, second?: Iterator<T>): IterableIterator<T> {
|
13
|
-
if (!second) return makeIterable(first);
|
14
|
-
return makeIterable({
|
15
|
-
next() {
|
16
|
-
const next = first.next();
|
17
|
-
if (!next.done) return next;
|
18
|
-
return second.next();
|
19
|
-
},
|
20
|
-
});
|
21
|
-
}
|
22
|
-
|
23
|
-
export function mergeIterators<A, B>(iteratorA: Iterator<A>, iteratorB: Iterator<B>): IterableIterator<[A, B]> {
|
24
|
-
const iterator: Iterator<[A, B]> = {
|
25
|
-
next() {
|
26
|
-
const nextA = iteratorA.next();
|
27
|
-
const nextB = iteratorB.next();
|
28
|
-
if (nextA.done && nextB.done) return { done: true, value: null };
|
29
|
-
return { value: [nextA.value, nextB.value] };
|
30
|
-
},
|
31
|
-
};
|
32
|
-
return makeIterable(iterator);
|
33
|
-
}
|
34
|
-
|
35
|
-
export function transformIterator<A, B>(iterator: Iterator<A>, transform: (value: A) => B): IterableIterator<B> {
|
36
|
-
return makeIterable({
|
37
|
-
next() {
|
38
|
-
const { done, value } = iterator.next();
|
39
|
-
return { done, value: done ? value : transform(value) };
|
40
|
-
},
|
41
|
-
});
|
42
|
-
}
|
43
|
-
|
44
|
-
/**
|
45
|
-
* Turns an array into an iterator. NOTE: an iterator can only be iterated once.
|
46
|
-
* @param array Array to be turned into an iterator
|
47
|
-
* @returns Iterator to iterate through the array
|
48
|
-
*/
|
49
|
-
export function arrayToIterator<T>(array: T[]): IterableIterator<T> {
|
50
|
-
let i = 0;
|
51
|
-
const iterator: Iterator<T> = {
|
52
|
-
next() {
|
53
|
-
const done = i >= array.length;
|
54
|
-
if (done) return { done, value: null };
|
55
|
-
return { value: array[i++] };
|
56
|
-
},
|
57
|
-
};
|
58
|
-
return makeIterable(iterator);
|
59
|
-
}
|
package/src/math.ts
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* For positive inputs: returns the greatest integer less than or equal to its numeric argument.
|
3
|
-
* For negative inputs: returns the smallest integer greater than or equal to its numeric argument.
|
4
|
-
*
|
5
|
-
* @param x A numeric expression.
|
6
|
-
* @returns Input rounded towards zero.
|
7
|
-
*/
|
8
|
-
export function roundTowardsZero(x: number) {
|
9
|
-
const sign = x < 0 ? -1 : 1;
|
10
|
-
return sign * Math.floor(Math.abs(x));
|
11
|
-
}
|
package/src/mobx.ts
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
import { IComputedValue, IObservableValue, reaction } from "mobx";
|
2
|
-
import { deferred } from "./deferred";
|
3
|
-
|
4
|
-
/**
|
5
|
-
* @param comp Computed/Observable value that is either defined or undefined
|
6
|
-
* @returns promise that resolves with the first truthy computed value
|
7
|
-
*/
|
8
|
-
export async function awaitValue<T>(comp: IComputedValue<T | undefined> | IObservableValue<T | undefined>): Promise<T> {
|
9
|
-
const [resolve, , promise] = deferred<T>();
|
10
|
-
|
11
|
-
const dispose = reaction(
|
12
|
-
() => comp.get(),
|
13
|
-
(value) => {
|
14
|
-
if (value) {
|
15
|
-
resolve(value);
|
16
|
-
}
|
17
|
-
},
|
18
|
-
{ fireImmediately: true },
|
19
|
-
);
|
20
|
-
|
21
|
-
const value = await promise;
|
22
|
-
// Dispose the reaction once the promise is resolved
|
23
|
-
dispose();
|
24
|
-
|
25
|
-
return value;
|
26
|
-
}
|
package/src/objects.ts
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Utility function to map a source object to an object with the same keys but mapped values
|
3
|
-
* @param source Source object to be mapped
|
4
|
-
* @param valueMap Mapping values of the source object to values of the target object
|
5
|
-
* @returns An object with the same keys as the source object but mapped values
|
6
|
-
*/
|
7
|
-
export function mapObject<S extends { [key: string]: unknown }, T extends { [key in keyof S]: unknown }>(
|
8
|
-
source: S,
|
9
|
-
valueMap: (value: S[keyof S], key: keyof S) => T[keyof S],
|
10
|
-
): T {
|
11
|
-
const target: Partial<{ [key in keyof typeof source]: T[keyof S] }> = {};
|
12
|
-
for (const key in source) {
|
13
|
-
target[key] = valueMap(source[key], key);
|
14
|
-
}
|
15
|
-
return target as T;
|
16
|
-
}
|
package/src/pack.spec.ts
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
import { pack, unpack } from "./pack";
|
2
|
-
|
3
|
-
describe("pack", () => {
|
4
|
-
it("should pack multiple numbers into one 32 bit integer", () => {
|
5
|
-
const packed = pack([1, 2], [8, 24]);
|
6
|
-
expect(packed).toBe(parseInt("1000000000000000000000010", 2));
|
7
|
-
});
|
8
|
-
|
9
|
-
it("should be the inverse of unpack", () => {
|
10
|
-
const bits = [8, 24];
|
11
|
-
const numbers = [16777218, 2072396467, -1];
|
12
|
-
|
13
|
-
for (const nums of numbers) {
|
14
|
-
expect(pack(unpack(nums, bits), bits)).toEqual(nums);
|
15
|
-
}
|
16
|
-
});
|
17
|
-
});
|
18
|
-
|
19
|
-
describe("unpack", () => {
|
20
|
-
it("should unpack a packed 32 bit integer into multiple numbers", () => {
|
21
|
-
const unpacked = unpack(parseInt("1000000000000000000000010", 2), [8, 24]);
|
22
|
-
expect(unpacked).toEqual([1, 2]);
|
23
|
-
});
|
24
|
-
|
25
|
-
it("should be the inverse of pack", () => {
|
26
|
-
const bits = [8, 24];
|
27
|
-
const numbers = [
|
28
|
-
[1, 2],
|
29
|
-
[123, 8798899],
|
30
|
-
[2 ** 8 - 1, 2 ** 24 - 1],
|
31
|
-
];
|
32
|
-
|
33
|
-
for (const nums of numbers) {
|
34
|
-
expect(unpack(pack(nums, bits), bits)).toEqual(nums);
|
35
|
-
}
|
36
|
-
});
|
37
|
-
});
|
package/src/pack.ts
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
function rightMask(input: number, keep: number): number {
|
2
|
-
return input & (2 ** keep - 1);
|
3
|
-
}
|
4
|
-
|
5
|
-
/**
|
6
|
-
* Packs two unsigned integers in one 32 bit unsigned integer
|
7
|
-
* @param numbers Unsigned integers to be packed in 32 bit integer
|
8
|
-
* @param bitsPerNumber Bits for each number
|
9
|
-
* @returns Packed 32 bit unsigned integer
|
10
|
-
*/
|
11
|
-
export function pack(numbers: number[], bitsPerNumber: number[]): number {
|
12
|
-
// Total number of bits must be 32
|
13
|
-
if (bitsPerNumber.reduce((acc, curr) => acc + curr, 0) > 32) {
|
14
|
-
throw new Error("JS pretends integers are 32 bit when bitshifts are involved");
|
15
|
-
}
|
16
|
-
|
17
|
-
// Array lengths must match
|
18
|
-
if (numbers.length !== bitsPerNumber.length) throw new Error("Arrays' lengths must match");
|
19
|
-
|
20
|
-
// Numbers must fit in number of bits and must be unsigned
|
21
|
-
for (let i = 0; i < numbers.length; i++) {
|
22
|
-
if (numbers[i] < 0) {
|
23
|
-
throw new Error("Underflow: can only pack unsigned integer");
|
24
|
-
}
|
25
|
-
if (numbers[i] > 2 ** bitsPerNumber[i] - 1) {
|
26
|
-
const error = `Overflow: ${numbers[i]} does not fit in ${bitsPerNumber[i]} bits`;
|
27
|
-
throw new Error(error);
|
28
|
-
}
|
29
|
-
}
|
30
|
-
|
31
|
-
// Pack number
|
32
|
-
let packed = 0;
|
33
|
-
for (let i = 0; i < numbers.length; i++) {
|
34
|
-
packed = (packed << bitsPerNumber[i]) | numbers[i];
|
35
|
-
}
|
36
|
-
return packed;
|
37
|
-
}
|
38
|
-
|
39
|
-
/**
|
40
|
-
* Unpacks a packed 32 bit unsigned integer into the original unsigned integers
|
41
|
-
* @param packed Packed 32 bit unsigned integer
|
42
|
-
* @param bitsPerNumber Bits for each unsigned integer
|
43
|
-
* @returns Array of unpacked unsignd integers
|
44
|
-
*/
|
45
|
-
export function unpack(packed: number, bitsPerNumber: number[]): number[] {
|
46
|
-
const numbers: number[] = [];
|
47
|
-
let shiftedPacked = packed;
|
48
|
-
for (let i = bitsPerNumber.length - 1; i >= 0; i--) {
|
49
|
-
numbers.unshift(rightMask(shiftedPacked, bitsPerNumber[i]));
|
50
|
-
shiftedPacked = shiftedPacked >>> bitsPerNumber[i];
|
51
|
-
}
|
52
|
-
return numbers;
|
53
|
-
}
|
54
|
-
|
55
|
-
export function packTuple(numbers: [number, number]): number {
|
56
|
-
return pack(numbers, [8, 24]);
|
57
|
-
}
|
58
|
-
|
59
|
-
export function unpackTuple(packed: number): [number, number] {
|
60
|
-
return unpack(packed, [8, 24]) as [number, number];
|
61
|
-
}
|
package/src/promise.ts
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2
|
-
import { deferred } from "./deferred";
|
3
|
-
import { sleep } from "./sleep";
|
4
|
-
|
5
|
-
export const range = function* (total = 0, step = 1, from = 0) {
|
6
|
-
// eslint-disable-next-line no-empty
|
7
|
-
for (let i = 0; i < total; yield from + i++ * step) {}
|
8
|
-
};
|
9
|
-
|
10
|
-
export async function rejectAfter<T>(ms: number, msg: string): Promise<T> {
|
11
|
-
await sleep(ms);
|
12
|
-
throw new Error(msg);
|
13
|
-
}
|
14
|
-
|
15
|
-
export const timeoutAfter = async <T>(promise: Promise<T>, ms: number, timeoutMsg: string) => {
|
16
|
-
return Promise.race([promise, rejectAfter<T>(ms, timeoutMsg)]);
|
17
|
-
};
|
18
|
-
|
19
|
-
export const callWithRetry = <T>(
|
20
|
-
fn: (...args: any[]) => Promise<T>,
|
21
|
-
args: any[] = [],
|
22
|
-
maxRetries = 10,
|
23
|
-
retryInterval = 1000,
|
24
|
-
): Promise<T> => {
|
25
|
-
const [resolve, reject, promise] = deferred<T>();
|
26
|
-
const process = async () => {
|
27
|
-
let res: T;
|
28
|
-
for (let i = 0; i < maxRetries; i++) {
|
29
|
-
try {
|
30
|
-
res = await fn(...args);
|
31
|
-
resolve(res);
|
32
|
-
break;
|
33
|
-
} catch (e) {
|
34
|
-
if (i < maxRetries - 1) {
|
35
|
-
console.info("[CallWithRetry Failed] attempt number=" + i, fn);
|
36
|
-
console.error(e);
|
37
|
-
await sleep(Math.min(retryInterval * 2 ** i + Math.random() * 100, 15000));
|
38
|
-
} else {
|
39
|
-
reject(e as unknown as Error);
|
40
|
-
}
|
41
|
-
}
|
42
|
-
}
|
43
|
-
};
|
44
|
-
process();
|
45
|
-
return promise;
|
46
|
-
};
|
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
|
-
});
|