@evolu/web 3.0.0-next.0 → 3.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +138 -57
- package/dist/src/Task.d.ts +4 -13
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +3 -28
- package/dist/src/Worker.d.ts +25 -3
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +232 -12
- package/dist/src/local-first/Db.worker.js +2 -2
- package/dist/src/local-first/Evolu.d.ts +8 -2
- package/dist/src/local-first/Evolu.d.ts.map +1 -1
- package/dist/src/local-first/Evolu.js +40 -8
- package/dist/src/local-first/Shared.worker.js +84 -3
- package/package.json +16 -13
- package/src/Sqlite.ts +47 -18
- package/src/Task.ts +5 -40
- package/src/Worker.ts +178 -10
- package/src/local-first/Db.worker.ts +2 -2
- package/src/local-first/Evolu.ts +62 -3
- package/src/local-first/Shared.worker.ts +44 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Evolu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/src/Sqlite.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Sqlite.d.ts","sourceRoot":"","sources":["../../src/Sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAa,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"Sqlite.d.ts","sourceRoot":"","sources":["../../src/Sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAa,MAAM,eAAe,CAAC;AA4BnE,eAAO,MAAM,sBAAsB,EAAE,kBA6GlC,CAAC"}
|
package/dist/src/Sqlite.js
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
1
53
|
import { bytesToHex, createPreparedStatementsCache, ok } from "@evolu/common";
|
|
2
54
|
import sqlite3InitModule, {} from "@evolu/sqlite-wasm";
|
|
3
55
|
// @ts-expect-error Missing types.
|
|
@@ -14,68 +66,97 @@ globalThis.sqlite3ApiConfig = {
|
|
|
14
66
|
};
|
|
15
67
|
// Init ASAP.
|
|
16
68
|
const sqlite3Promise = sqlite3InitModule();
|
|
69
|
+
const fileName = "evolu1.db";
|
|
17
70
|
export const createWasmSqliteDriver = (name, options) => async () => {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
71
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
72
|
+
try {
|
|
73
|
+
const sqlite3 = await sqlite3Promise;
|
|
74
|
+
const disposer = __addDisposableResource(env_1, new DisposableStack(), false);
|
|
75
|
+
const useDatabase = (database) => disposer.adopt(database, (database) => {
|
|
76
|
+
database.close();
|
|
77
|
+
});
|
|
78
|
+
let deleteDatabaseFile = false;
|
|
79
|
+
const createOpfsSAHPoolVfs = async (options) => {
|
|
80
|
+
const pool = await sqlite3.installOpfsSAHPoolVfs(options);
|
|
81
|
+
if (pool.isPaused())
|
|
82
|
+
await pool.unpauseVfs();
|
|
83
|
+
disposer.defer(() => {
|
|
84
|
+
if (deleteDatabaseFile)
|
|
85
|
+
pool.unlink(`/${fileName}`);
|
|
86
|
+
pool.pauseVfs();
|
|
32
87
|
});
|
|
33
|
-
|
|
34
|
-
|
|
88
|
+
return pool;
|
|
89
|
+
};
|
|
90
|
+
let db;
|
|
91
|
+
switch (options?.mode) {
|
|
92
|
+
case "memory":
|
|
93
|
+
db = useDatabase(new sqlite3.oo1.DB(":memory:"));
|
|
94
|
+
break;
|
|
95
|
+
case "encrypted": {
|
|
96
|
+
// MultipleCiphers encryption requires its VFS wrapper for OPFS SAH-pool.
|
|
97
|
+
// @ts-expect-error Missing types (update @evolu/sqlite-wasm types)
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
99
|
+
sqlite3.capi.sqlite3mc_vfs_create("opfs", 1);
|
|
100
|
+
const pool = await createOpfsSAHPoolVfs({
|
|
101
|
+
directory: `.${name}`,
|
|
102
|
+
});
|
|
103
|
+
db = useDatabase(new pool.OpfsSAHPoolDb(
|
|
104
|
+
// SQLite normalizes this URI filename to SAH-pool path "/evolu1.db".
|
|
105
|
+
`file:${fileName}?vfs=multipleciphers-opfs-sahpool`));
|
|
106
|
+
db.exec(`
|
|
35
107
|
PRAGMA cipher = 'sqlcipher';
|
|
36
108
|
PRAGMA key = "x'${bytesToHex(options.encryptionKey)}'";
|
|
37
109
|
`);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
default: {
|
|
113
|
+
const pool = await createOpfsSAHPoolVfs({ name });
|
|
114
|
+
db = useDatabase(new pool.OpfsSAHPoolDb(`file:${fileName}`));
|
|
115
|
+
}
|
|
43
116
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
117
|
+
const cache = disposer.use(createPreparedStatementsCache((sql) => db.prepare(sql), (statement) => {
|
|
118
|
+
statement.finalize();
|
|
119
|
+
}));
|
|
120
|
+
const disposables = disposer.move();
|
|
121
|
+
return ok({
|
|
122
|
+
exec: (query) => {
|
|
123
|
+
const prepared = cache.get(query);
|
|
124
|
+
if (prepared) {
|
|
125
|
+
if (query.parameters.length > 0)
|
|
126
|
+
prepared.bind(query.parameters);
|
|
127
|
+
const rows = [];
|
|
128
|
+
while (prepared.step()) {
|
|
129
|
+
rows.push(prepared.get({}));
|
|
130
|
+
}
|
|
131
|
+
prepared.reset();
|
|
132
|
+
return {
|
|
133
|
+
rows: rows,
|
|
134
|
+
changes: db.changes(),
|
|
135
|
+
};
|
|
60
136
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
137
|
+
const rows = db.exec(query.sql, {
|
|
138
|
+
returnValue: "resultRows",
|
|
139
|
+
rowMode: "object",
|
|
140
|
+
bind: query.parameters,
|
|
141
|
+
});
|
|
142
|
+
const changes = db.changes();
|
|
143
|
+
return { rows, changes };
|
|
144
|
+
},
|
|
145
|
+
export: () => sqlite3.capi.sqlite3_js_db_export(db),
|
|
146
|
+
deleteDatabase: () => {
|
|
147
|
+
deleteDatabaseFile = true;
|
|
148
|
+
disposables.dispose();
|
|
149
|
+
},
|
|
150
|
+
[Symbol.dispose]: () => {
|
|
151
|
+
disposables.dispose();
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
catch (e_1) {
|
|
156
|
+
env_1.error = e_1;
|
|
157
|
+
env_1.hasError = true;
|
|
158
|
+
}
|
|
159
|
+
finally {
|
|
160
|
+
__disposeResources(env_1);
|
|
161
|
+
}
|
|
81
162
|
};
|
package/dist/src/Task.d.ts
CHANGED
|
@@ -3,16 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
import { type CreateRun, type
|
|
7
|
-
/**
|
|
8
|
-
* Creates a {@link LeaderLock} backed by the Web Locks API.
|
|
9
|
-
*
|
|
10
|
-
* Waiting for the browser lock is intentionally unabortable. If a caller starts
|
|
11
|
-
* waiting and its {@link Run} or fiber is later aborted, the underlying Web
|
|
12
|
-
* Locks request keeps waiting until the browser grants the lock. Only the
|
|
13
|
-
* returned lease releases it.
|
|
14
|
-
*/
|
|
15
|
-
export declare const createLeaderLock: () => LeaderLock;
|
|
6
|
+
import { type CreateRun, type RunDefaultDeps } from "@evolu/common";
|
|
16
7
|
/**
|
|
17
8
|
* Creates {@link Run} for the browser with global error handling.
|
|
18
9
|
*
|
|
@@ -29,10 +20,10 @@ export declare const createLeaderLock: () => LeaderLock;
|
|
|
29
20
|
* });
|
|
30
21
|
*
|
|
31
22
|
* await using run = createRun({ console });
|
|
32
|
-
* await using
|
|
23
|
+
* await using disposer = new AsyncDisposableStack();
|
|
33
24
|
*
|
|
34
|
-
*
|
|
25
|
+
* disposer.use(await run.orThrow(startApp()));
|
|
35
26
|
* ```
|
|
36
27
|
*/
|
|
37
|
-
export declare const createRun: CreateRun<
|
|
28
|
+
export declare const createRun: CreateRun<RunDefaultDeps>;
|
|
38
29
|
//# sourceMappingURL=Task.d.ts.map
|
package/dist/src/Task.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Task.d.ts","sourceRoot":"","sources":["../../src/Task.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"Task.d.ts","sourceRoot":"","sources":["../../src/Task.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAGL,KAAK,SAAS,EAEd,KAAK,cAAc,EACpB,MAAM,eAAe,CAAC;AAEvB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,SAAS,EAAE,SAAS,CAAC,cAAc,CAuB/C,CAAC"}
|
package/dist/src/Task.js
CHANGED
|
@@ -3,32 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
import { createRun as createCommonRun, createUnknownError,
|
|
7
|
-
/**
|
|
8
|
-
* Creates a {@link LeaderLock} backed by the Web Locks API.
|
|
9
|
-
*
|
|
10
|
-
* Waiting for the browser lock is intentionally unabortable. If a caller starts
|
|
11
|
-
* waiting and its {@link Run} or fiber is later aborted, the underlying Web
|
|
12
|
-
* Locks request keeps waiting until the browser grants the lock. Only the
|
|
13
|
-
* returned lease releases it.
|
|
14
|
-
*/
|
|
15
|
-
export const createLeaderLock = () => ({
|
|
16
|
-
lock: (name) => async () => {
|
|
17
|
-
const acquired = Promise.withResolvers();
|
|
18
|
-
const release = Promise.withResolvers();
|
|
19
|
-
void globalThis.navigator.locks.request(`evolu-leaderlock-${name}`, { mode: "exclusive" }, async () => {
|
|
20
|
-
acquired.resolve();
|
|
21
|
-
await release.promise;
|
|
22
|
-
});
|
|
23
|
-
await acquired.promise;
|
|
24
|
-
return ok({
|
|
25
|
-
[Symbol.asyncDispose]: async () => {
|
|
26
|
-
release.resolve();
|
|
27
|
-
return Promise.resolve();
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
},
|
|
31
|
-
});
|
|
6
|
+
import { createRun as createCommonRun, createUnknownError, } from "@evolu/common";
|
|
32
7
|
/**
|
|
33
8
|
* Creates {@link Run} for the browser with global error handling.
|
|
34
9
|
*
|
|
@@ -45,9 +20,9 @@ export const createLeaderLock = () => ({
|
|
|
45
20
|
* });
|
|
46
21
|
*
|
|
47
22
|
* await using run = createRun({ console });
|
|
48
|
-
* await using
|
|
23
|
+
* await using disposer = new AsyncDisposableStack();
|
|
49
24
|
*
|
|
50
|
-
*
|
|
25
|
+
* disposer.use(await run.orThrow(startApp()));
|
|
51
26
|
* ```
|
|
52
27
|
*/
|
|
53
28
|
export const createRun = (deps) => {
|
package/dist/src/Worker.d.ts
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
|
-
import type { CreateMessagePort, MessageChannel, SharedWorker, SharedWorkerSelf, Worker, WorkerDeps, WorkerSelf } from "@evolu/common";
|
|
1
|
+
import type { CreateBroadcastChannel, CreateBroadcastChannelDep, CreateMessageChannelDep, CreateMessagePort, MessageChannel, SharedWorker, SharedWorkerSelf, Worker, WorkerDeps, WorkerSelf } from "@evolu/common";
|
|
2
2
|
/** Creates a {@link Worker} from a Web Worker. */
|
|
3
3
|
export declare const createWorker: <Input, Output>(nativeWorker: globalThis.Worker) => Worker<Input, Output>;
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Installs a one-tab SharedWorker polyfill backed by a Web Worker.
|
|
6
|
+
*
|
|
7
|
+
* Use it before constructing a SharedWorker in browsers without native
|
|
8
|
+
* SharedWorker support.
|
|
9
|
+
*/
|
|
10
|
+
export declare const installOneTabSharedWorkerPolyfill: () => void;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a {@link SharedWorker} from a Web SharedWorker.
|
|
13
|
+
*
|
|
14
|
+
* Call {@link installOneTabSharedWorkerPolyfill} first to support browsers
|
|
15
|
+
* without native SharedWorker support.
|
|
16
|
+
*/
|
|
5
17
|
export declare const createSharedWorker: <Input, Output = never>(nativeSharedWorker: globalThis.SharedWorker) => SharedWorker<Input, Output>;
|
|
6
18
|
/**
|
|
7
19
|
* Creates a {@link MessageChannel} from a Web MessageChannel.
|
|
@@ -21,6 +33,8 @@ export declare const createMessageChannel: <Input, Output = never>() => MessageC
|
|
|
21
33
|
* Use this for ports received via postMessage transfer.
|
|
22
34
|
*/
|
|
23
35
|
export declare const createMessagePort: CreateMessagePort;
|
|
36
|
+
/** Creates an Evolu {@link BroadcastChannel} from a Web BroadcastChannel. */
|
|
37
|
+
export declare const createBroadcastChannel: CreateBroadcastChannel;
|
|
24
38
|
/**
|
|
25
39
|
* Creates an Evolu {@link WorkerSelf} from a Web `DedicatedWorkerGlobalScope`
|
|
26
40
|
* (`self` inside a dedicated worker).
|
|
@@ -33,6 +47,14 @@ export declare const createWorkerSelf: <Input, Output = never>(nativeSelf: globa
|
|
|
33
47
|
* Disposing closes the shared worker scope for all connected clients.
|
|
34
48
|
*/
|
|
35
49
|
export declare const createSharedWorkerSelf: <Input, Output = never>(nativeSelf: globalThis.SharedWorkerGlobalScope) => SharedWorkerSelf<Input, Output>;
|
|
50
|
+
/**
|
|
51
|
+
* Creates a one-tab {@link SharedWorkerSelf} polyfill for browsers without
|
|
52
|
+
* native SharedWorker support.
|
|
53
|
+
*
|
|
54
|
+
* Delivers one synthetic `onConnect` port, queues startup messages until
|
|
55
|
+
* `onMessage` is set, and does not share state across tabs.
|
|
56
|
+
*/
|
|
57
|
+
export declare const createOneTabSharedWorkerSelfPolyfill: <Input, Output = never>(nativeSelf: globalThis.DedicatedWorkerGlobalScope) => SharedWorkerSelf<Input, Output>;
|
|
36
58
|
/** Creates deps shared by web worker entry points. */
|
|
37
|
-
export declare const createWorkerDeps: () => WorkerDeps;
|
|
59
|
+
export declare const createWorkerDeps: () => WorkerDeps & CreateBroadcastChannelDep & CreateMessageChannelDep;
|
|
38
60
|
//# sourceMappingURL=Worker.d.ts.map
|
package/dist/src/Worker.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Worker.d.ts","sourceRoot":"","sources":["../../src/Worker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"Worker.d.ts","sourceRoot":"","sources":["../../src/Worker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,sBAAsB,EACtB,yBAAyB,EACzB,uBAAuB,EACvB,iBAAiB,EACjB,cAAc,EAGd,YAAY,EACZ,gBAAgB,EAEhB,MAAM,EACN,UAAU,EACV,UAAU,EACX,MAAM,eAAe,CAAC;AAQvB,kDAAkD;AAClD,eAAO,MAAM,YAAY,GAAI,KAAK,EAAE,MAAM,EACxC,cAAc,UAAU,CAAC,MAAM,KAC9B,MAAM,CAAC,KAAK,EAAE,MAAM,CAAuB,CAAC;AAE/C;;;;;GAKG;AACH,eAAO,MAAM,iCAAiC,QAAO,IAiBpD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,KAAK,EAAE,MAAM,GAAG,KAAK,EACtD,oBAAoB,UAAU,CAAC,YAAY,KAC1C,YAAY,CAAC,KAAK,EAAE,MAAM,CAM5B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oBAAoB,GAAI,KAAK,EAAE,MAAM,GAAG,KAAK,OAAK,cAAc,CAC3E,KAAK,EACL,MAAM,CAcP,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,iBACuB,CAAC;AAExD,6EAA6E;AAC7E,eAAO,MAAM,sBAAsB,EAAE,sBAoCpC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAI,KAAK,EAAE,MAAM,GAAG,KAAK,EACpD,YAAY,UAAU,CAAC,0BAA0B,KAChD,UAAU,CAAC,KAAK,EAAE,MAAM,CAAoC,CAAC;AAEhE;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,GAAI,KAAK,EAAE,MAAM,GAAG,KAAK,EAC1D,YAAY,UAAU,CAAC,uBAAuB,KAC7C,gBAAgB,CAAC,KAAK,EAAE,MAAM,CAkBhC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oCAAoC,GAAI,KAAK,EAAE,MAAM,GAAG,KAAK,EACxE,YAAY,UAAU,CAAC,0BAA0B,KAChD,gBAAgB,CAAC,KAAK,EAAE,MAAM,CAyEhC,CAAC;AAEF,sDAAsD;AACtD,eAAO,MAAM,gBAAgB,QAAO,UAAU,GAC5C,yBAAyB,GACzB,uBAWD,CAAC"}
|
package/dist/src/Worker.js
CHANGED
|
@@ -1,7 +1,84 @@
|
|
|
1
|
-
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
53
|
+
import { assert, assertNotDisposed, createConsole, createConsoleStoreOutput, } from "@evolu/common";
|
|
2
54
|
/** Creates a {@link Worker} from a Web Worker. */
|
|
3
55
|
export const createWorker = (nativeWorker) => wrap(nativeWorker);
|
|
4
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* Installs a one-tab SharedWorker polyfill backed by a Web Worker.
|
|
58
|
+
*
|
|
59
|
+
* Use it before constructing a SharedWorker in browsers without native
|
|
60
|
+
* SharedWorker support.
|
|
61
|
+
*/
|
|
62
|
+
export const installOneTabSharedWorkerPolyfill = () => {
|
|
63
|
+
if (typeof globalThis.SharedWorker === "function")
|
|
64
|
+
return;
|
|
65
|
+
Object.defineProperty(globalThis, "SharedWorker", {
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true,
|
|
68
|
+
value: class {
|
|
69
|
+
port;
|
|
70
|
+
constructor(scriptURL, options) {
|
|
71
|
+
this.port = new globalThis.Worker(scriptURL, options);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Creates a {@link SharedWorker} from a Web SharedWorker.
|
|
78
|
+
*
|
|
79
|
+
* Call {@link installOneTabSharedWorkerPolyfill} first to support browsers
|
|
80
|
+
* without native SharedWorker support.
|
|
81
|
+
*/
|
|
5
82
|
export const createSharedWorker = (nativeSharedWorker) => {
|
|
6
83
|
const port = wrap(nativeSharedWorker.port);
|
|
7
84
|
return {
|
|
@@ -21,15 +98,26 @@ export const createSharedWorker = (nativeSharedWorker) => {
|
|
|
21
98
|
* ```
|
|
22
99
|
*/
|
|
23
100
|
export const createMessageChannel = () => {
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
101
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
102
|
+
try {
|
|
103
|
+
const channel = new globalThis.MessageChannel();
|
|
104
|
+
const disposer = __addDisposableResource(env_1, new DisposableStack(), false);
|
|
105
|
+
const port1 = disposer.use(wrap(channel.port1));
|
|
106
|
+
const port2 = disposer.use(wrap(channel.port2));
|
|
107
|
+
const disposables = disposer.move();
|
|
108
|
+
return {
|
|
109
|
+
port1,
|
|
110
|
+
port2,
|
|
111
|
+
[Symbol.dispose]: () => disposables.dispose(),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
catch (e_1) {
|
|
115
|
+
env_1.error = e_1;
|
|
116
|
+
env_1.hasError = true;
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
__disposeResources(env_1);
|
|
120
|
+
}
|
|
33
121
|
};
|
|
34
122
|
/**
|
|
35
123
|
* Creates an Evolu {@link MessagePort} from a Web MessagePort.
|
|
@@ -37,6 +125,47 @@ export const createMessageChannel = () => {
|
|
|
37
125
|
* Use this for ports received via postMessage transfer.
|
|
38
126
|
*/
|
|
39
127
|
export const createMessagePort = (nativePort) => wrap(nativePort);
|
|
128
|
+
/** Creates an Evolu {@link BroadcastChannel} from a Web BroadcastChannel. */
|
|
129
|
+
export const createBroadcastChannel = (name) => {
|
|
130
|
+
const env_2 = { stack: [], error: void 0, hasError: false };
|
|
131
|
+
try {
|
|
132
|
+
const nativeBroadcastChannel = new globalThis.BroadcastChannel(name);
|
|
133
|
+
const disposer = __addDisposableResource(env_2, new DisposableStack(), false);
|
|
134
|
+
disposer.defer(() => {
|
|
135
|
+
nativeBroadcastChannel.onmessage = null;
|
|
136
|
+
nativeBroadcastChannel.close();
|
|
137
|
+
});
|
|
138
|
+
const disposables = disposer.move();
|
|
139
|
+
let onMessageHandler = null;
|
|
140
|
+
return {
|
|
141
|
+
postMessage: (message) => {
|
|
142
|
+
assertNotDisposed(disposables);
|
|
143
|
+
nativeBroadcastChannel.postMessage(message);
|
|
144
|
+
},
|
|
145
|
+
get onMessage() {
|
|
146
|
+
return disposables.disposed ? null : onMessageHandler;
|
|
147
|
+
},
|
|
148
|
+
set onMessage(fn) {
|
|
149
|
+
if (disposables.disposed)
|
|
150
|
+
return;
|
|
151
|
+
onMessageHandler = fn;
|
|
152
|
+
nativeBroadcastChannel.onmessage = fn
|
|
153
|
+
? (event) => {
|
|
154
|
+
fn(event.data);
|
|
155
|
+
}
|
|
156
|
+
: null;
|
|
157
|
+
},
|
|
158
|
+
[Symbol.dispose]: () => disposables.dispose(),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
catch (e_2) {
|
|
162
|
+
env_2.error = e_2;
|
|
163
|
+
env_2.hasError = true;
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
__disposeResources(env_2);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
40
169
|
/**
|
|
41
170
|
* Creates an Evolu {@link WorkerSelf} from a Web `DedicatedWorkerGlobalScope`
|
|
42
171
|
* (`self` inside a dedicated worker).
|
|
@@ -62,6 +191,95 @@ export const createSharedWorkerSelf = (nativeSelf) => {
|
|
|
62
191
|
};
|
|
63
192
|
return self;
|
|
64
193
|
};
|
|
194
|
+
/**
|
|
195
|
+
* Creates a one-tab {@link SharedWorkerSelf} polyfill for browsers without
|
|
196
|
+
* native SharedWorker support.
|
|
197
|
+
*
|
|
198
|
+
* Delivers one synthetic `onConnect` port, queues startup messages until
|
|
199
|
+
* `onMessage` is set, and does not share state across tabs.
|
|
200
|
+
*/
|
|
201
|
+
export const createOneTabSharedWorkerSelfPolyfill = (nativeSelf) => {
|
|
202
|
+
const env_3 = { stack: [], error: void 0, hasError: false };
|
|
203
|
+
try {
|
|
204
|
+
const disposer = __addDisposableResource(env_3, new DisposableStack(), false);
|
|
205
|
+
let onConnectHandler = null;
|
|
206
|
+
let onMessageHandler = null;
|
|
207
|
+
const messages = [];
|
|
208
|
+
let connected = false;
|
|
209
|
+
disposer.defer(() => {
|
|
210
|
+
messages.length = 0;
|
|
211
|
+
onConnectHandler = null;
|
|
212
|
+
onMessageHandler = null;
|
|
213
|
+
nativeSelf.onmessage = null;
|
|
214
|
+
nativeSelf.close();
|
|
215
|
+
});
|
|
216
|
+
const disposables = disposer.move();
|
|
217
|
+
nativeSelf.onmessage = (event) => {
|
|
218
|
+
if (disposables.disposed)
|
|
219
|
+
return;
|
|
220
|
+
const handler = onMessageHandler;
|
|
221
|
+
if (handler)
|
|
222
|
+
handler(event.data);
|
|
223
|
+
else
|
|
224
|
+
messages.push(event.data);
|
|
225
|
+
};
|
|
226
|
+
const flushMessages = () => {
|
|
227
|
+
for (const message of messages.splice(0)) {
|
|
228
|
+
const handler = onMessageHandler;
|
|
229
|
+
if (!handler)
|
|
230
|
+
return;
|
|
231
|
+
handler(message);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
const port = {
|
|
235
|
+
postMessage: (message, transfer) => {
|
|
236
|
+
if (disposables.disposed)
|
|
237
|
+
return;
|
|
238
|
+
if (transfer == null)
|
|
239
|
+
nativeSelf.postMessage(message);
|
|
240
|
+
else
|
|
241
|
+
nativeSelf.postMessage(message, [...transfer]);
|
|
242
|
+
},
|
|
243
|
+
get onMessage() {
|
|
244
|
+
return disposables.disposed ? null : onMessageHandler;
|
|
245
|
+
},
|
|
246
|
+
set onMessage(fn) {
|
|
247
|
+
if (disposables.disposed)
|
|
248
|
+
return;
|
|
249
|
+
onMessageHandler = fn;
|
|
250
|
+
if (fn)
|
|
251
|
+
flushMessages();
|
|
252
|
+
},
|
|
253
|
+
native: nativeSelf,
|
|
254
|
+
[Symbol.dispose]: () => disposables.dispose(),
|
|
255
|
+
};
|
|
256
|
+
const connect = () => {
|
|
257
|
+
if (connected || !onConnectHandler)
|
|
258
|
+
return;
|
|
259
|
+
connected = true;
|
|
260
|
+
onConnectHandler(port);
|
|
261
|
+
};
|
|
262
|
+
return {
|
|
263
|
+
get onConnect() {
|
|
264
|
+
return onConnectHandler;
|
|
265
|
+
},
|
|
266
|
+
set onConnect(fn) {
|
|
267
|
+
if (disposables.disposed)
|
|
268
|
+
return;
|
|
269
|
+
onConnectHandler = fn;
|
|
270
|
+
connect();
|
|
271
|
+
},
|
|
272
|
+
[Symbol.dispose]: () => disposables.dispose(),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
catch (e_3) {
|
|
276
|
+
env_3.error = e_3;
|
|
277
|
+
env_3.hasError = true;
|
|
278
|
+
}
|
|
279
|
+
finally {
|
|
280
|
+
__disposeResources(env_3);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
65
283
|
/** Creates deps shared by web worker entry points. */
|
|
66
284
|
export const createWorkerDeps = () => {
|
|
67
285
|
const consoleStoreOutput = createConsoleStoreOutput();
|
|
@@ -69,6 +287,8 @@ export const createWorkerDeps = () => {
|
|
|
69
287
|
return {
|
|
70
288
|
console,
|
|
71
289
|
consoleStoreOutputEntry: consoleStoreOutput.entry,
|
|
290
|
+
createBroadcastChannel,
|
|
291
|
+
createMessageChannel,
|
|
72
292
|
createMessagePort,
|
|
73
293
|
};
|
|
74
294
|
};
|
|
@@ -99,7 +319,7 @@ const wrap = (native) => {
|
|
|
99
319
|
native: native,
|
|
100
320
|
[Symbol.dispose]: () => {
|
|
101
321
|
native.onmessage = null;
|
|
102
|
-
if (
|
|
322
|
+
if ("terminate" in native)
|
|
103
323
|
native.terminate();
|
|
104
324
|
else
|
|
105
325
|
native.close();
|