Skip to content

Commit a80174a

Browse files
committed
[persisters] YjsPersister
1 parent 7cc3a32 commit a80174a

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

src/persisters/persister-yjs.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import {
77
Values,
88
} from '../types/store.d';
99
import {IdObj, objEnsure, objHas, objMap, objNew} from '../common/obj';
10-
import {Persister, PersisterListener} from '../types/persisters.d';
1110
import {T, TINYBASE, V} from '../common/strings';
1211
import {Doc as YDoc, YEvent, Map as YMap} from 'yjs';
12+
import {
13+
YjsPersister,
14+
createYjsPersister as createYjsPersisterDecl,
15+
} from '../types/persisters/persister-yjs';
1316
import {arrayForEach, arrayIsEmpty, arrayShift} from '../common/array';
1417
import {ifNotUndefined, isUndefined, size} from '../common/other';
1518
import {Id} from '../types/common.d';
19+
import {PersisterListener} from '../types/persisters.d';
1620
import {createCustomPersister} from '../persisters';
1721
import {mapForEach} from '../common/map';
1822

@@ -174,12 +178,12 @@ const yMapMatch = (
174178
return changed;
175179
};
176180

177-
export const createYjsPersister = (
181+
export const createYjsPersister = ((
178182
store: Store,
179183
yDoc: YDoc,
180184
yMapName = TINYBASE,
181185
onIgnoredError?: (error: any) => void,
182-
): Persister => {
186+
): YjsPersister => {
183187
const yContent: YMap<any> = yDoc.getMap(yMapName);
184188

185189
const getPersisted = async (): Promise<[Tables, Values] | undefined> =>
@@ -219,5 +223,5 @@ export const createYjsPersister = (
219223
delPersisterListener,
220224
onIgnoredError,
221225
['getYDoc', yDoc],
222-
);
223-
};
226+
) as YjsPersister;
227+
}) as typeof createYjsPersisterDecl;

src/types/docs/persisters/persister-yjs.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,38 @@
1111
* @since v4.0.0
1212
*/
1313
/// persister-yjs
14+
/**
15+
* The YjsPersister interface is a minor extension to the Persister interface.
16+
*
17+
* It simply provides an extra getYDoc method for accessing the Yjs document the
18+
* Store is being persisted to.
19+
* @since v4.3.14
20+
*/
21+
/// YjsPersister
22+
{
23+
/**
24+
* The getYDoc method returns the Yjs document the Store is being persisted
25+
* to.
26+
* @returns The Yjs document.
27+
* @example
28+
* This example creates a Persister object against a newly-created Store and
29+
* then gets the Yjs document back out again.
30+
*
31+
* ```js
32+
* const doc = new Y.Doc();
33+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
34+
* const persister = createYjsPersister(store, doc);
35+
*
36+
* console.log(persister.getYDoc() == doc);
37+
* // -> true
38+
*
39+
* persister.destroy();
40+
* ```
41+
* @category Getter
42+
* @since v4.3.14
43+
*/
44+
/// YjsPersister.getYDoc
45+
}
1446
/**
1547
* The createYjsPersister function creates a Persister object that can persist
1648
* the Store to a Yjs document.
@@ -24,9 +56,9 @@
2456
* @param onIgnoredError An optional handler for the errors that the Persister
2557
* would otherwise ignore when trying to save or load data. This is suitable for
2658
* debugging persistence issues in a development environment, since v4.0.4.
27-
* @returns A reference to the new Persister object.
59+
* @returns A reference to the new YjsPersister object.
2860
* @example
29-
* This example creates a Persister object and persists the Store to a Yjs
61+
* This example creates a YjsPersister object and persists the Store to a Yjs
3062
* document.
3163
*
3264
* ```js
@@ -44,8 +76,8 @@
4476
* ```
4577
* @example
4678
* This more complex example uses Yjs updates to keep two Store objects (each
47-
* with their own Persister objects and Yjs documents) in sync with each other.
48-
* We use the `await` keyword extensively for the purpose of ensuring
79+
* with their own YjsPersister objects and Yjs documents) in sync with each
80+
* other. We use the `await` keyword extensively for the purpose of ensuring
4981
* sequentiality in this example.
5082
*
5183
* Typically, real-world synchronization would happen between two systems via a

src/types/persisters/persister-yjs.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import {Persister} from '../persisters.d';
44
import {Store} from '../store.d';
55
import {Doc as YDoc} from 'yjs';
66

7+
/// YjsPersister
8+
export interface YjsPersister extends Persister {
9+
/// YjsPersister.getYDoc
10+
getYDoc: () => YDoc;
11+
}
12+
713
/// createYjsPersister
814
export function createYjsPersister(
915
store: Store,
1016
yDoc: YDoc,
1117
yMapName?: string,
1218
onIgnoredError?: (error: any) => void,
13-
): Persister;
19+
): YjsPersister;

src/types/with-schemas/persisters/persister-yjs.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ import {OptionalSchemas, Store} from '../store.d';
44
import {Persister} from '../persisters.d';
55
import {Doc as YDoc} from 'yjs';
66

7+
/// YjsPersister
8+
export interface YjsPersister<Schemas extends OptionalSchemas>
9+
extends Persister<Schemas> {
10+
/// YjsPersister.getYDoc
11+
getYDoc: () => YDoc;
12+
}
13+
714
/// createYjsPersister
815
export function createYjsPersister<Schemas extends OptionalSchemas>(
916
store: Store<Schemas>,
1017
yDoc: YDoc,
1118
yMapName?: string,
1219
onIgnoredError?: (error: any) => void,
13-
): Persister<Schemas>;
20+
): YjsPersister<Schemas>;

0 commit comments

Comments
 (0)