tinybase 6.1.0-beta.0 → 6.1.0-beta.2
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.ts +1 -1
- package/@types/common/index.d.ts +4 -4
- package/@types/common/with-schemas/index.d.ts +4 -4
- package/@types/index.d.ts +1 -1
- package/@types/persisters/persister-automerge/index.d.ts +1 -1
- package/@types/persisters/persister-automerge/with-schemas/index.d.ts +1 -1
- package/@types/persisters/persister-cr-sqlite-wasm/index.d.ts +1 -1
- package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.ts +1 -1
- package/@types/persisters/persister-electric-sql/index.d.ts +4 -4
- package/@types/persisters/persister-electric-sql/with-schemas/index.d.ts +4 -4
- package/@types/persisters/persister-expo-sqlite/index.d.ts +1 -1
- package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.ts +1 -1
- package/@types/persisters/persister-libsql/index.d.ts +1 -1
- package/@types/persisters/persister-libsql/with-schemas/index.d.ts +1 -1
- package/@types/persisters/persister-partykit-client/index.d.ts +1 -1
- package/@types/persisters/persister-partykit-client/with-schemas/index.d.ts +1 -1
- package/@types/persisters/persister-partykit-server/index.d.ts +7 -7
- package/@types/persisters/persister-partykit-server/with-schemas/index.d.ts +1 -1
- package/@types/persisters/persister-pglite/index.d.ts +1 -1
- package/@types/persisters/persister-pglite/with-schemas/index.d.ts +1 -1
- package/@types/persisters/persister-postgres/index.d.ts +1 -1
- package/@types/persisters/persister-postgres/with-schemas/index.d.ts +1 -1
- package/@types/persisters/persister-powersync/index.d.ts +1 -1
- package/@types/persisters/persister-powersync/with-schemas/index.d.ts +1 -1
- package/@types/persisters/persister-sqlite3/index.d.ts +1 -1
- package/@types/persisters/persister-sqlite3/with-schemas/index.d.ts +1 -1
- package/@types/persisters/persister-yjs/index.d.ts +1 -1
- package/@types/persisters/persister-yjs/with-schemas/index.d.ts +1 -1
- package/@types/synchronizers/synchronizer-ws-client/index.d.ts +1 -1
- package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.ts +1 -1
- package/@types/synchronizers/synchronizer-ws-server/index.d.ts +1 -1
- package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.ts +1 -1
- package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.ts +1 -1
- package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.ts +1 -1
- package/@types/ui-react/index.d.ts +3 -3
- package/@types/ui-react/with-schemas/index.d.ts +1 -1
- package/@types/ui-react-dom/index.d.ts +1 -1
- package/@types/ui-react-dom/with-schemas/index.d.ts +1 -1
- package/@types/ui-react-inspector/with-schemas/index.d.ts +1 -1
- package/@types/with-schemas/index.d.ts +1 -1
- package/index.js +1579 -1567
- package/indexes/index.js +15 -3
- package/indexes/with-schemas/index.js +15 -3
- package/min/index.js +1 -1
- package/min/index.js.gz +0 -0
- package/min/indexes/index.js +1 -1
- package/min/indexes/index.js.gz +0 -0
- package/min/indexes/with-schemas/index.js +1 -1
- package/min/indexes/with-schemas/index.js.gz +0 -0
- package/min/persisters/index.js +1 -1
- package/min/persisters/index.js.gz +0 -0
- package/min/persisters/with-schemas/index.js +1 -1
- package/min/persisters/with-schemas/index.js.gz +0 -0
- package/min/ui-react/index.js +1 -1
- package/min/ui-react/index.js.gz +0 -0
- package/min/ui-react/with-schemas/index.js +1 -1
- package/min/ui-react/with-schemas/index.js.gz +0 -0
- package/min/with-schemas/index.js +1 -1
- package/min/with-schemas/index.js.gz +0 -0
- package/package.json +7 -10
- package/persisters/index.js +95 -95
- package/persisters/with-schemas/index.js +95 -95
- package/readme.md +13 -13
- package/releases.md +25 -25
- package/synchronizers/synchronizer-ws-server-durable-object/index.js +1 -1
- package/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +1 -1
- package/ui-react/index.js +64 -53
- package/ui-react/with-schemas/index.js +64 -53
- package/ui-react-dom/index.js +3 -3
- package/ui-react-dom/with-schemas/index.js +3 -3
- package/ui-react-inspector/index.js +4 -4
- package/ui-react-inspector/with-schemas/index.js +4 -4
- package/with-schemas/index.js +1579 -1567
- package/@types/_internal/queries/index.d.cts +0 -0
- package/@types/_internal/queries/with-schemas/index.d.cts +0 -22
- package/@types/_internal/store/index.d.cts +0 -3
- package/@types/_internal/store/with-schemas/index.d.cts +0 -106
- package/@types/_internal/ui-react/index.d.cts +0 -0
- package/@types/_internal/ui-react/with-schemas/index.d.cts +0 -1130
- package/@types/checkpoints/index.d.cts +0 -1059
- package/@types/checkpoints/with-schemas/index.d.cts +0 -1151
- package/@types/common/index.d.cts +0 -158
- package/@types/common/with-schemas/index.d.cts +0 -158
- package/@types/index.d.cts +0 -17
- package/@types/indexes/index.d.cts +0 -1064
- package/@types/indexes/with-schemas/index.d.cts +0 -1210
- package/@types/mergeable-store/index.d.cts +0 -1139
- package/@types/mergeable-store/with-schemas/index.d.cts +0 -1628
- package/@types/metrics/index.d.cts +0 -917
- package/@types/metrics/with-schemas/index.d.cts +0 -1004
- package/@types/persisters/index.d.cts +0 -1877
- package/@types/persisters/persister-automerge/index.d.cts +0 -165
- package/@types/persisters/persister-automerge/with-schemas/index.d.cts +0 -180
- package/@types/persisters/persister-browser/index.d.cts +0 -185
- package/@types/persisters/persister-browser/with-schemas/index.d.cts +0 -208
- package/@types/persisters/persister-cr-sqlite-wasm/index.d.cts +0 -159
- package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.cts +0 -178
- package/@types/persisters/persister-durable-object-storage/index.d.cts +0 -122
- package/@types/persisters/persister-durable-object-storage/with-schemas/index.d.cts +0 -136
- package/@types/persisters/persister-electric-sql/index.d.cts +0 -185
- package/@types/persisters/persister-electric-sql/with-schemas/index.d.cts +0 -204
- package/@types/persisters/persister-expo-sqlite/index.d.cts +0 -186
- package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.cts +0 -205
- package/@types/persisters/persister-file/index.d.cts +0 -94
- package/@types/persisters/persister-file/with-schemas/index.d.cts +0 -107
- package/@types/persisters/persister-indexed-db/index.d.cts +0 -120
- package/@types/persisters/persister-indexed-db/with-schemas/index.d.cts +0 -135
- package/@types/persisters/persister-libsql/index.d.cts +0 -158
- package/@types/persisters/persister-libsql/with-schemas/index.d.cts +0 -177
- package/@types/persisters/persister-partykit-client/index.d.cts +0 -195
- package/@types/persisters/persister-partykit-client/with-schemas/index.d.cts +0 -210
- package/@types/persisters/persister-partykit-server/index.d.cts +0 -650
- package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +0 -695
- package/@types/persisters/persister-pglite/index.d.cts +0 -177
- package/@types/persisters/persister-pglite/with-schemas/index.d.cts +0 -196
- package/@types/persisters/persister-postgres/index.d.cts +0 -166
- package/@types/persisters/persister-postgres/with-schemas/index.d.cts +0 -185
- package/@types/persisters/persister-powersync/index.d.cts +0 -174
- package/@types/persisters/persister-powersync/with-schemas/index.d.cts +0 -193
- package/@types/persisters/persister-remote/index.d.cts +0 -117
- package/@types/persisters/persister-remote/with-schemas/index.d.cts +0 -133
- package/@types/persisters/persister-sqlite-wasm/index.d.cts +0 -175
- package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.cts +0 -195
- package/@types/persisters/persister-sqlite3/index.d.cts +0 -176
- package/@types/persisters/persister-sqlite3/with-schemas/index.d.cts +0 -195
- package/@types/persisters/persister-yjs/index.d.cts +0 -161
- package/@types/persisters/persister-yjs/with-schemas/index.d.cts +0 -176
- package/@types/persisters/with-schemas/index.d.cts +0 -2054
- package/@types/queries/index.d.cts +0 -3695
- package/@types/queries/with-schemas/index.d.cts +0 -4016
- package/@types/relationships/index.d.cts +0 -1320
- package/@types/relationships/with-schemas/index.d.cts +0 -1474
- package/@types/store/index.d.cts +0 -7598
- package/@types/store/with-schemas/index.d.cts +0 -9278
- package/@types/synchronizers/index.d.cts +0 -485
- package/@types/synchronizers/synchronizer-broadcast-channel/index.d.cts +0 -121
- package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.cts +0 -137
- package/@types/synchronizers/synchronizer-local/index.d.cts +0 -95
- package/@types/synchronizers/synchronizer-local/with-schemas/index.d.cts +0 -114
- package/@types/synchronizers/synchronizer-ws-client/index.d.cts +0 -160
- package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.cts +0 -179
- package/@types/synchronizers/synchronizer-ws-server/index.d.cts +0 -736
- package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.cts +0 -765
- package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.cts +0 -311
- package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.cts +0 -349
- package/@types/synchronizers/synchronizer-ws-server-simple/index.d.cts +0 -144
- package/@types/synchronizers/synchronizer-ws-server-simple/with-schemas/index.d.cts +0 -144
- package/@types/synchronizers/with-schemas/index.d.cts +0 -503
- package/@types/ui-react/index.d.cts +0 -16640
- package/@types/ui-react/with-schemas/index.d.cts +0 -17281
- package/@types/ui-react-dom/index.d.cts +0 -1862
- package/@types/ui-react-dom/with-schemas/index.d.cts +0 -1994
- package/@types/ui-react-inspector/index.d.cts +0 -79
- package/@types/ui-react-inspector/with-schemas/index.d.cts +0 -1985
- package/@types/with-schemas/index.d.cts +0 -17
|
@@ -1,137 +0,0 @@
|
|
|
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
|
-
import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.cts';
|
|
11
|
-
import type {OptionalSchemas} from '../../../store/with-schemas/index.d.cts';
|
|
12
|
-
import type {Receive, Send, Synchronizer} from '../../with-schemas/index.d.cts';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The BroadcastChannelSynchronizer interface represents a Synchronizer that
|
|
16
|
-
* lets you synchronize MergeableStore data to and from other MergeableStore
|
|
17
|
-
* instances via a browser's BroadcastChannel API.
|
|
18
|
-
*
|
|
19
|
-
* You should use the createBroadcastChannelSynchronizer function to create a
|
|
20
|
-
* BroadcastChannelSynchronizer object.
|
|
21
|
-
*
|
|
22
|
-
* It is a minor extension to the Synchronizer interface and simply provides an
|
|
23
|
-
* extra getChannelName method for accessing the name of the channel being used.
|
|
24
|
-
* @category Synchronizer
|
|
25
|
-
* @since v5.0.0
|
|
26
|
-
*/
|
|
27
|
-
export interface BroadcastChannelSynchronizer<Schemas extends OptionalSchemas>
|
|
28
|
-
extends Synchronizer<Schemas> {
|
|
29
|
-
/**
|
|
30
|
-
* The getChannelName method returns the name of the channel being used for
|
|
31
|
-
* synchronization.
|
|
32
|
-
* @returns The channel name.
|
|
33
|
-
* @example
|
|
34
|
-
* This example creates a BroadcastChannelSynchronizer object for a
|
|
35
|
-
* newly-created MergeableStore and then gets the channel name back out again.
|
|
36
|
-
*
|
|
37
|
-
* ```js
|
|
38
|
-
* import {createMergeableStore} from 'tinybase';
|
|
39
|
-
* import {createBroadcastChannelSynchronizer} from 'tinybase/synchronizers/synchronizer-broadcast-channel';
|
|
40
|
-
*
|
|
41
|
-
* const store = createMergeableStore();
|
|
42
|
-
* const synchronizer = createBroadcastChannelSynchronizer(store, 'channelA');
|
|
43
|
-
*
|
|
44
|
-
* console.log(synchronizer.getChannelName());
|
|
45
|
-
* // -> 'channelA'
|
|
46
|
-
*
|
|
47
|
-
* synchronizer.destroy();
|
|
48
|
-
* ```
|
|
49
|
-
* @category Getter
|
|
50
|
-
* @since v5.0.0
|
|
51
|
-
*/
|
|
52
|
-
getChannelName(): string;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* The createBroadcastChannelSynchronizer function creates a
|
|
57
|
-
* BroadcastChannelSynchronizer object that can synchronize MergeableStore data
|
|
58
|
-
* to and from other MergeableStore instances via a browser's BroadcastChannel
|
|
59
|
-
* API.
|
|
60
|
-
*
|
|
61
|
-
* This has schema-based typing. The following is a simplified representation:
|
|
62
|
-
*
|
|
63
|
-
* ```ts override
|
|
64
|
-
* createBroadcastChannelSynchronizer(
|
|
65
|
-
* store: MergeableStore,
|
|
66
|
-
* channelName: string,
|
|
67
|
-
* onSend?: Send,
|
|
68
|
-
* onReceive?: Receive,
|
|
69
|
-
* onIgnoredError?: (error: any) => void,
|
|
70
|
-
* ): BroadcastChannelSynchronizer;
|
|
71
|
-
* ```
|
|
72
|
-
*
|
|
73
|
-
* As well as providing a reference to the MergeableStore to persist, you must
|
|
74
|
-
* provide a channel name, used by all the browser tabs, workers, or contexts
|
|
75
|
-
* that need to synchronize together.
|
|
76
|
-
*
|
|
77
|
-
* A final set of optional handlers can be provided to help debug sends,
|
|
78
|
-
* receives, and errors respectively.
|
|
79
|
-
* @param store The MergeableStore to synchronize.
|
|
80
|
-
* @param channelName The name of the channel to use.
|
|
81
|
-
* @param onSend An optional handler for the messages that this Synchronizer
|
|
82
|
-
* sends. This is suitable for debugging synchronization issues in a development
|
|
83
|
-
* environment, since v5.1.
|
|
84
|
-
* @param onReceive An optional handler for the messages that this Synchronizer
|
|
85
|
-
* receives. This is suitable for debugging synchronization issues in a
|
|
86
|
-
* development environment, since v5.1.
|
|
87
|
-
* @param onIgnoredError An optional handler for the errors that the
|
|
88
|
-
* Synchronizer would otherwise ignore when trying to synchronize data. This is
|
|
89
|
-
* suitable for debugging synchronization issues in a development environment.
|
|
90
|
-
* @returns A reference to the new BroadcastChannelSynchronizer object.
|
|
91
|
-
* @example
|
|
92
|
-
* This example creates two BroadcastChannelSynchronizer objects to synchronize
|
|
93
|
-
* one MergeableStore to another.
|
|
94
|
-
*
|
|
95
|
-
* ```js
|
|
96
|
-
* import {createMergeableStore} from 'tinybase';
|
|
97
|
-
* import {createBroadcastChannelSynchronizer} from 'tinybase/synchronizers/synchronizer-broadcast-channel';
|
|
98
|
-
*
|
|
99
|
-
* const store1 = createMergeableStore();
|
|
100
|
-
* const store2 = createMergeableStore();
|
|
101
|
-
*
|
|
102
|
-
* const synchronizer1 = createBroadcastChannelSynchronizer(
|
|
103
|
-
* store1,
|
|
104
|
-
* 'channelA',
|
|
105
|
-
* );
|
|
106
|
-
* const synchronizer2 = createBroadcastChannelSynchronizer(
|
|
107
|
-
* store2,
|
|
108
|
-
* 'channelA',
|
|
109
|
-
* );
|
|
110
|
-
*
|
|
111
|
-
* await synchronizer1.startSync();
|
|
112
|
-
* await synchronizer2.startSync();
|
|
113
|
-
*
|
|
114
|
-
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
115
|
-
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
116
|
-
*
|
|
117
|
-
* // ...
|
|
118
|
-
* console.log(store1.getTables());
|
|
119
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
120
|
-
* console.log(store2.getTables());
|
|
121
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
122
|
-
*
|
|
123
|
-
* synchronizer1.destroy();
|
|
124
|
-
* synchronizer2.destroy();
|
|
125
|
-
* ```
|
|
126
|
-
* @category Creation
|
|
127
|
-
* @since v5.0.0
|
|
128
|
-
*/
|
|
129
|
-
export function createBroadcastChannelSynchronizer<
|
|
130
|
-
Schemas extends OptionalSchemas,
|
|
131
|
-
>(
|
|
132
|
-
store: MergeableStore<Schemas>,
|
|
133
|
-
channelName: string,
|
|
134
|
-
onSend?: Send,
|
|
135
|
-
onReceive?: Receive,
|
|
136
|
-
onIgnoredError?: (error: any) => void,
|
|
137
|
-
): BroadcastChannelSynchronizer<Schemas>;
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-empty-object-type */
|
|
2
|
-
/**
|
|
3
|
-
* The synchronizer-local module of the TinyBase project lets you synchronize
|
|
4
|
-
* MergeableStore data to and from other MergeableStore instances on the same
|
|
5
|
-
* local machine.
|
|
6
|
-
* @see Synchronization guide
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
* @module synchronizer-local
|
|
9
|
-
* @since v5.0.0
|
|
10
|
-
*/
|
|
11
|
-
import type {MergeableStore} from '../../mergeable-store/index.d.cts';
|
|
12
|
-
import type {Receive, Send, Synchronizer} from '../index.d.cts';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The LocalSynchronizer interface represents a Synchronizer that lets you
|
|
16
|
-
* synchronize MergeableStore data to and from other MergeableStore instances on
|
|
17
|
-
* the same local machine.
|
|
18
|
-
*
|
|
19
|
-
* You should use the createLocalSynchronizer function to create a
|
|
20
|
-
* LocalSynchronizer object.
|
|
21
|
-
*
|
|
22
|
-
* Having no specialized methods, it is a synonym for the Synchronizer
|
|
23
|
-
* interface. This is also something of a showcase Synchronizer, rather than
|
|
24
|
-
* something you would use in a production environment. If you _do_ need to
|
|
25
|
-
* synchronize two in-memory MergeableStore instances, you may prefer to use the
|
|
26
|
-
* merge function on either one of them instead of going to the effort of
|
|
27
|
-
* setting up this Synchronizer.
|
|
28
|
-
* @category Synchronizer
|
|
29
|
-
* @since v5.0.0
|
|
30
|
-
*/
|
|
31
|
-
export interface LocalSynchronizer extends Synchronizer {}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The createLocalSynchronizer function creates a LocalSynchronizer object that
|
|
35
|
-
* can synchronize MergeableStore data to and from other MergeableStore
|
|
36
|
-
* instances on the same local machine.
|
|
37
|
-
*
|
|
38
|
-
* This is something of a showcase Synchronizer, rather than something you would
|
|
39
|
-
* use in a production environment. If you _do_ need to synchronize two
|
|
40
|
-
* in-memory MergeableStore instances, you may prefer to use the merge function
|
|
41
|
-
* on either one of them instead of going to the effort of setting up this
|
|
42
|
-
* Synchronizer.
|
|
43
|
-
*
|
|
44
|
-
* As well as providing a reference to the MergeableStore to persist, a final
|
|
45
|
-
* set of optional handlers can be provided to help debug sends, receives, and
|
|
46
|
-
* errors respectively.
|
|
47
|
-
* @param store The MergeableStore to synchronize.
|
|
48
|
-
* @param onSend An optional handler for the messages that this Synchronizer
|
|
49
|
-
* sends. This is suitable for debugging synchronization issues in a development
|
|
50
|
-
* environment, since v5.1.
|
|
51
|
-
* @param onReceive An optional handler for the messages that this Synchronizer
|
|
52
|
-
* receives. This is suitable for debugging synchronization issues in a
|
|
53
|
-
* development environment, since v5.1.
|
|
54
|
-
* @param onIgnoredError An optional handler for the errors that the
|
|
55
|
-
* Synchronizer would otherwise ignore when trying to synchronize data. This is
|
|
56
|
-
* suitable for debugging synchronization issues in a development environment.
|
|
57
|
-
* @returns A reference to the new LocalSynchronizer object.
|
|
58
|
-
* @example
|
|
59
|
-
* This example creates two LocalSynchronizer objects to synchronize one
|
|
60
|
-
* MergeableStore to another.
|
|
61
|
-
*
|
|
62
|
-
* ```js
|
|
63
|
-
* import {createMergeableStore} from 'tinybase';
|
|
64
|
-
* import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
|
|
65
|
-
*
|
|
66
|
-
* const store1 = createMergeableStore();
|
|
67
|
-
* const store2 = createMergeableStore();
|
|
68
|
-
*
|
|
69
|
-
* const synchronizer1 = createLocalSynchronizer(store1);
|
|
70
|
-
* const synchronizer2 = createLocalSynchronizer(store2);
|
|
71
|
-
*
|
|
72
|
-
* await synchronizer1.startSync();
|
|
73
|
-
* await synchronizer2.startSync();
|
|
74
|
-
*
|
|
75
|
-
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
76
|
-
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
77
|
-
*
|
|
78
|
-
* // ...
|
|
79
|
-
* console.log(store1.getTables());
|
|
80
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
81
|
-
* console.log(store2.getTables());
|
|
82
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
83
|
-
*
|
|
84
|
-
* synchronizer1.destroy();
|
|
85
|
-
* synchronizer2.destroy();
|
|
86
|
-
* ```
|
|
87
|
-
* @category Creation
|
|
88
|
-
* @since v5.0.0
|
|
89
|
-
*/
|
|
90
|
-
export function createLocalSynchronizer(
|
|
91
|
-
store: MergeableStore,
|
|
92
|
-
onSend?: Send,
|
|
93
|
-
onReceive?: Receive,
|
|
94
|
-
onIgnoredError?: (error: any) => void,
|
|
95
|
-
): LocalSynchronizer;
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-empty-object-type */
|
|
2
|
-
/**
|
|
3
|
-
* The synchronizer-local module of the TinyBase project lets you synchronize
|
|
4
|
-
* MergeableStore data to and from other MergeableStore instances on the same
|
|
5
|
-
* local machine.
|
|
6
|
-
* @see Synchronization guide
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
* @module synchronizer-local
|
|
9
|
-
* @since v5.0.0
|
|
10
|
-
*/
|
|
11
|
-
import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.cts';
|
|
12
|
-
import type {OptionalSchemas} from '../../../store/with-schemas/index.d.cts';
|
|
13
|
-
import type {Receive, Send, Synchronizer} from '../../with-schemas/index.d.cts';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* The LocalSynchronizer interface represents a Synchronizer that lets you
|
|
17
|
-
* synchronize MergeableStore data to and from other MergeableStore instances on
|
|
18
|
-
* the same local machine.
|
|
19
|
-
*
|
|
20
|
-
* This has schema-based typing. The following is a simplified representation:
|
|
21
|
-
*
|
|
22
|
-
* ```ts override
|
|
23
|
-
* export interface LocalSynchronizer extends Synchronizer {}
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* You should use the createLocalSynchronizer function to create a
|
|
27
|
-
* LocalSynchronizer object.
|
|
28
|
-
*
|
|
29
|
-
* Having no specialized methods, it is a synonym for the Synchronizer
|
|
30
|
-
* interface. This is also something of a showcase Synchronizer, rather than
|
|
31
|
-
* something you would use in a production environment. If you _do_ need to
|
|
32
|
-
* synchronize two in-memory MergeableStore instances, you may prefer to use the
|
|
33
|
-
* merge function on either one of them instead of going to the effort of
|
|
34
|
-
* setting up this Synchronizer.
|
|
35
|
-
* @category Synchronizer
|
|
36
|
-
* @since v5.0.0
|
|
37
|
-
*/
|
|
38
|
-
export interface LocalSynchronizer<Schemas extends OptionalSchemas>
|
|
39
|
-
extends Synchronizer<Schemas> {}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* The createLocalSynchronizer function creates a LocalSynchronizer object that
|
|
43
|
-
* can synchronize MergeableStore data to and from other MergeableStore
|
|
44
|
-
* instances on the same local machine.
|
|
45
|
-
*
|
|
46
|
-
* This has schema-based typing. The following is a simplified representation:
|
|
47
|
-
*
|
|
48
|
-
* ```ts override
|
|
49
|
-
* createLocalSynchronizer(
|
|
50
|
-
* store: MergeableStore,
|
|
51
|
-
* onSend?: Send,
|
|
52
|
-
* onReceive?: Receive,
|
|
53
|
-
* onIgnoredError?: (error: any) => void,
|
|
54
|
-
* ): LocalSynchronizer;
|
|
55
|
-
* ```
|
|
56
|
-
*
|
|
57
|
-
* This is something of a showcase Synchronizer, rather than something you would
|
|
58
|
-
* use in a production environment. If you _do_ need to synchronize two
|
|
59
|
-
* in-memory MergeableStore instances, you may prefer to use the merge function
|
|
60
|
-
* on either one of them instead of going to the effort of setting up this
|
|
61
|
-
* Synchronizer.
|
|
62
|
-
*
|
|
63
|
-
* As well as providing a reference to the MergeableStore to persist, a final
|
|
64
|
-
* set of optional handlers can be provided to help debug sends, receives, and
|
|
65
|
-
* errors respectively.
|
|
66
|
-
* @param store The MergeableStore to synchronize.
|
|
67
|
-
* @param onSend An optional handler for the messages that this Synchronizer
|
|
68
|
-
* sends. This is suitable for debugging synchronization issues in a development
|
|
69
|
-
* environment, since v5.1.
|
|
70
|
-
* @param onReceive An optional handler for the messages that this Synchronizer
|
|
71
|
-
* receives. This is suitable for debugging synchronization issues in a
|
|
72
|
-
* development environment, since v5.1.
|
|
73
|
-
* @param onIgnoredError An optional handler for the errors that the
|
|
74
|
-
* Synchronizer would otherwise ignore when trying to synchronize data. This is
|
|
75
|
-
* suitable for debugging synchronization issues in a development environment.
|
|
76
|
-
* @returns A reference to the new LocalSynchronizer object.
|
|
77
|
-
* @example
|
|
78
|
-
* This example creates two LocalSynchronizer objects to synchronize one
|
|
79
|
-
* MergeableStore to another.
|
|
80
|
-
*
|
|
81
|
-
* ```js
|
|
82
|
-
* import {createMergeableStore} from 'tinybase';
|
|
83
|
-
* import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
|
|
84
|
-
*
|
|
85
|
-
* const store1 = createMergeableStore();
|
|
86
|
-
* const store2 = createMergeableStore();
|
|
87
|
-
*
|
|
88
|
-
* const synchronizer1 = createLocalSynchronizer(store1);
|
|
89
|
-
* const synchronizer2 = createLocalSynchronizer(store2);
|
|
90
|
-
*
|
|
91
|
-
* await synchronizer1.startSync();
|
|
92
|
-
* await synchronizer2.startSync();
|
|
93
|
-
*
|
|
94
|
-
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
95
|
-
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
96
|
-
*
|
|
97
|
-
* // ...
|
|
98
|
-
* console.log(store1.getTables());
|
|
99
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
100
|
-
* console.log(store2.getTables());
|
|
101
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
102
|
-
*
|
|
103
|
-
* synchronizer1.destroy();
|
|
104
|
-
* synchronizer2.destroy();
|
|
105
|
-
* ```
|
|
106
|
-
* @category Creation
|
|
107
|
-
* @since v5.0.0
|
|
108
|
-
*/
|
|
109
|
-
export function createLocalSynchronizer<Schemas extends OptionalSchemas>(
|
|
110
|
-
store: MergeableStore<Schemas>,
|
|
111
|
-
onSend?: Send,
|
|
112
|
-
onReceive?: Receive,
|
|
113
|
-
onIgnoredError?: (error: any) => void,
|
|
114
|
-
): LocalSynchronizer<Schemas>;
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The synchronizer-ws module of the TinyBase project lets you synchronize
|
|
3
|
-
* MergeableStore data to and from other MergeableStore instances via WebSockets
|
|
4
|
-
* facilitated by a server.
|
|
5
|
-
* @see Synchronization guide
|
|
6
|
-
* @see Todo App v6 (collaboration) demo
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
* @module synchronizer-ws-client
|
|
9
|
-
* @since v5.0.0
|
|
10
|
-
*/
|
|
11
|
-
import type {MergeableStore} from '../../mergeable-store/index.d.cts';
|
|
12
|
-
import type {Receive, Send, Synchronizer} from '../index.d.cts';
|
|
13
|
-
import type {WebSocket as WsWebSocket} from 'ws';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* The WebSocketTypes type represents the valid types of WebSocket that can be
|
|
17
|
-
* used with the WsSynchronizer.
|
|
18
|
-
*
|
|
19
|
-
* This includes the browser-native WebSocket type, as well as the WebSocket
|
|
20
|
-
* type from the well-known `ws` package (such that the Synchronizer can be used
|
|
21
|
-
* in a server environment).
|
|
22
|
-
* @category Creation
|
|
23
|
-
* @since v5.0.0
|
|
24
|
-
*/
|
|
25
|
-
export type WebSocketTypes = WebSocket | WsWebSocket;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* The WsSynchronizer interface represents a Synchronizer that lets you
|
|
29
|
-
* synchronize MergeableStore data to and from other MergeableStore instances
|
|
30
|
-
* via WebSockets facilitated by a server.
|
|
31
|
-
*
|
|
32
|
-
* You should use the createWsSynchronizer function to create a WsSynchronizer
|
|
33
|
-
* object.
|
|
34
|
-
*
|
|
35
|
-
* It is a minor extension to the Synchronizer interface and simply provides an
|
|
36
|
-
* extra getWebSocket method for accessing a reference to the WebSocket being
|
|
37
|
-
* used.
|
|
38
|
-
* @category Synchronizer
|
|
39
|
-
* @since v5.0.0
|
|
40
|
-
*/
|
|
41
|
-
export interface WsSynchronizer<WebSocketType extends WebSocketTypes>
|
|
42
|
-
extends Synchronizer {
|
|
43
|
-
/**
|
|
44
|
-
* The getWebSocket method returns a reference to the WebSocket being used for
|
|
45
|
-
* synchronization.
|
|
46
|
-
* @returns The WebSocket reference.
|
|
47
|
-
* @example
|
|
48
|
-
* This example creates a server and WsSynchronizer object for a newly-created
|
|
49
|
-
* MergeableStore and then gets the WebSocket reference back out again.
|
|
50
|
-
*
|
|
51
|
-
* ```js
|
|
52
|
-
* import {createMergeableStore} from 'tinybase';
|
|
53
|
-
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
|
|
54
|
-
* import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
|
|
55
|
-
* import {WebSocket, WebSocketServer} from 'ws';
|
|
56
|
-
*
|
|
57
|
-
* const server = createWsServer(new WebSocketServer({port: 8046}));
|
|
58
|
-
*
|
|
59
|
-
* const store = createMergeableStore();
|
|
60
|
-
* const webSocket = new WebSocket('ws://localhost:8046');
|
|
61
|
-
* const synchronizer = await createWsSynchronizer(store, webSocket);
|
|
62
|
-
*
|
|
63
|
-
* console.log(synchronizer.getWebSocket() == webSocket);
|
|
64
|
-
* // -> true
|
|
65
|
-
*
|
|
66
|
-
* synchronizer.destroy();
|
|
67
|
-
* server.destroy();
|
|
68
|
-
* ```
|
|
69
|
-
* @category Getter
|
|
70
|
-
* @since v5.0.0
|
|
71
|
-
*/
|
|
72
|
-
getWebSocket(): WebSocketType;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* The createWsSynchronizer function creates a WsSynchronizer object that can
|
|
77
|
-
* synchronize MergeableStore data to and from other MergeableStore instances
|
|
78
|
-
* via WebSockets facilitated by a WsServer.
|
|
79
|
-
*
|
|
80
|
-
* As well as providing a reference to the MergeableStore to persist, you must
|
|
81
|
-
* provide a configured WebSocket to send synchronization messages over.
|
|
82
|
-
*
|
|
83
|
-
* Instead of the raw browser implementation of WebSocket, you may prefer to use
|
|
84
|
-
* the [Reconnecting
|
|
85
|
-
* WebSocket](https://github.com/pladaria/reconnecting-websocket) wrapper so
|
|
86
|
-
* that if a client goes offline, it can easily re-establish a connection when
|
|
87
|
-
* it comes back online. Its API is compatible with this Synchronizer.
|
|
88
|
-
*
|
|
89
|
-
* You can indicate how long the Synchronizer will wait for responses to message
|
|
90
|
-
* requests before timing out. A final set of optional handlers can be provided
|
|
91
|
-
* to help debug sends, receives, and errors respectively.
|
|
92
|
-
*
|
|
93
|
-
* This method is asynchronous because it will await the websocket's connection
|
|
94
|
-
* to the server. You will need to `await` a call to this function or handle the
|
|
95
|
-
* return type natively as a Promise.
|
|
96
|
-
* @param store The MergeableStore to synchronize.
|
|
97
|
-
* @param webSocket The WebSocket to send synchronization messages over.
|
|
98
|
-
* @param requestTimeoutSeconds An optional time in seconds that the
|
|
99
|
-
* Synchronizer will wait for responses to request messages, defaulting to 1.
|
|
100
|
-
* @param onSend An optional handler for the messages that this Synchronizer
|
|
101
|
-
* sends. This is suitable for debugging synchronization issues in a development
|
|
102
|
-
* environment, since v5.1.
|
|
103
|
-
* @param onReceive An optional handler for the messages that this Synchronizer
|
|
104
|
-
* receives. This is suitable for debugging synchronization issues in a
|
|
105
|
-
* development environment, since v5.1.
|
|
106
|
-
* @param onIgnoredError An optional handler for the errors that the
|
|
107
|
-
* Synchronizer would otherwise ignore when trying to synchronize data. This is
|
|
108
|
-
* suitable for debugging synchronization issues in a development environment.
|
|
109
|
-
* @returns A reference to the new WsSynchronizer object.
|
|
110
|
-
* @example
|
|
111
|
-
* This example creates two WsSynchronizer objects to synchronize one
|
|
112
|
-
* MergeableStore to another via a server.
|
|
113
|
-
*
|
|
114
|
-
* ```js
|
|
115
|
-
* import {createMergeableStore} from 'tinybase';
|
|
116
|
-
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
|
|
117
|
-
* import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
|
|
118
|
-
* import {WebSocket, WebSocketServer} from 'ws';
|
|
119
|
-
*
|
|
120
|
-
* const server = createWsServer(new WebSocketServer({port: 8047}));
|
|
121
|
-
*
|
|
122
|
-
* const store1 = createMergeableStore();
|
|
123
|
-
* const store2 = createMergeableStore();
|
|
124
|
-
*
|
|
125
|
-
* const synchronizer1 = await createWsSynchronizer(
|
|
126
|
-
* store1,
|
|
127
|
-
* new WebSocket('ws://localhost:8047'),
|
|
128
|
-
* );
|
|
129
|
-
* const synchronizer2 = await createWsSynchronizer(
|
|
130
|
-
* store2,
|
|
131
|
-
* new WebSocket('ws://localhost:8047'),
|
|
132
|
-
* );
|
|
133
|
-
*
|
|
134
|
-
* await synchronizer1.startSync();
|
|
135
|
-
* await synchronizer2.startSync();
|
|
136
|
-
*
|
|
137
|
-
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
138
|
-
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
139
|
-
*
|
|
140
|
-
* // ...
|
|
141
|
-
* console.log(store1.getTables());
|
|
142
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
143
|
-
* console.log(store2.getTables());
|
|
144
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
145
|
-
*
|
|
146
|
-
* synchronizer1.destroy();
|
|
147
|
-
* synchronizer2.destroy();
|
|
148
|
-
* server.destroy();
|
|
149
|
-
* ```
|
|
150
|
-
* @category Creation
|
|
151
|
-
* @since v5.0.0
|
|
152
|
-
*/
|
|
153
|
-
export function createWsSynchronizer<WebSocketType extends WebSocketTypes>(
|
|
154
|
-
store: MergeableStore,
|
|
155
|
-
webSocket: WebSocketType,
|
|
156
|
-
requestTimeoutSeconds?: number,
|
|
157
|
-
onSend?: Send,
|
|
158
|
-
onReceive?: Receive,
|
|
159
|
-
onIgnoredError?: (error: any) => void,
|
|
160
|
-
): Promise<WsSynchronizer<WebSocketType>>;
|
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The synchronizer-ws module of the TinyBase project lets you synchronize
|
|
3
|
-
* MergeableStore data to and from other MergeableStore instances via WebSockets
|
|
4
|
-
* facilitated by a server.
|
|
5
|
-
* @see Synchronization guide
|
|
6
|
-
* @see Todo App v6 (collaboration) demo
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
* @module synchronizer-ws-client
|
|
9
|
-
* @since v5.0.0
|
|
10
|
-
*/
|
|
11
|
-
import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.cts';
|
|
12
|
-
import type {OptionalSchemas} from '../../../store/with-schemas/index.d.cts';
|
|
13
|
-
import type {Receive, Send, Synchronizer} from '../../with-schemas/index.d.cts';
|
|
14
|
-
import type {WebSocket as WsWebSocket} from 'ws';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The WebSocketTypes type represents the valid types of WebSocket that can be
|
|
18
|
-
* used with the WsSynchronizer.
|
|
19
|
-
*
|
|
20
|
-
* This includes the browser-native WebSocket type, as well as the WebSocket
|
|
21
|
-
* type from the well-known `ws` package (such that the Synchronizer can be used
|
|
22
|
-
* in a server environment).
|
|
23
|
-
* @category Creation
|
|
24
|
-
* @since v5.0.0
|
|
25
|
-
*/
|
|
26
|
-
export type WebSocketTypes = WebSocket | WsWebSocket;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The WsSynchronizer interface represents a Synchronizer that lets you
|
|
30
|
-
* synchronize MergeableStore data to and from other MergeableStore instances
|
|
31
|
-
* via WebSockets facilitated by a server.
|
|
32
|
-
*
|
|
33
|
-
* You should use the createWsSynchronizer function to create a WsSynchronizer
|
|
34
|
-
* object.
|
|
35
|
-
*
|
|
36
|
-
* It is a minor extension to the Synchronizer interface and simply provides an
|
|
37
|
-
* extra getWebSocket method for accessing a reference to the WebSocket being
|
|
38
|
-
* used.
|
|
39
|
-
* @category Synchronizer
|
|
40
|
-
* @since v5.0.0
|
|
41
|
-
*/
|
|
42
|
-
export interface WsSynchronizer<
|
|
43
|
-
Schemas extends OptionalSchemas,
|
|
44
|
-
WebSocketType extends WebSocketTypes,
|
|
45
|
-
> extends Synchronizer<Schemas> {
|
|
46
|
-
/**
|
|
47
|
-
* The getWebSocket method returns a reference to the WebSocket being used for
|
|
48
|
-
* synchronization.
|
|
49
|
-
* @returns The WebSocket reference.
|
|
50
|
-
* @example
|
|
51
|
-
* This example creates a server and WsSynchronizer object for a newly-created
|
|
52
|
-
* MergeableStore and then gets the WebSocket reference back out again.
|
|
53
|
-
*
|
|
54
|
-
* ```js
|
|
55
|
-
* import {createMergeableStore} from 'tinybase';
|
|
56
|
-
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
|
|
57
|
-
* import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
|
|
58
|
-
* import {WebSocket, WebSocketServer} from 'ws';
|
|
59
|
-
*
|
|
60
|
-
* const server = createWsServer(new WebSocketServer({port: 8046}));
|
|
61
|
-
*
|
|
62
|
-
* const store = createMergeableStore();
|
|
63
|
-
* const webSocket = new WebSocket('ws://localhost:8046');
|
|
64
|
-
* const synchronizer = await createWsSynchronizer(store, webSocket);
|
|
65
|
-
*
|
|
66
|
-
* console.log(synchronizer.getWebSocket() == webSocket);
|
|
67
|
-
* // -> true
|
|
68
|
-
*
|
|
69
|
-
* synchronizer.destroy();
|
|
70
|
-
* server.destroy();
|
|
71
|
-
* ```
|
|
72
|
-
* @category Getter
|
|
73
|
-
* @since v5.0.0
|
|
74
|
-
*/
|
|
75
|
-
getWebSocket(): WebSocketType;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* The createWsSynchronizer function creates a WsSynchronizer object that can
|
|
80
|
-
* synchronize MergeableStore data to and from other MergeableStore instances
|
|
81
|
-
* via WebSockets facilitated by a WsServer.
|
|
82
|
-
*
|
|
83
|
-
* This has schema-based typing. The following is a simplified representation:
|
|
84
|
-
*
|
|
85
|
-
* ```ts override
|
|
86
|
-
* createWsSynchronizer<WebSocketType extends WebSocketTypes>(
|
|
87
|
-
* store: MergeableStore,
|
|
88
|
-
* webSocket: WebSocketType,
|
|
89
|
-
* requestTimeoutSeconds?: number,
|
|
90
|
-
* onSend?: Send,
|
|
91
|
-
* onReceive?: Receive,
|
|
92
|
-
* onIgnoredError?: (error: any) => void,
|
|
93
|
-
* ): Promise<WsSynchronizer<WebSocketType>>;
|
|
94
|
-
* ```
|
|
95
|
-
*
|
|
96
|
-
* As well as providing a reference to the MergeableStore to persist, you must
|
|
97
|
-
* provide a configured WebSocket to send synchronization messages over.
|
|
98
|
-
*
|
|
99
|
-
* Instead of the raw browser implementation of WebSocket, you may prefer to use
|
|
100
|
-
* the [Reconnecting
|
|
101
|
-
* WebSocket](https://github.com/pladaria/reconnecting-websocket) wrapper so
|
|
102
|
-
* that if a client goes offline, it can easily re-establish a connection when
|
|
103
|
-
* it comes back online. Its API is compatible with this Synchronizer.
|
|
104
|
-
*
|
|
105
|
-
* You can indicate how long the Synchronizer will wait for responses to message
|
|
106
|
-
* requests before timing out. A final set of optional handlers can be provided
|
|
107
|
-
* to help debug sends, receives, and errors respectively.
|
|
108
|
-
*
|
|
109
|
-
* This method is asynchronous because it will await the websocket's connection
|
|
110
|
-
* to the server. You will need to `await` a call to this function or handle the
|
|
111
|
-
* return type natively as a Promise.
|
|
112
|
-
* @param store The MergeableStore to synchronize.
|
|
113
|
-
* @param webSocket The WebSocket to send synchronization messages over.
|
|
114
|
-
* @param requestTimeoutSeconds An optional time in seconds that the
|
|
115
|
-
* Synchronizer will wait for responses to request messages, defaulting to 1.
|
|
116
|
-
* @param onSend An optional handler for the messages that this Synchronizer
|
|
117
|
-
* sends. This is suitable for debugging synchronization issues in a development
|
|
118
|
-
* environment, since v5.1.
|
|
119
|
-
* @param onReceive An optional handler for the messages that this Synchronizer
|
|
120
|
-
* receives. This is suitable for debugging synchronization issues in a
|
|
121
|
-
* development environment, since v5.1.
|
|
122
|
-
* @param onIgnoredError An optional handler for the errors that the
|
|
123
|
-
* Synchronizer would otherwise ignore when trying to synchronize data. This is
|
|
124
|
-
* suitable for debugging synchronization issues in a development environment.
|
|
125
|
-
* @returns A reference to the new WsSynchronizer object.
|
|
126
|
-
* @example
|
|
127
|
-
* This example creates two WsSynchronizer objects to synchronize one
|
|
128
|
-
* MergeableStore to another via a server.
|
|
129
|
-
*
|
|
130
|
-
* ```js
|
|
131
|
-
* import {createMergeableStore} from 'tinybase';
|
|
132
|
-
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
|
|
133
|
-
* import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
|
|
134
|
-
* import {WebSocket, WebSocketServer} from 'ws';
|
|
135
|
-
*
|
|
136
|
-
* const server = createWsServer(new WebSocketServer({port: 8047}));
|
|
137
|
-
*
|
|
138
|
-
* const store1 = createMergeableStore();
|
|
139
|
-
* const store2 = createMergeableStore();
|
|
140
|
-
*
|
|
141
|
-
* const synchronizer1 = await createWsSynchronizer(
|
|
142
|
-
* store1,
|
|
143
|
-
* new WebSocket('ws://localhost:8047'),
|
|
144
|
-
* );
|
|
145
|
-
* const synchronizer2 = await createWsSynchronizer(
|
|
146
|
-
* store2,
|
|
147
|
-
* new WebSocket('ws://localhost:8047'),
|
|
148
|
-
* );
|
|
149
|
-
*
|
|
150
|
-
* await synchronizer1.startSync();
|
|
151
|
-
* await synchronizer2.startSync();
|
|
152
|
-
*
|
|
153
|
-
* store1.setTables({pets: {fido: {species: 'dog'}}});
|
|
154
|
-
* store2.setTables({pets: {felix: {species: 'cat'}}});
|
|
155
|
-
*
|
|
156
|
-
* // ...
|
|
157
|
-
* console.log(store1.getTables());
|
|
158
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
159
|
-
* console.log(store2.getTables());
|
|
160
|
-
* // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
|
|
161
|
-
*
|
|
162
|
-
* synchronizer1.destroy();
|
|
163
|
-
* synchronizer2.destroy();
|
|
164
|
-
* server.destroy();
|
|
165
|
-
* ```
|
|
166
|
-
* @category Creation
|
|
167
|
-
* @since v5.0.0
|
|
168
|
-
*/
|
|
169
|
-
export function createWsSynchronizer<
|
|
170
|
-
Schemas extends OptionalSchemas,
|
|
171
|
-
WebSocketType extends WebSocketTypes,
|
|
172
|
-
>(
|
|
173
|
-
store: MergeableStore<Schemas>,
|
|
174
|
-
webSocket: WebSocketType,
|
|
175
|
-
requestTimeoutSeconds?: number,
|
|
176
|
-
onSend?: Send,
|
|
177
|
-
onReceive?: Receive,
|
|
178
|
-
onIgnoredError?: (error: any) => void,
|
|
179
|
-
): Promise<WsSynchronizer<Schemas, WebSocketType>>;
|