jazz-tools 0.8.15 → 0.8.17
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +283 -269
- package/dist/native/coValues/account.js +3 -6
- package/dist/native/coValues/account.js.map +1 -1
- package/dist/native/coValues/coList.js +3 -7
- package/dist/native/coValues/coList.js.map +1 -1
- package/dist/native/coValues/coMap.js +8 -14
- package/dist/native/coValues/coMap.js.map +1 -1
- package/dist/native/coValues/coStream.js +7 -12
- package/dist/native/coValues/coStream.js.map +1 -1
- package/dist/native/coValues/deepLoading.js +6 -3
- package/dist/native/coValues/deepLoading.js.map +1 -1
- package/dist/native/coValues/extensions/imageDef.js.map +1 -1
- package/dist/native/coValues/group.js +3 -6
- package/dist/native/coValues/group.js.map +1 -1
- package/dist/native/coValues/interfaces.js +4 -3
- package/dist/native/coValues/interfaces.js.map +1 -1
- package/dist/native/exports.js +3 -9
- package/dist/native/exports.js.map +1 -1
- package/dist/native/implementation/createContext.js +1 -2
- package/dist/native/implementation/createContext.js.map +1 -1
- package/dist/native/implementation/devtoolsFormatters.js +5 -25
- package/dist/native/implementation/devtoolsFormatters.js.map +1 -1
- package/dist/native/implementation/refs.js +1 -2
- package/dist/native/implementation/refs.js.map +1 -1
- package/dist/native/implementation/schema.js +1 -1
- package/dist/native/implementation/schema.js.map +1 -1
- package/dist/native/implementation/subscriptionScope.js +2 -4
- package/dist/native/implementation/subscriptionScope.js.map +1 -1
- package/dist/native/index.native.js +1 -1
- package/dist/native/index.native.js.map +1 -1
- package/dist/native/lib/cache.js.map +1 -1
- package/dist/native/lib/cache.test.js +1 -1
- package/dist/native/lib/cache.test.js.map +1 -1
- package/dist/web/coValues/account.js +3 -6
- package/dist/web/coValues/account.js.map +1 -1
- package/dist/web/coValues/coList.js +3 -7
- package/dist/web/coValues/coList.js.map +1 -1
- package/dist/web/coValues/coMap.js +8 -14
- package/dist/web/coValues/coMap.js.map +1 -1
- package/dist/web/coValues/coStream.js +7 -12
- package/dist/web/coValues/coStream.js.map +1 -1
- package/dist/web/coValues/deepLoading.js +6 -3
- package/dist/web/coValues/deepLoading.js.map +1 -1
- package/dist/web/coValues/extensions/imageDef.js.map +1 -1
- package/dist/web/coValues/group.js +3 -6
- package/dist/web/coValues/group.js.map +1 -1
- package/dist/web/coValues/interfaces.js +4 -3
- package/dist/web/coValues/interfaces.js.map +1 -1
- package/dist/web/exports.js +3 -9
- package/dist/web/exports.js.map +1 -1
- package/dist/web/implementation/createContext.js +1 -2
- package/dist/web/implementation/createContext.js.map +1 -1
- package/dist/web/implementation/devtoolsFormatters.js +5 -25
- package/dist/web/implementation/devtoolsFormatters.js.map +1 -1
- package/dist/web/implementation/refs.js +1 -2
- package/dist/web/implementation/refs.js.map +1 -1
- package/dist/web/implementation/schema.js +1 -1
- package/dist/web/implementation/schema.js.map +1 -1
- package/dist/web/implementation/subscriptionScope.js +2 -4
- package/dist/web/implementation/subscriptionScope.js.map +1 -1
- package/dist/web/lib/cache.js.map +1 -1
- package/dist/web/lib/cache.test.js +1 -1
- package/dist/web/lib/cache.test.js.map +1 -1
- package/package.json +5 -9
- package/src/coValues/account.ts +330 -339
- package/src/coValues/coList.ts +474 -495
- package/src/coValues/coMap.ts +584 -604
- package/src/coValues/coStream.ts +624 -650
- package/src/coValues/deepLoading.ts +184 -200
- package/src/coValues/extensions/imageDef.ts +44 -44
- package/src/coValues/group.ts +196 -210
- package/src/coValues/interfaces.ts +197 -199
- package/src/exports.ts +38 -26
- package/src/implementation/createContext.ts +206 -213
- package/src/implementation/devtoolsFormatters.ts +80 -100
- package/src/implementation/refs.ts +127 -139
- package/src/implementation/schema.ts +124 -128
- package/src/implementation/subscriptionScope.ts +111 -121
- package/src/index.native.ts +3 -3
- package/src/lib/cache.test.ts +48 -48
- package/src/lib/cache.ts +9 -9
- package/src/tests/coList.test.ts +264 -283
- package/src/tests/coMap.test.ts +741 -761
- package/src/tests/coStream.test.ts +405 -438
- package/src/tests/deepLoading.test.ts +251 -256
- package/src/tests/groupsAndAccounts.test.ts +70 -74
- package/src/tests/schema.test.ts +198 -198
- package/src/tests/subscribe.test.ts +312 -299
- package/tsconfig.json +2 -4
- package/tsconfig.native.json +4 -10
- package/tsconfig.web.json +4 -10
- package/.eslintrc.cjs +0 -24
- package/.prettierrc.js +0 -9
@@ -1,348 +1,361 @@
|
|
1
1
|
const Crypto = await WasmCrypto.create();
|
2
|
-
import { expect, describe, it, vi, onTestFinished } from "vitest";
|
3
2
|
import { connectedPeers } from "cojson/src/streamUtils.js";
|
3
|
+
import { describe, expect, it, onTestFinished, vi } from "vitest";
|
4
4
|
import {
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
Account,
|
6
|
+
CoList,
|
7
|
+
CoMap,
|
8
|
+
CoStream,
|
9
|
+
WasmCrypto,
|
10
|
+
co,
|
11
|
+
createJazzContext,
|
12
|
+
fixedCredentialsAuth,
|
13
|
+
isControlledAccount,
|
14
14
|
} from "../index.web.js";
|
15
15
|
import {
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
BinaryCoStream,
|
17
|
+
Group,
|
18
|
+
randomSessionProvider,
|
19
|
+
subscribeToCoValue,
|
20
20
|
} from "../internal.js";
|
21
21
|
|
22
22
|
class ChatRoom extends CoMap {
|
23
|
-
|
24
|
-
|
23
|
+
messages = co.ref(MessagesList);
|
24
|
+
name = co.string;
|
25
25
|
}
|
26
26
|
|
27
27
|
class Message extends CoMap {
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
text = co.string;
|
29
|
+
reactions = co.ref(ReactionsStream);
|
30
|
+
attachment = co.optional.ref(BinaryCoStream);
|
31
31
|
}
|
32
32
|
|
33
33
|
class MessagesList extends CoList.Of(co.ref(Message)) {}
|
34
34
|
class ReactionsStream extends CoStream.Of(co.string) {}
|
35
35
|
|
36
36
|
async function setupAccount() {
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
const me = await Account.create({
|
38
|
+
creationProps: { name: "Hermes Puggington" },
|
39
|
+
crypto: Crypto,
|
40
|
+
});
|
41
|
+
|
42
|
+
const [initialAsPeer, secondPeer] = connectedPeers("initial", "second", {
|
43
|
+
peer1role: "server",
|
44
|
+
peer2role: "client",
|
45
|
+
});
|
46
|
+
|
47
|
+
if (!isControlledAccount(me)) {
|
48
|
+
throw "me is not a controlled account";
|
49
|
+
}
|
50
|
+
me._raw.core.node.syncManager.addPeer(secondPeer);
|
51
|
+
const { account: meOnSecondPeer } = await createJazzContext({
|
52
|
+
auth: fixedCredentialsAuth({
|
53
|
+
accountID: me.id,
|
54
|
+
secret: me._raw.agentSecret,
|
55
|
+
}),
|
56
|
+
sessionProvider: randomSessionProvider,
|
57
|
+
peersToLoadFrom: [initialAsPeer],
|
58
|
+
crypto: Crypto,
|
59
|
+
});
|
60
|
+
|
61
|
+
return { me, meOnSecondPeer };
|
62
|
+
}
|
63
|
+
|
64
|
+
function createChatRoom(me: Account | Group, name: string) {
|
65
|
+
return ChatRoom.create(
|
66
|
+
{ messages: MessagesList.create([], { owner: me }), name },
|
67
|
+
{ owner: me },
|
68
|
+
);
|
69
|
+
}
|
70
|
+
|
71
|
+
function createMessage(me: Account | Group, text: string) {
|
72
|
+
return Message.create(
|
73
|
+
{ text, reactions: ReactionsStream.create([], { owner: me }) },
|
74
|
+
{ owner: me },
|
75
|
+
);
|
76
|
+
}
|
77
|
+
|
78
|
+
describe("subscribeToCoValue", () => {
|
79
|
+
it("subscribes to a CoMap", async () => {
|
80
|
+
const { me, meOnSecondPeer } = await setupAccount();
|
81
|
+
|
82
|
+
const chatRoom = createChatRoom(me, "General");
|
83
|
+
const updateFn = vi.fn();
|
84
|
+
|
85
|
+
const unsubscribe = subscribeToCoValue(
|
86
|
+
ChatRoom,
|
87
|
+
chatRoom.id,
|
88
|
+
meOnSecondPeer,
|
89
|
+
{},
|
90
|
+
updateFn,
|
91
|
+
);
|
92
|
+
|
93
|
+
onTestFinished(unsubscribe);
|
94
|
+
|
95
|
+
await waitFor(() => {
|
96
|
+
expect(updateFn).toHaveBeenCalled();
|
40
97
|
});
|
41
98
|
|
42
|
-
|
43
|
-
|
44
|
-
|
99
|
+
expect(updateFn).toHaveBeenCalledWith(
|
100
|
+
expect.objectContaining({
|
101
|
+
id: chatRoom.id,
|
102
|
+
messages: null,
|
103
|
+
name: "General",
|
104
|
+
}),
|
105
|
+
);
|
106
|
+
|
107
|
+
updateFn.mockClear();
|
108
|
+
|
109
|
+
await waitFor(() => {
|
110
|
+
expect(updateFn).toHaveBeenCalled();
|
45
111
|
});
|
46
112
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
113
|
+
expect(updateFn).toHaveBeenCalledWith(
|
114
|
+
expect.objectContaining({
|
115
|
+
id: chatRoom.id,
|
116
|
+
name: "General",
|
117
|
+
messages: expect.any(Array),
|
118
|
+
}),
|
119
|
+
);
|
120
|
+
|
121
|
+
updateFn.mockClear();
|
122
|
+
chatRoom.name = "Lounge";
|
123
|
+
|
124
|
+
await waitFor(() => {
|
125
|
+
expect(updateFn).toHaveBeenCalled();
|
59
126
|
});
|
60
127
|
|
61
|
-
|
62
|
-
|
128
|
+
expect(updateFn).toHaveBeenCalledWith(
|
129
|
+
expect.objectContaining({
|
130
|
+
id: chatRoom.id,
|
131
|
+
name: "Lounge",
|
132
|
+
messages: expect.any(Array),
|
133
|
+
}),
|
134
|
+
);
|
135
|
+
});
|
136
|
+
|
137
|
+
it("shouldn't fire updates until the declared load depth isn't reached", async () => {
|
138
|
+
const { me, meOnSecondPeer } = await setupAccount();
|
139
|
+
|
140
|
+
const chatRoom = createChatRoom(me, "General");
|
141
|
+
const updateFn = vi.fn();
|
142
|
+
|
143
|
+
const unsubscribe = subscribeToCoValue(
|
144
|
+
ChatRoom,
|
145
|
+
chatRoom.id,
|
146
|
+
meOnSecondPeer,
|
147
|
+
{
|
148
|
+
messages: [],
|
149
|
+
},
|
150
|
+
updateFn,
|
151
|
+
);
|
63
152
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
153
|
+
onTestFinished(unsubscribe);
|
154
|
+
|
155
|
+
await waitFor(() => {
|
156
|
+
expect(updateFn).toHaveBeenCalled();
|
157
|
+
});
|
158
|
+
|
159
|
+
expect(updateFn).toHaveBeenCalledTimes(1);
|
160
|
+
expect(updateFn).toHaveBeenCalledWith(
|
161
|
+
expect.objectContaining({
|
162
|
+
id: chatRoom.id,
|
163
|
+
name: "General",
|
164
|
+
messages: expect.any(Array),
|
165
|
+
}),
|
68
166
|
);
|
69
|
-
}
|
167
|
+
});
|
70
168
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
169
|
+
it("should fire updates when a ref entity is updates", async () => {
|
170
|
+
const { me, meOnSecondPeer } = await setupAccount();
|
171
|
+
|
172
|
+
const chatRoom = createChatRoom(me, "General");
|
173
|
+
const message = createMessage(
|
174
|
+
me,
|
175
|
+
"Hello Luigi, are you ready to save the princess?",
|
176
|
+
);
|
177
|
+
chatRoom.messages?.push(message);
|
178
|
+
|
179
|
+
const updateFn = vi.fn();
|
180
|
+
|
181
|
+
const unsubscribe = subscribeToCoValue(
|
182
|
+
ChatRoom,
|
183
|
+
chatRoom.id,
|
184
|
+
meOnSecondPeer,
|
185
|
+
{
|
186
|
+
messages: [{}],
|
187
|
+
},
|
188
|
+
updateFn,
|
75
189
|
);
|
76
|
-
}
|
77
190
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
const unsubscribe = subscribeToCoValue(
|
86
|
-
ChatRoom,
|
87
|
-
chatRoom.id,
|
88
|
-
meOnSecondPeer,
|
89
|
-
{},
|
90
|
-
updateFn,
|
91
|
-
);
|
92
|
-
|
93
|
-
onTestFinished(unsubscribe);
|
94
|
-
|
95
|
-
await waitFor(() => {
|
96
|
-
expect(updateFn).toHaveBeenCalled();
|
97
|
-
});
|
98
|
-
|
99
|
-
expect(updateFn).toHaveBeenCalledWith(
|
100
|
-
expect.objectContaining({
|
101
|
-
id: chatRoom.id,
|
102
|
-
messages: null,
|
103
|
-
name: "General",
|
104
|
-
}),
|
105
|
-
);
|
106
|
-
|
107
|
-
updateFn.mockClear();
|
108
|
-
|
109
|
-
await waitFor(() => {
|
110
|
-
expect(updateFn).toHaveBeenCalled();
|
111
|
-
});
|
112
|
-
|
113
|
-
expect(updateFn).toHaveBeenCalledWith(
|
114
|
-
expect.objectContaining({
|
115
|
-
id: chatRoom.id,
|
116
|
-
name: "General",
|
117
|
-
messages: expect.any(Array),
|
118
|
-
}),
|
119
|
-
);
|
120
|
-
|
121
|
-
updateFn.mockClear();
|
122
|
-
chatRoom.name = "Lounge";
|
123
|
-
|
124
|
-
await waitFor(() => {
|
125
|
-
expect(updateFn).toHaveBeenCalled();
|
126
|
-
});
|
127
|
-
|
128
|
-
expect(updateFn).toHaveBeenCalledWith(
|
129
|
-
expect.objectContaining({
|
130
|
-
id: chatRoom.id,
|
131
|
-
name: "Lounge",
|
132
|
-
messages: expect.any(Array),
|
133
|
-
}),
|
134
|
-
);
|
191
|
+
onTestFinished(unsubscribe);
|
192
|
+
|
193
|
+
await waitFor(() => {
|
194
|
+
const lastValue = updateFn.mock.lastCall[0];
|
195
|
+
|
196
|
+
expect(lastValue?.messages?.[0]?.text).toBe(message.text);
|
135
197
|
});
|
136
198
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
const unsubscribe = subscribeToCoValue(
|
144
|
-
ChatRoom,
|
145
|
-
chatRoom.id,
|
146
|
-
meOnSecondPeer,
|
147
|
-
{
|
148
|
-
messages: [],
|
149
|
-
},
|
150
|
-
updateFn,
|
151
|
-
);
|
152
|
-
|
153
|
-
onTestFinished(unsubscribe);
|
154
|
-
|
155
|
-
await waitFor(() => {
|
156
|
-
expect(updateFn).toHaveBeenCalled();
|
157
|
-
});
|
158
|
-
|
159
|
-
expect(updateFn).toHaveBeenCalledTimes(1);
|
160
|
-
expect(updateFn).toHaveBeenCalledWith(
|
161
|
-
expect.objectContaining({
|
162
|
-
id: chatRoom.id,
|
163
|
-
name: "General",
|
164
|
-
messages: expect.any(Array),
|
165
|
-
}),
|
166
|
-
);
|
199
|
+
message.text = "Nevermind, she was gone to the supermarket";
|
200
|
+
updateFn.mockClear();
|
201
|
+
|
202
|
+
await waitFor(() => {
|
203
|
+
expect(updateFn).toHaveBeenCalled();
|
167
204
|
});
|
168
205
|
|
169
|
-
|
170
|
-
|
206
|
+
const lastValue = updateFn.mock.lastCall[0];
|
207
|
+
expect(lastValue?.messages?.[0]?.text).toBe(
|
208
|
+
"Nevermind, she was gone to the supermarket",
|
209
|
+
);
|
210
|
+
});
|
171
211
|
|
172
|
-
|
173
|
-
|
174
|
-
chatRoom.messages?.push(message);
|
212
|
+
it("should handle the updates as immutable changes", async () => {
|
213
|
+
const { me, meOnSecondPeer } = await setupAccount();
|
175
214
|
|
176
|
-
|
215
|
+
const chatRoom = createChatRoom(me, "General");
|
216
|
+
const message = createMessage(
|
217
|
+
me,
|
218
|
+
"Hello Luigi, are you ready to save the princess?",
|
219
|
+
);
|
220
|
+
const message2 = createMessage(me, "Let's go!");
|
221
|
+
chatRoom.messages?.push(message);
|
222
|
+
chatRoom.messages?.push(message2);
|
223
|
+
|
224
|
+
const updateFn = vi.fn();
|
225
|
+
|
226
|
+
const unsubscribe = subscribeToCoValue(
|
227
|
+
ChatRoom,
|
228
|
+
chatRoom.id,
|
229
|
+
meOnSecondPeer,
|
230
|
+
{
|
231
|
+
messages: [
|
232
|
+
{
|
233
|
+
reactions: [],
|
234
|
+
},
|
235
|
+
],
|
236
|
+
},
|
237
|
+
updateFn,
|
238
|
+
);
|
177
239
|
|
178
|
-
|
179
|
-
ChatRoom,
|
180
|
-
chatRoom.id,
|
181
|
-
meOnSecondPeer,
|
182
|
-
{
|
183
|
-
messages: [{
|
184
|
-
}],
|
185
|
-
},
|
186
|
-
updateFn,
|
187
|
-
);
|
240
|
+
onTestFinished(unsubscribe);
|
188
241
|
|
189
|
-
|
242
|
+
await waitFor(() => {
|
243
|
+
const lastValue = updateFn.mock.lastCall[0];
|
190
244
|
|
191
|
-
|
192
|
-
|
245
|
+
expect(lastValue?.messages?.[0]?.text).toBe(message.text);
|
246
|
+
});
|
193
247
|
|
194
|
-
|
195
|
-
|
248
|
+
const initialValue = updateFn.mock.lastCall[0];
|
249
|
+
const initialMessagesList = initialValue?.messages;
|
250
|
+
const initialMessage1 = initialValue?.messages[0];
|
251
|
+
const initialMessage2 = initialValue?.messages[1];
|
252
|
+
const initialMessageReactions = initialValue?.messages[0].reactions;
|
196
253
|
|
197
|
-
|
198
|
-
updateFn.mockClear();
|
254
|
+
message.reactions?.push("👍");
|
199
255
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
const lastValue = updateFn.mock.lastCall[0];
|
205
|
-
expect(lastValue?.messages?.[0]?.text).toBe("Nevermind, she was gone to the supermarket");
|
256
|
+
updateFn.mockClear();
|
257
|
+
|
258
|
+
await waitFor(() => {
|
259
|
+
expect(updateFn).toHaveBeenCalled();
|
206
260
|
});
|
207
261
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
// This shouldn't change
|
260
|
-
expect(lastValue.messages[1]).toBe(initialMessage2);
|
261
|
-
|
262
|
-
// TODO: The initial should point at that snapshot in time
|
263
|
-
// expect(lastValue.messages).not.toBe(initialValue.messages);
|
264
|
-
// expect(lastValue.messages[0]).not.toBe(initialValue.messages[0]);
|
265
|
-
// expect(lastValue.messages[1]).toBe(initialValue.messages[1]);
|
266
|
-
// expect(lastValue.messages[0].reactions).not.toBe(initialValue.messages[0].reactions);
|
262
|
+
const lastValue = updateFn.mock.lastCall[0];
|
263
|
+
expect(lastValue).not.toBe(initialValue);
|
264
|
+
expect(lastValue.messages).not.toBe(initialMessagesList);
|
265
|
+
expect(lastValue.messages[0]).not.toBe(initialMessage1);
|
266
|
+
expect(lastValue.messages[0].reactions).not.toBe(initialMessageReactions);
|
267
|
+
|
268
|
+
// This shouldn't change
|
269
|
+
expect(lastValue.messages[1]).toBe(initialMessage2);
|
270
|
+
|
271
|
+
// TODO: The initial should point at that snapshot in time
|
272
|
+
// expect(lastValue.messages).not.toBe(initialValue.messages);
|
273
|
+
// expect(lastValue.messages[0]).not.toBe(initialValue.messages[0]);
|
274
|
+
// expect(lastValue.messages[1]).toBe(initialValue.messages[1]);
|
275
|
+
// expect(lastValue.messages[0].reactions).not.toBe(initialValue.messages[0].reactions);
|
276
|
+
});
|
277
|
+
|
278
|
+
it("should keep the same identity on the ref entities when a property is updated", async () => {
|
279
|
+
const { me, meOnSecondPeer } = await setupAccount();
|
280
|
+
|
281
|
+
const chatRoom = createChatRoom(me, "General");
|
282
|
+
const message = createMessage(
|
283
|
+
me,
|
284
|
+
"Hello Luigi, are you ready to save the princess?",
|
285
|
+
);
|
286
|
+
const message2 = createMessage(me, "Let's go!");
|
287
|
+
chatRoom.messages?.push(message);
|
288
|
+
chatRoom.messages?.push(message2);
|
289
|
+
|
290
|
+
const updateFn = vi.fn();
|
291
|
+
|
292
|
+
const unsubscribe = subscribeToCoValue(
|
293
|
+
ChatRoom,
|
294
|
+
chatRoom.id,
|
295
|
+
meOnSecondPeer,
|
296
|
+
{
|
297
|
+
messages: [
|
298
|
+
{
|
299
|
+
reactions: [],
|
300
|
+
},
|
301
|
+
],
|
302
|
+
},
|
303
|
+
updateFn,
|
304
|
+
);
|
305
|
+
|
306
|
+
onTestFinished(unsubscribe);
|
307
|
+
|
308
|
+
await waitFor(() => {
|
309
|
+
const lastValue = updateFn.mock.lastCall[0];
|
310
|
+
|
311
|
+
expect(lastValue?.messages?.[0]?.text).toBe(message.text);
|
312
|
+
expect(lastValue?.messages?.[1]?.text).toBe(message2.text);
|
267
313
|
});
|
268
314
|
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
chatRoom.messages?.push(message2);
|
277
|
-
|
278
|
-
const updateFn = vi.fn()
|
279
|
-
|
280
|
-
const unsubscribe = subscribeToCoValue(
|
281
|
-
ChatRoom,
|
282
|
-
chatRoom.id,
|
283
|
-
meOnSecondPeer,
|
284
|
-
{
|
285
|
-
messages: [{
|
286
|
-
reactions: [],
|
287
|
-
}],
|
288
|
-
},
|
289
|
-
updateFn,
|
290
|
-
);
|
291
|
-
|
292
|
-
onTestFinished(unsubscribe);
|
293
|
-
|
294
|
-
await waitFor(() => {
|
295
|
-
const lastValue = updateFn.mock.lastCall[0];
|
296
|
-
|
297
|
-
expect(lastValue?.messages?.[0]?.text).toBe(message.text);
|
298
|
-
expect(lastValue?.messages?.[1]?.text).toBe(message2.text);
|
299
|
-
});
|
300
|
-
|
301
|
-
const initialValue = updateFn.mock.lastCall[0];
|
302
|
-
chatRoom.name = "Me and Luigi";
|
303
|
-
|
304
|
-
updateFn.mockClear();
|
305
|
-
|
306
|
-
await waitFor(() => {
|
307
|
-
expect(updateFn).toHaveBeenCalled();
|
308
|
-
});
|
309
|
-
|
310
|
-
const lastValue = updateFn.mock.lastCall[0];
|
311
|
-
expect(lastValue).not.toBe(initialValue);
|
312
|
-
expect(lastValue.name).toBe("Me and Luigi");
|
313
|
-
expect(initialValue.name).toBe("General");
|
314
|
-
|
315
|
-
expect(lastValue.messages).toBe(initialValue.messages);
|
316
|
-
expect(lastValue.messages[0]).toBe(initialValue.messages[0]);
|
317
|
-
expect(lastValue.messages[1]).toBe(initialValue.messages[1]);
|
315
|
+
const initialValue = updateFn.mock.lastCall[0];
|
316
|
+
chatRoom.name = "Me and Luigi";
|
317
|
+
|
318
|
+
updateFn.mockClear();
|
319
|
+
|
320
|
+
await waitFor(() => {
|
321
|
+
expect(updateFn).toHaveBeenCalled();
|
318
322
|
});
|
319
|
-
});
|
320
323
|
|
324
|
+
const lastValue = updateFn.mock.lastCall[0];
|
325
|
+
expect(lastValue).not.toBe(initialValue);
|
326
|
+
expect(lastValue.name).toBe("Me and Luigi");
|
327
|
+
expect(initialValue.name).toBe("General");
|
328
|
+
|
329
|
+
expect(lastValue.messages).toBe(initialValue.messages);
|
330
|
+
expect(lastValue.messages[0]).toBe(initialValue.messages[0]);
|
331
|
+
expect(lastValue.messages[1]).toBe(initialValue.messages[1]);
|
332
|
+
});
|
333
|
+
});
|
321
334
|
|
322
335
|
function waitFor(callback: () => boolean | void) {
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
336
|
+
return new Promise<void>((resolve, reject) => {
|
337
|
+
const checkPassed = () => {
|
338
|
+
try {
|
339
|
+
return { ok: callback(), error: null };
|
340
|
+
} catch (error) {
|
341
|
+
return { ok: false, error };
|
342
|
+
}
|
343
|
+
};
|
344
|
+
|
345
|
+
let retries = 0;
|
346
|
+
|
347
|
+
const interval = setInterval(() => {
|
348
|
+
const { ok, error } = checkPassed();
|
349
|
+
|
350
|
+
if (ok !== false) {
|
351
|
+
clearInterval(interval);
|
352
|
+
resolve();
|
353
|
+
}
|
354
|
+
|
355
|
+
if (++retries > 10) {
|
356
|
+
clearInterval(interval);
|
357
|
+
reject(error);
|
358
|
+
}
|
359
|
+
}, 100);
|
360
|
+
});
|
348
361
|
}
|
package/tsconfig.json
CHANGED