jazz-tools 0.8.51 → 0.9.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/.turbo/turbo-build.log +7 -5
- package/CHANGELOG.md +9 -0
- package/dist/{chunk-MUJIKQFH.js → chunk-EK2I4C5W.js} +50 -29
- package/dist/chunk-EK2I4C5W.js.map +1 -0
- package/dist/index.native.js +1 -1
- package/dist/index.web.js +1 -1
- package/dist/testing.js +71 -0
- package/dist/testing.js.map +1 -0
- package/package.json +6 -2
- package/src/coValues/coFeed.ts +1 -1
- package/src/coValues/coList.ts +5 -3
- package/src/coValues/interfaces.ts +41 -27
- package/src/implementation/refs.ts +17 -0
- package/src/implementation/subscriptionScope.ts +4 -4
- package/src/testing.ts +103 -0
- package/src/tests/schemaUnion.test.ts +6 -2
- package/src/tests/subscribe.test.ts +12 -2
- package/src/tests/utils.ts +10 -6
- package/tsup.config.ts +1 -0
- package/dist/chunk-MUJIKQFH.js.map +0 -1
package/src/coValues/coFeed.ts
CHANGED
@@ -737,7 +737,7 @@ export class FileStream extends CoValueBase implements CoValue {
|
|
737
737
|
*/
|
738
738
|
if (!options?.allowUnfinished && !stream?.isBinaryStreamEnded()) {
|
739
739
|
stream = await new Promise<FileStream>((resolve) => {
|
740
|
-
|
740
|
+
subscribeToCoValue(this, id, as, [], (value, unsubscribe) => {
|
741
741
|
if (value.isBinaryStreamEnded()) {
|
742
742
|
unsubscribe();
|
743
743
|
resolve(value);
|
package/src/coValues/coList.ts
CHANGED
@@ -241,9 +241,11 @@ export class CoList<Item = any> extends Array<Item> implements CoValue {
|
|
241
241
|
}
|
242
242
|
|
243
243
|
push(...items: Item[]): number {
|
244
|
-
|
245
|
-
this.
|
246
|
-
|
244
|
+
this._raw.appendItems(
|
245
|
+
toRawItems(items, this._schema[ItemsSym]),
|
246
|
+
undefined,
|
247
|
+
"private",
|
248
|
+
);
|
247
249
|
|
248
250
|
return this._raw.entries().length;
|
249
251
|
}
|
@@ -161,18 +161,17 @@ export function loadCoValue<V extends CoValue, Depth>(
|
|
161
161
|
depth: Depth & DepthsIn<V>,
|
162
162
|
): Promise<DeeplyLoaded<V, Depth> | undefined> {
|
163
163
|
return new Promise((resolve) => {
|
164
|
-
|
164
|
+
subscribeToCoValue(
|
165
165
|
cls,
|
166
166
|
id,
|
167
167
|
as,
|
168
168
|
depth,
|
169
|
-
(value) => {
|
169
|
+
(value, unsubscribe) => {
|
170
170
|
resolve(value);
|
171
171
|
unsubscribe();
|
172
172
|
},
|
173
173
|
() => {
|
174
174
|
resolve(undefined);
|
175
|
-
unsubscribe();
|
176
175
|
},
|
177
176
|
);
|
178
177
|
});
|
@@ -195,37 +194,49 @@ export function subscribeToCoValue<V extends CoValue, Depth>(
|
|
195
194
|
id: ID<V>,
|
196
195
|
as: Account | AnonymousJazzAgent,
|
197
196
|
depth: Depth & DepthsIn<V>,
|
198
|
-
listener: (value: DeeplyLoaded<V, Depth
|
197
|
+
listener: (value: DeeplyLoaded<V, Depth>, unsubscribe: () => void) => void,
|
199
198
|
onUnavailable?: () => void,
|
199
|
+
syncResolution?: boolean,
|
200
200
|
): () => void {
|
201
201
|
const ref = new Ref(id, as, { ref: cls, optional: false });
|
202
202
|
|
203
203
|
let unsubscribed = false;
|
204
204
|
let unsubscribe: (() => void) | undefined;
|
205
205
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
}
|
222
|
-
|
206
|
+
function subscribe(value: V | undefined) {
|
207
|
+
if (!value) {
|
208
|
+
onUnavailable && onUnavailable();
|
209
|
+
return;
|
210
|
+
}
|
211
|
+
if (unsubscribed) return;
|
212
|
+
const subscription = new SubscriptionScope(
|
213
|
+
value,
|
214
|
+
cls as CoValueClass<V> & CoValueFromRaw<V>,
|
215
|
+
(update, subscription) => {
|
216
|
+
if (fulfillsDepth(depth, update)) {
|
217
|
+
listener(
|
218
|
+
update as DeeplyLoaded<V, Depth>,
|
219
|
+
subscription.unsubscribeAll,
|
220
|
+
);
|
221
|
+
}
|
222
|
+
},
|
223
|
+
);
|
223
224
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
225
|
+
unsubscribe = subscription.unsubscribeAll;
|
226
|
+
}
|
227
|
+
|
228
|
+
const sync = syncResolution ? ref.syncLoad() : undefined;
|
229
|
+
|
230
|
+
if (sync) {
|
231
|
+
subscribe(sync);
|
232
|
+
} else {
|
233
|
+
ref
|
234
|
+
.load()
|
235
|
+
.then((value) => subscribe(value))
|
236
|
+
.catch((e) => {
|
237
|
+
console.error("Failed to load / subscribe to CoValue", e);
|
238
|
+
});
|
239
|
+
}
|
229
240
|
|
230
241
|
return function unsubscribeAtAnyPoint() {
|
231
242
|
unsubscribed = true;
|
@@ -233,7 +244,9 @@ export function subscribeToCoValue<V extends CoValue, Depth>(
|
|
233
244
|
};
|
234
245
|
}
|
235
246
|
|
236
|
-
export function createCoValueObservable<V extends CoValue, Depth>(
|
247
|
+
export function createCoValueObservable<V extends CoValue, Depth>(options?: {
|
248
|
+
syncResolution?: boolean;
|
249
|
+
}) {
|
237
250
|
let currentValue: DeeplyLoaded<V, Depth> | undefined = undefined;
|
238
251
|
let subscriberCount = 0;
|
239
252
|
|
@@ -257,6 +270,7 @@ export function createCoValueObservable<V extends CoValue, Depth>() {
|
|
257
270
|
listener();
|
258
271
|
},
|
259
272
|
onUnavailable,
|
273
|
+
options?.syncResolution,
|
260
274
|
);
|
261
275
|
|
262
276
|
return () => {
|
@@ -55,6 +55,23 @@ export class Ref<out V extends CoValue> {
|
|
55
55
|
}
|
56
56
|
}
|
57
57
|
|
58
|
+
syncLoad(): V | undefined {
|
59
|
+
const node =
|
60
|
+
"node" in this.controlledAccount
|
61
|
+
? this.controlledAccount.node
|
62
|
+
: this.controlledAccount._raw.core.node;
|
63
|
+
|
64
|
+
const entry = node.coValuesStore.get(
|
65
|
+
this.id as unknown as CoID<RawCoValue>,
|
66
|
+
);
|
67
|
+
|
68
|
+
if (entry.state.type === "available") {
|
69
|
+
return new Ref(this.id, this.controlledAccount, this.schema).value!;
|
70
|
+
}
|
71
|
+
|
72
|
+
return undefined;
|
73
|
+
}
|
74
|
+
|
58
75
|
async load(): Promise<V | undefined> {
|
59
76
|
const result = await this.loadHelper();
|
60
77
|
if (result === "unavailable") {
|
@@ -37,7 +37,7 @@ export class SubscriptionScope<Root extends CoValue> {
|
|
37
37
|
constructor(
|
38
38
|
root: Root,
|
39
39
|
rootSchema: CoValueClass<Root> & CoValueFromRaw<Root>,
|
40
|
-
onUpdate: (newRoot: Root) => void,
|
40
|
+
onUpdate: (newRoot: Root, scope: SubscriptionScope<Root>) => void,
|
41
41
|
) {
|
42
42
|
this.rootEntry = {
|
43
43
|
state: "loaded" as const,
|
@@ -52,7 +52,7 @@ export class SubscriptionScope<Root extends CoValue> {
|
|
52
52
|
this.scheduleUpdate = () => {
|
53
53
|
const value = rootSchema.fromRaw(this.rootEntry.value) as Root;
|
54
54
|
subscriptionsScopes.set(value, this);
|
55
|
-
onUpdate(value);
|
55
|
+
onUpdate(value, this);
|
56
56
|
};
|
57
57
|
|
58
58
|
this.rootEntry.rawUnsub = root._raw.core.subscribe(
|
@@ -125,7 +125,7 @@ export class SubscriptionScope<Root extends CoValue> {
|
|
125
125
|
}
|
126
126
|
}
|
127
127
|
|
128
|
-
unsubscribeAll() {
|
128
|
+
unsubscribeAll = () => {
|
129
129
|
for (const entry of this.entries.values()) {
|
130
130
|
if (entry.state === "loaded") {
|
131
131
|
entry.rawUnsub();
|
@@ -134,5 +134,5 @@ export class SubscriptionScope<Root extends CoValue> {
|
|
134
134
|
}
|
135
135
|
}
|
136
136
|
this.entries.clear();
|
137
|
-
}
|
137
|
+
};
|
138
138
|
}
|
package/src/testing.ts
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
import { AgentSecret, CryptoProvider, Peer } from "cojson";
|
2
|
+
import { cojsonInternals } from "cojson";
|
3
|
+
import { PureJSCrypto } from "cojson/crypto";
|
4
|
+
import { Account, type AccountClass } from "./exports.js";
|
5
|
+
import {
|
6
|
+
type AnonymousJazzAgent,
|
7
|
+
type CoValueClass,
|
8
|
+
createAnonymousJazzContext,
|
9
|
+
} from "./internal.js";
|
10
|
+
|
11
|
+
type TestAccountSchema<Acc extends Account> = CoValueClass<Acc> & {
|
12
|
+
fromNode: (typeof Account)["fromNode"];
|
13
|
+
create: (options: {
|
14
|
+
creationProps: { name: string };
|
15
|
+
initialAgentSecret?: AgentSecret;
|
16
|
+
peersToLoadFrom?: Peer[];
|
17
|
+
crypto: CryptoProvider;
|
18
|
+
}) => Promise<Acc>;
|
19
|
+
};
|
20
|
+
|
21
|
+
class TestJSCrypto extends PureJSCrypto {
|
22
|
+
static async create() {
|
23
|
+
if ("navigator" in globalThis && navigator.userAgent.includes("jsdom")) {
|
24
|
+
// Mocking crypto seal & encrypt to make it work with JSDom. Getting "Error: Uint8Array expected" there
|
25
|
+
const crypto = new PureJSCrypto();
|
26
|
+
|
27
|
+
crypto.seal = (options) =>
|
28
|
+
`sealed_U${cojsonInternals.stableStringify(options.message)}` as any;
|
29
|
+
crypto.unseal = (sealed) =>
|
30
|
+
JSON.parse(sealed.substring("sealed_U".length));
|
31
|
+
crypto.encrypt = (message) =>
|
32
|
+
`encrypted_U${cojsonInternals.stableStringify(message)}` as any;
|
33
|
+
crypto.decryptRaw = (encrypted) =>
|
34
|
+
encrypted.substring("encrypted_U".length) as any;
|
35
|
+
|
36
|
+
return crypto;
|
37
|
+
}
|
38
|
+
|
39
|
+
// For non-jsdom environments, we use the real crypto
|
40
|
+
return new PureJSCrypto();
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
export async function createJazzTestAccount<Acc extends Account>(options?: {
|
45
|
+
AccountSchema?: CoValueClass<Acc>;
|
46
|
+
}): Promise<Acc> {
|
47
|
+
const AccountSchema = (options?.AccountSchema ??
|
48
|
+
Account) as unknown as TestAccountSchema<Acc>;
|
49
|
+
const account = await AccountSchema.create({
|
50
|
+
creationProps: {
|
51
|
+
name: "Test Account",
|
52
|
+
},
|
53
|
+
crypto: await TestJSCrypto.create(),
|
54
|
+
});
|
55
|
+
|
56
|
+
return account;
|
57
|
+
}
|
58
|
+
|
59
|
+
export async function createJazzTestGuest() {
|
60
|
+
const ctx = await createAnonymousJazzContext({
|
61
|
+
crypto: await PureJSCrypto.create(),
|
62
|
+
peersToLoadFrom: [],
|
63
|
+
});
|
64
|
+
|
65
|
+
return {
|
66
|
+
guest: ctx.agent,
|
67
|
+
};
|
68
|
+
}
|
69
|
+
|
70
|
+
export function getJazzContextShape<Acc extends Account>(
|
71
|
+
account: Acc | { guest: AnonymousJazzAgent },
|
72
|
+
) {
|
73
|
+
if ("guest" in account) {
|
74
|
+
return {
|
75
|
+
guest: account.guest,
|
76
|
+
AccountSchema: Account,
|
77
|
+
logOut: () => account.guest.node.gracefulShutdown(),
|
78
|
+
done: () => account.guest.node.gracefulShutdown(),
|
79
|
+
};
|
80
|
+
}
|
81
|
+
|
82
|
+
return {
|
83
|
+
me: account,
|
84
|
+
AccountSchema: account.constructor as AccountClass<Acc>,
|
85
|
+
logOut: () => account._raw.core.node.gracefulShutdown(),
|
86
|
+
done: () => account._raw.core.node.gracefulShutdown(),
|
87
|
+
};
|
88
|
+
}
|
89
|
+
|
90
|
+
export function linkAccounts(
|
91
|
+
a: Account,
|
92
|
+
b: Account,
|
93
|
+
aRole: "server" | "client" = "server",
|
94
|
+
bRole: "server" | "client" = "server",
|
95
|
+
) {
|
96
|
+
const [aPeer, bPeer] = cojsonInternals.connectedPeers(b.id, a.id, {
|
97
|
+
peer1role: aRole,
|
98
|
+
peer2role: bRole,
|
99
|
+
});
|
100
|
+
|
101
|
+
a._raw.core.node.syncManager.addPeer(aPeer);
|
102
|
+
b._raw.core.node.syncManager.addPeer(bPeer);
|
103
|
+
}
|
@@ -99,6 +99,7 @@ describe("SchemaUnion", () => {
|
|
99
99
|
{ type: "button", label: "Submit" },
|
100
100
|
{ owner: me },
|
101
101
|
);
|
102
|
+
let currentValue = "Submit";
|
102
103
|
const unsubscribe = subscribeToCoValue(
|
103
104
|
WidgetUnion,
|
104
105
|
buttonWidget.id,
|
@@ -106,13 +107,16 @@ describe("SchemaUnion", () => {
|
|
106
107
|
{},
|
107
108
|
(value: BaseWidget) => {
|
108
109
|
if (value instanceof ButtonWidget) {
|
109
|
-
expect(value.label).toBe(
|
110
|
-
unsubscribe();
|
110
|
+
expect(value.label).toBe(currentValue);
|
111
111
|
} else {
|
112
112
|
throw new Error("Unexpected widget type");
|
113
113
|
}
|
114
114
|
},
|
115
|
+
() => {},
|
116
|
+
true,
|
115
117
|
);
|
118
|
+
currentValue = "Changed";
|
116
119
|
buttonWidget.label = "Changed";
|
120
|
+
unsubscribe();
|
117
121
|
});
|
118
122
|
});
|
@@ -1,9 +1,15 @@
|
|
1
1
|
import { describe, expect, it, onTestFinished, vi } from "vitest";
|
2
|
-
import { Account, CoFeed, CoList, CoMap, co } from "../index.web.js";
|
3
2
|
import {
|
4
|
-
|
3
|
+
Account,
|
4
|
+
CoFeed,
|
5
|
+
CoList,
|
6
|
+
CoMap,
|
5
7
|
FileStream,
|
6
8
|
Group,
|
9
|
+
co,
|
10
|
+
} from "../index.web.js";
|
11
|
+
import {
|
12
|
+
type DepthsIn,
|
7
13
|
createCoValueObservable,
|
8
14
|
subscribeToCoValue,
|
9
15
|
} from "../internal.js";
|
@@ -64,6 +70,7 @@ describe("subscribeToCoValue", () => {
|
|
64
70
|
messages: null,
|
65
71
|
name: "General",
|
66
72
|
}),
|
73
|
+
expect.any(Function),
|
67
74
|
);
|
68
75
|
|
69
76
|
updateFn.mockClear();
|
@@ -78,6 +85,7 @@ describe("subscribeToCoValue", () => {
|
|
78
85
|
name: "General",
|
79
86
|
messages: expect.any(Array),
|
80
87
|
}),
|
88
|
+
expect.any(Function),
|
81
89
|
);
|
82
90
|
|
83
91
|
updateFn.mockClear();
|
@@ -93,6 +101,7 @@ describe("subscribeToCoValue", () => {
|
|
93
101
|
name: "Lounge",
|
94
102
|
messages: expect.any(Array),
|
95
103
|
}),
|
104
|
+
expect.any(Function),
|
96
105
|
);
|
97
106
|
});
|
98
107
|
|
@@ -125,6 +134,7 @@ describe("subscribeToCoValue", () => {
|
|
125
134
|
name: "General",
|
126
135
|
messages: expect.any(Array),
|
127
136
|
}),
|
137
|
+
expect.any(Function),
|
128
138
|
);
|
129
139
|
});
|
130
140
|
|
package/src/tests/utils.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { isControlledAccount } from "../coValues/account";
|
2
2
|
|
3
3
|
import { CoID, LocalNode, RawCoValue } from "cojson";
|
4
|
-
import {
|
4
|
+
import { cojsonInternals } from "cojson";
|
5
5
|
import {
|
6
6
|
Account,
|
7
7
|
WasmCrypto,
|
@@ -18,10 +18,14 @@ export async function setupAccount() {
|
|
18
18
|
crypto: Crypto,
|
19
19
|
});
|
20
20
|
|
21
|
-
const [initialAsPeer, secondPeer] = connectedPeers(
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
const [initialAsPeer, secondPeer] = cojsonInternals.connectedPeers(
|
22
|
+
"initial",
|
23
|
+
"second",
|
24
|
+
{
|
25
|
+
peer1role: "server",
|
26
|
+
peer2role: "client",
|
27
|
+
},
|
28
|
+
);
|
25
29
|
|
26
30
|
if (!isControlledAccount(me)) {
|
27
31
|
throw "me is not a controlled account";
|
@@ -41,7 +45,7 @@ export async function setupAccount() {
|
|
41
45
|
}
|
42
46
|
|
43
47
|
export async function setupTwoNodes() {
|
44
|
-
const [serverAsPeer, clientAsPeer] = connectedPeers(
|
48
|
+
const [serverAsPeer, clientAsPeer] = cojsonInternals.connectedPeers(
|
45
49
|
"clientToServer",
|
46
50
|
"serverToClient",
|
47
51
|
{
|