@evolu/common 8.0.0-next.5 → 8.0.0
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/dist/src/Array.d.ts +279 -173
- package/dist/src/Array.d.ts.map +1 -1
- package/dist/src/Array.js +105 -101
- package/dist/src/Assert.d.ts +39 -44
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +38 -31
- package/dist/src/BigInt.d.ts +6 -3
- package/dist/src/BigInt.d.ts.map +1 -1
- package/dist/src/BigInt.js +5 -2
- package/dist/src/Brand.d.ts +27 -40
- package/dist/src/Brand.d.ts.map +1 -1
- package/dist/src/Buffer.d.ts +27 -11
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Cache.d.ts +9 -4
- package/dist/src/Cache.d.ts.map +1 -1
- package/dist/src/Cache.js +8 -3
- package/dist/src/Callbacks.d.ts +30 -19
- package/dist/src/Callbacks.d.ts.map +1 -1
- package/dist/src/Console.d.ts +145 -66
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +91 -43
- package/dist/src/Crypto.d.ts +85 -42
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +44 -20
- package/dist/src/Eq.d.ts +87 -21
- package/dist/src/Eq.d.ts.map +1 -1
- package/dist/src/Eq.js +67 -19
- package/dist/src/Error.d.ts +3 -3
- package/dist/src/Error.d.ts.map +1 -1
- package/dist/src/Function.d.ts +119 -58
- package/dist/src/Function.d.ts.map +1 -1
- package/dist/src/Function.js +86 -43
- package/dist/src/Http.d.ts +394 -0
- package/dist/src/Http.d.ts.map +1 -0
- package/dist/src/Http.js +102 -0
- package/dist/src/Identicon.d.ts +16 -7
- package/dist/src/Identicon.d.ts.map +1 -1
- package/dist/src/Identicon.js +14 -5
- package/dist/src/LeakDetector.d.ts +88 -0
- package/dist/src/LeakDetector.d.ts.map +1 -0
- package/dist/src/LeakDetector.js +86 -0
- package/dist/src/LockManager.d.ts +18 -9
- package/dist/src/LockManager.d.ts.map +1 -1
- package/dist/src/LockManager.js +7 -8
- package/dist/src/Lookup.d.ts +43 -13
- package/dist/src/Lookup.d.ts.map +1 -1
- package/dist/src/Lookup.js +28 -8
- package/dist/src/Microtask.d.ts +1 -1
- package/dist/src/Microtask.d.ts.map +1 -1
- package/dist/src/Microtask.js +3 -7
- package/dist/src/Number.d.ts +49 -13
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +34 -5
- package/dist/src/Object.d.ts +160 -40
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +92 -46
- package/dist/src/Option.d.ts +10 -7
- package/dist/src/Option.d.ts.map +1 -1
- package/dist/src/Option.js +9 -6
- package/dist/src/Order.d.ts +21 -21
- package/dist/src/Order.d.ts.map +1 -1
- package/dist/src/Order.js +18 -21
- package/dist/src/Platform.d.ts +31 -0
- package/dist/src/Platform.d.ts.map +1 -1
- package/dist/src/Platform.js +98 -0
- package/dist/src/Random.d.ts +32 -17
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/Random.js +12 -8
- package/dist/src/Redacted.d.ts +43 -29
- package/dist/src/Redacted.d.ts.map +1 -1
- package/dist/src/Redacted.js +14 -3
- package/dist/src/Ref.d.ts +11 -3
- package/dist/src/Ref.d.ts.map +1 -1
- package/dist/src/Ref.js +2 -2
- package/dist/src/RefCount.d.ts +2 -6
- package/dist/src/RefCount.d.ts.map +1 -1
- package/dist/src/RefCount.js +33 -90
- package/dist/src/Relation.d.ts +70 -5
- package/dist/src/Relation.d.ts.map +1 -1
- package/dist/src/Relation.js +77 -2
- package/dist/src/Resource.d.ts +590 -155
- package/dist/src/Resource.d.ts.map +1 -1
- package/dist/src/Resource.js +592 -343
- package/dist/src/Result.d.ts +838 -191
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +163 -102
- package/dist/src/Schedule.d.ts +684 -231
- package/dist/src/Schedule.d.ts.map +1 -1
- package/dist/src/Schedule.js +766 -295
- package/dist/src/Set.d.ts +89 -40
- package/dist/src/Set.d.ts.map +1 -1
- package/dist/src/Set.js +50 -29
- package/dist/src/Sqlite.d.ts +29 -72
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +39 -45
- package/dist/src/StackTrace.d.ts +40 -0
- package/dist/src/StackTrace.d.ts.map +1 -0
- package/dist/src/StackTrace.js +140 -0
- package/dist/src/Store.d.ts +5 -4
- package/dist/src/Store.d.ts.map +1 -1
- package/dist/src/Store.js +5 -4
- package/dist/src/String.d.ts +7 -1
- package/dist/src/String.d.ts.map +1 -1
- package/dist/src/String.js +20 -7
- package/dist/src/Task.d.ts +3428 -1629
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +1876 -1279
- package/dist/src/Test.d.ts +17 -124
- package/dist/src/Test.d.ts.map +1 -1
- package/dist/src/Test.js +17 -68
- package/dist/src/Time.d.ts +108 -49
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +172 -73
- package/dist/src/Type.d.ts +3542 -1926
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +3070 -1784
- package/dist/src/Types.d.ts +200 -100
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/Types.js +39 -8
- package/dist/src/WebSocket.d.ts +48 -23
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +15 -6
- package/dist/src/Worker.d.ts +94 -36
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +58 -24
- package/dist/src/index.d.ts +55 -52
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -1
- package/dist/src/intl/cs.d.ts +94 -0
- package/dist/src/intl/cs.d.ts.map +1 -0
- package/dist/src/intl/cs.js +220 -0
- package/dist/src/intl/index.d.ts +7 -0
- package/dist/src/intl/index.d.ts.map +1 -0
- package/dist/src/intl/index.js +6 -0
- package/dist/src/local-first/Db.d.ts +12 -8
- package/dist/src/local-first/Db.d.ts.map +1 -1
- package/dist/src/local-first/Db.js +107 -130
- package/dist/src/local-first/Error.d.ts +4 -4
- package/dist/src/local-first/Evolu.d.ts +338 -92
- package/dist/src/local-first/Evolu.d.ts.map +1 -1
- package/dist/src/local-first/Evolu.js +47 -55
- package/dist/src/local-first/LocalAuth.d.ts +4 -4
- package/dist/src/local-first/LocalAuth.d.ts.map +1 -1
- package/dist/src/local-first/Owner.d.ts +61 -24
- package/dist/src/local-first/Owner.d.ts.map +1 -1
- package/dist/src/local-first/Owner.js +45 -8
- package/dist/src/local-first/Protocol.d.ts +44 -31
- package/dist/src/local-first/Protocol.d.ts.map +1 -1
- package/dist/src/local-first/Protocol.js +36 -13
- package/dist/src/local-first/Query.d.ts +147 -62
- package/dist/src/local-first/Query.d.ts.map +1 -1
- package/dist/src/local-first/Query.js +112 -58
- package/dist/src/local-first/Relay.d.ts +44 -14
- package/dist/src/local-first/Relay.d.ts.map +1 -1
- package/dist/src/local-first/Schema.d.ts +92 -41
- package/dist/src/local-first/Schema.d.ts.map +1 -1
- package/dist/src/local-first/Schema.js +25 -8
- package/dist/src/local-first/Shared.d.ts +23 -21
- package/dist/src/local-first/Shared.d.ts.map +1 -1
- package/dist/src/local-first/Shared.js +85 -42
- package/dist/src/local-first/Storage.d.ts +53 -32
- package/dist/src/local-first/Storage.d.ts.map +1 -1
- package/dist/src/local-first/Storage.js +194 -234
- package/dist/src/local-first/Timestamp.d.ts +21 -21
- package/dist/src/local-first/Timestamp.d.ts.map +1 -1
- package/dist/src/local-first/Timestamp.js +2 -2
- package/dist/src/local-first/index.d.ts +11 -11
- package/package.json +14 -13
- package/src/Array.ts +306 -205
- package/src/Assert.ts +48 -68
- package/src/BigInt.ts +6 -3
- package/src/Brand.ts +27 -40
- package/src/Buffer.ts +28 -12
- package/src/Cache.ts +9 -4
- package/src/Callbacks.ts +30 -19
- package/src/Console.ts +156 -84
- package/src/Crypto.ts +83 -41
- package/src/Eq.ts +95 -22
- package/src/Error.ts +5 -2
- package/src/Function.ts +167 -59
- package/src/Http.ts +549 -0
- package/src/Identicon.ts +16 -7
- package/src/LeakDetector.ts +182 -0
- package/src/LockManager.ts +43 -39
- package/src/Lookup.ts +62 -28
- package/src/Microtask.ts +19 -23
- package/src/Number.ts +64 -12
- package/src/Object.ts +173 -49
- package/src/Option.ts +10 -7
- package/src/Order.ts +23 -21
- package/src/Platform.ts +136 -0
- package/src/Polyfills.ts +6 -12
- package/src/Random.ts +32 -19
- package/src/Redacted.ts +44 -30
- package/src/Ref.ts +14 -6
- package/src/RefCount.ts +38 -80
- package/src/Relation.ts +202 -10
- package/src/Resource.ts +1199 -591
- package/src/Result.ts +930 -271
- package/src/Schedule.ts +859 -343
- package/src/Set.ts +89 -40
- package/src/Sqlite.ts +142 -137
- package/src/StackTrace.ts +230 -0
- package/src/Store.ts +8 -7
- package/src/String.ts +22 -5
- package/src/Task.ts +5109 -2845
- package/src/Test.ts +20 -167
- package/src/Time.ts +293 -90
- package/src/Type.ts +10034 -4117
- package/src/Types.ts +227 -101
- package/src/WebSocket.ts +64 -36
- package/src/Worker.ts +180 -72
- package/src/index.ts +55 -52
- package/src/intl/cs.ts +388 -0
- package/src/intl/index.ts +7 -0
- package/src/local-first/Db.ts +161 -190
- package/src/local-first/Error.ts +4 -4
- package/src/local-first/Evolu.ts +495 -250
- package/src/local-first/LocalAuth.ts +6 -12
- package/src/local-first/Owner.ts +58 -21
- package/src/local-first/Protocol.ts +49 -31
- package/src/local-first/Query.ts +146 -61
- package/src/local-first/Relay.ts +51 -21
- package/src/local-first/Schema.ts +109 -51
- package/src/local-first/Shared.ts +253 -214
- package/src/local-first/Storage.ts +266 -264
- package/src/local-first/Timestamp.ts +25 -18
- package/src/local-first/index.ts +11 -11
- package/dist/src/Skiplist.d.ts +0 -28
- package/dist/src/Skiplist.d.ts.map +0 -1
- package/dist/src/Skiplist.js +0 -64
- package/dist/src/Tracer.d.ts +0 -48
- package/dist/src/Tracer.d.ts.map +0 -1
- package/dist/src/Tracer.js +0 -6
- package/src/Skiplist.ts +0 -108
- package/src/Tracer.ts +0 -54
package/dist/src/Eq.d.ts
CHANGED
|
@@ -3,22 +3,70 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
import type { Order } from "./Order.
|
|
7
|
-
import type { JsonValue, JsonValueInput } from "./Type.
|
|
6
|
+
import type { Order } from "./Order.ts";
|
|
7
|
+
import type { JsonValue, JsonValueInput } from "./Type.ts";
|
|
8
8
|
/**
|
|
9
9
|
* Compares two values of the same type `A` for equality.
|
|
10
10
|
*
|
|
11
11
|
* Equality functions start with an 'eq' prefix, e.g., `eqString`.
|
|
12
12
|
*
|
|
13
|
-
*
|
|
13
|
+
* An `Eq` must define an equivalence relation over its intended domain:
|
|
14
|
+
*
|
|
15
|
+
* - **Reflexive**: `eq(a, a)` is `true`.
|
|
16
|
+
* - **Symmetric**: `eq(a, b)` equals `eq(b, a)`.
|
|
17
|
+
* - **Transitive**: if `eq(a, b)` and `eq(b, c)` are `true`, then `eq(a, c)` is
|
|
18
|
+
* `true`.
|
|
19
|
+
*
|
|
20
|
+
* Use {@link eqFromOrder} to derive equality from an {@link Order}.
|
|
21
|
+
*
|
|
22
|
+
* ### Example
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { createEqObject, eqNumber } from "@evolu/common";
|
|
26
|
+
*
|
|
27
|
+
* const eqPoint = createEqObject({ x: eqNumber, y: eqNumber });
|
|
28
|
+
*
|
|
29
|
+
* expect(eqPoint({ x: 1, y: 2 }, { x: 1, y: 2 })).toBe(true);
|
|
30
|
+
* expect(eqPoint({ x: 1, y: 2 }, { x: 2, y: 1 })).toBe(false);
|
|
31
|
+
* ```
|
|
14
32
|
*/
|
|
15
33
|
export type Eq<in A> = (x: A, y: A) => boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Compares two values with strict equality (`===`).
|
|
36
|
+
*
|
|
37
|
+
* Strict equality considers `NaN` unequal to itself. Use {@link eqSameValueZero}
|
|
38
|
+
* when values can contain `NaN` and equality must be reflexive.
|
|
39
|
+
*/
|
|
16
40
|
export declare const eqStrict: <A>(x: A, y: A) => boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Compares two values using SameValueZero equality.
|
|
43
|
+
*
|
|
44
|
+
* SameValueZero is the standard equality algorithm used by `Map`, `Set`, and
|
|
45
|
+
* `Array.prototype.includes`. It behaves like strict equality except that `NaN`
|
|
46
|
+
* equals itself. Both algorithms consider `0` and `-0` equal.
|
|
47
|
+
*
|
|
48
|
+
* ### Example
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { eqSameValueZero } from "@evolu/common";
|
|
52
|
+
*
|
|
53
|
+
* expect(eqSameValueZero(NaN, NaN)).toBe(true);
|
|
54
|
+
* expect(eqSameValueZero(0, -0)).toBe(true);
|
|
55
|
+
* expect(eqSameValueZero({}, {})).toBe(false);
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare const eqSameValueZero: <A>(x: A, y: A) => boolean;
|
|
59
|
+
/** An {@link Eq} for strings using strict equality. */
|
|
17
60
|
export declare const eqString: Eq<string>;
|
|
61
|
+
/** An {@link Eq} for numbers using {@link eqSameValueZero}. */
|
|
18
62
|
export declare const eqNumber: Eq<number>;
|
|
63
|
+
/** An {@link Eq} for bigints using strict equality. */
|
|
19
64
|
export declare const eqBigInt: Eq<bigint>;
|
|
65
|
+
/** An {@link Eq} for booleans using strict equality. */
|
|
20
66
|
export declare const eqBoolean: Eq<boolean>;
|
|
67
|
+
/** An {@link Eq} for `undefined`. */
|
|
21
68
|
export declare const eqUndefined: Eq<undefined>;
|
|
69
|
+
/** An {@link Eq} for `null`. */
|
|
22
70
|
export declare const eqNull: Eq<null>;
|
|
23
71
|
/** Derives an {@link Eq} from an {@link Order}. */
|
|
24
72
|
export declare const eqFromOrder: <A>(order: Order<A>) => Eq<A>;
|
|
@@ -29,10 +77,15 @@ export declare const eqFromOrder: <A>(order: Order<A>) => Eq<A>;
|
|
|
29
77
|
* ### Example
|
|
30
78
|
*
|
|
31
79
|
* ```ts
|
|
80
|
+
* import { createEqArrayLike, eqNumber } from "@evolu/common";
|
|
81
|
+
*
|
|
32
82
|
* const eqArrayNumber = createEqArrayLike(eqNumber);
|
|
33
|
-
*
|
|
34
|
-
* eqArrayNumber(
|
|
35
|
-
*
|
|
83
|
+
*
|
|
84
|
+
* expect(eqArrayNumber([1, 2, 3], [1, 2, 3])).toBe(true);
|
|
85
|
+
* expect(
|
|
86
|
+
* eqArrayNumber(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3])),
|
|
87
|
+
* ).toBe(true);
|
|
88
|
+
* expect(eqArrayNumber([1, 2, 3], [1, 2, 4])).toBe(false);
|
|
36
89
|
* ```
|
|
37
90
|
*/
|
|
38
91
|
export declare const createEqArrayLike: <A>(item: Eq<A>) => Eq<ArrayLike<A>>;
|
|
@@ -44,10 +97,12 @@ export declare const createEqArrayLike: <A>(item: Eq<A>) => Eq<ArrayLike<A>>;
|
|
|
44
97
|
* ### Example
|
|
45
98
|
*
|
|
46
99
|
* ```ts
|
|
100
|
+
* import { eqArrayStrict } from "@evolu/common";
|
|
101
|
+
*
|
|
47
102
|
* const a = { x: 1 };
|
|
48
103
|
* const b = { x: 1 };
|
|
49
|
-
* eqArrayStrict([a, a], [a, a])
|
|
50
|
-
* eqArrayStrict([a], [b])
|
|
104
|
+
* expect(eqArrayStrict([a, a], [a, a])).toBe(true);
|
|
105
|
+
* expect(eqArrayStrict([a], [b])).toBe(false);
|
|
51
106
|
* ```
|
|
52
107
|
*/
|
|
53
108
|
export declare const eqArrayStrict: <A>(x: ArrayLike<A>, y: ArrayLike<A>) => boolean;
|
|
@@ -57,9 +112,9 @@ export declare const eqArrayStrict: <A>(x: ArrayLike<A>, y: ArrayLike<A>) => boo
|
|
|
57
112
|
* ### Example
|
|
58
113
|
*
|
|
59
114
|
* ```ts
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* eqArrayNumber([1,
|
|
115
|
+
* import { eqArrayNumber } from "@evolu/common";
|
|
116
|
+
*
|
|
117
|
+
* expect(eqArrayNumber([1, NaN], [1, NaN])).toBe(true);
|
|
63
118
|
* ```
|
|
64
119
|
*/
|
|
65
120
|
export declare const eqArrayNumber: Eq<ArrayLike<number>>;
|
|
@@ -70,9 +125,12 @@ export declare const eqArrayNumber: Eq<ArrayLike<number>>;
|
|
|
70
125
|
* ### Example
|
|
71
126
|
*
|
|
72
127
|
* ```ts
|
|
128
|
+
* import { createEqObject, eqNumber } from "@evolu/common";
|
|
129
|
+
*
|
|
73
130
|
* const eqObjectNumber = createEqObject({ a: eqNumber });
|
|
74
|
-
*
|
|
75
|
-
* eqObjectNumber({ a: 1 }, { a:
|
|
131
|
+
*
|
|
132
|
+
* expect(eqObjectNumber({ a: 1 }, { a: 1 })).toBe(true);
|
|
133
|
+
* expect(eqObjectNumber({ a: 1 }, { a: 2 })).toBe(false);
|
|
76
134
|
* ```
|
|
77
135
|
*/
|
|
78
136
|
export declare const createEqObject: <A>(eqs: { [K in keyof A]: Eq<A[K]>; }) => Eq<{ readonly [K in keyof A]: A[K]; }>;
|
|
@@ -81,7 +139,8 @@ export declare const createEqObject: <A>(eqs: { [K in keyof A]: Eq<A[K]>; }) =>
|
|
|
81
139
|
*
|
|
82
140
|
* - Uses an iterative approach with a stack to handle large or deeply nested
|
|
83
141
|
* objects without risking stack overflow.
|
|
84
|
-
* -
|
|
142
|
+
* - Defensively handles circular references in runtime values without looping,
|
|
143
|
+
* although cyclic values are not valid JSON.
|
|
85
144
|
* - Unlike JSON.stringify, this function directly compares values, avoiding
|
|
86
145
|
* serialization overhead and leveraging short-circuit evaluation for faster
|
|
87
146
|
* failure on mismatched structures.
|
|
@@ -89,9 +148,12 @@ export declare const createEqObject: <A>(eqs: { [K in keyof A]: Eq<A[K]>; }) =>
|
|
|
89
148
|
* ### Example
|
|
90
149
|
*
|
|
91
150
|
* ```ts
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
151
|
+
* import { eqJsonValue, type JsonValue } from "@evolu/common";
|
|
152
|
+
*
|
|
153
|
+
* const first: JsonValue = { profile: { name: "Ada" } };
|
|
154
|
+
* const second: JsonValue = { profile: { name: "Ada" } };
|
|
155
|
+
*
|
|
156
|
+
* expect(eqJsonValue(first, second)).toBe(true);
|
|
95
157
|
* ```
|
|
96
158
|
*/
|
|
97
159
|
export declare const eqJsonValue: (a: JsonValue, b: JsonValue) => boolean;
|
|
@@ -100,7 +162,8 @@ export declare const eqJsonValue: (a: JsonValue, b: JsonValue) => boolean;
|
|
|
100
162
|
*
|
|
101
163
|
* - Uses an iterative approach with a stack to handle large or deeply nested
|
|
102
164
|
* objects without risking stack overflow.
|
|
103
|
-
* -
|
|
165
|
+
* - Defensively handles circular references in runtime values without looping,
|
|
166
|
+
* although cyclic values are not valid JSON.
|
|
104
167
|
* - Unlike JSON.stringify, this function directly compares values, avoiding
|
|
105
168
|
* serialization overhead and leveraging short-circuit evaluation for faster
|
|
106
169
|
* failure on mismatched structures.
|
|
@@ -108,9 +171,12 @@ export declare const eqJsonValue: (a: JsonValue, b: JsonValue) => boolean;
|
|
|
108
171
|
* ### Example
|
|
109
172
|
*
|
|
110
173
|
* ```ts
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
174
|
+
* import { eqJsonValueInput, type JsonValueInput } from "@evolu/common";
|
|
175
|
+
*
|
|
176
|
+
* const first: JsonValueInput = { profile: { name: "Ada" } };
|
|
177
|
+
* const second: JsonValueInput = { profile: { name: "Ada" } };
|
|
178
|
+
*
|
|
179
|
+
* expect(eqJsonValueInput(first, second)).toBe(true);
|
|
114
180
|
* ```
|
|
115
181
|
*/
|
|
116
182
|
export declare const eqJsonValueInput: (a: JsonValueInput, b: JsonValueInput) => boolean;
|
package/dist/src/Eq.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Eq.d.ts","sourceRoot":"","sources":["../../src/Eq.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,KAAK,EAGV,SAAS,EACT,cAAc,EACf,MAAM,WAAW,CAAC;AAEnB
|
|
1
|
+
{"version":3,"file":"Eq.d.ts","sourceRoot":"","sources":["../../src/Eq.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,KAAK,EAGV,SAAS,EACT,cAAc,EACf,MAAM,WAAW,CAAC;AAEnB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC;AAE/C;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAG,OAAkB,CAAC;AAE5D;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAG,OACpB,CAAC;AAE7B,uDAAuD;AACvD,eAAO,MAAM,QAAQ,EAAE,EAAE,CAAC,MAAM,CAAY,CAAC;AAE7C,+DAA+D;AAC/D,eAAO,MAAM,QAAQ,EAAE,EAAE,CAAC,MAAM,CAAmB,CAAC;AAEpD,uDAAuD;AACvD,eAAO,MAAM,QAAQ,EAAE,EAAE,CAAC,MAAM,CAAY,CAAC;AAE7C,wDAAwD;AACxD,eAAO,MAAM,SAAS,EAAE,EAAE,CAAC,OAAO,CAAY,CAAC;AAE/C,qCAAqC;AACrC,eAAO,MAAM,WAAW,EAAE,EAAE,CAAC,SAAS,CAAY,CAAC;AAEnD,gCAAgC;AAChC,eAAO,MAAM,MAAM,EAAE,EAAE,CAAC,IAAI,CAAY,CAAC;AAEzC,mDAAmD;AACnD,eAAO,MAAM,WAAW,GACrB,CAAC,SAAS,KAAK,CAAC,CAAC,CAAC,KAAG,EAAE,CAAC,CAAC,CAEP,CAAC;AAEtB;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,iBAAiB,GAC3B,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,KAAG,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAUhC,CAAC;AAEJ;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,aAAa,GA7FD,CAAC,uCARa,OAqG+B,CAAC;AAEvE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,aAAa,uBAA4C,CAAC;AAEvE;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,cAAc,GACxB,CAAC,OAAO,GAAG,CAAC,IAAI,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAE,KAAG,EAAE,CAAC,EACzC,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAC9B,CASA,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,WAAW,MAAO,SAAS,KAAK,SAAS,KAAG,OAsExD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,gBAAgB,MACxB,cAAc,KACd,cAAc,KAChB,OAAsD,CAAC"}
|
package/dist/src/Eq.js
CHANGED
|
@@ -3,12 +3,42 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
+
/**
|
|
7
|
+
* Compares two values with strict equality (`===`).
|
|
8
|
+
*
|
|
9
|
+
* Strict equality considers `NaN` unequal to itself. Use {@link eqSameValueZero}
|
|
10
|
+
* when values can contain `NaN` and equality must be reflexive.
|
|
11
|
+
*/
|
|
6
12
|
export const eqStrict = (x, y) => x === y;
|
|
13
|
+
/**
|
|
14
|
+
* Compares two values using SameValueZero equality.
|
|
15
|
+
*
|
|
16
|
+
* SameValueZero is the standard equality algorithm used by `Map`, `Set`, and
|
|
17
|
+
* `Array.prototype.includes`. It behaves like strict equality except that `NaN`
|
|
18
|
+
* equals itself. Both algorithms consider `0` and `-0` equal.
|
|
19
|
+
*
|
|
20
|
+
* ### Example
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { eqSameValueZero } from "@evolu/common";
|
|
24
|
+
*
|
|
25
|
+
* expect(eqSameValueZero(NaN, NaN)).toBe(true);
|
|
26
|
+
* expect(eqSameValueZero(0, -0)).toBe(true);
|
|
27
|
+
* expect(eqSameValueZero({}, {})).toBe(false);
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export const eqSameValueZero = (x, y) => x === y || Object.is(x, y);
|
|
31
|
+
/** An {@link Eq} for strings using strict equality. */
|
|
7
32
|
export const eqString = eqStrict;
|
|
8
|
-
|
|
33
|
+
/** An {@link Eq} for numbers using {@link eqSameValueZero}. */
|
|
34
|
+
export const eqNumber = eqSameValueZero;
|
|
35
|
+
/** An {@link Eq} for bigints using strict equality. */
|
|
9
36
|
export const eqBigInt = eqStrict;
|
|
37
|
+
/** An {@link Eq} for booleans using strict equality. */
|
|
10
38
|
export const eqBoolean = eqStrict;
|
|
39
|
+
/** An {@link Eq} for `undefined`. */
|
|
11
40
|
export const eqUndefined = eqStrict;
|
|
41
|
+
/** An {@link Eq} for `null`. */
|
|
12
42
|
export const eqNull = eqStrict;
|
|
13
43
|
/** Derives an {@link Eq} from an {@link Order}. */
|
|
14
44
|
export const eqFromOrder = (order) => (x, y) => order(x, y) === 0;
|
|
@@ -19,10 +49,15 @@ export const eqFromOrder = (order) => (x, y) => order(x, y) === 0;
|
|
|
19
49
|
* ### Example
|
|
20
50
|
*
|
|
21
51
|
* ```ts
|
|
52
|
+
* import { createEqArrayLike, eqNumber } from "@evolu/common";
|
|
53
|
+
*
|
|
22
54
|
* const eqArrayNumber = createEqArrayLike(eqNumber);
|
|
23
|
-
*
|
|
24
|
-
* eqArrayNumber(
|
|
25
|
-
*
|
|
55
|
+
*
|
|
56
|
+
* expect(eqArrayNumber([1, 2, 3], [1, 2, 3])).toBe(true);
|
|
57
|
+
* expect(
|
|
58
|
+
* eqArrayNumber(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3])),
|
|
59
|
+
* ).toBe(true);
|
|
60
|
+
* expect(eqArrayNumber([1, 2, 3], [1, 2, 4])).toBe(false);
|
|
26
61
|
* ```
|
|
27
62
|
*/
|
|
28
63
|
export const createEqArrayLike = (item) => (x, y) => {
|
|
@@ -44,10 +79,12 @@ export const createEqArrayLike = (item) => (x, y) => {
|
|
|
44
79
|
* ### Example
|
|
45
80
|
*
|
|
46
81
|
* ```ts
|
|
82
|
+
* import { eqArrayStrict } from "@evolu/common";
|
|
83
|
+
*
|
|
47
84
|
* const a = { x: 1 };
|
|
48
85
|
* const b = { x: 1 };
|
|
49
|
-
* eqArrayStrict([a, a], [a, a])
|
|
50
|
-
* eqArrayStrict([a], [b])
|
|
86
|
+
* expect(eqArrayStrict([a, a], [a, a])).toBe(true);
|
|
87
|
+
* expect(eqArrayStrict([a], [b])).toBe(false);
|
|
51
88
|
* ```
|
|
52
89
|
*/
|
|
53
90
|
export const eqArrayStrict = /*#__PURE__*/ createEqArrayLike(eqStrict);
|
|
@@ -57,9 +94,9 @@ export const eqArrayStrict = /*#__PURE__*/ createEqArrayLike(eqStrict);
|
|
|
57
94
|
* ### Example
|
|
58
95
|
*
|
|
59
96
|
* ```ts
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* eqArrayNumber([1,
|
|
97
|
+
* import { eqArrayNumber } from "@evolu/common";
|
|
98
|
+
*
|
|
99
|
+
* expect(eqArrayNumber([1, NaN], [1, NaN])).toBe(true);
|
|
63
100
|
* ```
|
|
64
101
|
*/
|
|
65
102
|
export const eqArrayNumber = /*#__PURE__*/ createEqArrayLike(eqNumber);
|
|
@@ -70,9 +107,12 @@ export const eqArrayNumber = /*#__PURE__*/ createEqArrayLike(eqNumber);
|
|
|
70
107
|
* ### Example
|
|
71
108
|
*
|
|
72
109
|
* ```ts
|
|
110
|
+
* import { createEqObject, eqNumber } from "@evolu/common";
|
|
111
|
+
*
|
|
73
112
|
* const eqObjectNumber = createEqObject({ a: eqNumber });
|
|
74
|
-
*
|
|
75
|
-
* eqObjectNumber({ a: 1 }, { a:
|
|
113
|
+
*
|
|
114
|
+
* expect(eqObjectNumber({ a: 1 }, { a: 1 })).toBe(true);
|
|
115
|
+
* expect(eqObjectNumber({ a: 1 }, { a: 2 })).toBe(false);
|
|
76
116
|
* ```
|
|
77
117
|
*/
|
|
78
118
|
export const createEqObject = (eqs) => (x, y) => {
|
|
@@ -90,7 +130,8 @@ export const createEqObject = (eqs) => (x, y) => {
|
|
|
90
130
|
*
|
|
91
131
|
* - Uses an iterative approach with a stack to handle large or deeply nested
|
|
92
132
|
* objects without risking stack overflow.
|
|
93
|
-
* -
|
|
133
|
+
* - Defensively handles circular references in runtime values without looping,
|
|
134
|
+
* although cyclic values are not valid JSON.
|
|
94
135
|
* - Unlike JSON.stringify, this function directly compares values, avoiding
|
|
95
136
|
* serialization overhead and leveraging short-circuit evaluation for faster
|
|
96
137
|
* failure on mismatched structures.
|
|
@@ -98,9 +139,12 @@ export const createEqObject = (eqs) => (x, y) => {
|
|
|
98
139
|
* ### Example
|
|
99
140
|
*
|
|
100
141
|
* ```ts
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
142
|
+
* import { eqJsonValue, type JsonValue } from "@evolu/common";
|
|
143
|
+
*
|
|
144
|
+
* const first: JsonValue = { profile: { name: "Ada" } };
|
|
145
|
+
* const second: JsonValue = { profile: { name: "Ada" } };
|
|
146
|
+
*
|
|
147
|
+
* expect(eqJsonValue(first, second)).toBe(true);
|
|
104
148
|
* ```
|
|
105
149
|
*/
|
|
106
150
|
export const eqJsonValue = (a, b) => {
|
|
@@ -171,7 +215,8 @@ export const eqJsonValue = (a, b) => {
|
|
|
171
215
|
*
|
|
172
216
|
* - Uses an iterative approach with a stack to handle large or deeply nested
|
|
173
217
|
* objects without risking stack overflow.
|
|
174
|
-
* -
|
|
218
|
+
* - Defensively handles circular references in runtime values without looping,
|
|
219
|
+
* although cyclic values are not valid JSON.
|
|
175
220
|
* - Unlike JSON.stringify, this function directly compares values, avoiding
|
|
176
221
|
* serialization overhead and leveraging short-circuit evaluation for faster
|
|
177
222
|
* failure on mismatched structures.
|
|
@@ -179,9 +224,12 @@ export const eqJsonValue = (a, b) => {
|
|
|
179
224
|
* ### Example
|
|
180
225
|
*
|
|
181
226
|
* ```ts
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
227
|
+
* import { eqJsonValueInput, type JsonValueInput } from "@evolu/common";
|
|
228
|
+
*
|
|
229
|
+
* const first: JsonValueInput = { profile: { name: "Ada" } };
|
|
230
|
+
* const second: JsonValueInput = { profile: { name: "Ada" } };
|
|
231
|
+
*
|
|
232
|
+
* expect(eqJsonValueInput(first, second)).toBe(true);
|
|
185
233
|
* ```
|
|
186
234
|
*/
|
|
187
235
|
export const eqJsonValueInput = (a, b) => eqJsonValue(a, b);
|
package/dist/src/Error.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
import { type InferType } from "./Type.
|
|
6
|
+
import { type InferType, type TypedType, Unknown } from "./Type.ts";
|
|
7
7
|
/**
|
|
8
8
|
* A wrapper for unknown errors caught at runtime.
|
|
9
9
|
*
|
|
@@ -16,8 +16,8 @@ import { type InferType } from "./Type.js";
|
|
|
16
16
|
*
|
|
17
17
|
* Use {@link createUnknownError} to create instances.
|
|
18
18
|
*/
|
|
19
|
-
export declare const UnknownError:
|
|
20
|
-
error:
|
|
19
|
+
export declare const UnknownError: TypedType<"UnknownError", {
|
|
20
|
+
readonly error: typeof Unknown;
|
|
21
21
|
}>;
|
|
22
22
|
export interface UnknownError extends InferType<typeof UnknownError> {
|
|
23
23
|
}
|
package/dist/src/Error.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../../src/Error.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,SAAS,
|
|
1
|
+
{"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../../src/Error.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,SAAS,EAAS,KAAK,SAAS,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE3E;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,YAAY,EAAE,SAAS,CAClC,cAAc,EACd;IAAE,QAAQ,CAAC,KAAK,EAAE,OAAO,OAAO,CAAA;CAAE,CAGlC,CAAC;AACH,MAAM,WAAW,YAAa,SAAQ,SAAS,CAAC,OAAO,YAAY,CAAC;CAAG;AAEvE;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,UAAW,OAAO,KAAG,YAkDnD,CAAC"}
|
package/dist/src/Function.d.ts
CHANGED
|
@@ -10,32 +10,36 @@
|
|
|
10
10
|
* ### Example
|
|
11
11
|
*
|
|
12
12
|
* ```ts
|
|
13
|
+
* import { exhaustiveCheck } from "@evolu/common";
|
|
14
|
+
*
|
|
13
15
|
* type Color = "red" | "green" | "blue";
|
|
16
|
+
* const handled: Array<string> = [];
|
|
14
17
|
*
|
|
15
18
|
* const handleColor = (color: Color): void => {
|
|
16
19
|
* switch (color) {
|
|
17
20
|
* case "red":
|
|
18
|
-
*
|
|
21
|
+
* handled.push("Handling red");
|
|
19
22
|
* break;
|
|
20
23
|
* case "green":
|
|
21
|
-
*
|
|
24
|
+
* handled.push("Handling green");
|
|
22
25
|
* break;
|
|
23
26
|
* case "blue":
|
|
24
|
-
*
|
|
27
|
+
* handled.push("Handling blue");
|
|
25
28
|
* break;
|
|
26
29
|
* default:
|
|
27
|
-
* exhaustiveCheck(color);
|
|
30
|
+
* exhaustiveCheck(color);
|
|
28
31
|
* }
|
|
29
32
|
* };
|
|
33
|
+
*
|
|
34
|
+
* handleColor("blue");
|
|
35
|
+
* expect(handled).toEqual(["Handling blue"]);
|
|
30
36
|
* ```
|
|
31
37
|
*
|
|
32
38
|
* Use this primarily in side-effect switches (`void` branches). For
|
|
33
39
|
* value-producing switches, TypeScript can enforce exhaustiveness without a
|
|
34
|
-
* `default` branch.
|
|
35
|
-
*
|
|
36
|
-
* ### Example
|
|
40
|
+
* `default` branch in either of the following styles.
|
|
37
41
|
*
|
|
38
|
-
* Return from
|
|
42
|
+
* ### Return from every case
|
|
39
43
|
*
|
|
40
44
|
* ```ts
|
|
41
45
|
* type Color = "red" | "green" | "blue";
|
|
@@ -50,11 +54,11 @@
|
|
|
50
54
|
* return "#0000ff";
|
|
51
55
|
* }
|
|
52
56
|
* };
|
|
53
|
-
* ```
|
|
54
57
|
*
|
|
55
|
-
*
|
|
58
|
+
* expect(colorToHex("green")).toBe("#00ff00");
|
|
59
|
+
* ```
|
|
56
60
|
*
|
|
57
|
-
*
|
|
61
|
+
* ### Assign in every case
|
|
58
62
|
*
|
|
59
63
|
* ```ts
|
|
60
64
|
* type Input =
|
|
@@ -62,7 +66,7 @@
|
|
|
62
66
|
* | { readonly type: "Query" }
|
|
63
67
|
* | { readonly type: "Export" };
|
|
64
68
|
*
|
|
65
|
-
* const
|
|
69
|
+
* const inputToKind = (input: Input): "A" | "B" | "C" => {
|
|
66
70
|
* let result: "A" | "B" | "C";
|
|
67
71
|
*
|
|
68
72
|
* switch (input.type) {
|
|
@@ -77,12 +81,10 @@
|
|
|
77
81
|
* break;
|
|
78
82
|
* }
|
|
79
83
|
*
|
|
80
|
-
*
|
|
84
|
+
* return result;
|
|
81
85
|
* };
|
|
82
86
|
*
|
|
83
|
-
*
|
|
84
|
-
* console.log(kind);
|
|
85
|
-
* };
|
|
87
|
+
* expect(inputToKind({ type: "Query" })).toBe("B");
|
|
86
88
|
* ```
|
|
87
89
|
*/
|
|
88
90
|
export declare const exhaustiveCheck: (value: never) => never;
|
|
@@ -95,71 +97,128 @@ export declare const exhaustiveCheck: (value: never) => never;
|
|
|
95
97
|
* ### Example
|
|
96
98
|
*
|
|
97
99
|
* ```ts
|
|
98
|
-
*
|
|
99
|
-
* const same = values.map(identity); // [1, 2, 3]
|
|
100
|
+
* import { identity } from "@evolu/common";
|
|
100
101
|
*
|
|
102
|
+
* const values = [1, 2, 3];
|
|
103
|
+
* const object = { value: 1 };
|
|
101
104
|
* const getTransform = (shouldDouble: boolean) =>
|
|
102
|
-
* shouldDouble ? (
|
|
105
|
+
* shouldDouble ? (value: number) => value * 2 : identity;
|
|
106
|
+
*
|
|
107
|
+
* expect(values.map(identity)).toEqual([1, 2, 3]);
|
|
108
|
+
* expect(identity(object)).toBe(object);
|
|
109
|
+
* expect(getTransform(false)(2)).toBe(2);
|
|
103
110
|
* ```
|
|
104
111
|
*/
|
|
105
112
|
export declare const identity: <A>(a: A) => A;
|
|
106
113
|
/**
|
|
107
|
-
*
|
|
108
|
-
*
|
|
114
|
+
* Creates an object that follows JavaScript disposal semantics.
|
|
115
|
+
*
|
|
116
|
+
* The first argument is the object to make disposable. The returned object gets
|
|
117
|
+
* a disposal method and its functions are wrapped with a disposal guard. This
|
|
118
|
+
* is the JavaScript equivalent of .NET `ObjectDisposedException`: once an
|
|
119
|
+
* object has been disposed, calling its methods is a programmer error and
|
|
120
|
+
* should throw immediately instead of continuing with invalid state. Evolu
|
|
121
|
+
* asserts this invariant with the "Cannot use a disposed object." message.
|
|
122
|
+
*
|
|
123
|
+
* The second argument is an optional disposer. When provided, it is
|
|
124
|
+
* [moved](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DisposableStack/move)
|
|
125
|
+
* into the returned object, and the returned object's disposal method disposes
|
|
126
|
+
* it. Omit it when the object has no cleanup resources but still must become
|
|
127
|
+
* unusable after disposal, such as with reference count helpers where disposal
|
|
128
|
+
* enforces correct ownership tracking.
|
|
129
|
+
*
|
|
130
|
+
* ### Example
|
|
131
|
+
*
|
|
132
|
+
* ```ts
|
|
133
|
+
* import { disposable } from "@evolu/common";
|
|
134
|
+
*
|
|
135
|
+
* let cleaned = false;
|
|
136
|
+
* const createResource = () => {
|
|
137
|
+
* using disposer = new DisposableStack();
|
|
138
|
+
* disposer.defer(() => {
|
|
139
|
+
* cleaned = true;
|
|
140
|
+
* });
|
|
141
|
+
* return disposable({ read: () => "ready" }, disposer);
|
|
142
|
+
* };
|
|
143
|
+
*
|
|
144
|
+
* const resource = createResource();
|
|
145
|
+
* expect(resource.read()).toBe("ready");
|
|
146
|
+
* resource[Symbol.dispose]();
|
|
147
|
+
*
|
|
148
|
+
* expect(cleaned).toBe(true);
|
|
149
|
+
* expect(() => resource.read()).toThrow("Cannot use a disposed object.");
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function disposable<T extends object>(value: T extends Disposable ? Omit<T, typeof Symbol.dispose> : T, disposer?: DisposableStack): T extends Disposable ? T : T & Disposable;
|
|
153
|
+
export declare function disposable<T extends object>(value: T extends AsyncDisposable ? Omit<T, typeof Symbol.asyncDispose> : T, disposer: AsyncDisposableStack): T extends AsyncDisposable ? T : T & AsyncDisposable;
|
|
154
|
+
export declare const isDisposable: (value: unknown) => value is Disposable | AsyncDisposable;
|
|
155
|
+
/**
|
|
156
|
+
* A function that takes no arguments and returns a value.
|
|
109
157
|
*
|
|
110
158
|
* Useful for:
|
|
111
159
|
*
|
|
112
|
-
* - Providing default callbacks (see {@link
|
|
160
|
+
* - Providing default callbacks (see {@link constVoid}, {@link constTrue}, etc.)
|
|
113
161
|
* - Delaying expensive operations until actually needed
|
|
114
162
|
* - Deferring side effects so the callee controls when they run
|
|
115
163
|
*
|
|
116
164
|
* ### Example
|
|
117
165
|
*
|
|
118
166
|
* ```ts
|
|
119
|
-
*
|
|
120
|
-
* const notify = (onDone: Lazy<void> = lazyVoid) => {
|
|
121
|
-
* onDone();
|
|
122
|
-
* };
|
|
167
|
+
* import { constVoid, type Thunk } from "@evolu/common";
|
|
123
168
|
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
* const data = getData();
|
|
169
|
+
* const notify = (onDone: Thunk<void> = constVoid) => onDone();
|
|
170
|
+
* notify();
|
|
127
171
|
*
|
|
128
|
-
*
|
|
129
|
-
* const
|
|
130
|
-
*
|
|
172
|
+
* let value = 0;
|
|
173
|
+
* const compute: Thunk<number> = () => ++value;
|
|
174
|
+
* const jobs: Array<Thunk<void>> = [];
|
|
175
|
+
* const schedule = (job: Thunk<void>): void => {
|
|
176
|
+
* jobs.push(job);
|
|
131
177
|
* };
|
|
132
|
-
* schedule(() =>
|
|
178
|
+
* schedule(() => {
|
|
179
|
+
* value += 10;
|
|
180
|
+
* });
|
|
181
|
+
*
|
|
182
|
+
* const computed = compute();
|
|
183
|
+
* jobs.shift()?.();
|
|
184
|
+
* expect(computed).toBe(1);
|
|
185
|
+
* expect(value).toBe(11);
|
|
133
186
|
* ```
|
|
134
187
|
*/
|
|
135
|
-
export type
|
|
188
|
+
export type Thunk<T> = () => T;
|
|
136
189
|
/**
|
|
137
|
-
* Creates a {@link
|
|
190
|
+
* Creates a {@link Thunk} that always returns a precomputed value.
|
|
138
191
|
*
|
|
139
|
-
* Use when the value is expensive to compute and want to compute it once at
|
|
192
|
+
* Use when the value is expensive to compute and you want to compute it once at
|
|
140
193
|
* definition time rather than on every call.
|
|
141
194
|
*
|
|
142
195
|
* ### Example
|
|
143
196
|
*
|
|
144
197
|
* ```ts
|
|
145
|
-
*
|
|
146
|
-
*
|
|
198
|
+
* import { constant } from "@evolu/common";
|
|
199
|
+
*
|
|
200
|
+
* let version = 0;
|
|
201
|
+
* const readConfig = () => ({ version: ++version });
|
|
202
|
+
* const getConstantConfig = constant(readConfig());
|
|
203
|
+
* const getFreshConfig = () => readConfig();
|
|
147
204
|
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
205
|
+
* expect(getConstantConfig()).toBe(getConstantConfig());
|
|
206
|
+
* expect(getConstantConfig().version).toBe(1);
|
|
207
|
+
* expect(getFreshConfig().version).toBe(2);
|
|
208
|
+
* expect(getFreshConfig().version).toBe(3);
|
|
150
209
|
* ```
|
|
151
210
|
*/
|
|
152
|
-
export declare const
|
|
153
|
-
/** A {@link
|
|
154
|
-
export declare const
|
|
155
|
-
/** A {@link
|
|
156
|
-
export declare const
|
|
157
|
-
/** A {@link
|
|
158
|
-
export declare const
|
|
159
|
-
/** A {@link
|
|
160
|
-
export declare const
|
|
161
|
-
/** A {@link
|
|
162
|
-
export declare const
|
|
211
|
+
export declare const constant: <T>(value: T) => Thunk<T>;
|
|
212
|
+
/** A {@link Thunk} that returns `true`. */
|
|
213
|
+
export declare const constTrue: Thunk<true>;
|
|
214
|
+
/** A {@link Thunk} that returns `false`. */
|
|
215
|
+
export declare const constFalse: Thunk<false>;
|
|
216
|
+
/** A {@link Thunk} that returns `null`. */
|
|
217
|
+
export declare const constNull: Thunk<null>;
|
|
218
|
+
/** A {@link Thunk} that returns `undefined`. */
|
|
219
|
+
export declare const constUndefined: Thunk<undefined>;
|
|
220
|
+
/** A {@link Thunk} that returns `undefined` for void callbacks. */
|
|
221
|
+
export declare const constVoid: Thunk<void>;
|
|
163
222
|
/**
|
|
164
223
|
* Development placeholder that always throws.
|
|
165
224
|
*
|
|
@@ -170,17 +229,19 @@ export declare const lazyVoid: Lazy<void>;
|
|
|
170
229
|
* ### Example
|
|
171
230
|
*
|
|
172
231
|
* ```ts
|
|
173
|
-
*
|
|
174
|
-
* const fetchUser = (id: UserId): Result<User, FetchError> => todo();
|
|
232
|
+
* import { todo } from "@evolu/common";
|
|
175
233
|
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
234
|
+
* interface Config {
|
|
235
|
+
* readonly theme: string;
|
|
236
|
+
* }
|
|
179
237
|
*
|
|
180
|
-
*
|
|
238
|
+
* const getCount = (): number => todo();
|
|
181
239
|
* const getConfig = () => todo<Config>();
|
|
182
240
|
*
|
|
183
|
-
* expectTypeOf
|
|
241
|
+
* expectTypeOf<
|
|
242
|
+
* [ReturnType<typeof getCount>, ReturnType<typeof getConfig>]
|
|
243
|
+
* >().toEqualTypeOf<[number, Config]>();
|
|
244
|
+
* expect(getCount).toThrow("not yet implemented");
|
|
184
245
|
* ```
|
|
185
246
|
*/
|
|
186
247
|
export declare const todo: <T>() => T;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Function.d.ts","sourceRoot":"","sources":["../../src/Function.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"Function.d.ts","sourceRoot":"","sources":["../../src/Function.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmFG;AACH,eAAO,MAAM,eAAe,UAAW,KAAK,KAAG,KAE9C,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,KAAK,CAAC,KAAG,CAAM,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EACzC,KAAK,EAAE,CAAC,SAAS,UAAU,GAAG,IAAI,CAAC,CAAC,EAAE,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAChE,QAAQ,CAAC,EAAE,eAAe,GACzB,CAAC,SAAS,UAAU,GAAG,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC;AAC7C,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EACzC,KAAK,EAAE,CAAC,SAAS,eAAe,GAAG,IAAI,CAAC,CAAC,EAAE,OAAO,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,EAC1E,QAAQ,EAAE,oBAAoB,GAC7B,CAAC,SAAS,eAAe,GAAG,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC;AA2BvD,eAAO,MAAM,YAAY,UAChB,OAAO,KACb,KAAK,IAAI,UAAU,GAAG,eAOxB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,QAAQ,GAClB,CAAC,SAAS,CAAC,KAAG,KAAK,CAAC,CAAC,CAEf,CAAC;AAEV,2CAA2C;AAC3C,eAAO,MAAM,SAAS,EAAE,KAAK,CAAC,IAAI,CAAgC,CAAC;AAEnE,4CAA4C;AAC5C,eAAO,MAAM,UAAU,EAAE,KAAK,CAAC,KAAK,CAAiC,CAAC;AAEtE,2CAA2C;AAC3C,eAAO,MAAM,SAAS,EAAE,KAAK,CAAC,IAAI,CAAgC,CAAC;AAEnE,gDAAgD;AAChD,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,SAAS,CACT,CAAC;AAEpC,mEAAmE;AACnE,eAAO,MAAM,SAAS,EAAE,KAAK,CAAC,IAAI,CAAkB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,eAAO,MAAM,IAAI,GAAI,CAAC,OAAK,CAE1B,CAAC"}
|