jazz-tools 0.8.15 → 0.8.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. package/CHANGELOG.md +276 -269
  2. package/dist/native/coValues/account.js +3 -6
  3. package/dist/native/coValues/account.js.map +1 -1
  4. package/dist/native/coValues/coList.js +3 -7
  5. package/dist/native/coValues/coList.js.map +1 -1
  6. package/dist/native/coValues/coMap.js +8 -14
  7. package/dist/native/coValues/coMap.js.map +1 -1
  8. package/dist/native/coValues/coStream.js +7 -12
  9. package/dist/native/coValues/coStream.js.map +1 -1
  10. package/dist/native/coValues/deepLoading.js +6 -3
  11. package/dist/native/coValues/deepLoading.js.map +1 -1
  12. package/dist/native/coValues/extensions/imageDef.js.map +1 -1
  13. package/dist/native/coValues/group.js +3 -6
  14. package/dist/native/coValues/group.js.map +1 -1
  15. package/dist/native/coValues/interfaces.js +4 -3
  16. package/dist/native/coValues/interfaces.js.map +1 -1
  17. package/dist/native/exports.js +3 -9
  18. package/dist/native/exports.js.map +1 -1
  19. package/dist/native/implementation/createContext.js +1 -2
  20. package/dist/native/implementation/createContext.js.map +1 -1
  21. package/dist/native/implementation/devtoolsFormatters.js +5 -25
  22. package/dist/native/implementation/devtoolsFormatters.js.map +1 -1
  23. package/dist/native/implementation/refs.js +1 -2
  24. package/dist/native/implementation/refs.js.map +1 -1
  25. package/dist/native/implementation/schema.js +1 -1
  26. package/dist/native/implementation/schema.js.map +1 -1
  27. package/dist/native/implementation/subscriptionScope.js +2 -4
  28. package/dist/native/implementation/subscriptionScope.js.map +1 -1
  29. package/dist/native/index.native.js +1 -1
  30. package/dist/native/index.native.js.map +1 -1
  31. package/dist/native/lib/cache.js.map +1 -1
  32. package/dist/native/lib/cache.test.js +1 -1
  33. package/dist/native/lib/cache.test.js.map +1 -1
  34. package/dist/web/coValues/account.js +3 -6
  35. package/dist/web/coValues/account.js.map +1 -1
  36. package/dist/web/coValues/coList.js +3 -7
  37. package/dist/web/coValues/coList.js.map +1 -1
  38. package/dist/web/coValues/coMap.js +8 -14
  39. package/dist/web/coValues/coMap.js.map +1 -1
  40. package/dist/web/coValues/coStream.js +7 -12
  41. package/dist/web/coValues/coStream.js.map +1 -1
  42. package/dist/web/coValues/deepLoading.js +6 -3
  43. package/dist/web/coValues/deepLoading.js.map +1 -1
  44. package/dist/web/coValues/extensions/imageDef.js.map +1 -1
  45. package/dist/web/coValues/group.js +3 -6
  46. package/dist/web/coValues/group.js.map +1 -1
  47. package/dist/web/coValues/interfaces.js +4 -3
  48. package/dist/web/coValues/interfaces.js.map +1 -1
  49. package/dist/web/exports.js +3 -9
  50. package/dist/web/exports.js.map +1 -1
  51. package/dist/web/implementation/createContext.js +1 -2
  52. package/dist/web/implementation/createContext.js.map +1 -1
  53. package/dist/web/implementation/devtoolsFormatters.js +5 -25
  54. package/dist/web/implementation/devtoolsFormatters.js.map +1 -1
  55. package/dist/web/implementation/refs.js +1 -2
  56. package/dist/web/implementation/refs.js.map +1 -1
  57. package/dist/web/implementation/schema.js +1 -1
  58. package/dist/web/implementation/schema.js.map +1 -1
  59. package/dist/web/implementation/subscriptionScope.js +2 -4
  60. package/dist/web/implementation/subscriptionScope.js.map +1 -1
  61. package/dist/web/lib/cache.js.map +1 -1
  62. package/dist/web/lib/cache.test.js +1 -1
  63. package/dist/web/lib/cache.test.js.map +1 -1
  64. package/package.json +5 -9
  65. package/src/coValues/account.ts +330 -339
  66. package/src/coValues/coList.ts +474 -495
  67. package/src/coValues/coMap.ts +584 -604
  68. package/src/coValues/coStream.ts +624 -650
  69. package/src/coValues/deepLoading.ts +184 -200
  70. package/src/coValues/extensions/imageDef.ts +44 -44
  71. package/src/coValues/group.ts +196 -210
  72. package/src/coValues/interfaces.ts +197 -199
  73. package/src/exports.ts +38 -26
  74. package/src/implementation/createContext.ts +206 -213
  75. package/src/implementation/devtoolsFormatters.ts +80 -100
  76. package/src/implementation/refs.ts +127 -139
  77. package/src/implementation/schema.ts +124 -128
  78. package/src/implementation/subscriptionScope.ts +111 -121
  79. package/src/index.native.ts +3 -3
  80. package/src/lib/cache.test.ts +48 -48
  81. package/src/lib/cache.ts +9 -9
  82. package/src/tests/coList.test.ts +264 -283
  83. package/src/tests/coMap.test.ts +741 -761
  84. package/src/tests/coStream.test.ts +405 -438
  85. package/src/tests/deepLoading.test.ts +251 -256
  86. package/src/tests/groupsAndAccounts.test.ts +70 -74
  87. package/src/tests/schema.test.ts +198 -198
  88. package/src/tests/subscribe.test.ts +312 -299
  89. package/tsconfig.json +2 -4
  90. package/tsconfig.native.json +4 -10
  91. package/tsconfig.web.json +4 -10
  92. package/.eslintrc.cjs +0 -24
  93. 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
