@markwasfy/loko-react 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 loko contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # @loko/react
2
+
3
+ React bindings for [`loko`](https://www.npmjs.com/package/loko) — offline storage + sync, with reactive hooks.
4
+
5
+ ```bash
6
+ npm install loko @loko/react
7
+ ```
8
+
9
+ ```tsx
10
+ import { SyncProvider, useCollection, useSyncStatus } from "@loko/react";
11
+ import { createSync, indexedDBStorage, restAdapter } from "loko";
12
+
13
+ const sync = createSync({
14
+ name: "myapp",
15
+ storage: indexedDBStorage(),
16
+ adapter: restAdapter({ url: "/api/sync" }),
17
+ });
18
+
19
+ function Todos() {
20
+ const { items, put, remove, loading } = useCollection<Todo>("todos");
21
+ const { status, sync: syncNow } = useSyncStatus();
22
+ if (loading) return <p>Loading…</p>;
23
+ return (
24
+ <>
25
+ <button onClick={() => syncNow()}>{status}</button>
26
+ {items.map((t) => (
27
+ <label key={t.id}>
28
+ <input type="checkbox" checked={t.done} onChange={() => put({ ...t, done: !t.done })} />
29
+ {t.text} <button onClick={() => remove(t.id)}>✕</button>
30
+ </label>
31
+ ))}
32
+ </>
33
+ );
34
+ }
35
+
36
+ export default function App() {
37
+ return (
38
+ <SyncProvider sync={sync}>
39
+ <Todos />
40
+ </SyncProvider>
41
+ );
42
+ }
43
+ ```
44
+
45
+ ## Hooks
46
+
47
+ - `useCollection<T>(name)` → `{ items, loading, put, remove }`
48
+ - `useDocument<T>(name, id)` → `{ item, loading, put, remove }`
49
+ - `useSyncStatus()` → `{ status, sync }`
50
+ - `useSync()` → the `Sync` instance from context
51
+
52
+ All hooks are built on `useSyncExternalStore` for correct concurrent-mode behavior and re-render on local, sync, and cross-tab changes.
53
+
54
+ Requires React 18+. License: MIT.
package/dist/index.cjs ADDED
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+
5
+ // src/index.ts
6
+ var SyncContext = react.createContext(null);
7
+ function SyncProvider({ sync, children }) {
8
+ return react.createElement(SyncContext.Provider, { value: sync }, children);
9
+ }
10
+ function useSync() {
11
+ const sync = react.useContext(SyncContext);
12
+ if (!sync) {
13
+ throw new Error("useSync(): no <SyncProvider> found in the component tree.");
14
+ }
15
+ return sync;
16
+ }
17
+ function useCollection(name) {
18
+ const sync = useSync();
19
+ const collection = react.useMemo(() => sync.collection(name), [sync, name]);
20
+ const snapshot = react.useRef({ items: [], loading: true });
21
+ const subscribe = react.useCallback(
22
+ (onStoreChange) => collection.subscribe((items2) => {
23
+ snapshot.current = { items: items2, loading: false };
24
+ onStoreChange();
25
+ }),
26
+ [collection]
27
+ );
28
+ const getSnapshot = react.useCallback(() => snapshot.current, []);
29
+ const { items, loading } = react.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
30
+ const put = react.useCallback((record) => collection.put(record), [collection]);
31
+ const remove = react.useCallback((id) => collection.delete(id), [collection]);
32
+ return { items, loading, put, remove };
33
+ }
34
+ function useDocument(name, id) {
35
+ const sync = useSync();
36
+ const collection = react.useMemo(() => sync.collection(name), [sync, name]);
37
+ const snapshot = react.useRef({
38
+ item: void 0,
39
+ loading: true
40
+ });
41
+ const subscribe = react.useCallback(
42
+ (onStoreChange) => collection.subscribeOne(id, (item2) => {
43
+ snapshot.current = { item: item2, loading: false };
44
+ onStoreChange();
45
+ }),
46
+ [collection, id]
47
+ );
48
+ const getSnapshot = react.useCallback(() => snapshot.current, []);
49
+ const { item, loading } = react.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
50
+ const put = react.useCallback((record) => collection.put(record), [collection]);
51
+ const remove = react.useCallback(() => collection.delete(id), [collection, id]);
52
+ return { item, loading, put, remove };
53
+ }
54
+ function useSyncStatus() {
55
+ const sync = useSync();
56
+ const status = react.useRef(sync.status);
57
+ const subscribe = react.useCallback(
58
+ (onStoreChange) => sync.on("status", ({ status: s }) => {
59
+ status.current = s;
60
+ onStoreChange();
61
+ }),
62
+ [sync]
63
+ );
64
+ const getSnapshot = react.useCallback(() => status.current, []);
65
+ const current = react.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
66
+ const trigger = react.useCallback(() => sync.sync(), [sync]);
67
+ return { status: current, sync: trigger };
68
+ }
69
+
70
+ exports.SyncProvider = SyncProvider;
71
+ exports.useCollection = useCollection;
72
+ exports.useDocument = useDocument;
73
+ exports.useSync = useSync;
74
+ exports.useSyncStatus = useSyncStatus;
75
+ //# sourceMappingURL=index.cjs.map
76
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["createContext","createElement","useContext","useMemo","useRef","useCallback","items","useSyncExternalStore","item"],"mappings":";;;;;AAYA,IAAM,WAAA,GAAcA,oBAA2B,IAA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AY,EAAE,CAAC,CAAA;AAExE,EAAA,OAAO,EAAE,IAAA,EAAM,OAAA,EAAS,GAAA,EAAK,MAAA,EAAO;AACtC;AAQO,SAAS,aAAA,GAAqC;AACnD,EAAA,MAAM,OAAO,OAAA,EAAQ;AACrB,EAAA,MAAM,MAAA,GAASD,YAAA,CAAmB,IAAA,CAAK,MAAM,CAAA;AAE7C,EAAA,MAAM,SAAA,GAAYC,iBAAA;AAAA,IAChB,CAAC,kBACC,IAAA,CAAK,EAAA,CAAG,UAAU,CAAC,EAAE,MAAA,EAAQ,CAAA,EAAE,KAAM;AACnC,MAAA,MAAA,CAAO,OAAA,GAAU,CAAA;AACjB,MAAA,aAAA,EAAc;AAAA,IAChB,CAAC,CAAA;AAAA,IACH,CAAC,IAAI;AAAA,GACP;AAEA,EAAA,MAAM,cAAcA,iBAAA,CAAY,MAAM,MAAA,CAAO,OAAA,EAAS,EAAE,CAAA;AACxD,EAAA,MAAM,OAAA,GAAUE,0BAAA,CAAqB,SAAA,EAAW,WAAA,EAAa,WAAW,CAAA;AAExE,EAAA,MAAM,OAAA,GAAUF,kBAAY,MAAM,IAAA,CAAK,MAAK,EAAG,CAAC,IAAI,CAAC,CAAA;AACrD,EAAA,OAAO,EAAE,MAAA,EAAQ,OAAA,EAAS,IAAA,EAAM,OAAA,EAAQ;AAC1C","file":"index.cjs","sourcesContent":["import type { Sync, SyncStatus } from \"@markwasfy/loko\";\nimport {\n createContext,\n createElement,\n useCallback,\n useContext,\n useMemo,\n useRef,\n useSyncExternalStore,\n type ReactNode,\n} from \"react\";\n\nconst SyncContext = createContext<Sync | null>(null);\n\nexport interface SyncProviderProps {\n sync: Sync;\n children?: ReactNode;\n}\n\n/** Provides a {@link Sync} instance to descendant hooks. */\nexport function SyncProvider({ sync, children }: SyncProviderProps): ReactNode {\n return createElement(SyncContext.Provider, { value: sync }, children);\n}\n\n/** Access the Sync instance from context. Throws if no provider is mounted. */\nexport function useSync(): Sync {\n const sync = useContext(SyncContext);\n if (!sync) {\n throw new Error(\"useSync(): no <SyncProvider> found in the component tree.\");\n }\n return sync;\n}\n\nexport interface UseCollectionResult<T> {\n items: T[];\n loading: boolean;\n put: (record: T) => Promise<T>;\n remove: (id: string) => Promise<void>;\n}\n\n/**\n * Subscribe a component to a collection. Re-renders on any change (local,\n * sync, or another tab). Built on `useSyncExternalStore`.\n */\nexport function useCollection<T extends Record<string, unknown> = Record<string, unknown>>(\n name: string,\n): UseCollectionResult<T> {\n const sync = useSync();\n const collection = useMemo(() => sync.collection<T>(name), [sync, name]);\n\n // Cache the snapshot as a stable object that changes on every emission so\n // useSyncExternalStore doesn't bail out (e.g. when an empty result repeats).\n const snapshot = useRef<{ items: T[]; loading: boolean }>({ items: [], loading: true });\n\n const subscribe = useCallback(\n (onStoreChange: () => void) =>\n collection.subscribe((items) => {\n snapshot.current = { items, loading: false };\n onStoreChange();\n }),\n [collection],\n );\n\n const getSnapshot = useCallback(() => snapshot.current, []);\n const { items, loading } = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n const put = useCallback((record: T) => collection.put(record), [collection]);\n const remove = useCallback((id: string) => collection.delete(id), [collection]);\n\n return { items, loading, put, remove };\n}\n\nexport interface UseDocumentResult<T> {\n item: T | undefined;\n loading: boolean;\n put: (record: T) => Promise<T>;\n remove: () => Promise<void>;\n}\n\n/** Subscribe a component to a single record by id. */\nexport function useDocument<T extends Record<string, unknown> = Record<string, unknown>>(\n name: string,\n id: string,\n): UseDocumentResult<T> {\n const sync = useSync();\n const collection = useMemo(() => sync.collection<T>(name), [sync, name]);\n\n const snapshot = useRef<{ item: T | undefined; loading: boolean }>({\n item: undefined,\n loading: true,\n });\n\n const subscribe = useCallback(\n (onStoreChange: () => void) =>\n collection.subscribeOne(id, (item) => {\n snapshot.current = { item, loading: false };\n onStoreChange();\n }),\n [collection, id],\n );\n\n const getSnapshot = useCallback(() => snapshot.current, []);\n const { item, loading } = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n const put = useCallback((record: T) => collection.put(record), [collection]);\n const remove = useCallback(() => collection.delete(id), [collection, id]);\n\n return { item, loading, put, remove };\n}\n\nexport interface UseSyncStatusResult {\n status: SyncStatus;\n sync: () => Promise<{ pushed: number; pulled: number }>;\n}\n\n/** Track sync status reactively and expose a manual trigger. */\nexport function useSyncStatus(): UseSyncStatusResult {\n const sync = useSync();\n const status = useRef<SyncStatus>(sync.status);\n\n const subscribe = useCallback(\n (onStoreChange: () => void) =>\n sync.on(\"status\", ({ status: s }) => {\n status.current = s;\n onStoreChange();\n }),\n [sync],\n );\n\n const getSnapshot = useCallback(() => status.current, []);\n const current = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n const trigger = useCallback(() => sync.sync(), [sync]);\n return { status: current, sync: trigger };\n}\n"]}
@@ -0,0 +1,41 @@
1
+ import { Sync, SyncStatus } from '@markwasfy/loko';
2
+ import { ReactNode } from 'react';
3
+
4
+ interface SyncProviderProps {
5
+ sync: Sync;
6
+ children?: ReactNode;
7
+ }
8
+ /** Provides a {@link Sync} instance to descendant hooks. */
9
+ declare function SyncProvider({ sync, children }: SyncProviderProps): ReactNode;
10
+ /** Access the Sync instance from context. Throws if no provider is mounted. */
11
+ declare function useSync(): Sync;
12
+ interface UseCollectionResult<T> {
13
+ items: T[];
14
+ loading: boolean;
15
+ put: (record: T) => Promise<T>;
16
+ remove: (id: string) => Promise<void>;
17
+ }
18
+ /**
19
+ * Subscribe a component to a collection. Re-renders on any change (local,
20
+ * sync, or another tab). Built on `useSyncExternalStore`.
21
+ */
22
+ declare function useCollection<T extends Record<string, unknown> = Record<string, unknown>>(name: string): UseCollectionResult<T>;
23
+ interface UseDocumentResult<T> {
24
+ item: T | undefined;
25
+ loading: boolean;
26
+ put: (record: T) => Promise<T>;
27
+ remove: () => Promise<void>;
28
+ }
29
+ /** Subscribe a component to a single record by id. */
30
+ declare function useDocument<T extends Record<string, unknown> = Record<string, unknown>>(name: string, id: string): UseDocumentResult<T>;
31
+ interface UseSyncStatusResult {
32
+ status: SyncStatus;
33
+ sync: () => Promise<{
34
+ pushed: number;
35
+ pulled: number;
36
+ }>;
37
+ }
38
+ /** Track sync status reactively and expose a manual trigger. */
39
+ declare function useSyncStatus(): UseSyncStatusResult;
40
+
41
+ export { SyncProvider, type SyncProviderProps, type UseCollectionResult, type UseDocumentResult, type UseSyncStatusResult, useCollection, useDocument, useSync, useSyncStatus };
@@ -0,0 +1,41 @@
1
+ import { Sync, SyncStatus } from '@markwasfy/loko';
2
+ import { ReactNode } from 'react';
3
+
4
+ interface SyncProviderProps {
5
+ sync: Sync;
6
+ children?: ReactNode;
7
+ }
8
+ /** Provides a {@link Sync} instance to descendant hooks. */
9
+ declare function SyncProvider({ sync, children }: SyncProviderProps): ReactNode;
10
+ /** Access the Sync instance from context. Throws if no provider is mounted. */
11
+ declare function useSync(): Sync;
12
+ interface UseCollectionResult<T> {
13
+ items: T[];
14
+ loading: boolean;
15
+ put: (record: T) => Promise<T>;
16
+ remove: (id: string) => Promise<void>;
17
+ }
18
+ /**
19
+ * Subscribe a component to a collection. Re-renders on any change (local,
20
+ * sync, or another tab). Built on `useSyncExternalStore`.
21
+ */
22
+ declare function useCollection<T extends Record<string, unknown> = Record<string, unknown>>(name: string): UseCollectionResult<T>;
23
+ interface UseDocumentResult<T> {
24
+ item: T | undefined;
25
+ loading: boolean;
26
+ put: (record: T) => Promise<T>;
27
+ remove: () => Promise<void>;
28
+ }
29
+ /** Subscribe a component to a single record by id. */
30
+ declare function useDocument<T extends Record<string, unknown> = Record<string, unknown>>(name: string, id: string): UseDocumentResult<T>;
31
+ interface UseSyncStatusResult {
32
+ status: SyncStatus;
33
+ sync: () => Promise<{
34
+ pushed: number;
35
+ pulled: number;
36
+ }>;
37
+ }
38
+ /** Track sync status reactively and expose a manual trigger. */
39
+ declare function useSyncStatus(): UseSyncStatusResult;
40
+
41
+ export { SyncProvider, type SyncProviderProps, type UseCollectionResult, type UseDocumentResult, type UseSyncStatusResult, useCollection, useDocument, useSync, useSyncStatus };
package/dist/index.js ADDED
@@ -0,0 +1,70 @@
1
+ import { createContext, createElement, useContext, useMemo, useRef, useCallback, useSyncExternalStore } from 'react';
2
+
3
+ // src/index.ts
4
+ var SyncContext = createContext(null);
5
+ function SyncProvider({ sync, children }) {
6
+ return createElement(SyncContext.Provider, { value: sync }, children);
7
+ }
8
+ function useSync() {
9
+ const sync = useContext(SyncContext);
10
+ if (!sync) {
11
+ throw new Error("useSync(): no <SyncProvider> found in the component tree.");
12
+ }
13
+ return sync;
14
+ }
15
+ function useCollection(name) {
16
+ const sync = useSync();
17
+ const collection = useMemo(() => sync.collection(name), [sync, name]);
18
+ const snapshot = useRef({ items: [], loading: true });
19
+ const subscribe = useCallback(
20
+ (onStoreChange) => collection.subscribe((items2) => {
21
+ snapshot.current = { items: items2, loading: false };
22
+ onStoreChange();
23
+ }),
24
+ [collection]
25
+ );
26
+ const getSnapshot = useCallback(() => snapshot.current, []);
27
+ const { items, loading } = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
28
+ const put = useCallback((record) => collection.put(record), [collection]);
29
+ const remove = useCallback((id) => collection.delete(id), [collection]);
30
+ return { items, loading, put, remove };
31
+ }
32
+ function useDocument(name, id) {
33
+ const sync = useSync();
34
+ const collection = useMemo(() => sync.collection(name), [sync, name]);
35
+ const snapshot = useRef({
36
+ item: void 0,
37
+ loading: true
38
+ });
39
+ const subscribe = useCallback(
40
+ (onStoreChange) => collection.subscribeOne(id, (item2) => {
41
+ snapshot.current = { item: item2, loading: false };
42
+ onStoreChange();
43
+ }),
44
+ [collection, id]
45
+ );
46
+ const getSnapshot = useCallback(() => snapshot.current, []);
47
+ const { item, loading } = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
48
+ const put = useCallback((record) => collection.put(record), [collection]);
49
+ const remove = useCallback(() => collection.delete(id), [collection, id]);
50
+ return { item, loading, put, remove };
51
+ }
52
+ function useSyncStatus() {
53
+ const sync = useSync();
54
+ const status = useRef(sync.status);
55
+ const subscribe = useCallback(
56
+ (onStoreChange) => sync.on("status", ({ status: s }) => {
57
+ status.current = s;
58
+ onStoreChange();
59
+ }),
60
+ [sync]
61
+ );
62
+ const getSnapshot = useCallback(() => status.current, []);
63
+ const current = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
64
+ const trigger = useCallback(() => sync.sync(), [sync]);
65
+ return { status: current, sync: trigger };
66
+ }
67
+
68
+ export { SyncProvider, useCollection, useDocument, useSync, useSyncStatus };
69
+ //# sourceMappingURL=index.js.map
70
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["items","item"],"mappings":";;;AAYA,IAAM,WAAA,GAAc,cAA2B,IAAI,CAAA;AAQ5C,SAAS,YAAA,CAAa,EAAE,IAAA,EAAM,QAAA,EAAS,EAAiC;AAC7E,EAAA,OAAO,cAAc,WAAA,CAAY,QAAA,EAAU,EAAE,KAAA,EAAO,IAAA,IAAQ,QAAQ,CAAA;AACtE;AAGO,SAAS,OAAA,GAAgB;AAC9B,EAAA,MAAM,IAAA,GAAO,WAAW,WAAW,CAAA;AACnC,EAAA,IAAI,CAAC,IAAA,EAAM;AACT,IAAA,MAAM,IAAI,MAAM,2DAA2D,CAAA;AAAA,EAC7E;AACA,EAAA,OAAO,IAAA;AACT;AAaO,SAAS,cACd,IAAA,EACwB;AACxB,EAAA,MAAM,OAAO,OAAA,EAAQ;AACrB,EAAA,MAAM,UAAA,GAAa,OAAA,CAAQ,MAAM,IAAA,CAAK,UAAA,CAAc,IAAI,CAAA,EAAG,CAAC,IAAA,EAAM,IAAI,CAAC,CAAA;AAIvE,EAAA,MAAM,QAAA,GAAW,OAAyC,EAAE,KAAA,EAAO,EAAC,EAAG,OAAA,EAAS,MAAM,CAAA;AAEtF,EAAA,MAAM,SAAA,GAAY,WAAA;AAAA,IAChB,CAAC,aAAA,KACC,UAAA,CAAW,SAAA,CAAU,CAACA,MAAAA,KAAU;AAC9B,MAAA,QAAA,CAAS,OAAA,GA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js","sourcesContent":["import type { Sync, SyncStatus } from \"@markwasfy/loko\";\nimport {\n createContext,\n createElement,\n useCallback,\n useContext,\n useMemo,\n useRef,\n useSyncExternalStore,\n type ReactNode,\n} from \"react\";\n\nconst SyncContext = createContext<Sync | null>(null);\n\nexport interface SyncProviderProps {\n sync: Sync;\n children?: ReactNode;\n}\n\n/** Provides a {@link Sync} instance to descendant hooks. */\nexport function SyncProvider({ sync, children }: SyncProviderProps): ReactNode {\n return createElement(SyncContext.Provider, { value: sync }, children);\n}\n\n/** Access the Sync instance from context. Throws if no provider is mounted. */\nexport function useSync(): Sync {\n const sync = useContext(SyncContext);\n if (!sync) {\n throw new Error(\"useSync(): no <SyncProvider> found in the component tree.\");\n }\n return sync;\n}\n\nexport interface UseCollectionResult<T> {\n items: T[];\n loading: boolean;\n put: (record: T) => Promise<T>;\n remove: (id: string) => Promise<void>;\n}\n\n/**\n * Subscribe a component to a collection. Re-renders on any change (local,\n * sync, or another tab). Built on `useSyncExternalStore`.\n */\nexport function useCollection<T extends Record<string, unknown> = Record<string, unknown>>(\n name: string,\n): UseCollectionResult<T> {\n const sync = useSync();\n const collection = useMemo(() => sync.collection<T>(name), [sync, name]);\n\n // Cache the snapshot as a stable object that changes on every emission so\n // useSyncExternalStore doesn't bail out (e.g. when an empty result repeats).\n const snapshot = useRef<{ items: T[]; loading: boolean }>({ items: [], loading: true });\n\n const subscribe = useCallback(\n (onStoreChange: () => void) =>\n collection.subscribe((items) => {\n snapshot.current = { items, loading: false };\n onStoreChange();\n }),\n [collection],\n );\n\n const getSnapshot = useCallback(() => snapshot.current, []);\n const { items, loading } = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n const put = useCallback((record: T) => collection.put(record), [collection]);\n const remove = useCallback((id: string) => collection.delete(id), [collection]);\n\n return { items, loading, put, remove };\n}\n\nexport interface UseDocumentResult<T> {\n item: T | undefined;\n loading: boolean;\n put: (record: T) => Promise<T>;\n remove: () => Promise<void>;\n}\n\n/** Subscribe a component to a single record by id. */\nexport function useDocument<T extends Record<string, unknown> = Record<string, unknown>>(\n name: string,\n id: string,\n): UseDocumentResult<T> {\n const sync = useSync();\n const collection = useMemo(() => sync.collection<T>(name), [sync, name]);\n\n const snapshot = useRef<{ item: T | undefined; loading: boolean }>({\n item: undefined,\n loading: true,\n });\n\n const subscribe = useCallback(\n (onStoreChange: () => void) =>\n collection.subscribeOne(id, (item) => {\n snapshot.current = { item, loading: false };\n onStoreChange();\n }),\n [collection, id],\n );\n\n const getSnapshot = useCallback(() => snapshot.current, []);\n const { item, loading } = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n const put = useCallback((record: T) => collection.put(record), [collection]);\n const remove = useCallback(() => collection.delete(id), [collection, id]);\n\n return { item, loading, put, remove };\n}\n\nexport interface UseSyncStatusResult {\n status: SyncStatus;\n sync: () => Promise<{ pushed: number; pulled: number }>;\n}\n\n/** Track sync status reactively and expose a manual trigger. */\nexport function useSyncStatus(): UseSyncStatusResult {\n const sync = useSync();\n const status = useRef<SyncStatus>(sync.status);\n\n const subscribe = useCallback(\n (onStoreChange: () => void) =>\n sync.on(\"status\", ({ status: s }) => {\n status.current = s;\n onStoreChange();\n }),\n [sync],\n );\n\n const getSnapshot = useCallback(() => status.current, []);\n const current = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n const trigger = useCallback(() => sync.sync(), [sync]);\n return { status: current, sync: trigger };\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@markwasfy/loko-react",
3
+ "version": "0.1.0",
4
+ "description": "React bindings for loko — offline storage + sync hooks (useCollection, useDocument, useSyncStatus).",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "keywords": [
9
+ "loko",
10
+ "offline",
11
+ "sync",
12
+ "react",
13
+ "hooks",
14
+ "indexeddb",
15
+ "local-first"
16
+ ],
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/MarkWasfy00/loko.git",
23
+ "directory": "packages/react"
24
+ },
25
+ "homepage": "https://github.com/MarkWasfy00/loko#readme",
26
+ "main": "./dist/index.cjs",
27
+ "module": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js",
33
+ "require": "./dist/index.cjs"
34
+ }
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "README.md"
39
+ ],
40
+ "peerDependencies": {
41
+ "react": ">=18",
42
+ "@markwasfy/loko": "^0.1.0"
43
+ },
44
+ "devDependencies": {
45
+ "@testing-library/react": "^16.1.0",
46
+ "@types/react": "^18.3.18",
47
+ "@types/react-dom": "^18.3.5",
48
+ "react": "^18.3.1",
49
+ "react-dom": "^18.3.1",
50
+ "tsup": "^8.3.5",
51
+ "typescript": "^5.7.2",
52
+ "@markwasfy/loko": "0.1.0"
53
+ },
54
+ "scripts": {
55
+ "build": "tsup",
56
+ "typecheck": "tsc --noEmit"
57
+ }
58
+ }