@nice2dev/ui-graphics 1.0.4 → 1.0.5

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.
Files changed (183) hide show
  1. package/dist/cjs/animation/AnimationEditor.js +1 -1
  2. package/dist/cjs/animation/Audience.js +1 -1
  3. package/dist/cjs/core/LocalUI.js +1 -1
  4. package/dist/cjs/core/yjsCollaboration.js +2 -2
  5. package/dist/cjs/core/yjsCollaboration.js.map +1 -1
  6. package/dist/cjs/game/GameAsset2dEditor.js +1 -1
  7. package/dist/cjs/node_modules/lib0/array.js +103 -0
  8. package/dist/cjs/node_modules/lib0/array.js.map +1 -0
  9. package/dist/cjs/node_modules/lib0/binary.js +42 -0
  10. package/dist/cjs/node_modules/lib0/binary.js.map +1 -0
  11. package/dist/cjs/node_modules/lib0/broadcastchannel.js +121 -0
  12. package/dist/cjs/node_modules/lib0/broadcastchannel.js.map +1 -0
  13. package/dist/cjs/node_modules/lib0/buffer.js +103 -0
  14. package/dist/cjs/node_modules/lib0/buffer.js.map +1 -0
  15. package/dist/cjs/node_modules/lib0/conditions.js +18 -0
  16. package/dist/cjs/node_modules/lib0/conditions.js.map +1 -0
  17. package/dist/cjs/node_modules/lib0/decoding.js +460 -0
  18. package/dist/cjs/node_modules/lib0/decoding.js.map +1 -0
  19. package/dist/cjs/node_modules/lib0/dom.js +55 -0
  20. package/dist/cjs/node_modules/lib0/dom.js.map +1 -0
  21. package/dist/cjs/node_modules/lib0/encoding.js +688 -0
  22. package/dist/cjs/node_modules/lib0/encoding.js.map +1 -0
  23. package/dist/cjs/node_modules/lib0/environment.js +129 -0
  24. package/dist/cjs/node_modules/lib0/environment.js.map +1 -0
  25. package/dist/cjs/node_modules/lib0/error.js +37 -0
  26. package/dist/cjs/node_modules/lib0/error.js.map +1 -0
  27. package/dist/cjs/node_modules/lib0/function.js +135 -0
  28. package/dist/cjs/node_modules/lib0/function.js.map +1 -0
  29. package/dist/cjs/node_modules/lib0/iterator.js +52 -0
  30. package/dist/cjs/node_modules/lib0/iterator.js.map +1 -0
  31. package/dist/cjs/node_modules/lib0/logging.common.js +63 -0
  32. package/dist/cjs/node_modules/lib0/logging.common.js.map +1 -0
  33. package/dist/cjs/node_modules/lib0/logging.js +128 -0
  34. package/dist/cjs/node_modules/lib0/logging.js.map +1 -0
  35. package/dist/cjs/node_modules/lib0/map.js +108 -0
  36. package/dist/cjs/node_modules/lib0/map.js.map +1 -0
  37. package/dist/cjs/node_modules/lib0/math.js +44 -0
  38. package/dist/cjs/node_modules/lib0/math.js.map +1 -0
  39. package/dist/cjs/node_modules/lib0/number.js +21 -0
  40. package/dist/cjs/node_modules/lib0/number.js.map +1 -0
  41. package/dist/cjs/node_modules/lib0/object.js +132 -0
  42. package/dist/cjs/node_modules/lib0/object.js.map +1 -0
  43. package/dist/cjs/node_modules/lib0/observable.js +168 -0
  44. package/dist/cjs/node_modules/lib0/observable.js.map +1 -0
  45. package/dist/cjs/node_modules/lib0/pair.js +33 -0
  46. package/dist/cjs/node_modules/lib0/pair.js.map +1 -0
  47. package/dist/cjs/node_modules/lib0/prng.js +95 -0
  48. package/dist/cjs/node_modules/lib0/prng.js.map +1 -0
  49. package/dist/cjs/node_modules/lib0/promise.js +33 -0
  50. package/dist/cjs/node_modules/lib0/promise.js.map +1 -0
  51. package/dist/cjs/node_modules/lib0/random.js +28 -0
  52. package/dist/cjs/node_modules/lib0/random.js.map +1 -0
  53. package/dist/cjs/node_modules/lib0/schema.js +1168 -0
  54. package/dist/cjs/node_modules/lib0/schema.js.map +1 -0
  55. package/dist/cjs/node_modules/lib0/set.js +12 -0
  56. package/dist/cjs/node_modules/lib0/set.js.map +1 -0
  57. package/dist/cjs/node_modules/lib0/storage.js +79 -0
  58. package/dist/cjs/node_modules/lib0/storage.js.map +1 -0
  59. package/dist/cjs/node_modules/lib0/string.js +94 -0
  60. package/dist/cjs/node_modules/lib0/string.js.map +1 -0
  61. package/dist/cjs/node_modules/lib0/symbol.js +15 -0
  62. package/dist/cjs/node_modules/lib0/symbol.js.map +1 -0
  63. package/dist/cjs/node_modules/lib0/time.js +18 -0
  64. package/dist/cjs/node_modules/lib0/time.js.map +1 -0
  65. package/dist/cjs/node_modules/lib0/trait/equality.js +32 -0
  66. package/dist/cjs/node_modules/lib0/trait/equality.js.map +1 -0
  67. package/dist/cjs/node_modules/lib0/url.js +20 -0
  68. package/dist/cjs/node_modules/lib0/url.js.map +1 -0
  69. package/dist/cjs/node_modules/lib0/webcrypto.js +8 -0
  70. package/dist/cjs/node_modules/lib0/webcrypto.js.map +1 -0
  71. package/dist/cjs/node_modules/y-protocols/auth.js +27 -0
  72. package/dist/cjs/node_modules/y-protocols/auth.js.map +1 -0
  73. package/dist/cjs/node_modules/y-protocols/awareness.js +277 -0
  74. package/dist/cjs/node_modules/y-protocols/awareness.js.map +1 -0
  75. package/dist/cjs/node_modules/y-protocols/sync.js +149 -0
  76. package/dist/cjs/node_modules/y-protocols/sync.js.map +1 -0
  77. package/dist/cjs/node_modules/y-websocket/src/y-websocket.js +521 -0
  78. package/dist/cjs/node_modules/y-websocket/src/y-websocket.js.map +1 -0
  79. package/dist/cjs/node_modules/yjs/dist/yjs.js +10399 -0
  80. package/dist/cjs/node_modules/yjs/dist/yjs.js.map +1 -0
  81. package/dist/cjs/packages/ui/dist/index.js +65997 -0
  82. package/dist/cjs/packages/ui/dist/index.js.map +1 -0
  83. package/dist/cjs/pixel/PixelEditor.js +1 -1
  84. package/dist/cjs/pixel/PixelEditorMenuBar.js +1 -1
  85. package/dist/cjs/pixel/PixelEditorRightPanel.js +1 -1
  86. package/dist/cjs/pixel/PixelEditorTimeline.js +1 -1
  87. package/dist/cjs/pixel/PixelEditorToolbar.js +1 -1
  88. package/dist/cjs/pixel/SpriteGeneratorPanel.js +1 -1
  89. package/dist/cjs/vector/VectorEditor.js +1 -1
  90. package/dist/cjs/vector/VectorEditorMenuBar.js +1 -1
  91. package/dist/cjs/vector/VectorEditorRightPanel.js +1 -1
  92. package/dist/esm/animation/AnimationEditor.js +5 -5
  93. package/dist/esm/animation/Audience.js +2 -2
  94. package/dist/esm/core/LocalUI.js +4 -4
  95. package/dist/esm/core/yjsCollaboration.js +2 -2
  96. package/dist/esm/core/yjsCollaboration.js.map +1 -1
  97. package/dist/esm/game/GameAsset2dEditor.js +19 -19
  98. package/dist/esm/node_modules/lib0/array.js +95 -0
  99. package/dist/esm/node_modules/lib0/array.js.map +1 -0
  100. package/dist/esm/node_modules/lib0/binary.js +30 -0
  101. package/dist/esm/node_modules/lib0/binary.js.map +1 -0
  102. package/dist/esm/node_modules/lib0/broadcastchannel.js +117 -0
  103. package/dist/esm/node_modules/lib0/broadcastchannel.js.map +1 -0
  104. package/dist/esm/node_modules/lib0/buffer.js +96 -0
  105. package/dist/esm/node_modules/lib0/buffer.js.map +1 -0
  106. package/dist/esm/node_modules/lib0/conditions.js +16 -0
  107. package/dist/esm/node_modules/lib0/conditions.js.map +1 -0
  108. package/dist/esm/node_modules/lib0/decoding.js +439 -0
  109. package/dist/esm/node_modules/lib0/decoding.js.map +1 -0
  110. package/dist/esm/node_modules/lib0/dom.js +48 -0
  111. package/dist/esm/node_modules/lib0/dom.js.map +1 -0
  112. package/dist/esm/node_modules/lib0/encoding.js +663 -0
  113. package/dist/esm/node_modules/lib0/encoding.js.map +1 -0
  114. package/dist/esm/node_modules/lib0/environment.js +121 -0
  115. package/dist/esm/node_modules/lib0/environment.js.map +1 -0
  116. package/dist/esm/node_modules/lib0/error.js +33 -0
  117. package/dist/esm/node_modules/lib0/error.js.map +1 -0
  118. package/dist/esm/node_modules/lib0/function.js +130 -0
  119. package/dist/esm/node_modules/lib0/function.js.map +1 -0
  120. package/dist/esm/node_modules/lib0/iterator.js +48 -0
  121. package/dist/esm/node_modules/lib0/iterator.js.map +1 -0
  122. package/dist/esm/node_modules/lib0/logging.common.js +52 -0
  123. package/dist/esm/node_modules/lib0/logging.common.js.map +1 -0
  124. package/dist/esm/node_modules/lib0/logging.js +115 -0
  125. package/dist/esm/node_modules/lib0/logging.js.map +1 -0
  126. package/dist/esm/node_modules/lib0/map.js +102 -0
  127. package/dist/esm/node_modules/lib0/map.js.map +1 -0
  128. package/dist/esm/node_modules/lib0/math.js +37 -0
  129. package/dist/esm/node_modules/lib0/math.js.map +1 -0
  130. package/dist/esm/node_modules/lib0/number.js +17 -0
  131. package/dist/esm/node_modules/lib0/number.js.map +1 -0
  132. package/dist/esm/node_modules/lib0/object.js +119 -0
  133. package/dist/esm/node_modules/lib0/object.js.map +1 -0
  134. package/dist/esm/node_modules/lib0/observable.js +165 -0
  135. package/dist/esm/node_modules/lib0/observable.js.map +1 -0
  136. package/dist/esm/node_modules/lib0/pair.js +30 -0
  137. package/dist/esm/node_modules/lib0/pair.js.map +1 -0
  138. package/dist/esm/node_modules/lib0/prng.js +87 -0
  139. package/dist/esm/node_modules/lib0/prng.js.map +1 -0
  140. package/dist/esm/node_modules/lib0/promise.js +31 -0
  141. package/dist/esm/node_modules/lib0/promise.js.map +1 -0
  142. package/dist/esm/node_modules/lib0/random.js +25 -0
  143. package/dist/esm/node_modules/lib0/random.js.map +1 -0
  144. package/dist/esm/node_modules/lib0/schema.js +1113 -0
  145. package/dist/esm/node_modules/lib0/schema.js.map +1 -0
  146. package/dist/esm/node_modules/lib0/set.js +10 -0
  147. package/dist/esm/node_modules/lib0/set.js.map +1 -0
  148. package/dist/esm/node_modules/lib0/storage.js +75 -0
  149. package/dist/esm/node_modules/lib0/storage.js.map +1 -0
  150. package/dist/esm/node_modules/lib0/string.js +85 -0
  151. package/dist/esm/node_modules/lib0/string.js.map +1 -0
  152. package/dist/esm/node_modules/lib0/symbol.js +13 -0
  153. package/dist/esm/node_modules/lib0/symbol.js.map +1 -0
  154. package/dist/esm/node_modules/lib0/time.js +16 -0
  155. package/dist/esm/node_modules/lib0/time.js.map +1 -0
  156. package/dist/esm/node_modules/lib0/trait/equality.js +29 -0
  157. package/dist/esm/node_modules/lib0/trait/equality.js.map +1 -0
  158. package/dist/esm/node_modules/lib0/url.js +18 -0
  159. package/dist/esm/node_modules/lib0/url.js.map +1 -0
  160. package/dist/esm/node_modules/lib0/webcrypto.js +6 -0
  161. package/dist/esm/node_modules/lib0/webcrypto.js.map +1 -0
  162. package/dist/esm/node_modules/y-protocols/auth.js +24 -0
  163. package/dist/esm/node_modules/y-protocols/auth.js.map +1 -0
  164. package/dist/esm/node_modules/y-protocols/awareness.js +271 -0
  165. package/dist/esm/node_modules/y-protocols/awareness.js.map +1 -0
  166. package/dist/esm/node_modules/y-protocols/sync.js +138 -0
  167. package/dist/esm/node_modules/y-protocols/sync.js.map +1 -0
  168. package/dist/esm/node_modules/y-websocket/src/y-websocket.js +515 -0
  169. package/dist/esm/node_modules/y-websocket/src/y-websocket.js.map +1 -0
  170. package/dist/esm/node_modules/yjs/dist/yjs.js +10295 -0
  171. package/dist/esm/node_modules/yjs/dist/yjs.js.map +1 -0
  172. package/dist/esm/packages/ui/dist/index.js +65571 -0
  173. package/dist/esm/packages/ui/dist/index.js.map +1 -0
  174. package/dist/esm/pixel/PixelEditor.js +3 -3
  175. package/dist/esm/pixel/PixelEditorMenuBar.js +2 -2
  176. package/dist/esm/pixel/PixelEditorRightPanel.js +3 -3
  177. package/dist/esm/pixel/PixelEditorTimeline.js +2 -2
  178. package/dist/esm/pixel/PixelEditorToolbar.js +2 -2
  179. package/dist/esm/pixel/SpriteGeneratorPanel.js +8 -8
  180. package/dist/esm/vector/VectorEditor.js +3 -3
  181. package/dist/esm/vector/VectorEditorMenuBar.js +3 -3
  182. package/dist/esm/vector/VectorEditorRightPanel.js +13 -13
  183. package/package.json +1 -1
