@evolu/common 6.0.1-preview.2 → 6.0.1-preview.21
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/Assert.d.ts +6 -3
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +7 -4
- package/dist/src/Brand.d.ts +75 -0
- package/dist/src/Brand.d.ts.map +1 -0
- package/dist/src/Brand.js +1 -0
- package/dist/src/Buffer.d.ts +1 -1
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Buffer.js +1 -1
- package/dist/src/CallbackRegistry.d.ts +53 -0
- package/dist/src/CallbackRegistry.d.ts.map +1 -0
- package/dist/src/CallbackRegistry.js +25 -0
- package/dist/src/Console.d.ts +31 -6
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +72 -9
- package/dist/src/Crypto.d.ts +61 -34
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +29 -42
- package/dist/src/Evolu/Db.d.ts +159 -66
- package/dist/src/Evolu/Db.d.ts.map +1 -1
- package/dist/src/Evolu/Db.js +300 -705
- package/dist/src/Evolu/Diff.d.ts +3 -3
- package/dist/src/Evolu/Diff.d.ts.map +1 -1
- package/dist/src/Evolu/Diff.js +7 -5
- package/dist/src/Evolu/Evolu.d.ts +200 -115
- package/dist/src/Evolu/Evolu.d.ts.map +1 -1
- package/dist/src/Evolu/Evolu.js +287 -157
- package/dist/src/Evolu/Internal.d.ts +0 -2
- package/dist/src/Evolu/Internal.d.ts.map +1 -1
- package/dist/src/Evolu/Internal.js +0 -2
- package/dist/src/Evolu/LocalAuth.d.ts +144 -0
- package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
- package/dist/src/Evolu/LocalAuth.js +171 -0
- package/dist/src/Evolu/Owner.d.ts +209 -124
- package/dist/src/Evolu/Owner.d.ts.map +1 -1
- package/dist/src/Evolu/Owner.js +118 -104
- package/dist/src/Evolu/Platform.d.ts +9 -7
- package/dist/src/Evolu/Platform.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.d.ts +256 -211
- package/dist/src/Evolu/Protocol.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.js +558 -359
- package/dist/src/Evolu/Public.d.ts +6 -8
- package/dist/src/Evolu/Public.d.ts.map +1 -1
- package/dist/src/Evolu/Public.js +2 -3
- package/dist/src/Evolu/PublicKysely.js +3 -3
- package/dist/src/Evolu/Query.d.ts +2 -1
- package/dist/src/Evolu/Query.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.d.ts +72 -4
- package/dist/src/Evolu/Relay.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.js +124 -13
- package/dist/src/Evolu/Schema.d.ts +129 -73
- package/dist/src/Evolu/Schema.d.ts.map +1 -1
- package/dist/src/Evolu/Schema.js +169 -89
- package/dist/src/Evolu/Storage.d.ts +159 -14
- package/dist/src/Evolu/Storage.d.ts.map +1 -1
- package/dist/src/Evolu/Storage.js +42 -32
- package/dist/src/Evolu/Sync.d.ts +68 -13
- package/dist/src/Evolu/Sync.d.ts.map +1 -1
- package/dist/src/Evolu/Sync.js +414 -20
- package/dist/src/Evolu/Timestamp.d.ts +35 -27
- package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
- package/dist/src/Evolu/Timestamp.js +27 -18
- package/dist/src/ManyToManyMap.d.ts +74 -10
- package/dist/src/ManyToManyMap.d.ts.map +1 -1
- package/dist/src/ManyToManyMap.js +41 -6
- package/dist/src/Number.d.ts +2 -1
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Random.d.ts +3 -2
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/RefCountedResourceManager.d.ts +119 -0
- package/dist/src/RefCountedResourceManager.d.ts.map +1 -0
- package/dist/src/RefCountedResourceManager.js +197 -0
- package/dist/src/Result.d.ts +176 -51
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +30 -241
- package/dist/src/Sqlite.d.ts +21 -4
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +50 -8
- package/dist/src/Task.d.ts +511 -0
- package/dist/src/Task.d.ts.map +1 -0
- package/dist/src/Task.js +410 -0
- package/dist/src/Time.d.ts +59 -0
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +87 -4
- package/dist/src/Type.d.ts +474 -327
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +502 -436
- package/dist/src/Types.d.ts +0 -74
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/WebSocket.d.ts +5 -2
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +12 -13
- package/dist/src/Worker.d.ts +39 -11
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +22 -4
- package/dist/src/index.d.ts +3 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -3
- package/package.json +14 -13
- package/src/Assert.ts +8 -7
- package/src/Brand.ts +75 -0
- package/src/Buffer.ts +1 -1
- package/src/CallbackRegistry.ts +84 -0
- package/src/Console.ts +91 -11
- package/src/Crypto.ts +94 -79
- package/src/Evolu/Db.ts +528 -1027
- package/src/Evolu/Diff.ts +7 -5
- package/src/Evolu/Evolu.ts +682 -329
- package/src/Evolu/Internal.ts +0 -2
- package/src/Evolu/LocalAuth.ts +422 -0
- package/src/Evolu/Owner.ts +282 -227
- package/src/Evolu/Platform.ts +9 -9
- package/src/Evolu/Protocol.ts +788 -640
- package/src/Evolu/Public.ts +7 -14
- package/src/Evolu/PublicKysely.ts +3 -3
- package/src/Evolu/Query.ts +2 -1
- package/src/Evolu/Relay.ts +238 -18
- package/src/Evolu/Schema.ts +391 -191
- package/src/Evolu/Storage.ts +275 -55
- package/src/Evolu/Sync.ts +708 -37
- package/src/Evolu/Timestamp.ts +38 -35
- package/src/ManyToManyMap.ts +127 -24
- package/src/Number.ts +2 -6
- package/src/Random.ts +3 -2
- package/src/RefCountedResourceManager.ts +368 -0
- package/src/Result.ts +183 -53
- package/src/Sqlite.ts +59 -23
- package/src/Task.ts +779 -0
- package/src/Time.ts +168 -4
- package/src/Type.ts +708 -660
- package/src/Types.ts +0 -76
- package/src/WebSocket.ts +23 -17
- package/src/Worker.ts +72 -23
- package/src/index.ts +3 -3
- package/dist/src/Callbacks.d.ts +0 -20
- package/dist/src/Callbacks.d.ts.map +0 -1
- package/dist/src/Callbacks.js +0 -18
- package/dist/src/Evolu/Config.d.ts +0 -69
- package/dist/src/Evolu/Config.d.ts.map +0 -1
- package/dist/src/Evolu/Config.js +0 -9
- package/dist/src/Evolu/Kysely.d.ts +0 -6
- package/dist/src/Evolu/Kysely.d.ts.map +0 -1
- package/dist/src/Evolu/Kysely.js +0 -21
- package/dist/src/NanoId.d.ts +0 -27
- package/dist/src/NanoId.d.ts.map +0 -1
- package/dist/src/NanoId.js +0 -6
- package/dist/src/Promise.d.ts +0 -180
- package/dist/src/Promise.d.ts.map +0 -1
- package/dist/src/Promise.js +0 -176
- package/src/Callbacks.ts +0 -43
- package/src/Evolu/Config.ts +0 -83
- package/src/Evolu/Kysely.ts +0 -38
- package/src/NanoId.ts +0 -39
- package/src/Promise.ts +0 -295
package/src/Types.ts
CHANGED
|
@@ -78,82 +78,6 @@ export type NullablePartial<
|
|
|
78
78
|
*/
|
|
79
79
|
export type IntentionalNever = never;
|
|
80
80
|
|
|
81
|
-
/**
|
|
82
|
-
* A utility interface for creating branded types.
|
|
83
|
-
*
|
|
84
|
-
* Branded types enhance type safety by differentiating otherwise identical base
|
|
85
|
-
* types, such as `number` or `string`, to enforce stricter type checks.
|
|
86
|
-
*
|
|
87
|
-
* Supports multiple brands, allowing types to act like flags.
|
|
88
|
-
*
|
|
89
|
-
* ### Example 1: Single Brand
|
|
90
|
-
*
|
|
91
|
-
* ```ts
|
|
92
|
-
* // A branded type definition
|
|
93
|
-
* type UserId = number & Brand<"UserId">;
|
|
94
|
-
*
|
|
95
|
-
* // A function that creates `UserId` values.
|
|
96
|
-
* // Casting with `as UserId` is unsafe, so `createUserId` must be unit-tested.
|
|
97
|
-
* const createUserId = (): UserId => {
|
|
98
|
-
* return 123 as UserId; // Unsafe casting
|
|
99
|
-
* };
|
|
100
|
-
*
|
|
101
|
-
* const userId = createUserId();
|
|
102
|
-
*
|
|
103
|
-
* // A function that accepts only `UserId`.
|
|
104
|
-
* const getUser = (id: UserId) => {
|
|
105
|
-
* // Implementation
|
|
106
|
-
* };
|
|
107
|
-
*
|
|
108
|
-
* getUser(userId); // ✅ Valid
|
|
109
|
-
* getUser(123); // ❌ TypeScript error
|
|
110
|
-
* getUser("123"); // ❌ TypeScript error
|
|
111
|
-
* ```
|
|
112
|
-
*
|
|
113
|
-
* ### Example 2: Multiple Brands
|
|
114
|
-
*
|
|
115
|
-
* ```ts
|
|
116
|
-
* // Define branded types
|
|
117
|
-
* type Min1 = string & Brand<"Min1">;
|
|
118
|
-
* type Max100 = string & Brand<"Max100">;
|
|
119
|
-
* type Min1Max100 = string & Brand<"Min1" | "Max100">;
|
|
120
|
-
*
|
|
121
|
-
* // Functions requiring specific brands
|
|
122
|
-
* const requiresMin1 = (value: Min1): void => {};
|
|
123
|
-
* const requiresMax100 = (value: Max100): void => {};
|
|
124
|
-
*
|
|
125
|
-
* // Values with single brands
|
|
126
|
-
* const min1Value: Min1 = "hello" as Min1;
|
|
127
|
-
* const max100Value: Max100 = "world" as Max100;
|
|
128
|
-
*
|
|
129
|
-
* // Value with multiple brands
|
|
130
|
-
* const min1Max100Value: Min1Max100 = "typescript" as Min1Max100;
|
|
131
|
-
*
|
|
132
|
-
* // Valid cases
|
|
133
|
-
* requiresMin1(min1Value); // ✅ Valid
|
|
134
|
-
* requiresMax100(max100Value); // ✅ Valid
|
|
135
|
-
* requiresMin1(min1Max100Value); // ✅ Valid: Min1Max100 satisfies Min1
|
|
136
|
-
* requiresMax100(min1Max100Value); // ✅ Valid: Min1Max100 satisfies Max100
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
|
-
export interface Brand<B extends string> {
|
|
140
|
-
readonly [__brand]: Readonly<Record<B, true>>;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
declare const __brand: unique symbol;
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Determines whether a type `T` is a branded type.
|
|
147
|
-
*
|
|
148
|
-
* Works with any base type intersected with a `Brand`.
|
|
149
|
-
*
|
|
150
|
-
* ### Examples
|
|
151
|
-
*
|
|
152
|
-
* - `IsBranded<string>` -> false
|
|
153
|
-
* - `IsBranded<string & Brand<"X">>` -> true
|
|
154
|
-
*/
|
|
155
|
-
export type IsBranded<T> = T extends Brand<string> ? true : false;
|
|
156
|
-
|
|
157
81
|
/**
|
|
158
82
|
* String | number | bigint | boolean | undefined | null
|
|
159
83
|
*
|
package/src/WebSocket.ts
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
import { assertNoErrorInCatch } from "./Assert.js";
|
|
8
8
|
import { constVoid } from "./Function.js";
|
|
9
|
-
import { retry, RetryError, RetryOptions } from "./Promise.js";
|
|
10
9
|
import { err, ok, Result } from "./Result.js";
|
|
10
|
+
import { retry, RetryError, RetryOptions } from "./Task.js";
|
|
11
|
+
import { maxPositiveInt } from "./Type.js";
|
|
11
12
|
|
|
12
13
|
/** WebSocket with auto-reconnect and offline support. */
|
|
13
14
|
export interface WebSocket extends Disposable {
|
|
@@ -20,10 +21,14 @@ export interface WebSocket extends Disposable {
|
|
|
20
21
|
) => Result<void, WebSocketSendError>;
|
|
21
22
|
|
|
22
23
|
readonly getReadyState: () => WebSocketReadyState;
|
|
24
|
+
|
|
25
|
+
/** Returns true if the WebSocket is open and ready to send data. */
|
|
26
|
+
readonly isOpen: () => boolean;
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
/**
|
|
26
|
-
* An error that occurs when trying to send data but WebSocket is
|
|
30
|
+
* An error that occurs when trying to send data but WebSocket is not available
|
|
31
|
+
* or is in the CONNECTING state.
|
|
27
32
|
*
|
|
28
33
|
* https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
|
|
29
34
|
*/
|
|
@@ -141,8 +146,9 @@ export interface WebSocketConnectionCloseError {
|
|
|
141
146
|
*
|
|
142
147
|
* TODO:
|
|
143
148
|
*/
|
|
144
|
-
export const createWebSocket: CreateWebSocket = (
|
|
145
|
-
|
|
149
|
+
export const createWebSocket: CreateWebSocket = (
|
|
150
|
+
url,
|
|
151
|
+
{
|
|
146
152
|
protocols,
|
|
147
153
|
binaryType,
|
|
148
154
|
onOpen,
|
|
@@ -151,14 +157,14 @@ export const createWebSocket: CreateWebSocket = (url, options = {}) => {
|
|
|
151
157
|
onError,
|
|
152
158
|
retryOptions,
|
|
153
159
|
WebSocketConstructor = globalThis.WebSocket,
|
|
154
|
-
} =
|
|
160
|
+
} = {},
|
|
161
|
+
) => {
|
|
155
162
|
let isDisposed = false;
|
|
156
163
|
|
|
157
164
|
const reconnectController = new AbortController();
|
|
158
165
|
|
|
159
166
|
const defaultRetryOptions: RetryOptions<WebSocketRetryError> = {
|
|
160
|
-
|
|
161
|
-
maxRetries: Infinity,
|
|
167
|
+
retries: maxPositiveInt, // Practically infinite retries
|
|
162
168
|
};
|
|
163
169
|
|
|
164
170
|
let socket: globalThis.WebSocket | null = null;
|
|
@@ -187,6 +193,10 @@ export const createWebSocket: CreateWebSocket = (url, options = {}) => {
|
|
|
187
193
|
* - Is resolved when WebSocket is disposed().
|
|
188
194
|
*/
|
|
189
195
|
retry(
|
|
196
|
+
{
|
|
197
|
+
...defaultRetryOptions,
|
|
198
|
+
...retryOptions,
|
|
199
|
+
},
|
|
190
200
|
(): Promise<Result<void, WebSocketRetryError>> =>
|
|
191
201
|
new Promise((resolve) => {
|
|
192
202
|
disposePromise = () => {
|
|
@@ -227,20 +237,16 @@ export const createWebSocket: CreateWebSocket = (url, options = {}) => {
|
|
|
227
237
|
onMessage?.(event.data as string | ArrayBuffer | Blob);
|
|
228
238
|
};
|
|
229
239
|
}),
|
|
230
|
-
|
|
231
|
-
...defaultRetryOptions,
|
|
232
|
-
...retryOptions,
|
|
233
|
-
},
|
|
234
|
-
)
|
|
240
|
+
)(reconnectController)
|
|
235
241
|
.then((result) => {
|
|
236
|
-
if (result.ok || result.error.type === "
|
|
237
|
-
onError?.(result.error);
|
|
242
|
+
if (result.ok || result.error.type === "AbortError") return;
|
|
243
|
+
onError?.(result.error as WebSocketError);
|
|
238
244
|
})
|
|
239
245
|
.catch((error: unknown) => {
|
|
240
246
|
assertNoErrorInCatch("WebSocket retry", error);
|
|
241
247
|
});
|
|
242
248
|
|
|
243
|
-
|
|
249
|
+
return {
|
|
244
250
|
send: (data) => {
|
|
245
251
|
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
|
|
246
252
|
if (!socket || socket.readyState === socket.CONNECTING) {
|
|
@@ -253,6 +259,8 @@ export const createWebSocket: CreateWebSocket = (url, options = {}) => {
|
|
|
253
259
|
getReadyState: () =>
|
|
254
260
|
socket ? nativeToStringState[socket.readyState] : "connecting",
|
|
255
261
|
|
|
262
|
+
isOpen: () => (socket ? socket.readyState === socket.OPEN : false),
|
|
263
|
+
|
|
256
264
|
[Symbol.dispose]() {
|
|
257
265
|
if (isDisposed) return;
|
|
258
266
|
isDisposed = true;
|
|
@@ -261,8 +269,6 @@ export const createWebSocket: CreateWebSocket = (url, options = {}) => {
|
|
|
261
269
|
disposePromise?.();
|
|
262
270
|
},
|
|
263
271
|
};
|
|
264
|
-
|
|
265
|
-
return webSocket;
|
|
266
272
|
};
|
|
267
273
|
|
|
268
274
|
const nativeToStringState: Record<number, WebSocketReadyState> = {
|
package/src/Worker.ts
CHANGED
|
@@ -20,6 +20,32 @@ export interface WorkerPostMessageDep<Output> {
|
|
|
20
20
|
readonly postMessage: (message: Output) => void;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Error reporting wrapper that catches synchronous errors in handlers and
|
|
25
|
+
* converts them to transferable error messages sent to the main thread.
|
|
26
|
+
*/
|
|
27
|
+
export type WithErrorReporting = <A extends Array<any>>(
|
|
28
|
+
handler: (...args: A) => void,
|
|
29
|
+
) => (...args: A) => void;
|
|
30
|
+
|
|
31
|
+
type HasInit<Input> =
|
|
32
|
+
Extract<Input, { type: "init" }> extends never
|
|
33
|
+
? ["Input must contain a variant with { type: 'init' }"]
|
|
34
|
+
: unknown;
|
|
35
|
+
|
|
36
|
+
type HasWorkerErrorOutput<T> =
|
|
37
|
+
Extract<T, { type: "onError" }> extends infer E
|
|
38
|
+
? [E] extends [never]
|
|
39
|
+
? [
|
|
40
|
+
"Output must contain { type: 'onError'; error: TransferableError | ... }",
|
|
41
|
+
]
|
|
42
|
+
: E extends { error: infer Err }
|
|
43
|
+
? TransferableError extends Err
|
|
44
|
+
? unknown
|
|
45
|
+
: ["Output.onError.error must include TransferableError"]
|
|
46
|
+
: ["Output.onError must have an error property"]
|
|
47
|
+
: never;
|
|
48
|
+
|
|
23
49
|
/**
|
|
24
50
|
* Creates a {@link Worker} that supports initialization with dependencies and
|
|
25
51
|
* safe error handling.
|
|
@@ -35,9 +61,7 @@ export const createInitializedWorker = <
|
|
|
35
61
|
readonly init: (
|
|
36
62
|
initMessage: Extract<Input, { type: "init" }>,
|
|
37
63
|
postMessage: (msg: Output) => void,
|
|
38
|
-
|
|
39
|
-
handler: (...args: A) => void,
|
|
40
|
-
) => (...args: A) => void,
|
|
64
|
+
withErrorReporting: WithErrorReporting,
|
|
41
65
|
) => Promise<Deps | null>;
|
|
42
66
|
readonly onMessage: (
|
|
43
67
|
deps: Deps,
|
|
@@ -62,7 +86,11 @@ export const createInitializedWorker = <
|
|
|
62
86
|
} as unknown as Output);
|
|
63
87
|
};
|
|
64
88
|
|
|
65
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Wraps function to catch errors and send them to the main thread instead of
|
|
91
|
+
* crashing the worker.
|
|
92
|
+
*/
|
|
93
|
+
const withErrorReporting =
|
|
66
94
|
<A extends Array<any>>(handler: (...args: A) => void) =>
|
|
67
95
|
(...args: A) => {
|
|
68
96
|
try {
|
|
@@ -78,7 +106,7 @@ export const createInitializedWorker = <
|
|
|
78
106
|
if (!deps) {
|
|
79
107
|
pendingMessages.push(message);
|
|
80
108
|
} else {
|
|
81
|
-
|
|
109
|
+
withErrorReporting(onMessage(deps))(message as NonInitMessage);
|
|
82
110
|
}
|
|
83
111
|
return;
|
|
84
112
|
}
|
|
@@ -89,13 +117,13 @@ export const createInitializedWorker = <
|
|
|
89
117
|
init(
|
|
90
118
|
message as Extract<Input, { type: "init" }>,
|
|
91
119
|
postMessage,
|
|
92
|
-
|
|
120
|
+
withErrorReporting,
|
|
93
121
|
)
|
|
94
122
|
.then((_deps) => {
|
|
95
123
|
if (_deps == null) return;
|
|
96
124
|
deps = _deps;
|
|
97
125
|
for (const message of pendingMessages) {
|
|
98
|
-
|
|
126
|
+
withErrorReporting(onMessage(deps))(message as NonInitMessage);
|
|
99
127
|
}
|
|
100
128
|
pendingMessages.length = 0;
|
|
101
129
|
})
|
|
@@ -110,20 +138,41 @@ export const createInitializedWorker = <
|
|
|
110
138
|
return worker;
|
|
111
139
|
};
|
|
112
140
|
|
|
113
|
-
type
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
:
|
|
141
|
+
/** Type helper to extract message types from a union type */
|
|
142
|
+
export type MessageHandlers<Input extends { readonly type: string }, Deps> = {
|
|
143
|
+
readonly [K in Input["type"]]: (
|
|
144
|
+
deps: Deps,
|
|
145
|
+
) => (message: Extract<Input, { type: K }>) => void;
|
|
146
|
+
};
|
|
117
147
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Creates a {@link Worker} with type-safe message handlers for each message
|
|
150
|
+
* type. This provides better type safety and organization compared to a single
|
|
151
|
+
* onMessage handler.
|
|
152
|
+
*/
|
|
153
|
+
export const createInitializedWorkerWithHandlers = <
|
|
154
|
+
Input extends { readonly type: string } & HasInit<Input>,
|
|
155
|
+
Output extends { readonly type: string } & HasWorkerErrorOutput<Output>,
|
|
156
|
+
Deps,
|
|
157
|
+
>({
|
|
158
|
+
init,
|
|
159
|
+
handlers,
|
|
160
|
+
}: {
|
|
161
|
+
readonly init: (
|
|
162
|
+
initMessage: Extract<Input, { type: "init" }>,
|
|
163
|
+
postMessage: (msg: Output) => void,
|
|
164
|
+
withErrorReporting: WithErrorReporting,
|
|
165
|
+
) => Promise<Deps | null>;
|
|
166
|
+
readonly handlers: Omit<MessageHandlers<Input, Deps>, "init">;
|
|
167
|
+
}): Worker<Input, Output> =>
|
|
168
|
+
createInitializedWorker({
|
|
169
|
+
init,
|
|
170
|
+
onMessage: (deps) => (message) => {
|
|
171
|
+
type NonInitMessageType = Exclude<Input["type"], "init">;
|
|
172
|
+
const messageType = message.type as NonInitMessageType;
|
|
173
|
+
const handler = handlers[messageType];
|
|
174
|
+
|
|
175
|
+
// TypeScript knows handler exists because handlers covers all non-init message types
|
|
176
|
+
handler(deps)(message as Extract<Input, { type: typeof messageType }>);
|
|
177
|
+
},
|
|
178
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export * from "./Array.js";
|
|
2
2
|
export * from "./Assert.js";
|
|
3
3
|
export * from "./BigInt.js";
|
|
4
|
+
export * from "./Brand.js";
|
|
4
5
|
export * from "./Buffer.js";
|
|
5
|
-
export * from "./
|
|
6
|
+
export * from "./CallbackRegistry.js";
|
|
6
7
|
export * from "./Console.js";
|
|
7
8
|
export * from "./Crypto.js";
|
|
8
9
|
export * from "./Eq.js";
|
|
@@ -10,17 +11,16 @@ export * from "./Error.js";
|
|
|
10
11
|
export * from "./Evolu/Public.js";
|
|
11
12
|
export * from "./Function.js";
|
|
12
13
|
export * from "./ManyToManyMap.js";
|
|
13
|
-
export * from "./NanoId.js";
|
|
14
14
|
export * from "./Number.js";
|
|
15
15
|
export * from "./Object.js";
|
|
16
16
|
export * from "./Order.js";
|
|
17
|
-
export * from "./Promise.js";
|
|
18
17
|
export * from "./Random.js";
|
|
19
18
|
export * from "./Ref.js";
|
|
20
19
|
export * from "./Result.js";
|
|
21
20
|
export * from "./Sqlite.js";
|
|
22
21
|
export * from "./Store.js";
|
|
23
22
|
export * from "./String.js";
|
|
23
|
+
export * from "./Task.js";
|
|
24
24
|
export * from "./Time.js";
|
|
25
25
|
export * from "./Type.js";
|
|
26
26
|
export * from "./Types.js";
|
package/dist/src/Callbacks.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { NanoIdLibDep } from "./NanoId.js";
|
|
2
|
-
import { Brand } from "./Types.js";
|
|
3
|
-
/**
|
|
4
|
-
* Manages one-time callback functions.
|
|
5
|
-
*
|
|
6
|
-
* Allows registering callbacks with a unique ID and executing them once with an
|
|
7
|
-
* optional argument. Executed callbacks are automatically removed.
|
|
8
|
-
*
|
|
9
|
-
* This is useful for managing event-driven or asynchronous workflows where
|
|
10
|
-
* callbacks need to be invoked only once.
|
|
11
|
-
*/
|
|
12
|
-
export interface Callbacks {
|
|
13
|
-
/** Registers a callback function and returns a unique ID. */
|
|
14
|
-
readonly register: (callback: (arg?: unknown) => void) => CallbackId;
|
|
15
|
-
/** Executes and removes a callback associated with the given ID. */
|
|
16
|
-
readonly execute: (id: CallbackId, arg?: unknown) => void;
|
|
17
|
-
}
|
|
18
|
-
export type CallbackId = string & Brand<"CallbackId">;
|
|
19
|
-
export declare const createCallbacks: (deps: NanoIdLibDep) => Callbacks;
|
|
20
|
-
//# sourceMappingURL=Callbacks.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Callbacks.d.ts","sourceRoot":"","sources":["../../src/Callbacks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;;;;;GAQG;AACH,MAAM,WAAW,SAAS;IACxB,6DAA6D;IAC7D,QAAQ,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,KAAK,IAAI,KAAK,UAAU,CAAC;IAErE,oEAAoE;IACpE,QAAQ,CAAC,OAAO,EAAE,CAAC,EAAE,EAAE,UAAU,EAAE,GAAG,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CAC3D;AAED,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;AAEtD,eAAO,MAAM,eAAe,GAAI,MAAM,YAAY,KAAG,SAoBpD,CAAC"}
|
package/dist/src/Callbacks.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export const createCallbacks = (deps) => {
|
|
2
|
-
const callbackMap = new Map();
|
|
3
|
-
const callbacks = {
|
|
4
|
-
register: (callback) => {
|
|
5
|
-
const id = deps.nanoIdLib.nanoid();
|
|
6
|
-
callbackMap.set(id, callback);
|
|
7
|
-
return id;
|
|
8
|
-
},
|
|
9
|
-
execute: (id, arg) => {
|
|
10
|
-
const callback = callbackMap.get(id);
|
|
11
|
-
if (callback) {
|
|
12
|
-
callbackMap.delete(id);
|
|
13
|
-
callback(arg);
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
};
|
|
17
|
-
return callbacks;
|
|
18
|
-
};
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { ConsoleConfig } from "../Console.js";
|
|
2
|
-
import { Mnemonic, SimpleName } from "../Type.js";
|
|
3
|
-
import type { DbIndexesBuilder } from "./Kysely.js";
|
|
4
|
-
export interface Config extends ConsoleConfig {
|
|
5
|
-
/**
|
|
6
|
-
* The name of Evolu instances. Evolu is multitenant - it can run more
|
|
7
|
-
* instances concurrently. Every Evolu instance has to have its own unique
|
|
8
|
-
* name. Database files are separated and invisible to each other.
|
|
9
|
-
*
|
|
10
|
-
* The default value is: `Evolu`.
|
|
11
|
-
*
|
|
12
|
-
* ### Example
|
|
13
|
-
*
|
|
14
|
-
* ```ts
|
|
15
|
-
* // name: getOrThrow(SimpleName.from("MyApp"))
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
readonly name: SimpleName;
|
|
19
|
-
/**
|
|
20
|
-
* URL for Evolu sync and backup server.
|
|
21
|
-
*
|
|
22
|
-
* The default value is `https://free.evoluhq.com`.
|
|
23
|
-
*/
|
|
24
|
-
readonly syncUrl: string;
|
|
25
|
-
/**
|
|
26
|
-
* URL to reload browser tabs after reset or restore.
|
|
27
|
-
*
|
|
28
|
-
* The default value is `/`.
|
|
29
|
-
*/
|
|
30
|
-
readonly reloadUrl: string;
|
|
31
|
-
/**
|
|
32
|
-
* Maximum physical clock drift allowed in ms.
|
|
33
|
-
*
|
|
34
|
-
* The default value is 5 * 60 * 1000 (5 minutes).
|
|
35
|
-
*/
|
|
36
|
-
readonly maxDrift: number;
|
|
37
|
-
/**
|
|
38
|
-
* Use the `indexes` option to define SQLite indexes.
|
|
39
|
-
*
|
|
40
|
-
* Table and column names are not typed because Kysely doesn't support it.
|
|
41
|
-
*
|
|
42
|
-
* https://medium.com/@JasonWyatt/squeezing-performance-from-sqlite-indexes-indexes-c4e175f3c346
|
|
43
|
-
*
|
|
44
|
-
* ### Example
|
|
45
|
-
*
|
|
46
|
-
* ```ts
|
|
47
|
-
* const evolu = createEvolu(evoluReactDeps)(Schema, {
|
|
48
|
-
* indexes: (create) => [
|
|
49
|
-
* create("todoCreatedAt").on("todo").column("createdAt"),
|
|
50
|
-
* create("todoCategoryCreatedAt")
|
|
51
|
-
* .on("todoCategory")
|
|
52
|
-
* .column("createdAt"),
|
|
53
|
-
* ],
|
|
54
|
-
* });
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
readonly indexes?: DbIndexesBuilder;
|
|
58
|
-
/**
|
|
59
|
-
* Use this option to create Evolu with the specified mnemonic. If omitted,
|
|
60
|
-
* the mnemonic will be autogenerated. That should be the default behavior
|
|
61
|
-
* until special UX requirements are needed (e.g., multitenancy).
|
|
62
|
-
*/
|
|
63
|
-
readonly mnemonic?: Mnemonic;
|
|
64
|
-
}
|
|
65
|
-
export interface ConfigDep {
|
|
66
|
-
readonly config: Config;
|
|
67
|
-
}
|
|
68
|
-
export declare const defaultConfig: Config;
|
|
69
|
-
//# sourceMappingURL=Config.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD,MAAM,WAAW,MAAO,SAAQ,aAAa;IAC3C;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAE1B;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAEzB;;;;OAIG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;;;;;;;;;;;;;;;;;;OAmBG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAEpC;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;CAC9B;AAED,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,eAAO,MAAM,aAAa,EAAE,MAM3B,CAAC"}
|
package/dist/src/Evolu/Config.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { getOrThrow } from "../Result.js";
|
|
2
|
-
import { SimpleName } from "../Type.js";
|
|
3
|
-
export const defaultConfig = {
|
|
4
|
-
name: getOrThrow(SimpleName.fromParent("Evolu")),
|
|
5
|
-
syncUrl: "https://free.evoluhq.com",
|
|
6
|
-
reloadUrl: "/",
|
|
7
|
-
maxDrift: 5 * 60 * 1000,
|
|
8
|
-
enableLogging: false,
|
|
9
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { CreateIndexBuilder, Kysely } from "kysely";
|
|
2
|
-
import { DbIndex } from "./Db.js";
|
|
3
|
-
export declare const kysely: Kysely<unknown>;
|
|
4
|
-
export type DbIndexesBuilder = (create: (indexName: string) => CreateIndexBuilder) => ReadonlyArray<CreateIndexBuilder<any>>;
|
|
5
|
-
export declare const createIndexes: (indexes?: DbIndexesBuilder) => ReadonlyArray<DbIndex>;
|
|
6
|
-
//# sourceMappingURL=Kysely.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Kysely.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Kysely.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAElB,MAAM,EAGP,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAGlC,eAAO,MAAM,MAAM,iBASjB,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,CAC7B,MAAM,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,kBAAkB,KAC9C,aAAa,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAC;AAI5C,eAAO,MAAM,aAAa,GACxB,UAAU,gBAAgB,KACzB,aAAa,CAAC,OAAO,CAQvB,CAAC"}
|
package/dist/src/Evolu/Kysely.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { DummyDriver, Kysely, SqliteAdapter, SqliteQueryCompiler, } from "kysely";
|
|
2
|
-
// https://kysely.dev/docs/recipes/splitting-query-building-and-execution
|
|
3
|
-
export const kysely = new Kysely({
|
|
4
|
-
dialect: {
|
|
5
|
-
createAdapter: () => new SqliteAdapter(),
|
|
6
|
-
createDriver: () => new DummyDriver(),
|
|
7
|
-
createIntrospector() {
|
|
8
|
-
throw new Error("Not implemeneted");
|
|
9
|
-
},
|
|
10
|
-
createQueryCompiler: () => new SqliteQueryCompiler(),
|
|
11
|
-
},
|
|
12
|
-
});
|
|
13
|
-
const createIndex = kysely.schema.createIndex.bind(kysely.schema);
|
|
14
|
-
export const createIndexes = (indexes) => {
|
|
15
|
-
if (!indexes)
|
|
16
|
-
return [];
|
|
17
|
-
return indexes(createIndex).map((index) => ({
|
|
18
|
-
name: index.toOperationNode().name.name,
|
|
19
|
-
sql: index.compile().sql,
|
|
20
|
-
}));
|
|
21
|
-
};
|
package/dist/src/NanoId.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Interface representing a NanoId library.
|
|
3
|
-
*
|
|
4
|
-
* It's useful for testing by allowing ID generation to be mocked, ensuring
|
|
5
|
-
* consistent results.
|
|
6
|
-
*
|
|
7
|
-
* @see https://github.com/ai/nanoid
|
|
8
|
-
*
|
|
9
|
-
* ### Example
|
|
10
|
-
*
|
|
11
|
-
* ```ts
|
|
12
|
-
* const foo = createFoo({
|
|
13
|
-
* ...createNanoIdLib(),
|
|
14
|
-
* ...createTime(),
|
|
15
|
-
* });
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export interface NanoIdLib {
|
|
19
|
-
readonly urlAlphabet: string;
|
|
20
|
-
readonly customAlphabet: (alphabet: string, defaultSize?: number) => (size?: number) => string;
|
|
21
|
-
readonly nanoid: (size?: number) => string;
|
|
22
|
-
}
|
|
23
|
-
export interface NanoIdLibDep {
|
|
24
|
-
readonly nanoIdLib: NanoIdLib;
|
|
25
|
-
}
|
|
26
|
-
export declare const createNanoIdLib: () => NanoIdLib;
|
|
27
|
-
//# sourceMappingURL=NanoId.d.ts.map
|
package/dist/src/NanoId.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"NanoId.d.ts","sourceRoot":"","sources":["../../src/NanoId.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAE7B,QAAQ,CAAC,cAAc,EAAE,CACvB,QAAQ,EAAE,MAAM,EAChB,WAAW,CAAC,EAAE,MAAM,KACjB,CAAC,IAAI,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IAE/B,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;CAC5C;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;CAC/B;AAED,eAAO,MAAM,eAAe,QAAO,SAIjC,CAAC"}
|