@difizen/libro-shared-model 0.0.2-alpha.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/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './api.js';
2
+ export * from './utils.js';
3
+ export * from './ymodels.js';
package/src/utils.ts ADDED
@@ -0,0 +1,46 @@
1
+ import type * as Y from 'yjs';
2
+
3
+ import type * as models from './api.js';
4
+
5
+ export function convertYMapEventToMapChange(
6
+ yMapEvent: Y.YMapEvent<any>,
7
+ ): models.MapChange {
8
+ const changes = new Map();
9
+ yMapEvent.changes.keys.forEach((event, key) => {
10
+ changes.set(key, {
11
+ action: event.action,
12
+ oldValue: event.oldValue,
13
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
14
+ // @ts-ignore
15
+ newValue: this.ymeta.get(key),
16
+ });
17
+ });
18
+ return changes;
19
+ }
20
+
21
+ /**
22
+ * Creates a mutual exclude function with the following property:
23
+ *
24
+ * ```js
25
+ * const mutex = createMutex()
26
+ * mutex(() => {
27
+ * // This function is immediately executed
28
+ * mutex(() => {
29
+ * // This function is not executed, as the mutex is already active.
30
+ * })
31
+ * })
32
+ * ```
33
+ */
34
+ export const createMutex = (): ((f: () => void) => void) => {
35
+ let token = true;
36
+ return (f: any): void => {
37
+ if (token) {
38
+ token = false;
39
+ try {
40
+ f();
41
+ } finally {
42
+ token = true;
43
+ }
44
+ }
45
+ };
46
+ };