@@ -0,0 +1,271 @@
1
+ import { writeVarUint, writeVarString, toUint8Array, createEncoder } from '../lib0/encoding.js';
2
+ import { readVarUint, readVarString, createDecoder } from '../lib0/decoding.js';
3
+ import { getUnixTime } from '../lib0/time.js';
4
+ import { floor } from '../lib0/math.js';
5
+ import { Observable } from '../lib0/observable.js';
6
+ import { equalityDeep } from '../lib0/function.js';
7
+
8
+ /**
9
+ * @module awareness-protocol
10
+ */
11
+
12
+
13
+ const outdatedTimeout = 30000;
14
+
15
+ /**
16
+ * @typedef {Object} MetaClientState
17
+ * @property {number} MetaClientState.clock
18
+ * @property {number} MetaClientState.lastUpdated unix timestamp
19
+ */
20
+
21
+ /**
22
+ * The Awareness class implements a simple shared state protocol that can be used for non-persistent data like awareness information
23
+ * (cursor, username, status, ..). Each client can update its own local state and listen to state changes of
24
+ * remote clients. Every client may set a state of a remote peer to `null` to mark the client as offline.
25
+ *
26
+ * Each client is identified by a unique client id (something we borrow from `doc.clientID`). A client can override
27
+ * its own state by propagating a message with an increasing timestamp (`clock`). If such a message is received, it is
28
+ * applied if the known state of that client is older than the new state (`clock < newClock`). If a client thinks that
29
+ * a remote client is offline, it may propagate a message with
30
+ * `{ clock: currentClientClock, state: null, client: remoteClient }`. If such a
31
+ * message is received, and the known clock of that client equals the received clock, it will override the state with `null`.
32
+ *
33
+ * Before a client disconnects, it should propagate a `null` state with an updated clock.
34
+ *
35
+ * Awareness states must be updated every 30 seconds. Otherwise the Awareness instance will delete the client state.
36
+ *
37
+ * @extends {Observable<string>}
38
+ */
39
+ class Awareness extends Observable {
40
+ /**
41
+ * @param {Y.Doc} doc
42
+ */
43
+ constructor (doc) {
44
+ super();
45
+ this.doc = doc;
46
+ /**
47
+ * @type {number}
48
+ */
49
+ this.clientID = doc.clientID;
50
+ /**
51
+ * Maps from client id to client state
52
+ * @type {Map<number, Object<string, any>>}
53
+ */
54
+ this.states = new Map();
55
+ /**
56
+ * @type {Map<number, MetaClientState>}
57
+ */
58
+ this.meta = new Map();
59
+ this._checkInterval = /** @type {any} */ (setInterval(() => {
60
+ const now = getUnixTime();
61
+ if (this.getLocalState() !== null && (outdatedTimeout / 2 <= now - /** @type {{lastUpdated:number}} */ (this.meta.get(this.clientID)).lastUpdated)) {
62
+ // renew local clock
63
+ this.setLocalState(this.getLocalState());
64
+ }
65
+ /**
66
+ * @type {Array<number>}
67
+ */
68
+ const remove = [];
69
+ this.meta.forEach((meta, clientid) => {
70
+ if (clientid !== this.clientID && outdatedTimeout <= now - meta.lastUpdated && this.states.has(clientid)) {
71
+ remove.push(clientid);
72
+ }
73
+ });
74
+ if (remove.length > 0) {
75
+ removeAwarenessStates(this, remove, 'timeout');
76
+ }
77
+ }, floor(outdatedTimeout / 10)));
78
+ doc.on('destroy', () => {
79
+ this.destroy();
80
+ });
81
+ this.setLocalState({});
82
+ }
83
+
84
+ destroy () {
85
+ this.emit('destroy', [this]);
86
+ this.setLocalState(null);
87
+ super.destroy();
88
+ clearInterval(this._checkInterval);
89
+ }
90
+
91
+ /**
92
+ * @return {Object<string,any>|null}
93
+ */
94
+ getLocalState () {
95
+ return this.states.get(this.clientID) || null
96
+ }
97
+
98
+ /**
99
+ * @param {Object<string,any>|null} state
100
+ */
101
+ setLocalState (state) {
102
+ const clientID = this.clientID;
103
+ const currLocalMeta = this.meta.get(clientID);
104
+ const clock = currLocalMeta === undefined ? 0 : currLocalMeta.clock + 1;
105
+ const prevState = this.states.get(clientID);
106
+ if (state === null) {
107
+ this.states.delete(clientID);
108
+ } else {
109
+ this.states.set(clientID, state);
110
+ }
111
+ this.meta.set(clientID, {
112
+ clock,
113
+ lastUpdated: getUnixTime()
114
+ });
115
+ const added = [];
116
+ const updated = [];
117
+ const filteredUpdated = [];
118
+ const removed = [];
119
+ if (state === null) {
120
+ removed.push(clientID);
121
+ } else if (prevState == null) {
122
+ if (state != null) {
123
+ added.push(clientID);
124
+ }
125
+ } else {
126
+ updated.push(clientID);
127
+ if (!equalityDeep(prevState, state)) {
128
+ filteredUpdated.push(clientID);
129
+ }
130
+ }
131
+ if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) {
132
+ this.emit('change', [{ added, updated: filteredUpdated, removed }, 'local']);
133
+ }
134
+ this.emit('update', [{ added, updated, removed }, 'local']);
135
+ }
136
+
137
+ /**
138
+ * @param {string} field
139
+ * @param {any} value
140
+ */
141
+ setLocalStateField (field, value) {
142
+ const state = this.getLocalState();
143
+ if (state !== null) {
144
+ this.setLocalState({
145
+ ...state,
146
+ [field]: value
147
+ });
148
+ }
149
+ }
150
+
151
+ /**
152
+ * @return {Map<number,Object<string,any>>}
153
+ */
154
+ getStates () {
155
+ return this.states
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Mark (remote) clients as inactive and remove them from the list of active peers.
161
+ * This change will be propagated to remote clients.
162
+ *
163
+ * @param {Awareness} awareness
164
+ * @param {Array<number>} clients
165
+ * @param {any} origin
166
+ */
167
+ const removeAwarenessStates = (awareness, clients, origin) => {
168
+ const removed = [];
169
+ for (let i = 0; i < clients.length; i++) {
170
+ const clientID = clients[i];
171
+ if (awareness.states.has(clientID)) {
172
+ awareness.states.delete(clientID);
173
+ if (clientID === awareness.clientID) {
174
+ const curMeta = /** @type {MetaClientState} */ (awareness.meta.get(clientID));
175
+ awareness.meta.set(clientID, {
176
+ clock: curMeta.clock + 1,
177
+ lastUpdated: getUnixTime()
178
+ });
179
+ }
180
+ removed.push(clientID);
181
+ }
182
+ }
183
+ if (removed.length > 0) {
184
+ awareness.emit('change', [{ added: [], updated: [], removed }, origin]);
185
+ awareness.emit('update', [{ added: [], updated: [], removed }, origin]);
186
+ }
187
+ };
188
+
189
+ /**
190
+ * @param {Awareness} awareness
191
+ * @param {Array<number>} clients
192
+ * @return {Uint8Array}
193
+ */
194
+ const encodeAwarenessUpdate = (awareness, clients, states = awareness.states) => {
195
+ const len = clients.length;
196
+ const encoder = createEncoder();
197
+ writeVarUint(encoder, len);
198
+ for (let i = 0; i < len; i++) {
199
+ const clientID = clients[i];
200
+ const state = states.get(clientID) || null;
201
+ const clock = /** @type {MetaClientState} */ (awareness.meta.get(clientID)).clock;
202
+ writeVarUint(encoder, clientID);
203
+ writeVarUint(encoder, clock);
204
+ writeVarString(encoder, JSON.stringify(state));
205
+ }
206
+ return toUint8Array(encoder)
207
+ };
208
+
209
+ /**
210
+ * @param {Awareness} awareness
211
+ * @param {Uint8Array} update
212
+ * @param {any} origin This will be added to the emitted change event
213
+ */
214
+ const applyAwarenessUpdate = (awareness, update, origin) => {
215
+ const decoder = createDecoder(update);
216
+ const timestamp = getUnixTime();
217
+ const added = [];
218
+ const updated = [];
219
+ const filteredUpdated = [];
220
+ const removed = [];
221
+ const len = readVarUint(decoder);
222
+ for (let i = 0; i < len; i++) {
223
+ const clientID = readVarUint(decoder);
224
+ let clock = readVarUint(decoder);
225
+ const state = JSON.parse(readVarString(decoder));
226
+ const clientMeta = awareness.meta.get(clientID);
227
+ const prevState = awareness.states.get(clientID);
228
+ const currClock = clientMeta === undefined ? 0 : clientMeta.clock;
229
+ if (currClock < clock || (currClock === clock && state === null && awareness.states.has(clientID))) {
230
+ if (state === null) {
231
+ // never let a remote client remove this local state
232
+ if (clientID === awareness.clientID && awareness.getLocalState() != null) {
233
+ // remote client removed the local state. Do not remote state. Broadcast a message indicating
234
+ // that this client still exists by increasing the clock
235
+ clock++;
236
+ } else {
237
+ awareness.states.delete(clientID);
238
+ }
239
+ } else {
240
+ awareness.states.set(clientID, state);
241
+ }
242
+ awareness.meta.set(clientID, {
243
+ clock,
244
+ lastUpdated: timestamp
245
+ });
246
+ if (clientMeta === undefined && state !== null) {
247
+ added.push(clientID);
248
+ } else if (clientMeta !== undefined && state === null) {
249
+ removed.push(clientID);
250
+ } else if (state !== null) {
251
+ if (!equalityDeep(state, prevState)) {
252
+ filteredUpdated.push(clientID);
253
+ }
254
+ updated.push(clientID);
255
+ }
256
+ }
257
+ }
258
+ if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) {
259
+ awareness.emit('change', [{
260
+ added, updated: filteredUpdated, removed
261
+ }, origin]);
262
+ }
263
+ if (added.length > 0 || updated.length > 0 || removed.length > 0) {
264
+ awareness.emit('update', [{
265
+ added, updated, removed
266
+ }, origin]);
267
+ }
268
+ };
269
+
270
+ export { Awareness, applyAwarenessUpdate, encodeAwarenessUpdate, outdatedTimeout, removeAwarenessStates };
271
+ //# sourceMappingURL=awareness.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"awareness.js","sources":["../../../../../../node_modules/y-protocols/awareness.js"],"sourcesContent":["/**\n * @module awareness-protocol\n */\n\nimport * as encoding from 'lib0/encoding'\nimport * as decoding from 'lib0/decoding'\nimport * as time from 'lib0/time'\nimport * as math from 'lib0/math'\nimport { Observable } from 'lib0/observable'\nimport * as f from 'lib0/function'\nimport * as Y from 'yjs' // eslint-disable-line\n\nexport const outdatedTimeout = 30000\n\n/**\n * @typedef {Object} MetaClientState\n * @property {number} MetaClientState.clock\n * @property {number} MetaClientState.lastUpdated unix timestamp\n */\n\n/**\n * The Awareness class implements a simple shared state protocol that can be used for non-persistent data like awareness information\n * (cursor, username, status, ..). Each client can update its own local state and listen to state changes of\n * remote clients. Every client may set a state of a remote peer to `null` to mark the client as offline.\n *\n * Each client is identified by a unique client id (something we borrow from `doc.clientID`). A client can override\n * its own state by propagating a message with an increasing timestamp (`clock`). If such a message is received, it is\n * applied if the known state of that client is older than the new state (`clock < newClock`). If a client thinks that\n * a remote client is offline, it may propagate a message with\n * `{ clock: currentClientClock, state: null, client: remoteClient }`. If such a\n * message is received, and the known clock of that client equals the received clock, it will override the state with `null`.\n *\n * Before a client disconnects, it should propagate a `null` state with an updated clock.\n *\n * Awareness states must be updated every 30 seconds. Otherwise the Awareness instance will delete the client state.\n *\n * @extends {Observable<string>}\n */\nexport class Awareness extends Observable {\n /**\n * @param {Y.Doc} doc\n */\n constructor (doc) {\n super()\n this.doc = doc\n /**\n * @type {number}\n */\n this.clientID = doc.clientID\n /**\n * Maps from client id to client state\n * @type {Map<number, Object<string, any>>}\n */\n this.states = new Map()\n /**\n * @type {Map<number, MetaClientState>}\n */\n this.meta = new Map()\n this._checkInterval = /** @type {any} */ (setInterval(() => {\n const now = time.getUnixTime()\n if (this.getLocalState() !== null && (outdatedTimeout / 2 <= now - /** @type {{lastUpdated:number}} */ (this.meta.get(this.clientID)).lastUpdated)) {\n // renew local clock\n this.setLocalState(this.getLocalState())\n }\n /**\n * @type {Array<number>}\n */\n const remove = []\n this.meta.forEach((meta, clientid) => {\n if (clientid !== this.clientID && outdatedTimeout <= now - meta.lastUpdated && this.states.has(clientid)) {\n remove.push(clientid)\n }\n })\n if (remove.length > 0) {\n removeAwarenessStates(this, remove, 'timeout')\n }\n }, math.floor(outdatedTimeout / 10)))\n doc.on('destroy', () => {\n this.destroy()\n })\n this.setLocalState({})\n }\n\n destroy () {\n this.emit('destroy', [this])\n this.setLocalState(null)\n super.destroy()\n clearInterval(this._checkInterval)\n }\n\n /**\n * @return {Object<string,any>|null}\n */\n getLocalState () {\n return this.states.get(this.clientID) || null\n }\n\n /**\n * @param {Object<string,any>|null} state\n */\n setLocalState (state) {\n const clientID = this.clientID\n const currLocalMeta = this.meta.get(clientID)\n const clock = currLocalMeta === undefined ? 0 : currLocalMeta.clock + 1\n const prevState = this.states.get(clientID)\n if (state === null) {\n this.states.delete(clientID)\n } else {\n this.states.set(clientID, state)\n }\n this.meta.set(clientID, {\n clock,\n lastUpdated: time.getUnixTime()\n })\n const added = []\n const updated = []\n const filteredUpdated = []\n const removed = []\n if (state === null) {\n removed.push(clientID)\n } else if (prevState == null) {\n if (state != null) {\n added.push(clientID)\n }\n } else {\n updated.push(clientID)\n if (!f.equalityDeep(prevState, state)) {\n filteredUpdated.push(clientID)\n }\n }\n if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) {\n this.emit('change', [{ added, updated: filteredUpdated, removed }, 'local'])\n }\n this.emit('update', [{ added, updated, removed }, 'local'])\n }\n\n /**\n * @param {string} field\n * @param {any} value\n */\n setLocalStateField (field, value) {\n const state = this.getLocalState()\n if (state !== null) {\n this.setLocalState({\n ...state,\n [field]: value\n })\n }\n }\n\n /**\n * @return {Map<number,Object<string,any>>}\n */\n getStates () {\n return this.states\n }\n}\n\n/**\n * Mark (remote) clients as inactive and remove them from the list of active peers.\n * This change will be propagated to remote clients.\n *\n * @param {Awareness} awareness\n * @param {Array<number>} clients\n * @param {any} origin\n */\nexport const removeAwarenessStates = (awareness, clients, origin) => {\n const removed = []\n for (let i = 0; i < clients.length; i++) {\n const clientID = clients[i]\n if (awareness.states.has(clientID)) {\n awareness.states.delete(clientID)\n if (clientID === awareness.clientID) {\n const curMeta = /** @type {MetaClientState} */ (awareness.meta.get(clientID))\n awareness.meta.set(clientID, {\n clock: curMeta.clock + 1,\n lastUpdated: time.getUnixTime()\n })\n }\n removed.push(clientID)\n }\n }\n if (removed.length > 0) {\n awareness.emit('change', [{ added: [], updated: [], removed }, origin])\n awareness.emit('update', [{ added: [], updated: [], removed }, origin])\n }\n}\n\n/**\n * @param {Awareness} awareness\n * @param {Array<number>} clients\n * @return {Uint8Array}\n */\nexport const encodeAwarenessUpdate = (awareness, clients, states = awareness.states) => {\n const len = clients.length\n const encoder = encoding.createEncoder()\n encoding.writeVarUint(encoder, len)\n for (let i = 0; i < len; i++) {\n const clientID = clients[i]\n const state = states.get(clientID) || null\n const clock = /** @type {MetaClientState} */ (awareness.meta.get(clientID)).clock\n encoding.writeVarUint(encoder, clientID)\n encoding.writeVarUint(encoder, clock)\n encoding.writeVarString(encoder, JSON.stringify(state))\n }\n return encoding.toUint8Array(encoder)\n}\n\n/**\n * Modify the content of an awareness update before re-encoding it to an awareness update.\n *\n * This might be useful when you have a central server that wants to ensure that clients\n * cant hijack somebody elses identity.\n *\n * @param {Uint8Array} update\n * @param {function(any):any} modify\n * @return {Uint8Array}\n */\nexport const modifyAwarenessUpdate = (update, modify) => {\n const decoder = decoding.createDecoder(update)\n const encoder = encoding.createEncoder()\n const len = decoding.readVarUint(decoder)\n encoding.writeVarUint(encoder, len)\n for (let i = 0; i < len; i++) {\n const clientID = decoding.readVarUint(decoder)\n const clock = decoding.readVarUint(decoder)\n const state = JSON.parse(decoding.readVarString(decoder))\n const modifiedState = modify(state)\n encoding.writeVarUint(encoder, clientID)\n encoding.writeVarUint(encoder, clock)\n encoding.writeVarString(encoder, JSON.stringify(modifiedState))\n }\n return encoding.toUint8Array(encoder)\n}\n\n/**\n * @param {Awareness} awareness\n * @param {Uint8Array} update\n * @param {any} origin This will be added to the emitted change event\n */\nexport const applyAwarenessUpdate = (awareness, update, origin) => {\n const decoder = decoding.createDecoder(update)\n const timestamp = time.getUnixTime()\n const added = []\n const updated = []\n const filteredUpdated = []\n const removed = []\n const len = decoding.readVarUint(decoder)\n for (let i = 0; i < len; i++) {\n const clientID = decoding.readVarUint(decoder)\n let clock = decoding.readVarUint(decoder)\n const state = JSON.parse(decoding.readVarString(decoder))\n const clientMeta = awareness.meta.get(clientID)\n const prevState = awareness.states.get(clientID)\n const currClock = clientMeta === undefined ? 0 : clientMeta.clock\n if (currClock < clock || (currClock === clock && state === null && awareness.states.has(clientID))) {\n if (state === null) {\n // never let a remote client remove this local state\n if (clientID === awareness.clientID && awareness.getLocalState() != null) {\n // remote client removed the local state. Do not remote state. Broadcast a message indicating\n // that this client still exists by increasing the clock\n clock++\n } else {\n awareness.states.delete(clientID)\n }\n } else {\n awareness.states.set(clientID, state)\n }\n awareness.meta.set(clientID, {\n clock,\n lastUpdated: timestamp\n })\n if (clientMeta === undefined && state !== null) {\n added.push(clientID)\n } else if (clientMeta !== undefined && state === null) {\n removed.push(clientID)\n } else if (state !== null) {\n if (!f.equalityDeep(state, prevState)) {\n filteredUpdated.push(clientID)\n }\n updated.push(clientID)\n }\n }\n }\n if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) {\n awareness.emit('change', [{\n added, updated: filteredUpdated, removed\n }, origin])\n }\n if (added.length > 0 || updated.length > 0 || removed.length > 0) {\n awareness.emit('update', [{\n added, updated, removed\n }, origin])\n }\n}\n"],"names":["time.getUnixTime","math.floor","f.equalityDeep","encoding.createEncoder","encoding.writeVarUint","encoding.writeVarString","encoding.toUint8Array","decoding.createDecoder","decoding.readVarUint","decoding.readVarString"],"mappings":";;;;;;;AAAA;AACA;AACA;;;AAUY,MAAC,eAAe,GAAG;;AAE/B;AACA;AACA;AACA;AACA;;AAEA;AACA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C,QAAQ;AAC3B,MAAM,CAAC,MAAM,IAAI,UAAU,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE;AAC7D,QAAQ,OAAO,CAAC,IAAI,CAAC,QAAQ;AAC7B,MAAM,CAAC,MAAM,IAAI,KAAK,KAAK,IAAI,EAAE;AACjC,QAAQ,IAAI,CAACP,YAAc,CAAC,KAAK,EAAE,SAAS,CAAC,EAAE;AAC/C,UAAU,eAAe,CAAC,IAAI,CAAC,QAAQ;AACvC,QAAQ;AACR,QAAQ,OAAO,CAAC,IAAI,CAAC,QAAQ;AAC7B,MAAM;AACN,IAAI;AACJ,EAAE;AACF,EAAE,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE;AAC5E,IAAI,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;AAC9B,MAAM,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE;AACvC,KAAK,EAAE,MAAM,CAAC;AACd,EAAE;AACF,EAAE,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE;AACpE,IAAI,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;AAC9B,MAAM,KAAK,EAAE,OAAO,EAAE;AACtB,KAAK,EAAE,MAAM,CAAC;AACd,EAAE;AACF;;;;","x_google_ignoreList":[0]}
@@ -0,0 +1,138 @@
1
+ import { writeVarUint, writeVarUint8Array } from '../lib0/encoding.js';
2
+ import { readVarUint, readVarUint8Array } from '../lib0/decoding.js';
3
+ import { encodeStateVector, encodeStateAsUpdate, applyUpdate } from '../yjs/dist/yjs.js';
4
+
5
+ /**
6
+ * @module sync-protocol
7
+ */
8
+
9
+
10
+ /**
11
+ * @typedef {Map<number, number>} StateMap
12
+ */
13
+
14
+ /**
15
+ * Core Yjs defines two message types:
16
+ * • YjsSyncStep1: Includes the State Set of the sending client. When received, the client should reply with YjsSyncStep2.
17
+ * • YjsSyncStep2: Includes all missing structs and the complete delete set. When received, the client is assured that it
18
+ * received all information from the remote client.
19
+ *
20
+ * In a peer-to-peer network, you may want to introduce a SyncDone message type. Both parties should initiate the connection
21
+ * with SyncStep1. When a client received SyncStep2, it should reply with SyncDone. When the local client received both
22
+ * SyncStep2 and SyncDone, it is assured that it is synced to the remote client.
23
+ *
24
+ * In a client-server model, you want to handle this differently: The client should initiate the connection with SyncStep1.
25
+ * When the server receives SyncStep1, it should reply with SyncStep2 immediately followed by SyncStep1. The client replies
26
+ * with SyncStep2 when it receives SyncStep1. Optionally the server may send a SyncDone after it received SyncStep2, so the
27
+ * client knows that the sync is finished. There are two reasons for this more elaborated sync model: 1. This protocol can
28
+ * easily be implemented on top of http and websockets. 2. The server should only reply to requests, and not initiate them.
29
+ * Therefore it is necessary that the client initiates the sync.
30
+ *
31
+ * Construction of a message:
32
+ * [messageType : varUint, message definition..]
33
+ *
34
+ * Note: A message does not include information about the room name. This must to be handled by the upper layer protocol!
35
+ *
36
+ * stringify[messageType] stringifies a message definition (messageType is already read from the bufffer)
37
+ */
38
+
39
+ const messageYjsSyncStep1 = 0;
40
+ const messageYjsSyncStep2 = 1;
41
+ const messageYjsUpdate = 2;
42
+
43
+ /**
44
+ * Create a sync step 1 message based on the state of the current shared document.
45
+ *
46
+ * @param {encoding.Encoder} encoder
47
+ * @param {Y.Doc} doc
48
+ */
49
+ const writeSyncStep1 = (encoder, doc) => {
50
+ writeVarUint(encoder, messageYjsSyncStep1);
51
+ const sv = encodeStateVector(doc);
52
+ writeVarUint8Array(encoder, sv);
53
+ };
54
+
55
+ /**
56
+ * @param {encoding.Encoder} encoder
57
+ * @param {Y.Doc} doc
58
+ * @param {Uint8Array} [encodedStateVector]
59
+ */
60
+ const writeSyncStep2 = (encoder, doc, encodedStateVector) => {
61
+ writeVarUint(encoder, messageYjsSyncStep2);
62
+ writeVarUint8Array(encoder, encodeStateAsUpdate(doc, encodedStateVector));
63
+ };
64
+
65
+ /**
66
+ * Read SyncStep1 message and reply with SyncStep2.
67
+ *
68
+ * @param {decoding.Decoder} decoder The reply to the received message
69
+ * @param {encoding.Encoder} encoder The received message
70
+ * @param {Y.Doc} doc
71
+ */
72
+ const readSyncStep1 = (decoder, encoder, doc) =>
73
+ writeSyncStep2(encoder, doc, readVarUint8Array(decoder));
74
+
75
+ /**
76
+ * Read and apply Structs and then DeleteStore to a y instance.
77
+ *
78
+ * @param {decoding.Decoder} decoder
79
+ * @param {Y.Doc} doc
80
+ * @param {any} transactionOrigin
81
+ * @param {(error:Error)=>any} [errorHandler]
82
+ */
83
+ const readSyncStep2 = (decoder, doc, transactionOrigin, errorHandler) => {
84
+ try {
85
+ applyUpdate(doc, readVarUint8Array(decoder), transactionOrigin);
86
+ } catch (error) {
87
+ if (errorHandler != null) errorHandler(/** @type {Error} */ (error));
88
+ // This catches errors that are thrown by event handlers
89
+ console.error('Caught error while handling a Yjs update', error);
90
+ }
91
+ };
92
+
93
+ /**
94
+ * @param {encoding.Encoder} encoder
95
+ * @param {Uint8Array} update
96
+ */
97
+ const writeUpdate = (encoder, update) => {
98
+ writeVarUint(encoder, messageYjsUpdate);
99
+ writeVarUint8Array(encoder, update);
100
+ };
101
+
102
+ /**
103
+ * Read and apply Structs and then DeleteStore to a y instance.
104
+ *
105
+ * @param {decoding.Decoder} decoder
106
+ * @param {Y.Doc} doc
107
+ * @param {any} transactionOrigin
108
+ * @param {(error:Error)=>any} [errorHandler]
109
+ */
110
+ const readUpdate = readSyncStep2;
111
+
112
+ /**
113
+ * @param {decoding.Decoder} decoder A message received from another client
114
+ * @param {encoding.Encoder} encoder The reply message. Does not need to be sent if empty.
115
+ * @param {Y.Doc} doc
116
+ * @param {any} transactionOrigin
117
+ * @param {(error:Error)=>any} [errorHandler] Optional error handler that catches errors when reading Yjs messages.
118
+ */
119
+ const readSyncMessage = (decoder, encoder, doc, transactionOrigin, errorHandler) => {
120
+ const messageType = readVarUint(decoder);
121
+ switch (messageType) {
122
+ case messageYjsSyncStep1:
123
+ readSyncStep1(decoder, encoder, doc);
124
+ break
125
+ case messageYjsSyncStep2:
126
+ readSyncStep2(decoder, doc, transactionOrigin, errorHandler);
127
+ break
128
+ case messageYjsUpdate:
129
+ readUpdate(decoder, doc, transactionOrigin, errorHandler);
130
+ break
131
+ default:
132
+ throw new Error('Unknown message type')
133
+ }
134
+ return messageType
135
+ };
136
+
137
+ export { messageYjsSyncStep1, messageYjsSyncStep2, messageYjsUpdate, readSyncMessage, readSyncStep1, readSyncStep2, readUpdate, writeSyncStep1, writeSyncStep2, writeUpdate };
138
+ //# sourceMappingURL=sync.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.js","sources":["../../../../../../node_modules/y-protocols/sync.js"],"sourcesContent":["/**\n * @module sync-protocol\n */\n\nimport * as encoding from 'lib0/encoding'\nimport * as decoding from 'lib0/decoding'\nimport * as Y from 'yjs'\n\n/**\n * @typedef {Map<number, number>} StateMap\n */\n\n/**\n * Core Yjs defines two message types:\n * • YjsSyncStep1: Includes the State Set of the sending client. When received, the client should reply with YjsSyncStep2.\n * • YjsSyncStep2: Includes all missing structs and the complete delete set. When received, the client is assured that it\n * received all information from the remote client.\n *\n * In a peer-to-peer network, you may want to introduce a SyncDone message type. Both parties should initiate the connection\n * with SyncStep1. When a client received SyncStep2, it should reply with SyncDone. When the local client received both\n * SyncStep2 and SyncDone, it is assured that it is synced to the remote client.\n *\n * In a client-server model, you want to handle this differently: The client should initiate the connection with SyncStep1.\n * When the server receives SyncStep1, it should reply with SyncStep2 immediately followed by SyncStep1. The client replies\n * with SyncStep2 when it receives SyncStep1. Optionally the server may send a SyncDone after it received SyncStep2, so the\n * client knows that the sync is finished. There are two reasons for this more elaborated sync model: 1. This protocol can\n * easily be implemented on top of http and websockets. 2. The server should only reply to requests, and not initiate them.\n * Therefore it is necessary that the client initiates the sync.\n *\n * Construction of a message:\n * [messageType : varUint, message definition..]\n *\n * Note: A message does not include information about the room name. This must to be handled by the upper layer protocol!\n *\n * stringify[messageType] stringifies a message definition (messageType is already read from the bufffer)\n */\n\nexport const messageYjsSyncStep1 = 0\nexport const messageYjsSyncStep2 = 1\nexport const messageYjsUpdate = 2\n\n/**\n * Create a sync step 1 message based on the state of the current shared document.\n *\n * @param {encoding.Encoder} encoder\n * @param {Y.Doc} doc\n */\nexport const writeSyncStep1 = (encoder, doc) => {\n encoding.writeVarUint(encoder, messageYjsSyncStep1)\n const sv = Y.encodeStateVector(doc)\n encoding.writeVarUint8Array(encoder, sv)\n}\n\n/**\n * @param {encoding.Encoder} encoder\n * @param {Y.Doc} doc\n * @param {Uint8Array} [encodedStateVector]\n */\nexport const writeSyncStep2 = (encoder, doc, encodedStateVector) => {\n encoding.writeVarUint(encoder, messageYjsSyncStep2)\n encoding.writeVarUint8Array(encoder, Y.encodeStateAsUpdate(doc, encodedStateVector))\n}\n\n/**\n * Read SyncStep1 message and reply with SyncStep2.\n *\n * @param {decoding.Decoder} decoder The reply to the received message\n * @param {encoding.Encoder} encoder The received message\n * @param {Y.Doc} doc\n */\nexport const readSyncStep1 = (decoder, encoder, doc) =>\n writeSyncStep2(encoder, doc, decoding.readVarUint8Array(decoder))\n\n/**\n * Read and apply Structs and then DeleteStore to a y instance.\n *\n * @param {decoding.Decoder} decoder\n * @param {Y.Doc} doc\n * @param {any} transactionOrigin\n * @param {(error:Error)=>any} [errorHandler]\n */\nexport const readSyncStep2 = (decoder, doc, transactionOrigin, errorHandler) => {\n try {\n Y.applyUpdate(doc, decoding.readVarUint8Array(decoder), transactionOrigin)\n } catch (error) {\n if (errorHandler != null) errorHandler(/** @type {Error} */ (error))\n // This catches errors that are thrown by event handlers\n console.error('Caught error while handling a Yjs update', error)\n }\n}\n\n/**\n * @param {encoding.Encoder} encoder\n * @param {Uint8Array} update\n */\nexport const writeUpdate = (encoder, update) => {\n encoding.writeVarUint(encoder, messageYjsUpdate)\n encoding.writeVarUint8Array(encoder, update)\n}\n\n/**\n * Read and apply Structs and then DeleteStore to a y instance.\n *\n * @param {decoding.Decoder} decoder\n * @param {Y.Doc} doc\n * @param {any} transactionOrigin\n * @param {(error:Error)=>any} [errorHandler]\n */\nexport const readUpdate = readSyncStep2\n\n/**\n * @param {decoding.Decoder} decoder A message received from another client\n * @param {encoding.Encoder} encoder The reply message. Does not need to be sent if empty.\n * @param {Y.Doc} doc\n * @param {any} transactionOrigin\n * @param {(error:Error)=>any} [errorHandler] Optional error handler that catches errors when reading Yjs messages.\n */\nexport const readSyncMessage = (decoder, encoder, doc, transactionOrigin, errorHandler) => {\n const messageType = decoding.readVarUint(decoder)\n switch (messageType) {\n case messageYjsSyncStep1:\n readSyncStep1(decoder, encoder, doc)\n break\n case messageYjsSyncStep2:\n readSyncStep2(decoder, doc, transactionOrigin, errorHandler)\n break\n case messageYjsUpdate:\n readUpdate(decoder, doc, transactionOrigin, errorHandler)\n break\n default:\n throw new Error('Unknown message type')\n }\n return messageType\n}\n"],"names":["encoding.writeVarUint","Y.encodeStateVector","encoding.writeVarUint8Array","Y.encodeStateAsUpdate","decoding.readVarUint8Array","Y.applyUpdate","decoding.readVarUint"],"mappings":";;;;AAAA;AACA;AACA;;;AAMA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEY,MAAC,mBAAmB,GAAG;AACvB,MAAC,mBAAmB,GAAG;AACvB,MAAC,gBAAgB,GAAG;;AAEhC;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,cAAc,GAAG,CAAC,OAAO,EAAE,GAAG,KAAK;AAChD,EAAEA,YAAqB,CAAC,OAAO,EAAE,mBAAmB;AACpD,EAAE,MAAM,EAAE,GAAGC,iBAAmB,CAAC,GAAG;AACpC,EAAEC,kBAA2B,CAAC,OAAO,EAAE,EAAE;AACzC;;AAE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