- Account,
6
- CoList,
7
- CoMap,
8
- CoStream,
9
- WasmCrypto,
10
- co,
11
- isControlledAccount,
12
- createJazzContext,
13
- fixedCredentialsAuth,
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
- BinaryCoStream,
17
- Group,
18
- randomSessionProvider,
19
- subscribeToCoValue,
16
+ BinaryCoStream,
17
+ Group,
18
+ randomSessionProvider,
19
+ subscribeToCoValue,
20
20
  } from "../internal.js";
21
21
 
22
22
  class ChatRoom extends CoMap {
23
- messages = co.ref(MessagesList);
24
- name = co.string;
23
+ messages = co.ref(MessagesList);
24
+ name = co.string;
25
25
  }
26
26
 
27
27
  class Message extends CoMap {
28
- text = co.string;
29
- reactions = co.ref(ReactionsStream);
30
- attachment = co.optional.ref(BinaryCoStream);
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
- const me = await Account.create({
38
- creationProps: { name: "Hermes Puggington" },
39
- crypto: Crypto,
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
- const [initialAsPeer, secondPeer] = connectedPeers("initial", "second", {
43
- peer1role: "server",
44
- peer2role: "client",
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
- 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,
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
- return { me, meOnSecondPeer };
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
- function createChatRoom(me: Account | Group, name: string) {
65
- return ChatRoom.create(
66
- { messages: MessagesList.create([], { owner: me }), name },
67
- { owner: me },
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
- function createMessage(me: Account | Group, text: string) {
72
- return Message.create(
73
- { text, reactions: ReactionsStream.create([], { owner: me }) },
74
- { owner: me },
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
- 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();
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
- 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
- );
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
- it("should fire updates when a ref entity is updates", async () => {
170
- const { me, meOnSecondPeer } = await setupAccount();
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
- const chatRoom = createChatRoom(me, "General");
173
- const message = createMessage(me, "Hello Luigi, are you ready to save the princess?");
174
- chatRoom.messages?.push(message);
212
+ it("should handle the updates as immutable changes", async () => {
213
+ const { me, meOnSecondPeer } = await setupAccount();
175
214
 
176
- const updateFn = vi.fn()
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
- const unsubscribe = subscribeToCoValue(
179
- ChatRoom,
180
- chatRoom.id,
181
- meOnSecondPeer,
182
- {
183
- messages: [{
184
- }],
185
- },
186
- updateFn,
187
- );
240
+ onTestFinished(unsubscribe);
188
241
 
189
- onTestFinished(unsubscribe);
242
+ await waitFor(() => {
243
+ const lastValue = updateFn.mock.lastCall[0];
190
244
 
191
- await waitFor(() => {
192
- const lastValue = updateFn.mock.lastCall[0];
245
+ expect(lastValue?.messages?.[0]?.text).toBe(message.text);
246
+ });
193
247
 
194
- expect(lastValue?.messages?.[0]?.text).toBe(message.text);
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
- message.text = "Nevermind, she was gone to the supermarket";
198
- updateFn.mockClear();
254
+ message.reactions?.push("👍");
199
255
 
200
- await waitFor(() => {
201
- expect(updateFn).toHaveBeenCalled();
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
- it("should handle the updates as immutable changes", async () => {
209
- const { me, meOnSecondPeer } = await setupAccount();
210
-
211
- const chatRoom = createChatRoom(me, "General");
212
- const message = createMessage(me, "Hello Luigi, are you ready to save the princess?");
213
- const message2 = createMessage(me, "Let's go!");
214
- chatRoom.messages?.push(message);
215
- chatRoom.messages?.push(message2);
216
-
217
- const updateFn = vi.fn()
218
-
219
- const unsubscribe = subscribeToCoValue(
220
- ChatRoom,
221
- chatRoom.id,
222
- meOnSecondPeer,
223
- {
224
- messages: [{
225
- reactions: [],
226
- }],
227
- },
228
- updateFn,
229
- );
230
-
231
- onTestFinished(unsubscribe);
232
-
233
- await waitFor(() => {
234
- const lastValue = updateFn.mock.lastCall[0];
235
-
236
- expect(lastValue?.messages?.[0]?.text).toBe(message.text);
237
- });
238
-
239
- const initialValue = updateFn.mock.lastCall[0];
240
- const initialMessagesList = initialValue?.messages;
241
- const initialMessage1 = initialValue?.messages[0];
242
- const initialMessage2 = initialValue?.messages[1];
243
- const initialMessageReactions = initialValue?.messages[0].reactions;
244
-
245
- message.reactions?.push("👍");
246
-
247
- updateFn.mockClear();
248
-
249
- await waitFor(() => {
250
- expect(updateFn).toHaveBeenCalled();
251
- });
252
-
253
- const lastValue = updateFn.mock.lastCall[0];
254
- expect(lastValue).not.toBe(initialValue);
255
- expect(lastValue.messages).not.toBe(initialMessagesList);
256
- expect(lastValue.messages[0]).not.toBe(initialMessage1);
257
- expect(lastValue.messages[0].reactions).not.toBe(initialMessageReactions);
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
- it("should keep the same identity on the ref entities when a property is updated", async () => {
270
- const { me, meOnSecondPeer } = await setupAccount();
271
-
272
- const chatRoom = createChatRoom(me, "General");
273
- const message = createMessage(me, "Hello Luigi, are you ready to save the princess?");
274
- const message2 = createMessage(me, "Let's go!");
275
- chatRoom.messages?.push(message);
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
- return new Promise<void>((resolve, reject) => {
324
- const checkPassed = () => {
325
- try {
326
- return { ok: callback(), error: null };
327
- } catch (error) {
328
- return { ok: false, error };
329
- }
330
- };
331
-
332
- let retries = 0;
333
-
334
- const interval = setInterval(() => {
335
- const { ok, error } = checkPassed();
336
-
337
- if (ok !== false) {
338
- clearInterval(interval);
339
- resolve();
340
- }
341
-
342
- if (++retries > 10) {
343
- clearInterval(interval);
344
- reject(error);
345
- }
346
- }, 100);
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
@@ -1,8 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "lib": [
4
- "ESNext"
5
- ],
3
+ "lib": ["ESNext"],
6
4
  "module": "esnext",
7
5
  "target": "es2022",
8
6
  "moduleResolution": "bundler",
@@ -13,4 +11,4 @@
13
11
  "noUncheckedIndexedAccess": true,
14
12
  "esModuleInterop": true
15
13
  }
16
- }
14
+ }