tinybase 5.0.0-beta.27 → 5.0.0-beta.28
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/@types/_internal/ui-react/with-schemas/index.d.cts +274 -0
- package/@types/_internal/ui-react/with-schemas/index.d.ts +274 -0
- package/@types/checkpoints/index.d.cts +30 -1
- package/@types/checkpoints/index.d.ts +30 -1
- package/@types/checkpoints/with-schemas/index.d.cts +30 -1
- package/@types/checkpoints/with-schemas/index.d.ts +30 -1
- package/@types/common/index.d.cts +9 -0
- package/@types/common/index.d.ts +9 -0
- package/@types/common/with-schemas/index.d.cts +9 -0
- package/@types/common/with-schemas/index.d.ts +9 -0
- package/@types/index.d.cts +1 -0
- package/@types/index.d.ts +1 -0
- package/@types/indexes/index.d.cts +32 -1
- package/@types/indexes/index.d.ts +32 -1
- package/@types/indexes/with-schemas/index.d.cts +32 -1
- package/@types/indexes/with-schemas/index.d.ts +32 -1
- package/@types/mergeable-store/index.d.cts +0 -2
- package/@types/mergeable-store/index.d.ts +0 -2
- package/@types/mergeable-store/with-schemas/index.d.cts +2 -2
- package/@types/mergeable-store/with-schemas/index.d.ts +2 -2
- package/@types/metrics/index.d.cts +30 -4
- package/@types/metrics/index.d.ts +30 -4
- package/@types/metrics/with-schemas/index.d.cts +30 -4
- package/@types/metrics/with-schemas/index.d.ts +30 -4
- package/@types/persisters/index.d.cts +87 -25
- package/@types/persisters/index.d.ts +87 -25
- package/@types/persisters/persister-automerge/index.d.cts +4 -4
- package/@types/persisters/persister-automerge/index.d.ts +4 -4
- package/@types/persisters/persister-automerge/with-schemas/index.d.cts +4 -4
- package/@types/persisters/persister-automerge/with-schemas/index.d.ts +4 -4
- package/@types/persisters/persister-browser/index.d.cts +7 -4
- package/@types/persisters/persister-browser/index.d.ts +7 -4
- package/@types/persisters/persister-browser/with-schemas/index.d.cts +7 -4
- package/@types/persisters/persister-browser/with-schemas/index.d.ts +7 -4
- package/@types/persisters/persister-cr-sqlite-wasm/index.d.cts +4 -4
- package/@types/persisters/persister-cr-sqlite-wasm/index.d.ts +4 -4
- package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.cts +4 -4
- package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.ts +4 -4
- package/@types/persisters/persister-electric-sql/index.d.cts +4 -4
- package/@types/persisters/persister-electric-sql/index.d.ts +4 -4
- package/@types/persisters/persister-electric-sql/with-schemas/index.d.cts +4 -4
- package/@types/persisters/persister-electric-sql/with-schemas/index.d.ts +4 -4
- package/@types/persisters/persister-expo-sqlite/index.d.cts +4 -4
- package/@types/persisters/persister-expo-sqlite/index.d.ts +4 -4
- package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.cts +4 -4
- package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.ts +4 -4
- package/@types/persisters/persister-file/index.d.cts +6 -4
- package/@types/persisters/persister-file/index.d.ts +6 -4
- package/@types/persisters/persister-file/with-schemas/index.d.cts +6 -4
- package/@types/persisters/persister-file/with-schemas/index.d.ts +6 -4
- package/@types/persisters/persister-indexed-db/index.d.cts +5 -4
- package/@types/persisters/persister-indexed-db/index.d.ts +5 -4
- package/@types/persisters/persister-indexed-db/with-schemas/index.d.cts +5 -4
- package/@types/persisters/persister-indexed-db/with-schemas/index.d.ts +5 -4
- package/@types/persisters/persister-libsql/index.d.cts +5 -4
- package/@types/persisters/persister-libsql/index.d.ts +5 -4
- package/@types/persisters/persister-libsql/with-schemas/index.d.cts +5 -4
- package/@types/persisters/persister-libsql/with-schemas/index.d.ts +5 -4
- package/@types/persisters/persister-partykit-client/index.d.cts +12 -6
- package/@types/persisters/persister-partykit-client/index.d.ts +12 -6
- package/@types/persisters/persister-partykit-client/with-schemas/index.d.cts +12 -6
- package/@types/persisters/persister-partykit-client/with-schemas/index.d.ts +12 -6
- package/@types/persisters/persister-partykit-server/index.d.cts +11 -3
- package/@types/persisters/persister-partykit-server/index.d.ts +11 -3
- package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +11 -3
- package/@types/persisters/persister-partykit-server/with-schemas/index.d.ts +11 -3
- package/@types/persisters/persister-powersync/index.d.cts +5 -4
- package/@types/persisters/persister-powersync/index.d.ts +5 -4
- package/@types/persisters/persister-powersync/with-schemas/index.d.cts +5 -4
- package/@types/persisters/persister-powersync/with-schemas/index.d.ts +5 -4
- package/@types/persisters/persister-remote/index.d.cts +6 -4
- package/@types/persisters/persister-remote/index.d.ts +6 -4
- package/@types/persisters/persister-remote/with-schemas/index.d.cts +6 -4
- package/@types/persisters/persister-remote/with-schemas/index.d.ts +6 -4
- package/@types/persisters/persister-sqlite-wasm/index.d.cts +4 -4
- package/@types/persisters/persister-sqlite-wasm/index.d.ts +4 -4
- package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.cts +4 -4
- package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.ts +4 -4
- package/@types/persisters/persister-sqlite3/index.d.cts +4 -4
- package/@types/persisters/persister-sqlite3/index.d.ts +4 -4
- package/@types/persisters/persister-sqlite3/with-schemas/index.d.cts +4 -4
- package/@types/persisters/persister-sqlite3/with-schemas/index.d.ts +4 -4
- package/@types/persisters/persister-yjs/index.d.cts +4 -4
- package/@types/persisters/persister-yjs/index.d.ts +4 -4
- package/@types/persisters/persister-yjs/with-schemas/index.d.cts +4 -4
- package/@types/persisters/persister-yjs/with-schemas/index.d.ts +4 -4
- package/@types/persisters/with-schemas/index.d.cts +87 -25
- package/@types/persisters/with-schemas/index.d.ts +87 -25
- package/@types/queries/index.d.cts +64 -4
- package/@types/queries/index.d.ts +64 -4
- package/@types/queries/with-schemas/index.d.cts +64 -4
- package/@types/queries/with-schemas/index.d.ts +64 -4
- package/@types/relationships/index.d.cts +34 -1
- package/@types/relationships/index.d.ts +34 -1
- package/@types/relationships/with-schemas/index.d.cts +34 -1
- package/@types/relationships/with-schemas/index.d.ts +34 -1
- package/@types/store/index.d.cts +125 -0
- package/@types/store/index.d.ts +125 -0
- package/@types/store/with-schemas/index.d.cts +125 -0
- package/@types/store/with-schemas/index.d.ts +125 -0
- package/@types/synchronizers/index.d.cts +32 -10
- package/@types/synchronizers/index.d.ts +32 -10
- package/@types/synchronizers/synchronizer-broadcast-channel/index.d.cts +114 -0
- package/@types/synchronizers/synchronizer-broadcast-channel/index.d.ts +114 -0
- package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.cts +128 -0
- package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.ts +128 -0
- package/@types/synchronizers/synchronizer-local/index.d.cts +16 -17
- package/@types/synchronizers/synchronizer-local/index.d.ts +16 -17
- package/@types/synchronizers/synchronizer-local/with-schemas/index.d.cts +16 -17
- package/@types/synchronizers/synchronizer-local/with-schemas/index.d.ts +16 -17
- package/@types/synchronizers/synchronizer-ws-client/index.d.cts +66 -6
- package/@types/synchronizers/synchronizer-ws-client/index.d.ts +66 -6
- package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.cts +77 -6
- package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.ts +77 -6
- package/@types/synchronizers/synchronizer-ws-server/index.d.cts +486 -7
- package/@types/synchronizers/synchronizer-ws-server/index.d.ts +486 -7
- package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.cts +486 -7
- package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.ts +486 -7
- package/@types/synchronizers/with-schemas/index.d.cts +29 -11
- package/@types/synchronizers/with-schemas/index.d.ts +29 -11
- package/@types/tools/index.d.cts +22 -0
- package/@types/tools/index.d.ts +22 -0
- package/@types/tools/with-schemas/index.d.cts +22 -0
- package/@types/tools/with-schemas/index.d.ts +22 -0
- package/@types/ui-react/index.d.cts +375 -1
- package/@types/ui-react/index.d.ts +375 -1
- package/@types/ui-react/with-schemas/index.d.cts +100 -1
- package/@types/ui-react/with-schemas/index.d.ts +100 -1
- package/@types/ui-react-dom/index.d.cts +112 -0
- package/@types/ui-react-dom/index.d.ts +112 -0
- package/@types/ui-react-dom/with-schemas/index.d.cts +112 -0
- package/@types/ui-react-dom/with-schemas/index.d.ts +112 -0
- package/@types/ui-react-inspector/index.d.cts +6 -2
- package/@types/ui-react-inspector/index.d.ts +6 -2
- package/@types/ui-react-inspector/with-schemas/index.d.cts +118 -2
- package/@types/ui-react-inspector/with-schemas/index.d.ts +118 -2
- package/@types/with-schemas/index.d.cts +1 -0
- package/@types/with-schemas/index.d.ts +1 -0
- package/cjs/es6/index.cjs +2 -2
- package/cjs/es6/min/synchronizers/synchronizer-broadcast-channel/index.cjs +1 -0
- package/cjs/es6/min/synchronizers/synchronizer-broadcast-channel/index.cjs.gz +0 -0
- package/cjs/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs +1 -0
- package/cjs/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs.gz +0 -0
- package/cjs/es6/min/synchronizers/synchronizer-ws-client/index.cjs +1 -1
- package/cjs/es6/min/synchronizers/synchronizer-ws-client/index.cjs.gz +0 -0
- package/cjs/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.cjs +1 -1
- package/cjs/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.cjs.gz +0 -0
- package/cjs/es6/synchronizers/index.cjs +2 -2
- package/cjs/es6/synchronizers/synchronizer-broadcast-channel/index.cjs +628 -0
- package/cjs/es6/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs +628 -0
- package/cjs/es6/synchronizers/synchronizer-local/index.cjs +4 -4
- package/cjs/es6/synchronizers/synchronizer-local/with-schemas/index.cjs +4 -4
- package/cjs/es6/synchronizers/synchronizer-ws-client/index.cjs +15 -21
- package/cjs/es6/synchronizers/synchronizer-ws-client/with-schemas/index.cjs +15 -21
- package/cjs/es6/synchronizers/with-schemas/index.cjs +2 -2
- package/cjs/es6/with-schemas/index.cjs +2 -2
- package/cjs/index.cjs +2 -2
- package/cjs/min/synchronizers/synchronizer-broadcast-channel/index.cjs +1 -0
- package/cjs/min/synchronizers/synchronizer-broadcast-channel/index.cjs.gz +0 -0
- package/cjs/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs +1 -0
- package/cjs/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs.gz +0 -0
- package/cjs/min/synchronizers/synchronizer-ws-client/index.cjs +1 -1
- package/cjs/min/synchronizers/synchronizer-ws-client/index.cjs.gz +0 -0
- package/cjs/min/synchronizers/synchronizer-ws-client/with-schemas/index.cjs +1 -1
- package/cjs/min/synchronizers/synchronizer-ws-client/with-schemas/index.cjs.gz +0 -0
- package/cjs/synchronizers/index.cjs +2 -2
- package/cjs/synchronizers/synchronizer-broadcast-channel/index.cjs +500 -0
- package/cjs/synchronizers/synchronizer-broadcast-channel/with-schemas/index.cjs +500 -0
- package/cjs/synchronizers/synchronizer-local/index.cjs +4 -4
- package/cjs/synchronizers/synchronizer-local/with-schemas/index.cjs +4 -4
- package/cjs/synchronizers/synchronizer-ws-client/index.cjs +15 -21
- package/cjs/synchronizers/synchronizer-ws-client/with-schemas/index.cjs +15 -21
- package/cjs/synchronizers/with-schemas/index.cjs +2 -2
- package/cjs/with-schemas/index.cjs +2 -2
- package/es6/index.js +2 -2
- package/es6/min/synchronizers/synchronizer-broadcast-channel/index.js +1 -0
- package/es6/min/synchronizers/synchronizer-broadcast-channel/index.js.gz +0 -0
- package/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +1 -0
- package/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js.gz +0 -0
- package/es6/min/synchronizers/synchronizer-ws-client/index.js +1 -1
- package/es6/min/synchronizers/synchronizer-ws-client/index.js.gz +0 -0
- package/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.js +1 -1
- package/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.js.gz +0 -0
- package/es6/synchronizers/index.js +2 -2
- package/es6/synchronizers/synchronizer-broadcast-channel/index.js +626 -0
- package/es6/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +626 -0
- package/es6/synchronizers/synchronizer-local/index.js +4 -4
- package/es6/synchronizers/synchronizer-local/with-schemas/index.js +4 -4
- package/es6/synchronizers/synchronizer-ws-client/index.js +15 -21
- package/es6/synchronizers/synchronizer-ws-client/with-schemas/index.js +15 -21
- package/es6/synchronizers/with-schemas/index.js +2 -2
- package/es6/with-schemas/index.js +2 -2
- package/index.js +2 -2
- package/min/synchronizers/synchronizer-broadcast-channel/index.js +1 -0
- package/min/synchronizers/synchronizer-broadcast-channel/index.js.gz +0 -0
- package/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +1 -0
- package/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js.gz +0 -0
- package/min/synchronizers/synchronizer-ws-client/index.js +1 -1
- package/min/synchronizers/synchronizer-ws-client/index.js.gz +0 -0
- package/min/synchronizers/synchronizer-ws-client/with-schemas/index.js +1 -1
- package/min/synchronizers/synchronizer-ws-client/with-schemas/index.js.gz +0 -0
- package/package.json +105 -1
- package/readme.md +9 -9
- package/releases.md +4 -4
- package/synchronizers/index.js +2 -2
- package/synchronizers/synchronizer-broadcast-channel/index.js +498 -0
- package/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +498 -0
- package/synchronizers/synchronizer-local/index.js +4 -4
- package/synchronizers/synchronizer-local/with-schemas/index.js +4 -4
- package/synchronizers/synchronizer-ws-client/index.js +15 -21
- package/synchronizers/synchronizer-ws-client/with-schemas/index.js +15 -21
- package/synchronizers/with-schemas/index.js +2 -2
- package/umd/es6/index.js +2 -2
- package/umd/es6/min/synchronizers/synchronizer-broadcast-channel/index.js +1 -0
- package/umd/es6/min/synchronizers/synchronizer-broadcast-channel/index.js.gz +0 -0
- package/umd/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +1 -0
- package/umd/es6/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js.gz +0 -0
- package/umd/es6/min/synchronizers/synchronizer-ws-client/index.js +1 -1
- package/umd/es6/min/synchronizers/synchronizer-ws-client/index.js.gz +0 -0
- package/umd/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.js +1 -1
- package/umd/es6/min/synchronizers/synchronizer-ws-client/with-schemas/index.js.gz +0 -0
- package/umd/es6/synchronizers/index.js +2 -2
- package/umd/es6/synchronizers/synchronizer-broadcast-channel/index.js +654 -0
- package/umd/es6/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +654 -0
- package/umd/es6/synchronizers/synchronizer-local/index.js +4 -4
- package/umd/es6/synchronizers/synchronizer-local/with-schemas/index.js +4 -4
- package/umd/es6/synchronizers/synchronizer-ws-client/index.js +15 -21
- package/umd/es6/synchronizers/synchronizer-ws-client/with-schemas/index.js +15 -21
- package/umd/es6/synchronizers/with-schemas/index.js +2 -2
- package/umd/es6/with-schemas/index.js +2 -2
- package/umd/index.js +2 -2
- package/umd/min/synchronizers/synchronizer-broadcast-channel/index.js +1 -0
- package/umd/min/synchronizers/synchronizer-broadcast-channel/index.js.gz +0 -0
- package/umd/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +1 -0
- package/umd/min/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js.gz +0 -0
- package/umd/min/synchronizers/synchronizer-ws-client/index.js +1 -1
- package/umd/min/synchronizers/synchronizer-ws-client/index.js.gz +0 -0
- package/umd/min/synchronizers/synchronizer-ws-client/with-schemas/index.js +1 -1
- package/umd/min/synchronizers/synchronizer-ws-client/with-schemas/index.js.gz +0 -0
- package/umd/synchronizers/index.js +2 -2
- package/umd/synchronizers/synchronizer-broadcast-channel/index.js +516 -0
- package/umd/synchronizers/synchronizer-broadcast-channel/with-schemas/index.js +516 -0
- package/umd/synchronizers/synchronizer-local/index.js +4 -4
- package/umd/synchronizers/synchronizer-local/with-schemas/index.js +4 -4
- package/umd/synchronizers/synchronizer-ws-client/index.js +15 -21
- package/umd/synchronizers/synchronizer-ws-client/with-schemas/index.js +15 -21
- package/umd/synchronizers/with-schemas/index.js +2 -2
- package/umd/with-schemas/index.js +2 -2
- package/with-schemas/index.js +2 -2
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The synchronizer-broadcast-channel module of the TinyBase project lets you
|
|
3
|
+
* synchronize MergeableStore data to and from other MergeableStore instances
|
|
4
|
+
* via a browser's BroadcastChannel API.
|
|
5
|
+
* @see Synchronization guide
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module synchronizer-broadcast-channel
|
|
8
|
+
* @since v5.0.0
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.ts';
|
|
12
|
+
import type {OptionalSchemas} from '../../../store/with-schemas/index.d.ts';
|
|
13
|
+
import type {Synchronizer} from '../../with-schemas/index.d.ts';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The BroadcastChannelSynchronizer interface represents a Synchronizer that
|
|
17
|
+
* lets you synchronize MergeableStore data to and from other MergeableStore
|
|
18
|
+
* instances via a browser's BroadcastChannel API.
|
|
19
|
+
*
|
|
20
|
+
* You should use the createBroadcastChannelSynchronizer function to create a
|
|
21
|
+
* BroadcastChannelSynchronizer object.
|
|
22
|
+
*
|
|
23
|
+
* It is a minor extension to the Synchronizer interface and simply provides an
|
|
24
|
+
* extra getChannelName method for accessing the name of the channel being used.
|
|
25
|
+
* @category Synchronizer
|
|
26
|
+
* @since v5.0.0
|
|
27
|
+
*/
|
|
28
|
+
export interface BroadcastChannelSynchronizer<Schemas extends OptionalSchemas>
|
|
29
|
+
extends Synchronizer<Schemas> {
|
|
30
|
+
/**
|
|
31
|
+
* The getChannelName method returns the name of the channel being used for
|
|
32
|
+
* synchronization.
|
|
33
|
+
* @returns The channel name.
|
|
34
|
+
* @example
|
|
35
|
+
* This example creates a BroadcastChannelSynchronizer object for a
|
|
36
|
+
* newly-created MergeableStore and then gets the channel name back out again.
|
|
37
|
+
*
|
|
38
|
+
* ```js
|
|
39
|
+
* import {createBroadcastChannelSynchronizer} from 'tinybase/synchronizers/synchronizer-broadcast-channel';
|
|
40
|
+
* import {createMergeableStore} from 'tinybase';
|
|
41
|
+
*
|
|
42
|
+
* const store = createMergeableStore();
|
|
43
|
+
* const synchronizer = await createBroadcastChannelSynchronizer(
|
|
44
|
+
* store,
|
|
45
|
+
* 'channelA',
|
|
46
|
+
* );
|
|
47
|
+
*
|
|
48
|
+
* console.log(synchronizer.getChannelName());
|
|
49
|
+
* // -> 'channelA'
|
|
50
|
+
*
|
|
51
|
+
* synchronizer.destroy();
|
|
52
|
+
* ```
|
|
53
|
+
* @category Getter
|
|
54
|
+
* @since v5.0.0
|
|
55
|
+
*/
|
|
56
|
+
getChannelName(): string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The createBroadcastChannelSynchronizer function creates a
|
|
61
|
+
* BroadcastChannelSynchronizer object that can synchronize MergeableStore data
|
|
62
|
+
* to and from other MergeableStore instances via a browser's BroadcastChannel
|
|
63
|
+
* API.
|
|
64
|
+
*
|
|
65
|
+
* This has schema-based typing. The following is a simplified representation:
|
|
66
|
+
*
|
|
67
|
+
* ```ts override
|
|
68
|
+
* createBroadcastChannelSynchronizer(
|
|
69
|
+
* store: MergeableStore,
|
|
70
|
+
* channelName: string,
|
|
71
|
+
* onIgnoredError?: (error: any) => void,
|
|
72
|
+
* ): BroadcastChannelSynchronizer;
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* As well as providing a reference to the MergeableStore to persist, you must
|
|
76
|
+
* provide a channel name, used by all the browser tabs, workers, or contexts
|
|
77
|
+
* that need to synchronize together.
|
|
78
|
+
* @param store The MergeableStore to synchronize.
|
|
79
|
+
* @param channelName The name of the channel to use.
|
|
80
|
+
* @param onIgnoredError An optional handler for the errors that the
|
|
81
|
+
* Synchronizer would otherwise ignore when trying to synchronize data. This is
|
|
82
|
+
* suitable for debugging synchronization issues in a development environment.
|
|
83
|
+
* @returns A reference to the new BroadcastChannelSynchronizer object.
|
|
84
|
+
* @example
|
|
85
|
+
* This example creates two BroadcastChannelSynchronizer objects to synchronize
|
|
86
|
+
* one MergeableStore to another.
|
|
87
|
+
*
|
|
88
|
+
* ```js
|
|
89
|
+
* import {createBroadcastChannelSynchronizer} from 'tinybase/synchronizers/synchronizer-broadcast-channel';
|
|
90
|
+
* import {createMergeableStore} from 'tinybase';
|
|
91
|
+
*
|
|
92
|
+
* const store1 = createMergeableStore();
|
|
93
|
+
* const store2 = createMergeableStore();
|
|
94
|
+
*
|
|
95
|
+
* const synchronizer1 = createBroadcastChannelSynchronizer(
|
|
96
|
+
* store1,
|
|
97
|
+
* 'channelA',
|
|
98
|
+
* );
|
|
99
|
+
* const synchronizer2 = createBroadcastChannelSynchronizer(
|
|
100
|
+
* store2,
|
|
101
|
+
* 'channelA',
|
|
102
|
+
* );
|
|
103
|
+
*
|
|
104
|
+
* await synchronizer1.startSync();
|
|
105
|
+
* await synchronizer2.startSync();
|
|
106
|
+
*
|
|
107
|
+
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
108
|
+
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
109
|
+
*
|
|
110
|
+
* // ...
|
|
111
|
+
* console.log(store1.getTables());
|
|
112
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
113
|
+
* console.log(store2.getTables());
|
|
114
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
115
|
+
*
|
|
116
|
+
* synchronizer1.destroy();
|
|
117
|
+
* synchronizer2.destroy();
|
|
118
|
+
* ```
|
|
119
|
+
* @category Creation
|
|
120
|
+
* @since v5.0.0
|
|
121
|
+
*/
|
|
122
|
+
export function createBroadcastChannelSynchronizer<
|
|
123
|
+
Schemas extends OptionalSchemas,
|
|
124
|
+
>(
|
|
125
|
+
store: MergeableStore<Schemas>,
|
|
126
|
+
channelName: string,
|
|
127
|
+
onIgnoredError?: (error: any) => void,
|
|
128
|
+
): BroadcastChannelSynchronizer<Schemas>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The synchronizer-local module of the TinyBase project lets you synchronize
|
|
3
3
|
* MergeableStore data to and from other MergeableStore instances on the same
|
|
4
4
|
* local machine.
|
|
5
|
-
* @see
|
|
5
|
+
* @see Synchronization guide
|
|
6
6
|
* @packageDocumentation
|
|
7
7
|
* @module synchronizer-local
|
|
8
8
|
* @since v5.0.0
|
|
@@ -16,15 +16,15 @@ import type {Synchronizer} from '../index.d.cts';
|
|
|
16
16
|
* synchronize MergeableStore data to and from other MergeableStore instances on
|
|
17
17
|
* the same local machine.
|
|
18
18
|
*
|
|
19
|
+
* You should use the createLocalSynchronizer function to create a
|
|
20
|
+
* LocalSynchronizer object.
|
|
21
|
+
*
|
|
19
22
|
* Having no specialized methods, it is a synonym for the Synchronizer
|
|
20
23
|
* interface. This is also something of a showcase Synchronizer, rather than
|
|
21
24
|
* something you would use in a production environment. If you _do_ need to
|
|
22
25
|
* synchronize two in-memory MergeableStore instances, you may prefer to use the
|
|
23
26
|
* merge function on either one of them instead of going to the effort of
|
|
24
27
|
* setting up this Synchronizer.
|
|
25
|
-
*
|
|
26
|
-
* You should use the createLocalSynchronizer function to create a
|
|
27
|
-
* LocalSynchronizer object.
|
|
28
28
|
* @category Synchronizer
|
|
29
29
|
* @since v5.0.0
|
|
30
30
|
*/
|
|
@@ -49,31 +49,30 @@ export interface LocalSynchronizer extends Synchronizer {}
|
|
|
49
49
|
* suitable for debugging synchronization issues in a development environment.
|
|
50
50
|
* @returns A reference to the new LocalSynchronizer object.
|
|
51
51
|
* @example
|
|
52
|
-
* This example creates
|
|
52
|
+
* This example creates two LocalSynchronizer objects to synchronize one
|
|
53
53
|
* MergeableStore to another.
|
|
54
54
|
*
|
|
55
55
|
* ```js
|
|
56
56
|
* import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
|
|
57
57
|
* import {createMergeableStore} from 'tinybase';
|
|
58
58
|
*
|
|
59
|
-
* const store1 = createMergeableStore(
|
|
60
|
-
*
|
|
61
|
-
* });
|
|
62
|
-
* const synchronizer1 = createLocalSynchronizer(store1);
|
|
59
|
+
* const store1 = createMergeableStore();
|
|
60
|
+
* const store2 = createMergeableStore();
|
|
63
61
|
*
|
|
64
|
-
* const
|
|
62
|
+
* const synchronizer1 = createLocalSynchronizer(store1);
|
|
65
63
|
* const synchronizer2 = createLocalSynchronizer(store2);
|
|
64
|
+
*
|
|
65
|
+
* await synchronizer1.startSync();
|
|
66
66
|
* await synchronizer2.startSync();
|
|
67
67
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* // Store2 will be synced with Store1.
|
|
68
|
+
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
69
|
+
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
71
70
|
*
|
|
71
|
+
* // ...
|
|
72
|
+
* console.log(store1.getTables());
|
|
73
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
72
74
|
* console.log(store2.getTables());
|
|
73
|
-
* // -> {pets: {fido: {species: 'dog'}}}
|
|
74
|
-
*
|
|
75
|
-
* await synchronizer1.load();
|
|
76
|
-
* // Store1 will be synced with Store2.
|
|
75
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
77
76
|
*
|
|
78
77
|
* synchronizer1.destroy();
|
|
79
78
|
* synchronizer2.destroy();
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The synchronizer-local module of the TinyBase project lets you synchronize
|
|
3
3
|
* MergeableStore data to and from other MergeableStore instances on the same
|
|
4
4
|
* local machine.
|
|
5
|
-
* @see
|
|
5
|
+
* @see Synchronization guide
|
|
6
6
|
* @packageDocumentation
|
|
7
7
|
* @module synchronizer-local
|
|
8
8
|
* @since v5.0.0
|
|
@@ -16,15 +16,15 @@ import type {Synchronizer} from '../index.d.ts';
|
|
|
16
16
|
* synchronize MergeableStore data to and from other MergeableStore instances on
|
|
17
17
|
* the same local machine.
|
|
18
18
|
*
|
|
19
|
+
* You should use the createLocalSynchronizer function to create a
|
|
20
|
+
* LocalSynchronizer object.
|
|
21
|
+
*
|
|
19
22
|
* Having no specialized methods, it is a synonym for the Synchronizer
|
|
20
23
|
* interface. This is also something of a showcase Synchronizer, rather than
|
|
21
24
|
* something you would use in a production environment. If you _do_ need to
|
|
22
25
|
* synchronize two in-memory MergeableStore instances, you may prefer to use the
|
|
23
26
|
* merge function on either one of them instead of going to the effort of
|
|
24
27
|
* setting up this Synchronizer.
|
|
25
|
-
*
|
|
26
|
-
* You should use the createLocalSynchronizer function to create a
|
|
27
|
-
* LocalSynchronizer object.
|
|
28
28
|
* @category Synchronizer
|
|
29
29
|
* @since v5.0.0
|
|
30
30
|
*/
|
|
@@ -49,31 +49,30 @@ export interface LocalSynchronizer extends Synchronizer {}
|
|
|
49
49
|
* suitable for debugging synchronization issues in a development environment.
|
|
50
50
|
* @returns A reference to the new LocalSynchronizer object.
|
|
51
51
|
* @example
|
|
52
|
-
* This example creates
|
|
52
|
+
* This example creates two LocalSynchronizer objects to synchronize one
|
|
53
53
|
* MergeableStore to another.
|
|
54
54
|
*
|
|
55
55
|
* ```js
|
|
56
56
|
* import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
|
|
57
57
|
* import {createMergeableStore} from 'tinybase';
|
|
58
58
|
*
|
|
59
|
-
* const store1 = createMergeableStore(
|
|
60
|
-
*
|
|
61
|
-
* });
|
|
62
|
-
* const synchronizer1 = createLocalSynchronizer(store1);
|
|
59
|
+
* const store1 = createMergeableStore();
|
|
60
|
+
* const store2 = createMergeableStore();
|
|
63
61
|
*
|
|
64
|
-
* const
|
|
62
|
+
* const synchronizer1 = createLocalSynchronizer(store1);
|
|
65
63
|
* const synchronizer2 = createLocalSynchronizer(store2);
|
|
64
|
+
*
|
|
65
|
+
* await synchronizer1.startSync();
|
|
66
66
|
* await synchronizer2.startSync();
|
|
67
67
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* // Store2 will be synced with Store1.
|
|
68
|
+
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
69
|
+
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
71
70
|
*
|
|
71
|
+
* // ...
|
|
72
|
+
* console.log(store1.getTables());
|
|
73
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
72
74
|
* console.log(store2.getTables());
|
|
73
|
-
* // -> {pets: {fido: {species: 'dog'}}}
|
|
74
|
-
*
|
|
75
|
-
* await synchronizer1.load();
|
|
76
|
-
* // Store1 will be synced with Store2.
|
|
75
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
77
76
|
*
|
|
78
77
|
* synchronizer1.destroy();
|
|
79
78
|
* synchronizer2.destroy();
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The synchronizer-local module of the TinyBase project lets you synchronize
|
|
3
3
|
* MergeableStore data to and from other MergeableStore instances on the same
|
|
4
4
|
* local machine.
|
|
5
|
-
* @see
|
|
5
|
+
* @see Synchronization guide
|
|
6
6
|
* @packageDocumentation
|
|
7
7
|
* @module synchronizer-local
|
|
8
8
|
* @since v5.0.0
|
|
@@ -23,15 +23,15 @@ import type {Synchronizer} from '../../with-schemas/index.d.cts';
|
|
|
23
23
|
* export interface LocalSynchronizer extends Synchronizer {}
|
|
24
24
|
* ```
|
|
25
25
|
*
|
|
26
|
+
* You should use the createLocalSynchronizer function to create a
|
|
27
|
+
* LocalSynchronizer object.
|
|
28
|
+
*
|
|
26
29
|
* Having no specialized methods, it is a synonym for the Synchronizer
|
|
27
30
|
* interface. This is also something of a showcase Synchronizer, rather than
|
|
28
31
|
* something you would use in a production environment. If you _do_ need to
|
|
29
32
|
* synchronize two in-memory MergeableStore instances, you may prefer to use the
|
|
30
33
|
* merge function on either one of them instead of going to the effort of
|
|
31
34
|
* setting up this Synchronizer.
|
|
32
|
-
*
|
|
33
|
-
* You should use the createLocalSynchronizer function to create a
|
|
34
|
-
* LocalSynchronizer object.
|
|
35
35
|
* @category Synchronizer
|
|
36
36
|
* @since v5.0.0
|
|
37
37
|
*/
|
|
@@ -66,31 +66,30 @@ export interface LocalSynchronizer<Schemas extends OptionalSchemas>
|
|
|
66
66
|
* suitable for debugging synchronization issues in a development environment.
|
|
67
67
|
* @returns A reference to the new LocalSynchronizer object.
|
|
68
68
|
* @example
|
|
69
|
-
* This example creates
|
|
69
|
+
* This example creates two LocalSynchronizer objects to synchronize one
|
|
70
70
|
* MergeableStore to another.
|
|
71
71
|
*
|
|
72
72
|
* ```js
|
|
73
73
|
* import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
|
|
74
74
|
* import {createMergeableStore} from 'tinybase';
|
|
75
75
|
*
|
|
76
|
-
* const store1 = createMergeableStore(
|
|
77
|
-
*
|
|
78
|
-
* });
|
|
79
|
-
* const synchronizer1 = createLocalSynchronizer(store1);
|
|
76
|
+
* const store1 = createMergeableStore();
|
|
77
|
+
* const store2 = createMergeableStore();
|
|
80
78
|
*
|
|
81
|
-
* const
|
|
79
|
+
* const synchronizer1 = createLocalSynchronizer(store1);
|
|
82
80
|
* const synchronizer2 = createLocalSynchronizer(store2);
|
|
81
|
+
*
|
|
82
|
+
* await synchronizer1.startSync();
|
|
83
83
|
* await synchronizer2.startSync();
|
|
84
84
|
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
* // Store2 will be synced with Store1.
|
|
85
|
+
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
86
|
+
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
88
87
|
*
|
|
88
|
+
* // ...
|
|
89
|
+
* console.log(store1.getTables());
|
|
90
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
89
91
|
* console.log(store2.getTables());
|
|
90
|
-
* // -> {pets: {fido: {species: 'dog'}}}
|
|
91
|
-
*
|
|
92
|
-
* await synchronizer1.load();
|
|
93
|
-
* // Store1 will be synced with Store2.
|
|
92
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
94
93
|
*
|
|
95
94
|
* synchronizer1.destroy();
|
|
96
95
|
* synchronizer2.destroy();
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The synchronizer-local module of the TinyBase project lets you synchronize
|
|
3
3
|
* MergeableStore data to and from other MergeableStore instances on the same
|
|
4
4
|
* local machine.
|
|
5
|
-
* @see
|
|
5
|
+
* @see Synchronization guide
|
|
6
6
|
* @packageDocumentation
|
|
7
7
|
* @module synchronizer-local
|
|
8
8
|
* @since v5.0.0
|
|
@@ -23,15 +23,15 @@ import type {Synchronizer} from '../../with-schemas/index.d.ts';
|
|
|
23
23
|
* export interface LocalSynchronizer extends Synchronizer {}
|
|
24
24
|
* ```
|
|
25
25
|
*
|
|
26
|
+
* You should use the createLocalSynchronizer function to create a
|
|
27
|
+
* LocalSynchronizer object.
|
|
28
|
+
*
|
|
26
29
|
* Having no specialized methods, it is a synonym for the Synchronizer
|
|
27
30
|
* interface. This is also something of a showcase Synchronizer, rather than
|
|
28
31
|
* something you would use in a production environment. If you _do_ need to
|
|
29
32
|
* synchronize two in-memory MergeableStore instances, you may prefer to use the
|
|
30
33
|
* merge function on either one of them instead of going to the effort of
|
|
31
34
|
* setting up this Synchronizer.
|
|
32
|
-
*
|
|
33
|
-
* You should use the createLocalSynchronizer function to create a
|
|
34
|
-
* LocalSynchronizer object.
|
|
35
35
|
* @category Synchronizer
|
|
36
36
|
* @since v5.0.0
|
|
37
37
|
*/
|
|
@@ -66,31 +66,30 @@ export interface LocalSynchronizer<Schemas extends OptionalSchemas>
|
|
|
66
66
|
* suitable for debugging synchronization issues in a development environment.
|
|
67
67
|
* @returns A reference to the new LocalSynchronizer object.
|
|
68
68
|
* @example
|
|
69
|
-
* This example creates
|
|
69
|
+
* This example creates two LocalSynchronizer objects to synchronize one
|
|
70
70
|
* MergeableStore to another.
|
|
71
71
|
*
|
|
72
72
|
* ```js
|
|
73
73
|
* import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
|
|
74
74
|
* import {createMergeableStore} from 'tinybase';
|
|
75
75
|
*
|
|
76
|
-
* const store1 = createMergeableStore(
|
|
77
|
-
*
|
|
78
|
-
* });
|
|
79
|
-
* const synchronizer1 = createLocalSynchronizer(store1);
|
|
76
|
+
* const store1 = createMergeableStore();
|
|
77
|
+
* const store2 = createMergeableStore();
|
|
80
78
|
*
|
|
81
|
-
* const
|
|
79
|
+
* const synchronizer1 = createLocalSynchronizer(store1);
|
|
82
80
|
* const synchronizer2 = createLocalSynchronizer(store2);
|
|
81
|
+
*
|
|
82
|
+
* await synchronizer1.startSync();
|
|
83
83
|
* await synchronizer2.startSync();
|
|
84
84
|
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
* // Store2 will be synced with Store1.
|
|
85
|
+
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
86
|
+
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
88
87
|
*
|
|
88
|
+
* // ...
|
|
89
|
+
* console.log(store1.getTables());
|
|
90
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
89
91
|
* console.log(store2.getTables());
|
|
90
|
-
* // -> {pets: {fido: {species: 'dog'}}}
|
|
91
|
-
*
|
|
92
|
-
* await synchronizer1.load();
|
|
93
|
-
* // Store1 will be synced with Store2.
|
|
92
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
94
93
|
*
|
|
95
94
|
* synchronizer1.destroy();
|
|
96
95
|
* synchronizer2.destroy();
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The synchronizer-ws module of the TinyBase project lets you synchronize
|
|
3
3
|
* MergeableStore data to and from other MergeableStore instances via WebSockets
|
|
4
4
|
* facilitated by a server.
|
|
5
|
-
* @see
|
|
5
|
+
* @see Synchronization guide
|
|
6
6
|
* @see Todo App v6 (collaboration) demo
|
|
7
7
|
* @packageDocumentation
|
|
8
8
|
* @module synchronizer-ws-client
|
|
@@ -20,6 +20,8 @@ import type {WebSocket as WsWebSocket} from 'ws';
|
|
|
20
20
|
* This includes the browser-native WebSocket type, as well as the WebSocket
|
|
21
21
|
* type from the well-known `ws` package (such that the Synchronizer can be used
|
|
22
22
|
* in a server environment).
|
|
23
|
+
* @category Creation
|
|
24
|
+
* @since v5.0.0
|
|
23
25
|
*/
|
|
24
26
|
export type WebSocketTypes = WebSocket | WsWebSocket;
|
|
25
27
|
|
|
@@ -28,19 +30,19 @@ export type WebSocketTypes = WebSocket | WsWebSocket;
|
|
|
28
30
|
* synchronize MergeableStore data to and from other MergeableStore instances
|
|
29
31
|
* via WebSockets facilitated by a server.
|
|
30
32
|
*
|
|
33
|
+
* You should use the createWsSynchronizer function to create a WsSynchronizer
|
|
34
|
+
* object.
|
|
35
|
+
*
|
|
31
36
|
* It is a minor extension to the Synchronizer interface and simply provides an
|
|
32
37
|
* extra getWebSocket method for accessing a reference to the WebSocket being
|
|
33
38
|
* used.
|
|
34
|
-
*
|
|
35
|
-
* You should use the createWsSynchronizer function to create a WsSynchronizer
|
|
36
|
-
* object.
|
|
37
39
|
* @category Synchronizer
|
|
38
40
|
* @since v5.0.0
|
|
39
41
|
*/
|
|
40
42
|
export interface WsSynchronizer<WebSocketType extends WebSocketTypes>
|
|
41
43
|
extends Synchronizer {
|
|
42
44
|
/**
|
|
43
|
-
* The getWebSocket method returns reference to the WebSocket being used for
|
|
45
|
+
* The getWebSocket method returns a reference to the WebSocket being used for
|
|
44
46
|
* synchronization.
|
|
45
47
|
* @returns The WebSocket reference.
|
|
46
48
|
* @example
|
|
@@ -72,8 +74,66 @@ export interface WsSynchronizer<WebSocketType extends WebSocketTypes>
|
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
/**
|
|
75
|
-
* The createWsSynchronizer function
|
|
77
|
+
* The createWsSynchronizer function creates a WsSynchronizer object that can
|
|
78
|
+
* synchronize MergeableStore data to and from other MergeableStore instances
|
|
79
|
+
* via WebSockets facilitated by a WsServer.
|
|
80
|
+
*
|
|
81
|
+
* As well as providing a reference to the MergeableStore to persist, you must
|
|
82
|
+
* provide a configured WebSocket to send synchronization messages over.
|
|
83
|
+
*
|
|
84
|
+
* This method is asynchronous because it will await the websocket's connection
|
|
85
|
+
* to the server. You will need to `await` a call to this function or handle the
|
|
86
|
+
* return type natively as a Promise.
|
|
87
|
+
* @param store The MergeableStore to synchronize.
|
|
88
|
+
* @param webSocket The WebSocket to send synchronization messages over.
|
|
89
|
+
* @param requestTimeoutSeconds An optional time in seconds that the
|
|
90
|
+
* Synchronizer will wait for responses to request messages, defaulting to 1.
|
|
91
|
+
* @param onIgnoredError An optional handler for the errors that the
|
|
92
|
+
* Synchronizer would otherwise ignore when trying to synchronize data. This is
|
|
93
|
+
* suitable for debugging synchronization issues in a development environment.
|
|
94
|
+
* @returns A reference to the new WsSynchronizer object.
|
|
95
|
+
* @example
|
|
96
|
+
* This example creates two WsSynchronizer objects to synchronize one
|
|
97
|
+
* MergeableStore to another via a server.
|
|
98
|
+
*
|
|
99
|
+
* ```js
|
|
100
|
+
* import {WebSocket, WebSocketServer} from 'ws';
|
|
101
|
+
* import {createMergeableStore} from 'tinybase';
|
|
102
|
+
* import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
|
|
103
|
+
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
|
|
104
|
+
*
|
|
105
|
+
* const server = createWsServer(new WebSocketServer({port: 8047}));
|
|
106
|
+
*
|
|
107
|
+
* const store1 = createMergeableStore();
|
|
108
|
+
* const store2 = createMergeableStore();
|
|
109
|
+
*
|
|
110
|
+
* const synchronizer1 = await createWsSynchronizer(
|
|
111
|
+
* store1,
|
|
112
|
+
* new WebSocket('ws://localhost:8047'),
|
|
113
|
+
* );
|
|
114
|
+
* const synchronizer2 = await createWsSynchronizer(
|
|
115
|
+
* store2,
|
|
116
|
+
* new WebSocket('ws://localhost:8047'),
|
|
117
|
+
* );
|
|
118
|
+
*
|
|
119
|
+
* await synchronizer1.startSync();
|
|
120
|
+
* await synchronizer2.startSync();
|
|
121
|
+
*
|
|
122
|
+
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
123
|
+
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
124
|
+
*
|
|
125
|
+
* // ...
|
|
126
|
+
* console.log(store1.getTables());
|
|
127
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
128
|
+
* console.log(store2.getTables());
|
|
129
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
130
|
+
*
|
|
131
|
+
* synchronizer1.destroy();
|
|
132
|
+
* synchronizer2.destroy();
|
|
133
|
+
* server.destroy();
|
|
134
|
+
* ```
|
|
76
135
|
* @category Creation
|
|
136
|
+
* @since v5.0.0
|
|
77
137
|
*/
|
|
78
138
|
export function createWsSynchronizer<WebSocketType extends WebSocketTypes>(
|
|
79
139
|
store: MergeableStore,
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The synchronizer-ws module of the TinyBase project lets you synchronize
|
|
3
3
|
* MergeableStore data to and from other MergeableStore instances via WebSockets
|
|
4
4
|
* facilitated by a server.
|
|
5
|
-
* @see
|
|
5
|
+
* @see Synchronization guide
|
|
6
6
|
* @see Todo App v6 (collaboration) demo
|
|
7
7
|
* @packageDocumentation
|
|
8
8
|
* @module synchronizer-ws-client
|
|
@@ -20,6 +20,8 @@ import type {WebSocket as WsWebSocket} from 'ws';
|
|
|
20
20
|
* This includes the browser-native WebSocket type, as well as the WebSocket
|
|
21
21
|
* type from the well-known `ws` package (such that the Synchronizer can be used
|
|
22
22
|
* in a server environment).
|
|
23
|
+
* @category Creation
|
|
24
|
+
* @since v5.0.0
|
|
23
25
|
*/
|
|
24
26
|
export type WebSocketTypes = WebSocket | WsWebSocket;
|
|
25
27
|
|
|
@@ -28,19 +30,19 @@ export type WebSocketTypes = WebSocket | WsWebSocket;
|
|
|
28
30
|
* synchronize MergeableStore data to and from other MergeableStore instances
|
|
29
31
|
* via WebSockets facilitated by a server.
|
|
30
32
|
*
|
|
33
|
+
* You should use the createWsSynchronizer function to create a WsSynchronizer
|
|
34
|
+
* object.
|
|
35
|
+
*
|
|
31
36
|
* It is a minor extension to the Synchronizer interface and simply provides an
|
|
32
37
|
* extra getWebSocket method for accessing a reference to the WebSocket being
|
|
33
38
|
* used.
|
|
34
|
-
*
|
|
35
|
-
* You should use the createWsSynchronizer function to create a WsSynchronizer
|
|
36
|
-
* object.
|
|
37
39
|
* @category Synchronizer
|
|
38
40
|
* @since v5.0.0
|
|
39
41
|
*/
|
|
40
42
|
export interface WsSynchronizer<WebSocketType extends WebSocketTypes>
|
|
41
43
|
extends Synchronizer {
|
|
42
44
|
/**
|
|
43
|
-
* The getWebSocket method returns reference to the WebSocket being used for
|
|
45
|
+
* The getWebSocket method returns a reference to the WebSocket being used for
|
|
44
46
|
* synchronization.
|
|
45
47
|
* @returns The WebSocket reference.
|
|
46
48
|
* @example
|
|
@@ -72,8 +74,66 @@ export interface WsSynchronizer<WebSocketType extends WebSocketTypes>
|
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
/**
|
|
75
|
-
* The createWsSynchronizer function
|
|
77
|
+
* The createWsSynchronizer function creates a WsSynchronizer object that can
|
|
78
|
+
* synchronize MergeableStore data to and from other MergeableStore instances
|
|
79
|
+
* via WebSockets facilitated by a WsServer.
|
|
80
|
+
*
|
|
81
|
+
* As well as providing a reference to the MergeableStore to persist, you must
|
|
82
|
+
* provide a configured WebSocket to send synchronization messages over.
|
|
83
|
+
*
|
|
84
|
+
* This method is asynchronous because it will await the websocket's connection
|
|
85
|
+
* to the server. You will need to `await` a call to this function or handle the
|
|
86
|
+
* return type natively as a Promise.
|
|
87
|
+
* @param store The MergeableStore to synchronize.
|
|
88
|
+
* @param webSocket The WebSocket to send synchronization messages over.
|
|
89
|
+
* @param requestTimeoutSeconds An optional time in seconds that the
|
|
90
|
+
* Synchronizer will wait for responses to request messages, defaulting to 1.
|
|
91
|
+
* @param onIgnoredError An optional handler for the errors that the
|
|
92
|
+
* Synchronizer would otherwise ignore when trying to synchronize data. This is
|
|
93
|
+
* suitable for debugging synchronization issues in a development environment.
|
|
94
|
+
* @returns A reference to the new WsSynchronizer object.
|
|
95
|
+
* @example
|
|
96
|
+
* This example creates two WsSynchronizer objects to synchronize one
|
|
97
|
+
* MergeableStore to another via a server.
|
|
98
|
+
*
|
|
99
|
+
* ```js
|
|
100
|
+
* import {WebSocket, WebSocketServer} from 'ws';
|
|
101
|
+
* import {createMergeableStore} from 'tinybase';
|
|
102
|
+
* import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
|
|
103
|
+
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
|
|
104
|
+
*
|
|
105
|
+
* const server = createWsServer(new WebSocketServer({port: 8047}));
|
|
106
|
+
*
|
|
107
|
+
* const store1 = createMergeableStore();
|
|
108
|
+
* const store2 = createMergeableStore();
|
|
109
|
+
*
|
|
110
|
+
* const synchronizer1 = await createWsSynchronizer(
|
|
111
|
+
* store1,
|
|
112
|
+
* new WebSocket('ws://localhost:8047'),
|
|
113
|
+
* );
|
|
114
|
+
* const synchronizer2 = await createWsSynchronizer(
|
|
115
|
+
* store2,
|
|
116
|
+
* new WebSocket('ws://localhost:8047'),
|
|
117
|
+
* );
|
|
118
|
+
*
|
|
119
|
+
* await synchronizer1.startSync();
|
|
120
|
+
* await synchronizer2.startSync();
|
|
121
|
+
*
|
|
122
|
+
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
123
|
+
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
124
|
+
*
|
|
125
|
+
* // ...
|
|
126
|
+
* console.log(store1.getTables());
|
|
127
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
128
|
+
* console.log(store2.getTables());
|
|
129
|
+
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
130
|
+
*
|
|
131
|
+
* synchronizer1.destroy();
|
|
132
|
+
* synchronizer2.destroy();
|
|
133
|
+
* server.destroy();
|
|
134
|
+
* ```
|
|
76
135
|
* @category Creation
|
|
136
|
+
* @since v5.0.0
|
|
77
137
|
*/
|
|
78
138
|
export function createWsSynchronizer<WebSocketType extends WebSocketTypes>(
|
|
79
139
|
store: MergeableStore,
|