@voidhash/mimic-react 0.0.1 → 0.0.3

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 (61) hide show
  1. package/.turbo/turbo-build.log +60 -26
  2. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs +14 -0
  3. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs +14 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs +27 -0
  5. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs +27 -0
  6. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.cjs +16 -0
  7. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.mjs +16 -0
  8. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.cjs +11 -0
  9. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.mjs +11 -0
  10. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.cjs +18 -0
  11. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.mjs +12 -0
  12. package/dist/zustand/index.cjs +2 -94
  13. package/dist/zustand/index.d.cts +3 -115
  14. package/dist/zustand/index.d.mts +3 -115
  15. package/dist/zustand/index.mjs +2 -95
  16. package/dist/zustand/middleware.cjs +95 -0
  17. package/dist/zustand/middleware.d.cts +47 -0
  18. package/dist/zustand/middleware.d.cts.map +1 -0
  19. package/dist/zustand/middleware.d.mts +47 -0
  20. package/dist/zustand/middleware.d.mts.map +1 -0
  21. package/dist/zustand/middleware.mjs +96 -0
  22. package/dist/zustand/middleware.mjs.map +1 -0
  23. package/dist/zustand/types.d.cts +75 -0
  24. package/dist/zustand/types.d.cts.map +1 -0
  25. package/dist/zustand/types.d.mts +75 -0
  26. package/dist/zustand/types.d.mts.map +1 -0
  27. package/dist/zustand-commander/commander.cjs +187 -0
  28. package/dist/zustand-commander/commander.d.cts +52 -0
  29. package/dist/zustand-commander/commander.d.cts.map +1 -0
  30. package/dist/zustand-commander/commander.d.mts +52 -0
  31. package/dist/zustand-commander/commander.d.mts.map +1 -0
  32. package/dist/zustand-commander/commander.mjs +185 -0
  33. package/dist/zustand-commander/commander.mjs.map +1 -0
  34. package/dist/zustand-commander/hooks.cjs +145 -0
  35. package/dist/zustand-commander/hooks.d.cts +78 -0
  36. package/dist/zustand-commander/hooks.d.cts.map +1 -0
  37. package/dist/zustand-commander/hooks.d.mts +78 -0
  38. package/dist/zustand-commander/hooks.d.mts.map +1 -0
  39. package/dist/zustand-commander/hooks.mjs +144 -0
  40. package/dist/zustand-commander/hooks.mjs.map +1 -0
  41. package/dist/zustand-commander/index.cjs +15 -354
  42. package/dist/zustand-commander/index.d.cts +4 -313
  43. package/dist/zustand-commander/index.d.mts +4 -313
  44. package/dist/zustand-commander/index.mjs +4 -344
  45. package/dist/zustand-commander/types.cjs +28 -0
  46. package/dist/zustand-commander/types.d.cts +195 -0
  47. package/dist/zustand-commander/types.d.cts.map +1 -0
  48. package/dist/zustand-commander/types.d.mts +195 -0
  49. package/dist/zustand-commander/types.d.mts.map +1 -0
  50. package/dist/zustand-commander/types.mjs +25 -0
  51. package/dist/zustand-commander/types.mjs.map +1 -0
  52. package/package.json +3 -3
  53. package/tsdown.config.ts +1 -1
  54. package/dist/objectSpread2-CIP_6jda.cjs +0 -73
  55. package/dist/objectSpread2-CxTyNSYl.mjs +0 -67
  56. package/dist/zustand/index.d.cts.map +0 -1
  57. package/dist/zustand/index.d.mts.map +0 -1
  58. package/dist/zustand/index.mjs.map +0 -1
  59. package/dist/zustand-commander/index.d.cts.map +0 -1
  60. package/dist/zustand-commander/index.d.mts.map +0 -1
  61. package/dist/zustand-commander/index.mjs.map +0 -1
@@ -1,96 +1,3 @@
1
- import { t as _objectSpread2 } from "../objectSpread2-CxTyNSYl.mjs";
1
+ import { mimic } from "./middleware.mjs";
2
2
 
