@powersync/common 0.0.0-dev-20260504100448 → 0.0.0-dev-20260630141119
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/lib/attachments/AttachmentContext.d.ts +9 -8
- package/lib/attachments/AttachmentContext.js +8 -3
- package/lib/attachments/AttachmentContext.js.map +1 -1
- package/lib/attachments/AttachmentErrorHandler.d.ts +6 -6
- package/lib/attachments/AttachmentQueue.d.ts +85 -36
- package/lib/attachments/AttachmentQueue.js +19 -21
- package/lib/attachments/AttachmentQueue.js.map +1 -1
- package/lib/attachments/AttachmentService.d.ts +3 -3
- package/lib/attachments/AttachmentService.js +4 -3
- package/lib/attachments/AttachmentService.js.map +1 -1
- package/lib/attachments/LocalStorageAdapter.d.ts +14 -8
- package/lib/attachments/LocalStorageAdapter.js +3 -0
- package/lib/attachments/LocalStorageAdapter.js.map +1 -1
- package/lib/attachments/RemoteStorageAdapter.d.ts +4 -4
- package/lib/attachments/Schema.d.ts +14 -6
- package/lib/attachments/Schema.js +8 -3
- package/lib/attachments/Schema.js.map +1 -1
- package/lib/attachments/SyncingService.d.ts +2 -2
- package/lib/attachments/SyncingService.js +8 -3
- package/lib/attachments/SyncingService.js.map +1 -1
- package/lib/attachments/WatchedAttachmentItem.d.ts +3 -1
- package/lib/client/{AbstractPowerSyncDatabase.d.ts → CommonPowerSyncDatabase.d.ts} +112 -238
- package/lib/client/CommonPowerSyncDatabase.js +2 -0
- package/lib/client/CommonPowerSyncDatabase.js.map +1 -0
- package/lib/client/Query.d.ts +9 -0
- package/lib/client/SQLOpenFactory.d.ts +44 -10
- package/lib/client/SQLOpenFactory.js +1 -19
- package/lib/client/SQLOpenFactory.js.map +1 -1
- package/lib/client/compilableQueryWatch.d.ts +8 -2
- package/lib/client/compilableQueryWatch.js +4 -2
- package/lib/client/compilableQueryWatch.js.map +1 -1
- package/lib/client/connection/PowerSyncBackendConnector.d.ts +6 -3
- package/lib/client/connection/PowerSyncCredentials.d.ts +3 -0
- package/lib/client/runOnSchemaChange.d.ts +5 -2
- package/lib/client/runOnSchemaChange.js +3 -0
- package/lib/client/runOnSchemaChange.js.map +1 -1
- package/lib/client/sync/bucket/CrudBatch.d.ts +2 -0
- package/lib/client/sync/bucket/CrudBatch.js +2 -0
- package/lib/client/sync/bucket/CrudBatch.js.map +1 -1
- package/lib/client/sync/bucket/CrudEntry.d.ts +9 -31
- package/lib/client/sync/bucket/CrudEntry.js +2 -97
- package/lib/client/sync/bucket/CrudEntry.js.map +1 -1
- package/lib/client/sync/bucket/CrudTransaction.d.ts +3 -0
- package/lib/client/sync/bucket/CrudTransaction.js +3 -0
- package/lib/client/sync/bucket/CrudTransaction.js.map +1 -1
- package/lib/client/sync/options.d.ts +65 -0
- package/lib/client/sync/options.js +25 -0
- package/lib/client/sync/options.js.map +1 -0
- package/lib/client/sync/stream/JsonValue.d.ts +3 -0
- package/lib/client/sync/sync-streams.d.ts +23 -8
- package/lib/client/triggers/TriggerManager.d.ts +28 -56
- package/lib/client/triggers/TriggerManager.js +2 -1
- package/lib/client/triggers/TriggerManager.js.map +1 -1
- package/lib/client/triggers/sanitizeSQL.d.ts +4 -0
- package/lib/client/triggers/sanitizeSQL.js +4 -0
- package/lib/client/triggers/sanitizeSQL.js.map +1 -1
- package/lib/client/watched/GetAllQuery.d.ts +7 -3
- package/lib/client/watched/GetAllQuery.js +3 -1
- package/lib/client/watched/GetAllQuery.js.map +1 -1
- package/lib/client/watched/WatchedQuery.d.ts +20 -6
- package/lib/client/watched/WatchedQuery.js +3 -5
- package/lib/client/watched/WatchedQuery.js.map +1 -1
- package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +15 -45
- package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -172
- package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +4 -22
- package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -82
- package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/comparators.d.ts +8 -0
- package/lib/client/watched/processors/comparators.js +4 -0
- package/lib/client/watched/processors/comparators.js.map +1 -1
- package/lib/db/DBAdapter.d.ts +90 -113
- package/lib/db/DBAdapter.js +70 -89
- package/lib/db/DBAdapter.js.map +1 -1
- package/lib/db/QueryResult.d.ts +104 -0
- package/lib/db/QueryResult.js +96 -0
- package/lib/db/QueryResult.js.map +1 -0
- package/lib/db/crud/SyncProgress.d.ts +7 -14
- package/lib/db/crud/SyncProgress.js +1 -60
- package/lib/db/crud/SyncProgress.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +54 -91
- package/lib/db/crud/SyncStatus.js +1 -245
- package/lib/db/crud/SyncStatus.js.map +1 -1
- package/lib/db/crud/UploadQueueStatus.d.ts +3 -0
- package/lib/db/crud/UploadQueueStatus.js +3 -0
- package/lib/db/crud/UploadQueueStatus.js.map +1 -1
- package/lib/db/schema/Column.d.ts +22 -1
- package/lib/db/schema/Column.js +10 -4
- package/lib/db/schema/Column.js.map +1 -1
- package/lib/db/schema/Index.d.ts +8 -3
- package/lib/db/schema/Index.js +4 -1
- package/lib/db/schema/Index.js.map +1 -1
- package/lib/db/schema/IndexedColumn.d.ts +8 -3
- package/lib/db/schema/IndexedColumn.js +4 -1
- package/lib/db/schema/IndexedColumn.js.map +1 -1
- package/lib/db/schema/RawTable.d.ts +7 -1
- package/lib/db/schema/Schema.d.ts +10 -29
- package/lib/db/schema/Schema.js +3 -1
- package/lib/db/schema/Schema.js.map +1 -1
- package/lib/db/schema/Table.d.ts +69 -91
- package/lib/db/schema/Table.js +88 -87
- package/lib/db/schema/Table.js.map +1 -1
- package/lib/index.d.ts +6 -22
- package/lib/index.js +5 -22
- package/lib/index.js.map +1 -1
- package/lib/types/types.d.ts +6 -0
- package/lib/utils/BaseObserver.d.ts +12 -0
- package/lib/utils/BaseObserver.js +3 -0
- package/lib/utils/BaseObserver.js.map +1 -1
- package/lib/utils/Logger.d.ts +60 -22
- package/lib/utils/Logger.js +38 -30
- package/lib/utils/Logger.js.map +1 -1
- package/lib/utils/MetaBaseObserver.d.ts +11 -11
- package/lib/utils/MetaBaseObserver.js +1 -50
- package/lib/utils/MetaBaseObserver.js.map +1 -1
- package/lib/utils/mutex.d.ts +3 -45
- package/lib/utils/mutex.js +1 -153
- package/lib/utils/mutex.js.map +1 -1
- package/package.json +9 -46
- package/src/attachments/AttachmentContext.ts +15 -13
- package/src/attachments/AttachmentErrorHandler.ts +6 -6
- package/src/attachments/AttachmentQueue.ts +99 -42
- package/src/attachments/AttachmentService.ts +7 -6
- package/src/attachments/LocalStorageAdapter.ts +14 -8
- package/src/attachments/README.md +2 -0
- package/src/attachments/RemoteStorageAdapter.ts +4 -4
- package/src/attachments/Schema.ts +14 -6
- package/src/attachments/SyncingService.ts +10 -6
- package/src/attachments/WatchedAttachmentItem.ts +3 -1
- package/src/client/CommonPowerSyncDatabase.ts +540 -0
- package/src/client/Query.ts +9 -0
- package/src/client/SQLOpenFactory.ts +47 -19
- package/src/client/compilableQueryWatch.ts +9 -4
- package/src/client/connection/PowerSyncBackendConnector.ts +6 -3
- package/src/client/connection/PowerSyncCredentials.ts +3 -0
- package/src/client/runOnSchemaChange.ts +5 -2
- package/src/client/sync/bucket/CrudBatch.ts +2 -0
- package/src/client/sync/bucket/CrudEntry.ts +10 -101
- package/src/client/sync/bucket/CrudTransaction.ts +3 -0
- package/src/client/sync/options.ts +77 -0
- package/src/client/sync/stream/JsonValue.ts +3 -0
- package/src/client/sync/sync-streams.ts +23 -10
- package/src/client/triggers/TriggerManager.ts +29 -59
- package/src/client/triggers/sanitizeSQL.ts +5 -0
- package/src/client/watched/GetAllQuery.ts +8 -4
- package/src/client/watched/WatchedQuery.ts +20 -11
- package/src/client/watched/processors/DifferentialQueryProcessor.ts +19 -219
- package/src/client/watched/processors/OnChangeQueryProcessor.ts +4 -109
- package/src/client/watched/processors/comparators.ts +8 -0
- package/src/db/DBAdapter.ts +160 -175
- package/src/db/QueryResult.ts +195 -0
- package/src/db/crud/SyncProgress.ts +8 -43
- package/src/db/crud/SyncStatus.ts +66 -216
- package/src/db/crud/UploadQueueStatus.ts +3 -0
- package/src/db/schema/Column.ts +22 -5
- package/src/db/schema/Index.ts +9 -3
- package/src/db/schema/IndexedColumn.ts +9 -3
- package/src/db/schema/RawTable.ts +7 -1
- package/src/db/schema/Schema.ts +12 -7
- package/src/db/schema/Table.ts +130 -180
- package/src/index.ts +6 -22
- package/src/types/types.ts +6 -0
- package/src/utils/BaseObserver.ts +12 -0
- package/src/utils/Logger.ts +86 -31
- package/src/utils/MetaBaseObserver.ts +14 -60
- package/src/utils/mutex.ts +4 -189
- package/dist/bundle.cjs +0 -14136
- package/dist/bundle.cjs.map +0 -1
- package/dist/bundle.mjs +0 -14056
- package/dist/bundle.mjs.map +0 -1
- package/dist/bundle.node.cjs +0 -11613
- package/dist/bundle.node.cjs.map +0 -1
- package/dist/bundle.node.mjs +0 -11533
- package/dist/bundle.node.mjs.map +0 -1
- package/dist/index.d.cts +0 -4052
- package/legacy/sync_protocol.d.ts +0 -103
- package/lib/client/AbstractPowerSyncDatabase.js +0 -987
- package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
- package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -22
- package/lib/client/AbstractPowerSyncOpenFactory.js +0 -25
- package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
- package/lib/client/ConnectionManager.d.ts +0 -112
- package/lib/client/ConnectionManager.js +0 -294
- package/lib/client/ConnectionManager.js.map +0 -1
- package/lib/client/CustomQuery.d.ts +0 -22
- package/lib/client/CustomQuery.js +0 -43
- package/lib/client/CustomQuery.js.map +0 -1
- package/lib/client/constants.d.ts +0 -1
- package/lib/client/constants.js +0 -2
- package/lib/client/constants.js.map +0 -1
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -40
- package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -19
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
- package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -34
- package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -162
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +0 -151
- package/lib/client/sync/stream/AbstractRemote.js +0 -473
- package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -218
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -663
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
- package/lib/client/sync/stream/WebsocketClientTransport.d.ts +0 -15
- package/lib/client/sync/stream/WebsocketClientTransport.js +0 -61
- package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
- package/lib/client/sync/stream/core-instruction.d.ts +0 -71
- package/lib/client/sync/stream/core-instruction.js +0 -27
- package/lib/client/sync/stream/core-instruction.js.map +0 -1
- package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +0 -6
- package/lib/client/triggers/MemoryTriggerClaimManager.js +0 -21
- package/lib/client/triggers/MemoryTriggerClaimManager.js.map +0 -1
- package/lib/client/triggers/TriggerManagerImpl.d.ts +0 -39
- package/lib/client/triggers/TriggerManagerImpl.js +0 -405
- package/lib/client/triggers/TriggerManagerImpl.js.map +0 -1
- package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -68
- package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -151
- package/lib/client/watched/processors/AbstractQueryProcessor.js.map +0 -1
- package/lib/db/ConnectionClosedError.d.ts +0 -10
- package/lib/db/ConnectionClosedError.js +0 -21
- package/lib/db/ConnectionClosedError.js.map +0 -1
- package/lib/db/schema/TableV2.d.ts +0 -9
- package/lib/db/schema/TableV2.js +0 -9
- package/lib/db/schema/TableV2.js.map +0 -1
- package/lib/utils/AbortOperation.d.ts +0 -9
- package/lib/utils/AbortOperation.js +0 -19
- package/lib/utils/AbortOperation.js.map +0 -1
- package/lib/utils/ControlledExecutor.d.ts +0 -25
- package/lib/utils/ControlledExecutor.js +0 -51
- package/lib/utils/ControlledExecutor.js.map +0 -1
- package/lib/utils/async.d.ts +0 -14
- package/lib/utils/async.js +0 -46
- package/lib/utils/async.js.map +0 -1
- package/lib/utils/parseQuery.d.ts +0 -6
- package/lib/utils/parseQuery.js +0 -17
- package/lib/utils/parseQuery.js.map +0 -1
- package/lib/utils/queue.d.ts +0 -16
- package/lib/utils/queue.js +0 -42
- package/lib/utils/queue.js.map +0 -1
- package/lib/utils/stream_transform.d.ts +0 -39
- package/lib/utils/stream_transform.js +0 -206
- package/lib/utils/stream_transform.js.map +0 -1
- package/src/client/AbstractPowerSyncDatabase.ts +0 -1362
- package/src/client/AbstractPowerSyncOpenFactory.ts +0 -39
- package/src/client/ConnectionManager.ts +0 -402
- package/src/client/CustomQuery.ts +0 -56
- package/src/client/constants.ts +0 -1
- package/src/client/sync/bucket/BucketStorageAdapter.ts +0 -49
- package/src/client/sync/bucket/SqliteBucketStorage.ts +0 -199
- package/src/client/sync/stream/AbstractRemote.ts +0 -602
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +0 -905
- package/src/client/sync/stream/WebsocketClientTransport.ts +0 -80
- package/src/client/sync/stream/core-instruction.ts +0 -98
- package/src/client/triggers/MemoryTriggerClaimManager.ts +0 -25
- package/src/client/triggers/TriggerManagerImpl.ts +0 -496
- package/src/client/watched/processors/AbstractQueryProcessor.ts +0 -226
- package/src/db/ConnectionClosedError.ts +0 -23
- package/src/db/schema/TableV2.ts +0 -9
- package/src/utils/AbortOperation.ts +0 -17
- package/src/utils/ControlledExecutor.ts +0 -72
- package/src/utils/async.ts +0 -50
- package/src/utils/parseQuery.ts +0 -25
- package/src/utils/queue.ts +0 -48
- package/src/utils/stream_transform.ts +0 -252
|
@@ -1,663 +0,0 @@
|
|
|
1
|
-
import Logger from 'js-logger';
|
|
2
|
-
import { SyncStatus } from '../../../db/crud/SyncStatus.js';
|
|
3
|
-
import { AbortOperation } from '../../../utils/AbortOperation.js';
|
|
4
|
-
import { BaseObserver } from '../../../utils/BaseObserver.js';
|
|
5
|
-
import { throttleLeadingTrailing } from '../../../utils/async.js';
|
|
6
|
-
import { PowerSyncControlCommand } from '../bucket/BucketStorageAdapter.js';
|
|
7
|
-
import { FetchStrategy } from './AbstractRemote.js';
|
|
8
|
-
import { coreStatusToJs } from './core-instruction.js';
|
|
9
|
-
import { injectable, map } from '../../../utils/stream_transform.js';
|
|
10
|
-
export var LockType;
|
|
11
|
-
(function (LockType) {
|
|
12
|
-
LockType["CRUD"] = "crud";
|
|
13
|
-
LockType["SYNC"] = "sync";
|
|
14
|
-
})(LockType || (LockType = {}));
|
|
15
|
-
export var SyncStreamConnectionMethod;
|
|
16
|
-
(function (SyncStreamConnectionMethod) {
|
|
17
|
-
SyncStreamConnectionMethod["HTTP"] = "http";
|
|
18
|
-
SyncStreamConnectionMethod["WEB_SOCKET"] = "web-socket";
|
|
19
|
-
})(SyncStreamConnectionMethod || (SyncStreamConnectionMethod = {}));
|
|
20
|
-
export var SyncClientImplementation;
|
|
21
|
-
(function (SyncClientImplementation) {
|
|
22
|
-
/**
|
|
23
|
-
* This implementation offloads the sync line decoding and handling into the PowerSync
|
|
24
|
-
* core extension.
|
|
25
|
-
*
|
|
26
|
-
* This is the only option, as an older JavaScript client implementation has been removed from the SDK.
|
|
27
|
-
*
|
|
28
|
-
* ## Compatibility warning
|
|
29
|
-
*
|
|
30
|
-
* The Rust sync client stores sync data in a format that is slightly different than the one used
|
|
31
|
-
* by the old JavaScript client. When adopting the {@link RUST} client on existing databases, the PowerSync SDK will
|
|
32
|
-
* migrate the format automatically.
|
|
33
|
-
*
|
|
34
|
-
* SDK versions supporting both the JavaScript and the Rust client support both formats with the JavaScript client
|
|
35
|
-
* implementaiton. However, downgrading to an SDK version that only supports the JavaScript client would not be
|
|
36
|
-
* possible anymore. Problematic SDK versions have been released before 2025-06-09.
|
|
37
|
-
*/
|
|
38
|
-
SyncClientImplementation["RUST"] = "rust";
|
|
39
|
-
})(SyncClientImplementation || (SyncClientImplementation = {}));
|
|
40
|
-
/**
|
|
41
|
-
* The default {@link SyncClientImplementation} to use, {@link SyncClientImplementation.RUST}.
|
|
42
|
-
*/
|
|
43
|
-
export const DEFAULT_SYNC_CLIENT_IMPLEMENTATION = SyncClientImplementation.RUST;
|
|
44
|
-
export const DEFAULT_CRUD_UPLOAD_THROTTLE_MS = 1000;
|
|
45
|
-
export const DEFAULT_RETRY_DELAY_MS = 5000;
|
|
46
|
-
export const DEFAULT_STREAMING_SYNC_OPTIONS = {
|
|
47
|
-
retryDelayMs: DEFAULT_RETRY_DELAY_MS,
|
|
48
|
-
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
|
|
49
|
-
};
|
|
50
|
-
export const DEFAULT_STREAM_CONNECTION_OPTIONS = {
|
|
51
|
-
appMetadata: {},
|
|
52
|
-
connectionMethod: SyncStreamConnectionMethod.WEB_SOCKET,
|
|
53
|
-
clientImplementation: DEFAULT_SYNC_CLIENT_IMPLEMENTATION,
|
|
54
|
-
fetchStrategy: FetchStrategy.Buffered,
|
|
55
|
-
params: {},
|
|
56
|
-
serializedSchema: undefined,
|
|
57
|
-
includeDefaultStreams: true
|
|
58
|
-
};
|
|
59
|
-
// The priority we assume when we receive checkpoint lines where no priority is set.
|
|
60
|
-
// This is the default priority used by the sync service, but can be set to an arbitrary
|
|
61
|
-
// value since sync services without priorities also won't send partial sync completion
|
|
62
|
-
// messages.
|
|
63
|
-
const FALLBACK_PRIORITY = 3;
|
|
64
|
-
export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
65
|
-
options;
|
|
66
|
-
abortController;
|
|
67
|
-
// In rare cases, mostly for tests, uploads can be triggered without being properly connected.
|
|
68
|
-
// This allows ensuring that all upload processes can be aborted.
|
|
69
|
-
uploadAbortController;
|
|
70
|
-
crudUpdateListener;
|
|
71
|
-
streamingSyncPromise;
|
|
72
|
-
logger;
|
|
73
|
-
activeStreams;
|
|
74
|
-
connectionMayHaveChanged = false;
|
|
75
|
-
isUploadingCrud = false;
|
|
76
|
-
notifyCompletedUploads;
|
|
77
|
-
handleActiveStreamsChange;
|
|
78
|
-
syncStatus;
|
|
79
|
-
triggerCrudUpload;
|
|
80
|
-
constructor(options) {
|
|
81
|
-
super();
|
|
82
|
-
this.options = options;
|
|
83
|
-
this.activeStreams = options.subscriptions;
|
|
84
|
-
this.logger = options.logger ?? Logger.get('PowerSyncStream');
|
|
85
|
-
this.syncStatus = new SyncStatus({
|
|
86
|
-
connected: false,
|
|
87
|
-
connecting: false,
|
|
88
|
-
lastSyncedAt: undefined,
|
|
89
|
-
dataFlow: {
|
|
90
|
-
uploading: false,
|
|
91
|
-
downloading: false
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
this.abortController = null;
|
|
95
|
-
this.triggerCrudUpload = throttleLeadingTrailing(() => {
|
|
96
|
-
if (!this.syncStatus.connected || this.isUploadingCrud) {
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
this.isUploadingCrud = true;
|
|
100
|
-
this._uploadAllCrud().finally(() => {
|
|
101
|
-
this.notifyCompletedUploads?.();
|
|
102
|
-
this.isUploadingCrud = false;
|
|
103
|
-
});
|
|
104
|
-
}, this.options.crudUploadThrottleMs);
|
|
105
|
-
}
|
|
106
|
-
async waitForReady() { }
|
|
107
|
-
waitForStatus(status) {
|
|
108
|
-
return this.waitUntilStatusMatches((currentStatus) => {
|
|
109
|
-
/**
|
|
110
|
-
* Match only the partial status options provided in the
|
|
111
|
-
* matching status
|
|
112
|
-
*/
|
|
113
|
-
const matchPartialObject = (compA, compB) => {
|
|
114
|
-
return Object.entries(compA).every(([key, value]) => {
|
|
115
|
-
const comparisonBValue = compB[key];
|
|
116
|
-
if (typeof value == 'object' && typeof comparisonBValue == 'object') {
|
|
117
|
-
return matchPartialObject(value, comparisonBValue);
|
|
118
|
-
}
|
|
119
|
-
return value == comparisonBValue;
|
|
120
|
-
});
|
|
121
|
-
};
|
|
122
|
-
return matchPartialObject(status, currentStatus);
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
waitUntilStatusMatches(predicate) {
|
|
126
|
-
return new Promise((resolve) => {
|
|
127
|
-
if (predicate(this.syncStatus)) {
|
|
128
|
-
resolve();
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
const l = this.registerListener({
|
|
132
|
-
statusChanged: (updatedStatus) => {
|
|
133
|
-
if (predicate(updatedStatus)) {
|
|
134
|
-
resolve();
|
|
135
|
-
l?.();
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
get lastSyncedAt() {
|
|
142
|
-
const lastSynced = this.syncStatus.lastSyncedAt;
|
|
143
|
-
return lastSynced && new Date(lastSynced);
|
|
144
|
-
}
|
|
145
|
-
get isConnected() {
|
|
146
|
-
return this.syncStatus.connected;
|
|
147
|
-
}
|
|
148
|
-
async dispose() {
|
|
149
|
-
super.dispose();
|
|
150
|
-
this.crudUpdateListener?.();
|
|
151
|
-
this.crudUpdateListener = undefined;
|
|
152
|
-
this.uploadAbortController?.abort();
|
|
153
|
-
}
|
|
154
|
-
async getWriteCheckpoint() {
|
|
155
|
-
const clientId = await this.options.adapter.getClientId();
|
|
156
|
-
let path = `/write-checkpoint2.json?client_id=${clientId}`;
|
|
157
|
-
const response = await this.options.remote.get(path);
|
|
158
|
-
const checkpoint = response['data']['write_checkpoint'];
|
|
159
|
-
this.logger.debug(`Created write checkpoint: ${checkpoint}`);
|
|
160
|
-
return checkpoint;
|
|
161
|
-
}
|
|
162
|
-
async _uploadAllCrud() {
|
|
163
|
-
return this.obtainLock({
|
|
164
|
-
type: LockType.CRUD,
|
|
165
|
-
callback: async () => {
|
|
166
|
-
/**
|
|
167
|
-
* Keep track of the first item in the CRUD queue for the last `uploadCrud` iteration.
|
|
168
|
-
*/
|
|
169
|
-
let checkedCrudItem;
|
|
170
|
-
const controller = new AbortController();
|
|
171
|
-
this.uploadAbortController = controller;
|
|
172
|
-
this.abortController?.signal.addEventListener('abort', () => {
|
|
173
|
-
controller.abort();
|
|
174
|
-
}, { once: true });
|
|
175
|
-
while (!controller.signal.aborted) {
|
|
176
|
-
try {
|
|
177
|
-
/**
|
|
178
|
-
* This is the first item in the FIFO CRUD queue.
|
|
179
|
-
*/
|
|
180
|
-
const nextCrudItem = await this.options.adapter.nextCrudItem();
|
|
181
|
-
if (nextCrudItem) {
|
|
182
|
-
this.updateSyncStatus({
|
|
183
|
-
dataFlow: {
|
|
184
|
-
uploading: true
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
if (nextCrudItem.clientId == checkedCrudItem?.clientId) {
|
|
188
|
-
// This will force a higher log level than exceptions which are caught here.
|
|
189
|
-
this.logger.warn(`Potentially previously uploaded CRUD entries are still present in the upload queue.
|
|
190
|
-
Make sure to handle uploads and complete CRUD transactions or batches by calling and awaiting their [.complete()] method.
|
|
191
|
-
The next upload iteration will be delayed.`);
|
|
192
|
-
throw new Error('Delaying due to previously encountered CRUD item.');
|
|
193
|
-
}
|
|
194
|
-
checkedCrudItem = nextCrudItem;
|
|
195
|
-
await this.options.uploadCrud();
|
|
196
|
-
this.updateSyncStatus({
|
|
197
|
-
dataFlow: {
|
|
198
|
-
uploadError: undefined
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
// Uploading is completed
|
|
204
|
-
const neededUpdate = await this.options.adapter.updateLocalTarget(() => this.getWriteCheckpoint());
|
|
205
|
-
if (neededUpdate == false && checkedCrudItem != null) {
|
|
206
|
-
// Only log this if there was something to upload
|
|
207
|
-
this.logger.debug('Upload complete, no write checkpoint needed.');
|
|
208
|
-
}
|
|
209
|
-
break;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
catch (ex) {
|
|
213
|
-
checkedCrudItem = undefined;
|
|
214
|
-
this.updateSyncStatus({
|
|
215
|
-
dataFlow: {
|
|
216
|
-
uploading: false,
|
|
217
|
-
uploadError: ex
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
await this.delayRetry(controller.signal);
|
|
221
|
-
if (!this.isConnected) {
|
|
222
|
-
// Exit the upload loop if the sync stream is no longer connected
|
|
223
|
-
break;
|
|
224
|
-
}
|
|
225
|
-
this.logger.debug(`Caught exception when uploading. Upload will retry after a delay. Exception: ${ex.message}`);
|
|
226
|
-
}
|
|
227
|
-
finally {
|
|
228
|
-
this.updateSyncStatus({
|
|
229
|
-
dataFlow: {
|
|
230
|
-
uploading: false
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
this.uploadAbortController = undefined;
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
async connect(options) {
|
|
240
|
-
if (this.abortController) {
|
|
241
|
-
await this.disconnect();
|
|
242
|
-
}
|
|
243
|
-
const controller = new AbortController();
|
|
244
|
-
this.abortController = controller;
|
|
245
|
-
this.streamingSyncPromise = this.streamingSync(this.abortController.signal, options);
|
|
246
|
-
// Return a promise that resolves when the connection status is updated to indicate that we're connected.
|
|
247
|
-
return new Promise((resolve) => {
|
|
248
|
-
const disposer = this.registerListener({
|
|
249
|
-
statusChanged: (status) => {
|
|
250
|
-
if (status.dataFlowStatus.downloadError != null) {
|
|
251
|
-
this.logger.warn('Initial connect attempt did not successfully connect to server');
|
|
252
|
-
}
|
|
253
|
-
else if (status.connecting) {
|
|
254
|
-
// Still connecting.
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
disposer();
|
|
258
|
-
resolve();
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
async disconnect() {
|
|
264
|
-
if (!this.abortController) {
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
// This might be called multiple times
|
|
268
|
-
if (!this.abortController.signal.aborted) {
|
|
269
|
-
this.abortController.abort(new AbortOperation('Disconnect has been requested'));
|
|
270
|
-
}
|
|
271
|
-
// Await any pending operations before completing the disconnect operation
|
|
272
|
-
try {
|
|
273
|
-
await this.streamingSyncPromise;
|
|
274
|
-
}
|
|
275
|
-
catch (ex) {
|
|
276
|
-
// The operation might have failed, all we care about is if it has completed
|
|
277
|
-
this.logger.warn(ex);
|
|
278
|
-
}
|
|
279
|
-
this.streamingSyncPromise = undefined;
|
|
280
|
-
this.abortController = null;
|
|
281
|
-
this.updateSyncStatus({ connected: false, connecting: false });
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* @deprecated use [connect instead]
|
|
285
|
-
*/
|
|
286
|
-
async streamingSync(signal, options) {
|
|
287
|
-
if (!signal) {
|
|
288
|
-
this.abortController = new AbortController();
|
|
289
|
-
signal = this.abortController.signal;
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Listen for CRUD updates and trigger upstream uploads
|
|
293
|
-
*/
|
|
294
|
-
this.crudUpdateListener = this.options.adapter.registerListener({
|
|
295
|
-
crudUpdate: () => this.triggerCrudUpload()
|
|
296
|
-
});
|
|
297
|
-
/**
|
|
298
|
-
* Create a new abort controller which aborts items downstream.
|
|
299
|
-
* This is needed to close any previous connections on exception.
|
|
300
|
-
*/
|
|
301
|
-
let nestedAbortController = new AbortController();
|
|
302
|
-
signal.addEventListener('abort', () => {
|
|
303
|
-
/**
|
|
304
|
-
* A request for disconnect was received upstream. Relay the request
|
|
305
|
-
* to the nested abort controller.
|
|
306
|
-
*/
|
|
307
|
-
nestedAbortController.abort(signal?.reason ?? new AbortOperation('Received command to disconnect from upstream'));
|
|
308
|
-
this.crudUpdateListener?.();
|
|
309
|
-
this.crudUpdateListener = undefined;
|
|
310
|
-
this.updateSyncStatus({
|
|
311
|
-
connected: false,
|
|
312
|
-
connecting: false,
|
|
313
|
-
dataFlow: {
|
|
314
|
-
downloading: false,
|
|
315
|
-
downloadProgress: null
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
});
|
|
319
|
-
/**
|
|
320
|
-
* This loops runs until [retry] is false or the abort signal is set to aborted.
|
|
321
|
-
* Aborting the nestedAbortController will:
|
|
322
|
-
* - Abort any pending fetch requests
|
|
323
|
-
* - Close any sync stream ReadableStreams (which will also close any established network requests)
|
|
324
|
-
*/
|
|
325
|
-
while (true) {
|
|
326
|
-
this.updateSyncStatus({ connecting: true });
|
|
327
|
-
let shouldDelayRetry = true;
|
|
328
|
-
let result = null;
|
|
329
|
-
try {
|
|
330
|
-
if (signal?.aborted) {
|
|
331
|
-
break;
|
|
332
|
-
}
|
|
333
|
-
result = await this.streamingSyncIteration(nestedAbortController.signal, options);
|
|
334
|
-
// Continue immediately, streamingSyncIteration will wait before completing if necessary.
|
|
335
|
-
}
|
|
336
|
-
catch (ex) {
|
|
337
|
-
/**
|
|
338
|
-
* Either:
|
|
339
|
-
* - A network request failed with a failed connection or not OKAY response code.
|
|
340
|
-
* - There was a sync processing error.
|
|
341
|
-
* - The connection was aborted.
|
|
342
|
-
* This loop will retry after a delay if the connection was not aborted.
|
|
343
|
-
* The nested abort controller will cleanup any open network requests and streams.
|
|
344
|
-
* The WebRemote should only abort pending fetch requests or close active Readable streams.
|
|
345
|
-
*/
|
|
346
|
-
if (ex instanceof AbortOperation) {
|
|
347
|
-
this.logger.warn(ex);
|
|
348
|
-
shouldDelayRetry = false;
|
|
349
|
-
// A disconnect was requested, we should not delay since there is no explicit retry
|
|
350
|
-
}
|
|
351
|
-
else if (this.connectionMayHaveChanged && ex.message?.indexOf('No iteration is active') >= 0) {
|
|
352
|
-
this.connectionMayHaveChanged = false;
|
|
353
|
-
this.logger.info('Sync error after changed connection, retrying immediately');
|
|
354
|
-
shouldDelayRetry = false;
|
|
355
|
-
}
|
|
356
|
-
else {
|
|
357
|
-
this.logger.error(ex);
|
|
358
|
-
}
|
|
359
|
-
this.updateSyncStatus({
|
|
360
|
-
dataFlow: {
|
|
361
|
-
downloadError: ex
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
finally {
|
|
366
|
-
this.notifyCompletedUploads = undefined;
|
|
367
|
-
if (!signal.aborted) {
|
|
368
|
-
nestedAbortController.abort(new AbortOperation('Closing sync stream network requests before retry.'));
|
|
369
|
-
nestedAbortController = new AbortController();
|
|
370
|
-
}
|
|
371
|
-
if (result?.immediateRestart != true) {
|
|
372
|
-
this.updateSyncStatus({
|
|
373
|
-
connected: false,
|
|
374
|
-
connecting: true // May be unnecessary
|
|
375
|
-
});
|
|
376
|
-
// On error, wait a little before retrying
|
|
377
|
-
if (shouldDelayRetry) {
|
|
378
|
-
await this.delayRetry(nestedAbortController.signal);
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
// Mark as disconnected if here
|
|
384
|
-
this.updateSyncStatus({ connected: false, connecting: false });
|
|
385
|
-
}
|
|
386
|
-
markConnectionMayHaveChanged() {
|
|
387
|
-
// By setting this field, we'll immediately retry if the next sync event causes an error triggered by us not having
|
|
388
|
-
// an active sync iteration on the connection in use.
|
|
389
|
-
this.connectionMayHaveChanged = true;
|
|
390
|
-
// This triggers a `powersync_control` invocation if a sync iteration is currently active. This is a cheap call to
|
|
391
|
-
// make when no subscriptions have actually changed, we're mainly interested in this immediately throwing if no
|
|
392
|
-
// iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
|
|
393
|
-
this.handleActiveStreamsChange?.();
|
|
394
|
-
}
|
|
395
|
-
/**
|
|
396
|
-
* Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
|
|
397
|
-
* Because subkeys are always strings, this leads to quotes being added around them in `ps_oplog`.
|
|
398
|
-
* While this is not a problem as long as it's done consistently, it causes issues when a database
|
|
399
|
-
* created by the JS SDK is used with other SDKs, or (more likely) when the new Rust sync client
|
|
400
|
-
* is enabled.
|
|
401
|
-
*
|
|
402
|
-
* So, we add a migration from the old key format (with quotes) to the new one (no quotes). The
|
|
403
|
-
* migration is only triggered when necessary (for now). The function returns whether the new format
|
|
404
|
-
* should be used, so that the JS SDK is able to write to updated databases.
|
|
405
|
-
*
|
|
406
|
-
* @param requireFixedKeyFormat Whether we require the new format or also support the old one.
|
|
407
|
-
* The Rust client requires the new subkey format.
|
|
408
|
-
* @returns Whether the database is now using the new, fixed subkey format.
|
|
409
|
-
*/
|
|
410
|
-
async requireKeyFormat(requireFixedKeyFormat) {
|
|
411
|
-
const hasMigrated = await this.options.adapter.hasMigratedSubkeys();
|
|
412
|
-
if (requireFixedKeyFormat && !hasMigrated) {
|
|
413
|
-
await this.options.adapter.migrateToFixedSubkeys();
|
|
414
|
-
return true;
|
|
415
|
-
}
|
|
416
|
-
else {
|
|
417
|
-
return hasMigrated;
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
streamingSyncIteration(signal, options) {
|
|
421
|
-
return this.obtainLock({
|
|
422
|
-
type: LockType.SYNC,
|
|
423
|
-
signal,
|
|
424
|
-
callback: async () => {
|
|
425
|
-
const resolvedOptions = {
|
|
426
|
-
...DEFAULT_STREAM_CONNECTION_OPTIONS,
|
|
427
|
-
...(options ?? {})
|
|
428
|
-
};
|
|
429
|
-
// Validate app metadata
|
|
430
|
-
const invalidMetadata = Object.entries(resolvedOptions.appMetadata).filter(([_, value]) => typeof value != 'string');
|
|
431
|
-
if (invalidMetadata.length > 0) {
|
|
432
|
-
throw new Error(`Invalid appMetadata provided. Only string values are allowed. Invalid values: ${invalidMetadata.map(([key, value]) => `${key}: ${value}`).join(', ')}`);
|
|
433
|
-
}
|
|
434
|
-
const clientImplementation = resolvedOptions.clientImplementation;
|
|
435
|
-
this.updateSyncStatus({ clientImplementation });
|
|
436
|
-
await this.requireKeyFormat(true);
|
|
437
|
-
return await this.rustSyncIteration(signal, resolvedOptions);
|
|
438
|
-
}
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
async receiveSyncLines(data) {
|
|
442
|
-
const { options, connection } = data;
|
|
443
|
-
const remote = this.options.remote;
|
|
444
|
-
if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
|
|
445
|
-
return await remote.fetchStream(options);
|
|
446
|
-
}
|
|
447
|
-
else {
|
|
448
|
-
return await this.options.remote.socketStreamRaw({
|
|
449
|
-
...options,
|
|
450
|
-
...{ fetchStrategy: connection.fetchStrategy }
|
|
451
|
-
});
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
async rustSyncIteration(signal, resolvedOptions) {
|
|
455
|
-
const syncImplementation = this;
|
|
456
|
-
const adapter = this.options.adapter;
|
|
457
|
-
const remote = this.options.remote;
|
|
458
|
-
const controller = new AbortController();
|
|
459
|
-
const abort = () => {
|
|
460
|
-
return controller.abort(signal.reason);
|
|
461
|
-
};
|
|
462
|
-
signal.addEventListener('abort', abort);
|
|
463
|
-
let receivingLines = null;
|
|
464
|
-
let hadSyncLine = false;
|
|
465
|
-
let hideDisconnectOnRestart = false;
|
|
466
|
-
if (signal.aborted) {
|
|
467
|
-
throw new AbortOperation('Connection request has been aborted');
|
|
468
|
-
}
|
|
469
|
-
// Pending sync lines received from the service, as well as local events that trigger a powersync_control
|
|
470
|
-
// invocation (local events include refreshed tokens and completed uploads).
|
|
471
|
-
// This is a single data stream so that we can handle all control calls from a single place.
|
|
472
|
-
let controlInvocations = null;
|
|
473
|
-
async function connect(instr) {
|
|
474
|
-
const syncOptions = {
|
|
475
|
-
path: '/sync/stream',
|
|
476
|
-
abortSignal: controller.signal,
|
|
477
|
-
data: instr.request
|
|
478
|
-
};
|
|
479
|
-
controlInvocations = injectable(map(await syncImplementation.receiveSyncLines({
|
|
480
|
-
options: syncOptions,
|
|
481
|
-
connection: resolvedOptions
|
|
482
|
-
}), (line) => {
|
|
483
|
-
if (typeof line == 'string') {
|
|
484
|
-
return {
|
|
485
|
-
command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
|
|
486
|
-
payload: line
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
|
-
else {
|
|
490
|
-
return {
|
|
491
|
-
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
492
|
-
payload: line
|
|
493
|
-
};
|
|
494
|
-
}
|
|
495
|
-
}));
|
|
496
|
-
// The rust client will set connected: true after the first sync line because that's when it gets invoked, but
|
|
497
|
-
// we're already connected here and can report that.
|
|
498
|
-
syncImplementation.updateSyncStatus({ connected: true });
|
|
499
|
-
try {
|
|
500
|
-
while (true) {
|
|
501
|
-
let event = await controlInvocations.next();
|
|
502
|
-
if (event.done) {
|
|
503
|
-
break;
|
|
504
|
-
}
|
|
505
|
-
const line = event.value;
|
|
506
|
-
await control(line.command, line.payload);
|
|
507
|
-
if (!hadSyncLine) {
|
|
508
|
-
syncImplementation.triggerCrudUpload();
|
|
509
|
-
hadSyncLine = true;
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
finally {
|
|
514
|
-
abort();
|
|
515
|
-
signal.removeEventListener('abort', abort);
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
async function stop() {
|
|
519
|
-
await control(PowerSyncControlCommand.STOP);
|
|
520
|
-
}
|
|
521
|
-
async function control(op, payload) {
|
|
522
|
-
const rawResponse = await adapter.control(op, payload ?? null);
|
|
523
|
-
const logger = syncImplementation.logger;
|
|
524
|
-
logger.trace('powersync_control', op, payload == null || typeof payload == 'string' ? payload : '<bytes>', rawResponse);
|
|
525
|
-
if (op != PowerSyncControlCommand.STOP) {
|
|
526
|
-
// Evidently we have a working connection here, otherwise powersync_control would have failed.
|
|
527
|
-
syncImplementation.connectionMayHaveChanged = false;
|
|
528
|
-
}
|
|
529
|
-
await handleInstructions(JSON.parse(rawResponse));
|
|
530
|
-
}
|
|
531
|
-
async function handleInstruction(instruction) {
|
|
532
|
-
if ('LogLine' in instruction) {
|
|
533
|
-
switch (instruction.LogLine.severity) {
|
|
534
|
-
case 'DEBUG':
|
|
535
|
-
syncImplementation.logger.debug(instruction.LogLine.line);
|
|
536
|
-
break;
|
|
537
|
-
case 'INFO':
|
|
538
|
-
syncImplementation.logger.info(instruction.LogLine.line);
|
|
539
|
-
break;
|
|
540
|
-
case 'WARNING':
|
|
541
|
-
syncImplementation.logger.warn(instruction.LogLine.line);
|
|
542
|
-
break;
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
else if ('UpdateSyncStatus' in instruction) {
|
|
546
|
-
syncImplementation.updateSyncStatus(coreStatusToJs(instruction.UpdateSyncStatus.status));
|
|
547
|
-
}
|
|
548
|
-
else if ('EstablishSyncStream' in instruction) {
|
|
549
|
-
if (receivingLines != null) {
|
|
550
|
-
// Already connected, this shouldn't happen during a single iteration.
|
|
551
|
-
throw 'Unexpected request to establish sync stream, already connected';
|
|
552
|
-
}
|
|
553
|
-
receivingLines = connect(instruction.EstablishSyncStream);
|
|
554
|
-
}
|
|
555
|
-
else if ('FetchCredentials' in instruction) {
|
|
556
|
-
if (instruction.FetchCredentials.did_expire) {
|
|
557
|
-
remote.invalidateCredentials();
|
|
558
|
-
}
|
|
559
|
-
else {
|
|
560
|
-
remote.invalidateCredentials();
|
|
561
|
-
// Restart iteration after the credentials have been refreshed.
|
|
562
|
-
remote.fetchCredentials().then((_) => {
|
|
563
|
-
controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
|
|
564
|
-
}, (err) => {
|
|
565
|
-
syncImplementation.logger.warn('Could not prefetch credentials', err);
|
|
566
|
-
});
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
else if ('CloseSyncStream' in instruction) {
|
|
570
|
-
controller.abort();
|
|
571
|
-
hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
|
|
572
|
-
}
|
|
573
|
-
else if ('FlushFileSystem' in instruction) {
|
|
574
|
-
// Not necessary on JS platforms.
|
|
575
|
-
}
|
|
576
|
-
else if ('DidCompleteSync' in instruction) {
|
|
577
|
-
syncImplementation.updateSyncStatus({
|
|
578
|
-
dataFlow: {
|
|
579
|
-
downloadError: undefined
|
|
580
|
-
}
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
async function handleInstructions(instructions) {
|
|
585
|
-
for (const instr of instructions) {
|
|
586
|
-
await handleInstruction(instr);
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
try {
|
|
590
|
-
const options = {
|
|
591
|
-
parameters: resolvedOptions.params,
|
|
592
|
-
app_metadata: resolvedOptions.appMetadata,
|
|
593
|
-
active_streams: this.activeStreams,
|
|
594
|
-
include_defaults: resolvedOptions.includeDefaultStreams
|
|
595
|
-
};
|
|
596
|
-
if (resolvedOptions.serializedSchema) {
|
|
597
|
-
options.schema = resolvedOptions.serializedSchema;
|
|
598
|
-
}
|
|
599
|
-
await control(PowerSyncControlCommand.START, JSON.stringify(options));
|
|
600
|
-
this.notifyCompletedUploads = () => {
|
|
601
|
-
controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
602
|
-
};
|
|
603
|
-
this.handleActiveStreamsChange = () => {
|
|
604
|
-
controlInvocations?.inject({
|
|
605
|
-
command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
|
|
606
|
-
payload: JSON.stringify(this.activeStreams)
|
|
607
|
-
});
|
|
608
|
-
};
|
|
609
|
-
await receivingLines;
|
|
610
|
-
}
|
|
611
|
-
finally {
|
|
612
|
-
this.notifyCompletedUploads = this.handleActiveStreamsChange = undefined;
|
|
613
|
-
await stop();
|
|
614
|
-
}
|
|
615
|
-
return { immediateRestart: hideDisconnectOnRestart };
|
|
616
|
-
}
|
|
617
|
-
updateSyncStatus(options) {
|
|
618
|
-
const updatedStatus = new SyncStatus({
|
|
619
|
-
connected: options.connected ?? this.syncStatus.connected,
|
|
620
|
-
connecting: !options.connected && (options.connecting ?? this.syncStatus.connecting),
|
|
621
|
-
lastSyncedAt: options.lastSyncedAt ?? this.syncStatus.lastSyncedAt,
|
|
622
|
-
dataFlow: {
|
|
623
|
-
...this.syncStatus.dataFlowStatus,
|
|
624
|
-
...options.dataFlow
|
|
625
|
-
},
|
|
626
|
-
priorityStatusEntries: options.priorityStatusEntries ?? this.syncStatus.priorityStatusEntries,
|
|
627
|
-
clientImplementation: options.clientImplementation ?? this.syncStatus.clientImplementation
|
|
628
|
-
});
|
|
629
|
-
if (!this.syncStatus.isEqual(updatedStatus)) {
|
|
630
|
-
this.syncStatus = updatedStatus;
|
|
631
|
-
// Only trigger this is there was a change
|
|
632
|
-
this.iterateListeners((cb) => cb.statusChanged?.(updatedStatus));
|
|
633
|
-
}
|
|
634
|
-
// trigger this for all updates
|
|
635
|
-
this.iterateListeners((cb) => cb.statusUpdated?.(options));
|
|
636
|
-
}
|
|
637
|
-
async delayRetry(signal) {
|
|
638
|
-
return new Promise((resolve) => {
|
|
639
|
-
if (signal?.aborted) {
|
|
640
|
-
// If the signal is already aborted, resolve immediately
|
|
641
|
-
resolve();
|
|
642
|
-
return;
|
|
643
|
-
}
|
|
644
|
-
const { retryDelayMs } = this.options;
|
|
645
|
-
let timeoutId;
|
|
646
|
-
const endDelay = () => {
|
|
647
|
-
resolve();
|
|
648
|
-
if (timeoutId) {
|
|
649
|
-
clearTimeout(timeoutId);
|
|
650
|
-
timeoutId = undefined;
|
|
651
|
-
}
|
|
652
|
-
signal?.removeEventListener('abort', endDelay);
|
|
653
|
-
};
|
|
654
|
-
signal?.addEventListener('abort', endDelay, { once: true });
|
|
655
|
-
timeoutId = setTimeout(endDelay, retryDelayMs);
|
|
656
|
-
});
|
|
657
|
-
}
|
|
658
|
-
updateSubscriptions(subscriptions) {
|
|
659
|
-
this.activeStreams = subscriptions;
|
|
660
|
-
this.handleActiveStreamsChange?.();
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
//# sourceMappingURL=AbstractStreamingSyncImplementation.js.map
|