@signaldb/sync 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ import type { Change, Snapshot, SyncOperation } from './types';
5
5
  type SyncOptions<T extends Record<string, any>> = {
6
6
  name: string;
7
7
  } & T;
8
+ type CleanupFunction = (() => void | Promise<void>) | void;
8
9
  interface Options<CollectionOptions extends Record<string, any>, ItemType extends BaseItem<IdType> = BaseItem, IdType = any> {
9
10
  pull: (collectionOptions: SyncOptions<CollectionOptions>, pullParameters: {
10
11
  lastFinishedSyncStart?: number;
@@ -13,11 +14,12 @@ interface Options<CollectionOptions extends Record<string, any>, ItemType extend
13
14
  push: (collectionOptions: SyncOptions<CollectionOptions>, pushParameters: {
14
15
  changes: Changeset<ItemType>;
15
16
  }) => Promise<void>;
16
- registerRemoteChange?: (collectionOptions: SyncOptions<CollectionOptions>, onChange: (data?: LoadResponse<ItemType>) => Promise<void>) => void;
17
+ registerRemoteChange?: (collectionOptions: SyncOptions<CollectionOptions>, onChange: (data?: LoadResponse<ItemType>) => Promise<void>) => CleanupFunction | Promise<CleanupFunction>;
17
18
  id?: string;
18
19
  persistenceAdapter?: (id: string, registerErrorHandler: (handler: (error: Error) => void) => void) => PersistenceAdapter<any, any>;
19
20
  reactivity?: ReactivityAdapter;
20
21
  onError?: (collectionOptions: SyncOptions<CollectionOptions>, error: Error) => void;
22
+ autostart?: boolean;
21
23
  }
22
24
  /**
23
25
  * Class to manage syncing of collections.
@@ -47,10 +49,13 @@ interface Options<CollectionOptions extends Record<string, any>, ItemType extend
47
49
  */
48
50
  export default class SyncManager<CollectionOptions extends Record<string, any>, ItemType extends BaseItem<IdType> = BaseItem, IdType = any> {
49
51
  protected options: Options<CollectionOptions, ItemType, IdType>;
50
- protected collections: Map<string, [
51
- Collection<ItemType, IdType, any>,
52
- SyncOptions<CollectionOptions>
53
- ]>;
52
+ protected collections: Map<string, {
53
+ collection: Collection<ItemType, IdType, any>;
54
+ options: SyncOptions<CollectionOptions>;
55
+ readyPromise: Promise<void>;
56
+ syncPaused: boolean;
57
+ cleanupFunction?: CleanupFunction;
58
+ }>;
54
59
  protected changes: Collection<Change<ItemType>, string>;
55
60
  protected snapshots: Collection<Snapshot<ItemType>, string>;
56
61
  protected syncOperations: Collection<SyncOperation, string>;
@@ -82,11 +87,25 @@ export default class SyncManager<CollectionOptions extends Record<string, any>,
82
87
  dispose(): Promise<void>;
83
88
  /**
84
89
  * Gets a collection with it's options by name
90
+ * @deprecated Use getCollectionProperties instead.
85
91
  * @param name Name of the collection
86
92
  * @throws Will throw an error if the name wasn't found
87
93
  * @returns Tuple of collection and options
88
94
  */
89
- getCollection(name: string): [Collection<ItemType, IdType, any>, SyncOptions<CollectionOptions>];
95
+ getCollection(name: string): (Collection<ItemType, IdType, any> | SyncOptions<CollectionOptions>)[];
96
+ /**
97
+ * Gets collection options by name
98
+ * @param name Name of the collection
99
+ * @throws Will throw an error if the name wasn't found
100
+ * @returns An object of all properties of the collection
101
+ */
102
+ getCollectionProperties(name: string): {
103
+ collection: Collection<ItemType, IdType, any>;
104
+ options: SyncOptions<CollectionOptions>;
105
+ readyPromise: Promise<void>;
106
+ syncPaused: boolean;
107
+ cleanupFunction?: CleanupFunction | undefined;
108
+ };
90
109
  /**
91
110
  * Adds a collection to the sync manager.
92
111
  * @param collection Collection to add
@@ -96,6 +115,30 @@ export default class SyncManager<CollectionOptions extends Record<string, any>,
96
115
  addCollection(collection: Collection<ItemType, IdType, any>, options: SyncOptions<CollectionOptions>): void;
97
116
  protected deboucedPush: (name: string) => void;
98
117
  protected schedulePush(name: string): void;
118
+ /**
119
+ * Setup all collections to be synced with remote changes
120
+ * and enable automatic pushing changes to the remote source.
121
+ */
122
+ startAll(): Promise<void>;
123
+ /**
124
+ * Setup a collection to be synced with remote changes
125
+ * and enable automatic pushing changes to the remote source.
126
+ * @param name Name of the collection
127
+ */
128
+ startSync(name: string): Promise<void>;
129
+ /**
130
+ * Pauses the sync process for all collections.
131
+ * This means that the collections will not be synced with remote changes
132
+ * and changes will not automatically be pushed to the remote source.
133
+ */
134
+ pauseAll(): Promise<void>;
135
+ /**
136
+ * Pauses the sync process for a collection.
137
+ * This means that the collection will not be synced with remote changes
138
+ * and changes will not automatically be pushed to the remote source.
139
+ * @param name Name of the collection
140
+ */
141
+ pauseSync(name: string): Promise<void>;
99
142
  /**
100
143
  * Starts the sync process for all collections
101
144
  */