3
- //#region src/zustand/middleware.ts
4
- /**
5
- * Creates a MimicObject from the current document state.
6
- */
7
- const createMimicObject = (document) => {
8
- const presence = document.presence ? {
9
- selfId: document.presence.selfId(),
10
- self: document.presence.self(),
11
- others: new Map(document.presence.others()),
12
- all: new Map(document.presence.all())
13
- } : void 0;
14
- return {
15
- document,
16
- snapshot: document.root.toSnapshot(),
17
- presence,
18
- isConnected: document.isConnected(),
19
- isReady: document.isReady(),
20
- pendingCount: document.getPendingCount(),
21
- hasPendingChanges: document.hasPendingChanges()
22
- };
23
- };
24
- /**
25
- * Implementation of the mimic middleware.
26
- */
27
- const mimicImpl = (document, config, options = {}) => {
28
- const { autoSubscribe = true, autoConnect = true } = options;
29
- return (set, get, api) => {
30
- const initialMimic = createMimicObject(document);
31
- const updateMimicState = () => {
32
- const newMimic = createMimicObject(document);
33
- set((state) => _objectSpread2(_objectSpread2({}, state), {}, { mimic: newMimic }), false);
34
- };
35
- if (autoSubscribe) {
36
- var _document$presence;
37
- document.subscribe({
38
- onStateChange: () => {
39
- updateMimicState();
40
- },
41
- onConnectionChange: () => {
42
- updateMimicState();
43
- },
44
- onReady: () => {
45
- updateMimicState();
46
- }
47
- });
48
- (_document$presence = document.presence) === null || _document$presence === void 0 || _document$presence.subscribe({ onPresenceChange: () => {
49
- updateMimicState();
50
- } });
51
- }
52
- if (autoConnect) document.connect();
53
- return _objectSpread2(_objectSpread2({}, config(set, get, api)), {}, { mimic: initialMimic });
54
- };
55
- };
56
- /**
57
- * Zustand middleware that integrates a ClientDocument.
58
- *
59
- * Adds a `mimic` object to the store containing:
60
- * - `document`: The ClientDocument instance for performing transactions
61
- * - `snapshot`: Read-only snapshot of the document state (reactive)
62
- * - `presence`: Reactive presence snapshot (self + others). Undefined if presence is not enabled on the ClientDocument.
63
- * - `isConnected`: Connection status
64
- * - `isReady`: Ready status
65
- * - `pendingCount`: Number of pending transactions
66
- * - `hasPendingChanges`: Whether there are pending changes
67
- *
68
- * @example
69
- * ```ts
70
- * import { create } from 'zustand'
71
- * import { mimic } from '@voidhash/mimic-react/zustand'
72
- *
73
- * const useStore = create(
74
- * mimic(clientDocument, (set, get) => ({
75
- * // Your additional store state
76
- * }))
77
- * )
78
- *
79
- * // Read snapshot (reactive)
80
- * const snapshot = useStore(state => state.mimic.snapshot)
81
- *
82
- * // Read presence (reactive, if enabled)
83
- * const myPresence = useStore(state => state.mimic.presence?.self)
84
- * const othersPresence = useStore(state => state.mimic.presence?.others)
85
- *
86
- * // Write via document
87
- * store.getState().mimic.document.transaction(root => {
88
- * root.name.set("New Name")
89
- * })
90
- * ```
91
- */
92
- const mimic = mimicImpl;
93
-
94
- //#endregion
95
- export { mimic };
96
- //# sourceMappingURL=index.mjs.map
3
+ export { mimic };
@@ -0,0 +1,95 @@
1
+ const require_objectSpread2 = require('../_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs');
2
+
3
+ //#region src/zustand/middleware.ts
4
+ /**
5
+ * Creates a MimicObject from the current document state.
6
+ */
7
+ const createMimicObject = (document) => {
8
+ const presence = document.presence ? {
9
+ selfId: document.presence.selfId(),
10
+ self: document.presence.self(),
11
+ others: new Map(document.presence.others()),
12
+ all: new Map(document.presence.all())
13
+ } : void 0;
14
+ return {
15
+ document,
16
+ snapshot: document.root.toSnapshot(),
17
+ presence,
18
+ isConnected: document.isConnected(),
19
+ isReady: document.isReady(),
20
+ pendingCount: document.getPendingCount(),
21
+ hasPendingChanges: document.hasPendingChanges()
22
+ };
23
+ };
24
+ /**
25
+ * Implementation of the mimic middleware.
26
+ */
27
+ const mimicImpl = (document, config, options = {}) => {
28
+ const { autoSubscribe = true, autoConnect = true } = options;
29
+ return (set, get, api) => {
30
+ const initialMimic = createMimicObject(document);
31
+ const updateMimicState = () => {
32
+ const newMimic = createMimicObject(document);
33
+ set((state) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, state), {}, { mimic: newMimic }), false);
34
+ };
35
+ if (autoSubscribe) {
36
+ var _document$presence;
37
+ document.subscribe({
38
+ onStateChange: () => {
39
+ updateMimicState();
40
+ },
41
+ onConnectionChange: () => {
42
+ updateMimicState();
43
+ },
44
+ onReady: () => {
45
+ updateMimicState();
46
+ }
47
+ });
48
+ (_document$presence = document.presence) === null || _document$presence === void 0 || _document$presence.subscribe({ onPresenceChange: () => {
49
+ updateMimicState();
50
+ } });
51
+ }
52
+ if (autoConnect) document.connect();
53
+ return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config(set, get, api)), {}, { mimic: initialMimic });
54
+ };
55
+ };
56
+ /**
57
+ * Zustand middleware that integrates a ClientDocument.
58
+ *
59
+ * Adds a `mimic` object to the store containing:
60
+ * - `document`: The ClientDocument instance for performing transactions
61
+ * - `snapshot`: Read-only snapshot of the document state (reactive)
62
+ * - `presence`: Reactive presence snapshot (self + others). Undefined if presence is not enabled on the ClientDocument.
63
+ * - `isConnected`: Connection status
64
+ * - `isReady`: Ready status
65
+ * - `pendingCount`: Number of pending transactions
66
+ * - `hasPendingChanges`: Whether there are pending changes
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * import { create } from 'zustand'
71
+ * import { mimic } from '@voidhash/mimic-react/zustand'
72
+ *
73
+ * const useStore = create(
74
+ * mimic(clientDocument, (set, get) => ({
75
+ * // Your additional store state
76
+ * }))
77
+ * )
78
+ *
79
+ * // Read snapshot (reactive)
80
+ * const snapshot = useStore(state => state.mimic.snapshot)
81
+ *
82
+ * // Read presence (reactive, if enabled)
83
+ * const myPresence = useStore(state => state.mimic.presence?.self)
84
+ * const othersPresence = useStore(state => state.mimic.presence?.others)
85
+ *
86
+ * // Write via document
87
+ * store.getState().mimic.document.transaction(root => {
88
+ * root.name.set("New Name")
89
+ * })
90
+ * ```
91
+ */
92
+ const mimic = mimicImpl;
93
+
94
+ //#endregion
95
+ exports.mimic = mimic;
@@ -0,0 +1,47 @@
1
+ import { MimicMiddlewareOptions, MimicSlice } from "./types.cjs";
2
+ import { StateCreator, StoreMutatorIdentifier } from "zustand";
3
+ import { ClientDocument } from "@voidhash/mimic/client";
4
+ import { Presence, Primitive } from "@voidhash/mimic";
5
+
6
+ //#region src/zustand/middleware.d.ts
7
+ type MimicMiddleware = <TSchema extends Primitive.AnyPrimitive, TPresence extends Presence.AnyPresence | undefined = undefined, T extends object = object, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(document: ClientDocument.ClientDocument<TSchema, TPresence>, config: StateCreator<T & MimicSlice<TSchema, TPresence>, Mps, Mcs, T>, options?: MimicMiddlewareOptions) => StateCreator<T & MimicSlice<TSchema, TPresence>, Mps, Mcs, T & MimicSlice<TSchema, TPresence>>;
8
+ /**
9
+ * Zustand middleware that integrates a ClientDocument.
10
+ *
11
+ * Adds a `mimic` object to the store containing:
12
+ * - `document`: The ClientDocument instance for performing transactions
13
+ * - `snapshot`: Read-only snapshot of the document state (reactive)
14
+ * - `presence`: Reactive presence snapshot (self + others). Undefined if presence is not enabled on the ClientDocument.
15
+ * - `isConnected`: Connection status
16
+ * - `isReady`: Ready status
17
+ * - `pendingCount`: Number of pending transactions
18
+ * - `hasPendingChanges`: Whether there are pending changes
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * import { create } from 'zustand'
23
+ * import { mimic } from '@voidhash/mimic-react/zustand'
24
+ *
25
+ * const useStore = create(
26
+ * mimic(clientDocument, (set, get) => ({
27
+ * // Your additional store state
28
+ * }))
29
+ * )
30
+ *
31
+ * // Read snapshot (reactive)
32
+ * const snapshot = useStore(state => state.mimic.snapshot)
33
+ *
34
+ * // Read presence (reactive, if enabled)
35
+ * const myPresence = useStore(state => state.mimic.presence?.self)
36
+ * const othersPresence = useStore(state => state.mimic.presence?.others)
37
+ *
38
+ * // Write via document
39
+ * store.getState().mimic.document.transaction(root => {
40
+ * root.name.set("New Name")
41
+ * })
42
+ * ```
43
+ */
44
+ declare const mimic: MimicMiddleware;
45
+ //#endregion
46
+ export { mimic };
47
+ //# sourceMappingURL=middleware.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.d.cts","names":[],"sources":["../../src/zustand/middleware.ts"],"sourcesContent":[],"mappings":";;;;;;KAaK,eAAA,oBACa,SAAA,CAAU,gCACR,QAAA,CAAS,6EAEd,sDACA,mDAEH,cAAA,CAAe,eAAe,SAAS,oBACzC,aAAa,IAAI,WAAW,SAAS,YAAY,KAAK,KAAK,cACzD,2BACP,aAAa,IAAI,WAAW,SAAS,YAAY,KAAK,KAAK,IAAI,WAAW,SAAS;;AAhBpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmKpB;;;;;;cAAa,OAAgC"}
@@ -0,0 +1,47 @@
1
+ import { MimicMiddlewareOptions, MimicSlice } from "./types.mjs";
2
+ import { StateCreator, StoreMutatorIdentifier } from "zustand";
3
+ import { ClientDocument } from "@voidhash/mimic/client";
4
+ import { Presence, Primitive } from "@voidhash/mimic";
5
+
6
+ //#region src/zustand/middleware.d.ts
7
+ type MimicMiddleware = <TSchema extends Primitive.AnyPrimitive, TPresence extends Presence.AnyPresence | undefined = undefined, T extends object = object, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(document: ClientDocument.ClientDocument<TSchema, TPresence>, config: StateCreator<T & MimicSlice<TSchema, TPresence>, Mps, Mcs, T>, options?: MimicMiddlewareOptions) => StateCreator<T & MimicSlice<TSchema, TPresence>, Mps, Mcs, T & MimicSlice<TSchema, TPresence>>;
8
+ /**
9
+ * Zustand middleware that integrates a ClientDocument.
10
+ *
11
+ * Adds a `mimic` object to the store containing:
12
+ * - `document`: The ClientDocument instance for performing transactions
13
+ * - `snapshot`: Read-only snapshot of the document state (reactive)
14
+ * - `presence`: Reactive presence snapshot (self + others). Undefined if presence is not enabled on the ClientDocument.
15
+ * - `isConnected`: Connection status
16
+ * - `isReady`: Ready status
17
+ * - `pendingCount`: Number of pending transactions
18
+ * - `hasPendingChanges`: Whether there are pending changes
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * import { create } from 'zustand'
23
+ * import { mimic } from '@voidhash/mimic-react/zustand'
24
+ *
25
+ * const useStore = create(
26
+ * mimic(clientDocument, (set, get) => ({
27
+ * // Your additional store state
28
+ * }))
29
+ * )
30
+ *
31
+ * // Read snapshot (reactive)
32
+ * const snapshot = useStore(state => state.mimic.snapshot)
33
+ *
34
+ * // Read presence (reactive, if enabled)
35
+ * const myPresence = useStore(state => state.mimic.presence?.self)
36
+ * const othersPresence = useStore(state => state.mimic.presence?.others)
37
+ *
38
+ * // Write via document
39
+ * store.getState().mimic.document.transaction(root => {
40
+ * root.name.set("New Name")
41
+ * })
42
+ * ```
43
+ */
44
+ declare const mimic: MimicMiddleware;
45
+ //#endregion
46
+ export { mimic };
47
+ //# sourceMappingURL=middleware.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.d.mts","names":[],"sources":["../../src/zustand/middleware.ts"],"sourcesContent":[],"mappings":";;;;;;KAaK,eAAA,oBACa,SAAA,CAAU,gCACR,QAAA,CAAS,6EAEd,sDACA,mDAEH,cAAA,CAAe,eAAe,SAAS,oBACzC,aAAa,IAAI,WAAW,SAAS,YAAY,KAAK,KAAK,cACzD,2BACP,aAAa,IAAI,WAAW,SAAS,YAAY,KAAK,KAAK,IAAI,WAAW,SAAS;;AAhBpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmKpB;;;;;;cAAa,OAAgC"}
@@ -0,0 +1,96 @@
1
+ import { _objectSpread2 } from "../_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs";
2
+
3
+ //#region src/zustand/middleware.ts
4
+ /**
5
+ * Creates a MimicObject from the current document state.
6
+ */
7
+ const createMimicObject = (document) => {
8
+ const presence = document.presence ? {
9
+ selfId: document.presence.selfId(),
10
+ self: document.presence.self(),
11
+ others: new Map(document.presence.others()),
12
+ all: new Map(document.presence.all())
13
+ } : void 0;
14
+ return {
15
+ document,
16
+ snapshot: document.root.toSnapshot(),
17
+ presence,
18
+ isConnected: document.isConnected(),
19
+ isReady: document.isReady(),
20
+ pendingCount: document.getPendingCount(),
21
+ hasPendingChanges: document.hasPendingChanges()
22
+ };
23
+ };
24
+ /**
25
+ * Implementation of the mimic middleware.
26
+ */
27
+ const mimicImpl = (document, config, options = {}) => {
28
+ const { autoSubscribe = true, autoConnect = true } = options;
29
+ return (set, get, api) => {
30
+ const initialMimic = createMimicObject(document);
31
+ const updateMimicState = () => {
32
+ const newMimic = createMimicObject(document);
33
+ set((state) => _objectSpread2(_objectSpread2({}, state), {}, { mimic: newMimic }), false);
34
+ };
35
+ if (autoSubscribe) {
36
+ var _document$presence;
37
+ document.subscribe({
38
+ onStateChange: () => {
39
+ updateMimicState();
40
+ },
41
+ onConnectionChange: () => {
42
+ updateMimicState();
43
+ },
44
+ onReady: () => {
45
+ updateMimicState();
46
+ }
47
+ });
48
+ (_document$presence = document.presence) === null || _document$presence === void 0 || _document$presence.subscribe({ onPresenceChange: () => {
49
+ updateMimicState();
50
+ } });
51
+ }
52
+ if (autoConnect) document.connect();
53
+ return _objectSpread2(_objectSpread2({}, config(set, get, api)), {}, { mimic: initialMimic });
54
+ };
55
+ };
56
+ /**
57
+ * Zustand middleware that integrates a ClientDocument.
58
+ *
59
+ * Adds a `mimic` object to the store containing:
60
+ * - `document`: The ClientDocument instance for performing transactions
61
+ * - `snapshot`: Read-only snapshot of the document state (reactive)
62
+ * - `presence`: Reactive presence snapshot (self + others). Undefined if presence is not enabled on the ClientDocument.
63
+ * - `isConnected`: Connection status
64
+ * - `isReady`: Ready status
65
+ * - `pendingCount`: Number of pending transactions
66
+ * - `hasPendingChanges`: Whether there are pending changes
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * import { create } from 'zustand'
71
+ * import { mimic } from '@voidhash/mimic-react/zustand'
72
+ *
73
+ * const useStore = create(
74
+ * mimic(clientDocument, (set, get) => ({
75
+ * // Your additional store state
76
+ * }))
77
+ * )
78
+ *
79
+ * // Read snapshot (reactive)
80
+ * const snapshot = useStore(state => state.mimic.snapshot)
81
+ *
82
+ * // Read presence (reactive, if enabled)
83
+ * const myPresence = useStore(state => state.mimic.presence?.self)
84
+ * const othersPresence = useStore(state => state.mimic.presence?.others)
85
+ *
86
+ * // Write via document
87
+ * store.getState().mimic.document.transaction(root => {
88
+ * root.name.set("New Name")
89
+ * })
90
+ * ```
91
+ */
92
+ const mimic = mimicImpl;
93
+
94
+ //#endregion
95
+ export { mimic };
96
+ //# sourceMappingURL=middleware.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.mjs","names":["mimicImpl: MimicMiddlewareImpl"],"sources":["../../src/zustand/middleware.ts"],"sourcesContent":["import type { StateCreator, StoreMutatorIdentifier } from \"zustand\";\nimport type { ClientDocument } from \"@voidhash/mimic/client\";\nimport type { Primitive, Presence } from \"@voidhash/mimic\";\nimport type {\n MimicSlice,\n MimicObject,\n MimicMiddlewareOptions,\n} from \"./types.js\";\n\n// =============================================================================\n// Middleware Implementation\n// =============================================================================\n\ntype MimicMiddleware = <\n TSchema extends Primitive.AnyPrimitive,\n TPresence extends Presence.AnyPresence | undefined = undefined,\n T extends object = object,\n Mps extends [StoreMutatorIdentifier, unknown][] = [],\n Mcs extends [StoreMutatorIdentifier, unknown][] = [],\n>(\n document: ClientDocument.ClientDocument<TSchema, TPresence>,\n config: StateCreator<T & MimicSlice<TSchema, TPresence>, Mps, Mcs, T>,\n options?: MimicMiddlewareOptions\n) => StateCreator<T & MimicSlice<TSchema, TPresence>, Mps, Mcs, T & MimicSlice<TSchema, TPresence>>;\n\ntype MimicMiddlewareImpl = <\n TSchema extends Primitive.AnyPrimitive,\n TPresence extends Presence.AnyPresence | undefined = undefined,\n T extends object = object,\n>(\n document: ClientDocument.ClientDocument<TSchema, TPresence>,\n config: StateCreator<T & MimicSlice<TSchema, TPresence>, [], [], T>,\n options?: MimicMiddlewareOptions\n) => StateCreator<T & MimicSlice<TSchema, TPresence>, [], [], T & MimicSlice<TSchema, TPresence>>;\n\n/**\n * Creates a MimicObject from the current document state.\n */\nconst createMimicObject = <\n TSchema extends Primitive.AnyPrimitive,\n TPresence extends Presence.AnyPresence | undefined = undefined\n>(\n document: ClientDocument.ClientDocument<TSchema, TPresence>\n): MimicObject<TSchema, TPresence> => {\n const presence = document.presence\n ? {\n selfId: document.presence.selfId(),\n self: document.presence.self(),\n // Important: clone Maps to ensure zustand selectors re-render\n // when presence changes (the underlying ClientDocument mutates Maps in-place).\n others: new Map(document.presence.others()),\n all: new Map(document.presence.all()),\n }\n : undefined;\n\n return {\n document,\n snapshot: document.root.toSnapshot() as Primitive.InferSnapshot<TSchema>,\n presence: presence as MimicObject<TSchema, TPresence>[\"presence\"],\n isConnected: document.isConnected(),\n isReady: document.isReady(),\n pendingCount: document.getPendingCount(),\n hasPendingChanges: document.hasPendingChanges(),\n };\n};\n\n/**\n * Implementation of the mimic middleware.\n */\nconst mimicImpl: MimicMiddlewareImpl = <\n TSchema extends Primitive.AnyPrimitive,\n TPresence extends Presence.AnyPresence | undefined = undefined,\n _T extends object = object\n>(\n document: ClientDocument.ClientDocument<TSchema, TPresence>,\n config: any,\n options: MimicMiddlewareOptions = {}\n) => {\n const { autoSubscribe = true, autoConnect = true } = options;\n\n return (set: any, get: any, api: any) => {\n // Create initial mimic slice\n const initialMimic = createMimicObject(document);\n\n // Helper to update mimic state\n const updateMimicState = () => {\n const newMimic = createMimicObject(document);\n set(\n (state: any) => ({\n ...state,\n mimic: newMimic,\n }),\n false\n );\n };\n\n // Subscribe to document changes\n if (autoSubscribe) {\n document.subscribe({\n onStateChange: () => {\n updateMimicState();\n },\n onConnectionChange: () => {\n updateMimicState();\n },\n onReady: () => {\n updateMimicState();\n },\n });\n\n // Subscribe to presence changes (if presence schema is enabled)\n document.presence?.subscribe({\n onPresenceChange: () => {\n updateMimicState();\n },\n });\n }\n\n if (autoConnect) {\n document.connect();\n }\n\n // Get user's state - pass through set/get/api directly\n // The user's set calls won't affect mimic state since we update it separately\n const userState = config(set, get, api);\n\n // Combine user state with mimic slice\n return {\n ...userState,\n mimic: initialMimic,\n };\n };\n};\n\n/**\n * Zustand middleware that integrates a ClientDocument.\n * \n * Adds a `mimic` object to the store containing:\n * - `document`: The ClientDocument instance for performing transactions\n * - `snapshot`: Read-only snapshot of the document state (reactive)\n * - `presence`: Reactive presence snapshot (self + others). Undefined if presence is not enabled on the ClientDocument.\n * - `isConnected`: Connection status\n * - `isReady`: Ready status\n * - `pendingCount`: Number of pending transactions\n * - `hasPendingChanges`: Whether there are pending changes\n * \n * @example\n * ```ts\n * import { create } from 'zustand'\n * import { mimic } from '@voidhash/mimic-react/zustand'\n * \n * const useStore = create(\n * mimic(clientDocument, (set, get) => ({\n * // Your additional store state\n * }))\n * )\n * \n * // Read snapshot (reactive)\n * const snapshot = useStore(state => state.mimic.snapshot)\n * \n * // Read presence (reactive, if enabled)\n * const myPresence = useStore(state => state.mimic.presence?.self)\n * const othersPresence = useStore(state => state.mimic.presence?.others)\n * \n * // Write via document\n * store.getState().mimic.document.transaction(root => {\n * root.name.set(\"New Name\")\n * })\n * ```\n */\nexport const mimic = mimicImpl as unknown as MimicMiddleware;\n"],"mappings":";;;;;;AAsCA,MAAM,qBAIJ,aACoC;CACpC,MAAM,WAAW,SAAS,WACtB;EACE,QAAQ,SAAS,SAAS,QAAQ;EAClC,MAAM,SAAS,SAAS,MAAM;EAG9B,QAAQ,IAAI,IAAI,SAAS,SAAS,QAAQ,CAAC;EAC3C,KAAK,IAAI,IAAI,SAAS,SAAS,KAAK,CAAC;EACtC,GACD;AAEJ,QAAO;EACL;EACA,UAAU,SAAS,KAAK,YAAY;EAC1B;EACV,aAAa,SAAS,aAAa;EACnC,SAAS,SAAS,SAAS;EAC3B,cAAc,SAAS,iBAAiB;EACxC,mBAAmB,SAAS,mBAAmB;EAChD;;;;;AAMH,MAAMA,aAKJ,UACA,QACA,UAAkC,EAAE,KACjC;CACH,MAAM,EAAE,gBAAgB,MAAM,cAAc,SAAS;AAErD,SAAQ,KAAU,KAAU,QAAa;EAEvC,MAAM,eAAe,kBAAkB,SAAS;EAGhD,MAAM,yBAAyB;GAC7B,MAAM,WAAW,kBAAkB,SAAS;AAC5C,QACG,4CACI,cACH,OAAO,aAET,MACD;;AAIH,MAAI,eAAe;;AACjB,YAAS,UAAU;IACjB,qBAAqB;AACnB,uBAAkB;;IAEpB,0BAA0B;AACxB,uBAAkB;;IAEpB,eAAe;AACb,uBAAkB;;IAErB,CAAC;AAGF,kCAAS,0EAAU,UAAU,EAC3B,wBAAwB;AACtB,sBAAkB;MAErB,CAAC;;AAGJ,MAAI,YACF,UAAS,SAAS;AAQpB,2CAHkB,OAAO,KAAK,KAAK,IAAI,SAKrC,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCb,MAAa,QAAQ"}
@@ -0,0 +1,75 @@
1
+ import { StateCreator, StoreMutatorIdentifier } from "zustand";
2
+ import { ClientDocument } from "@voidhash/mimic/client";
3
+ import { Presence, Primitive } from "@voidhash/mimic";
4
+
5
+ //#region src/zustand/types.d.ts
6
+
7
+ /**
8
+ * Presence data exposed on the zustand store (reactive snapshot).
9
+ */
10
+ interface MimicPresence<TPresence extends Presence.AnyPresence> {
11
+ /**
12
+ * This client's connection ID (set after receiving presence snapshot).
13
+ * Undefined before the snapshot is received.
14
+ */
15
+ readonly selfId: string | undefined;
16
+ /**
17
+ * This client's current presence data.
18
+ * Undefined if not set.
19
+ */
20
+ readonly self: Presence.Infer<TPresence> | undefined;
21
+ /**
22
+ * Other clients' presence entries (connectionId -> entry).
23
+ */
24
+ readonly others: ReadonlyMap<string, Presence.PresenceEntry<Presence.Infer<TPresence>>>;
25
+ /**
26
+ * All presence entries including self (connectionId -> entry).
27
+ */
28
+ readonly all: ReadonlyMap<string, Presence.PresenceEntry<Presence.Infer<TPresence>>>;
29
+ }
30
+ /**
31
+ * The mimic object containing the document and client state.
32
+ * This is added to the zustand store by the middleware.
33
+ */
34
+ interface MimicObject<TSchema extends Primitive.AnyPrimitive, TPresence extends Presence.AnyPresence | undefined = undefined> {
35
+ /** The ClientDocument instance for performing transactions */
36
+ readonly document: ClientDocument.ClientDocument<TSchema, TPresence>;
37
+ /** Read-only snapshot of the document state */
38
+ readonly snapshot: Primitive.InferSnapshot<TSchema>;
39
+ /**
40
+ * Reactive presence snapshot (self + others).
41
+ * Undefined when the ClientDocument was created without a presence schema.
42
+ */
43
+ readonly presence: TPresence extends Presence.AnyPresence ? MimicPresence<TPresence> : undefined;
44
+ /** Whether the client is connected to the server */
45
+ readonly isConnected: boolean;
46
+ /** Whether the client is fully initialized and ready */
47
+ readonly isReady: boolean;
48
+ /** Number of pending transactions */
49
+ readonly pendingCount: number;
50
+ /** Whether there are pending changes */
51
+ readonly hasPendingChanges: boolean;
52
+ }
53
+ /**
54
+ * The state slice added by the mimic middleware.
55
+ */
56
+ interface MimicSlice<TSchema extends Primitive.AnyPrimitive, TPresence extends Presence.AnyPresence | undefined = undefined> {
57
+ /** The mimic object containing document and state */
58
+ readonly mimic: MimicObject<TSchema, TPresence>;
59
+ }
60
+ /**
61
+ * Type for state creator with mimic slice merged.
62
+ */
63
+ type MimicStateCreator<TSchema extends Primitive.AnyPrimitive, TPresence extends Presence.AnyPresence | undefined, T, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []> = StateCreator<T & MimicSlice<TSchema, TPresence>, Mps, Mcs, T>;
64
+ /**
65
+ * Options for the mimic middleware.
66
+ */
67
+ interface MimicMiddlewareOptions {
68
+ /** If true, automatically subscribe when store is created (default: true) */
69
+ readonly autoSubscribe?: boolean;
70
+ /** If true, automatically attempt to connect the document to the remote server */
71
+ readonly autoConnect?: boolean;
72
+ }
73
+ //#endregion
74
+ export { MimicMiddlewareOptions, MimicObject, MimicSlice, MimicStateCreator };
75
+ //# sourceMappingURL=types.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.cts","names":[],"sources":["../../src/zustand/types.ts"],"sourc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,kCACM,SAAA,CAAU,gCACR,QAAA,CAAS,yCAEd,sDACA,2CACX,aAAa,IAAI,WAAW,SAAS,YAAY,KAAK,KAAK;;;;UAK9C,sBAAA"}
@@ -0,0 +1,75 @@
1
+ import { StateCreator, StoreMutatorIdentifier } from "zustand";
2
+ import { ClientDocument } from "@voidhash/mimic/client";
3
+ import { Presence, Primitive } from "@voidhash/mimic";
4
+
5
+ //#region src/zustand/types.d.ts
6
+
7
+ /**
8
+ * Presence data exposed on the zustand store (reactive snapshot).
9
+ */
10
+ interface MimicPresence<TPresence extends Presence.AnyPresence> {
11
+ /**
12
+ * This client's connection ID (set after receiving presence snapshot).
13
+ * Undefined before the snapshot is received.
14
+ */
15
+ readonly selfId: string | undefined;
16
+ /**
17
+ * This client's current presence data.
18
+ * Undefined if not set.
19
+ */
20
+ readonly self: Presence.Infer<TPresence> | undefined;
21
+ /**
22
+ * Other clients' presence entries (connectionId -> entry).
23
+ */
24
+ readonly others: ReadonlyMap<string, Presence.PresenceEntry<Presence.Infer<TPresence>>>;
25
+ /**
26
+ * All presence entries including self (connectionId -> entry).
27
+ */
28
+ readonly all: ReadonlyMap<string, Presence.PresenceEntry<Presence.Infer<TPresence>>>;
29
+ }
30
+ /**
31
+ * The mimic object containing the document and client state.
32
+ * This is added to the zustand store by the middleware.
33
+ */
34
+ interface MimicObject<TSchema extends Primitive.AnyPrimitive, TPresence extends Presence.AnyPresence | undefined = undefined> {
35
+ /** The ClientDocument instance for performing transactions */
36
+ readonly document: ClientDocument.ClientDocument<TSchema, TPresence>;
37
+ /** Read-only snapshot of the document state */
38
+ readonly snapshot: Primitive.InferSnapshot<TSchema>;
39
+ /**
40
+ * Reactive presence snapshot (self + others).
41
+ * Undefined when the ClientDocument was created without a presence schema.
42
+ */
43
+ readonly presence: TPresence extends Presence.AnyPresence ? MimicPresence<TPresence> : undefined;
44
+ /** Whether the client is connected to the server */
45
+ readonly isConnected: boolean;
46
+ /** Whether the client is fully initialized and ready */
47
+ readonly isReady: boolean;
48
+ /** Number of pending transactions */
49
+ readonly pendingCount: number;
50
+ /** Whether there are pending changes */
51
+ readonly hasPendingChanges: boolean;
52
+ }
53
+ /**
54
+ * The state slice added by the mimic middleware.
55
+ */
56
+ interface MimicSlice<TSchema extends Primitive.AnyPrimitive, TPresence extends Presence.AnyPresence | undefined = undefined> {
57
+ /** The mimic object containing document and state */
58
+ readonly mimic: MimicObject<TSchema, TPresence>;
59
+ }
60
+ /**
61
+ * Type for state creator with mimic slice merged.
62
+ */
63
+ type MimicStateCreator<TSchema extends Primitive.AnyPrimitive, TPresence extends Presence.AnyPresence | undefined, T, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []> = StateCreator<T & MimicSlice<TSchema, TPresence>, Mps, Mcs, T>;
64
+ /**
65
+ * Options for the mimic middleware.
66
+ */
67
+ interface MimicMiddlewareOptions {
68
+ /** If true, automatically subscribe when store is created (default: true) */
69
+ readonly autoSubscribe?: boolean;
70
+ /** If true, automatically attempt to connect the document to the remote server */
71
+ readonly autoConnect?: boolean;
72
+ }
73
+ //#endregion
74
+ export { MimicMiddlewareOptions, MimicObject, MimicSlice, MimicStateCreator };
75
+ //# sourceMappingURL=types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.mts","names":[],"sources":["../../src/zustand/types.ts"],"sourcesContent":[],"mappings":";;;;;;;;AAWA;AAAiD,UAAhC,aAAyC,CAAA,kBAAT,QAAA,CAAS,WAAA,CAAA,CAAA;EAW1B;;;;EAO5B,SAAS,MAAA,EAAA,MAAA,GAAA,SAAA;EAFM;;;;EAQH,SAAA,IAAA,EAbC,QAAA,CAAS,KAaV,CAbgB,SAahB,CAAA,GAAA,SAAA;EAAW;AAU3B;;EAEoB,SAAS,MAAA,EApBV,WAoBU,CAAA,MAAA,EAlBzB,QAAA,CAAS,aAkBgB,CAlBF,QAAA,CAAS,KAkBP,CAlBa,SAkBb,CAAA,CAAA,CAAA;EAGsB;;;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