|
| 1 | +/** |
| 2 | + * Jotai integration for Loro Mirror - Atomic state management with Loro CRDT synchronization |
| 3 | + * |
| 4 | + * This package provides atom-based state management following Jotai's bottom-up approach. |
| 5 | + * Each piece of state is represented as an atom, enabling fine-grained reactivity and composition. |
| 6 | + */ |
| 7 | + |
| 8 | +import { atom, WritableAtom } from 'jotai'; |
| 9 | + |
| 10 | +// Import types only to avoid module resolution issues |
| 11 | +import type { LoroDoc } from "loro-crdt"; |
| 12 | +import { createStore, SchemaType, Store } from "loro-mirror"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Configuration for creating a Loro Mirror atom |
| 16 | + */ |
| 17 | +export interface LoroMirrorAtomConfig<T = any> { |
| 18 | + /** |
| 19 | + * The Loro document to sync with |
| 20 | + */ |
| 21 | + doc: LoroDoc; |
| 22 | + |
| 23 | + /** |
| 24 | + * The schema definition for the state |
| 25 | + */ |
| 26 | + schema: any; |
| 27 | + |
| 28 | + |
| 29 | + /** |
| 30 | + * Initial state (optional) |
| 31 | + */ |
| 32 | + initialState?: Partial<T>; |
| 33 | + |
| 34 | + /** |
| 35 | + * Whether to validate state updates against the schema |
| 36 | + * @default true |
| 37 | + */ |
| 38 | + validateUpdates?: boolean; |
| 39 | + |
| 40 | + /** |
| 41 | + * Whether to throw errors on validation failures |
| 42 | + * @default false |
| 43 | + */ |
| 44 | + throwOnValidationError?: boolean; |
| 45 | + |
| 46 | + /** |
| 47 | + * Debug mode - logs operations |
| 48 | + * @default false |
| 49 | + */ |
| 50 | + debug?: boolean; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +/** |
| 55 | + * Creates a primary state atom that syncs with Loro |
| 56 | + * |
| 57 | + * This is the main atom that holds the synchronized state. |
| 58 | + * It automatically syncs with the Loro document and notifies subscribers. |
| 59 | + * |
| 60 | + * @example |
| 61 | + * ```tsx |
| 62 | + * const todoSchema = schema({ |
| 63 | + * todos: schema.LoroList(schema.LoroMap({ |
| 64 | + * id: schema.String(), |
| 65 | + * text: schema.String(), |
| 66 | + * completed: schema.Boolean({ defaultValue: false }), |
| 67 | + * })), |
| 68 | + * }); |
| 69 | + * |
| 70 | + * const todoAtom = loroAtom({ |
| 71 | + * doc: new LoroDoc(), |
| 72 | + * schema: todoSchema, |
| 73 | + * initialState: { todos: [] }, |
| 74 | + * key: 'todos' |
| 75 | + * }); |
| 76 | + * |
| 77 | + * function TodoApp() { |
| 78 | + * const [state, setState] = useAtom(todoAtom); |
| 79 | + * // Use state and setState... |
| 80 | + * } |
| 81 | + * ``` |
| 82 | + */ |
| 83 | +export function loroMirrorAtom<T = any>( |
| 84 | + config: LoroMirrorAtomConfig<T> |
| 85 | +): WritableAtom<T, [T | ((prev: T) => T)], void> { |
| 86 | + const store = createStore(config); |
| 87 | + const stateAtom = atom(store.getState()); |
| 88 | + let sub: () => void | undefined; |
| 89 | + const initAtom = atom(null, async (_get, set, action: "init" | "destroy") => { |
| 90 | + if (action === "init") { |
| 91 | + sub = store.subscribe((state) => { |
| 92 | + set(stateAtom, state); |
| 93 | + }); |
| 94 | + } else { |
| 95 | + sub?.() |
| 96 | + } |
| 97 | + }) |
| 98 | + |
| 99 | + initAtom.onMount = (action) => { |
| 100 | + action("init"); |
| 101 | + return () => { |
| 102 | + action("destroy"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + const base = atom( |
| 107 | + (get) => { |
| 108 | + get(initAtom) |
| 109 | + return get(stateAtom); |
| 110 | + }, |
| 111 | + (get, _set, update) => { |
| 112 | + const currentState = get(stateAtom); |
| 113 | + if (typeof update === 'function') { |
| 114 | + const newState = (update as (prev: T) => T)(currentState); |
| 115 | + store.setState(newState as Partial<T>); |
| 116 | + } else { |
| 117 | + store.setState(update as Partial<T>); |
| 118 | + } |
| 119 | + } |
| 120 | + ); |
| 121 | + |
| 122 | + return base; |
| 123 | +} |
0 commit comments