@signaldb/sync 1.0.0 → 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>;
@@ -59,6 +64,7 @@ export default class SyncManager<CollectionOptions extends Record<string, any>,
59
64
  protected persistenceReady: Promise<void>;
60
65
  protected isDisposed: boolean;
61
66
  protected instanceId: string;
67
+ protected id: string;
62
68
  /**
63
69
  * @param options Collection options
64
70
  * @param options.pull Function to pull data from remote source.
@@ -81,11 +87,25 @@ export default class SyncManager<CollectionOptions extends Record<string, any>,
81
87
  dispose(): Promise<void>;
82
88
  /**
83
89
  * Gets a collection with it's options by name
90
+ * @deprecated Use getCollectionProperties instead.
84
91
  * @param name Name of the collection
85
92
  * @throws Will throw an error if the name wasn't found
86
93
  * @returns Tuple of collection and options
87
94
  */
88
- 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
+ };
89
109
  /**
90
110
  * Adds a collection to the sync manager.
91
111
  * @param collection Collection to add
@@ -95,6 +115,30 @@ export default class SyncManager<CollectionOptions extends Record<string, any>,
95
115
  addCollection(collection: Collection<ItemType, IdType, any>, options: SyncOptions<CollectionOptions>): void;
96
116
  protected deboucedPush: (name: string) => void;
97
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>;
98
142
  /**
99
143
  * Starts the sync process for all collections
100
144
  */
@@ -6,4 +6,4 @@ import type { Change } from './types';
6
6
  * @param changes The changes to apply to the items
7
7
  * @returns The new items after applying the changes
8
8
  */
9
- export default function applyChanges<ItemType extends BaseItem<IdType>, IdType>(items: ItemType[], changes: Change<ItemType, IdType>[]): ItemType[];
9
+ export default function applyChanges<ItemType extends BaseItem<IdType>, IdType>(items: ItemType[], changes: Change<ItemType, IdType>[]): Promise<ItemType[]>;