@aws-amplify/datastore 5.0.32 → 5.0.33-s-auth.30d0cd2.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.
- package/dist/cjs/authModeStrategies/multiAuthStrategy.js +1 -1
- package/dist/cjs/authModeStrategies/multiAuthStrategy.js.map +1 -1
- package/dist/cjs/datastore/datastore.js +46 -41
- package/dist/cjs/datastore/datastore.js.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/predicates/index.js +1 -0
- package/dist/cjs/predicates/index.js.map +1 -1
- package/dist/cjs/predicates/next.js +13 -12
- package/dist/cjs/predicates/next.js.map +1 -1
- package/dist/cjs/predicates/sort.js +2 -2
- package/dist/cjs/predicates/sort.js.map +1 -1
- package/dist/cjs/storage/adapter/AsyncStorageAdapter.js +21 -18
- package/dist/cjs/storage/adapter/AsyncStorageAdapter.js.map +1 -1
- package/dist/cjs/storage/adapter/AsyncStorageDatabase.js +10 -9
- package/dist/cjs/storage/adapter/AsyncStorageDatabase.js.map +1 -1
- package/dist/cjs/storage/adapter/InMemoryStore.js +4 -1
- package/dist/cjs/storage/adapter/InMemoryStore.js.map +1 -1
- package/dist/cjs/storage/adapter/IndexedDBAdapter.js +21 -23
- package/dist/cjs/storage/adapter/IndexedDBAdapter.js.map +1 -1
- package/dist/cjs/storage/adapter/StorageAdapterBase.js +7 -7
- package/dist/cjs/storage/adapter/StorageAdapterBase.js.map +1 -1
- package/dist/cjs/storage/adapter/getDefaultAdapter/index.js +4 -1
- package/dist/cjs/storage/adapter/getDefaultAdapter/index.js.map +1 -1
- package/dist/cjs/storage/adapter/getDefaultAdapter/index.native.js +1 -0
- package/dist/cjs/storage/adapter/getDefaultAdapter/index.native.js.map +1 -1
- package/dist/cjs/storage/relationship.js +1 -0
- package/dist/cjs/storage/relationship.js.map +1 -1
- package/dist/cjs/storage/storage.js +14 -14
- package/dist/cjs/storage/storage.js.map +1 -1
- package/dist/cjs/sync/datastoreConnectivity.js +0 -3
- package/dist/cjs/sync/datastoreConnectivity.js.map +1 -1
- package/dist/cjs/sync/index.js +47 -43
- package/dist/cjs/sync/index.js.map +1 -1
- package/dist/cjs/sync/merger.js +1 -1
- package/dist/cjs/sync/merger.js.map +1 -1
- package/dist/cjs/sync/outbox.js +19 -17
- package/dist/cjs/sync/outbox.js.map +1 -1
- package/dist/cjs/sync/processors/errorMaps.js.map +1 -1
- package/dist/cjs/sync/processors/mutation.js +31 -28
- package/dist/cjs/sync/processors/mutation.js.map +1 -1
- package/dist/cjs/sync/processors/subscription.js +21 -12
- package/dist/cjs/sync/processors/subscription.js.map +1 -1
- package/dist/cjs/sync/processors/sync.js +25 -21
- package/dist/cjs/sync/processors/sync.js.map +1 -1
- package/dist/cjs/sync/utils.js +20 -21
- package/dist/cjs/sync/utils.js.map +1 -1
- package/dist/cjs/types.js +1 -0
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/util.js +46 -25
- package/dist/cjs/util.js.map +1 -1
- package/dist/esm/authModeStrategies/multiAuthStrategy.d.ts +1 -1
- package/dist/esm/authModeStrategies/multiAuthStrategy.mjs +1 -1
- package/dist/esm/authModeStrategies/multiAuthStrategy.mjs.map +1 -1
- package/dist/esm/datastore/datastore.d.ts +4 -6
- package/dist/esm/datastore/datastore.mjs +42 -34
- package/dist/esm/datastore/datastore.mjs.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.mjs +2 -2
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/predicates/index.mjs +1 -0
- package/dist/esm/predicates/index.mjs.map +1 -1
- package/dist/esm/predicates/next.d.ts +7 -7
- package/dist/esm/predicates/next.mjs +13 -12
- package/dist/esm/predicates/next.mjs.map +1 -1
- package/dist/esm/predicates/sort.d.ts +1 -1
- package/dist/esm/predicates/sort.mjs +2 -2
- package/dist/esm/predicates/sort.mjs.map +1 -1
- package/dist/esm/storage/adapter/AsyncStorageAdapter.d.ts +1 -1
- package/dist/esm/storage/adapter/AsyncStorageAdapter.mjs +21 -18
- package/dist/esm/storage/adapter/AsyncStorageAdapter.mjs.map +1 -1
- package/dist/esm/storage/adapter/AsyncStorageDatabase.mjs +10 -9
- package/dist/esm/storage/adapter/AsyncStorageDatabase.mjs.map +1 -1
- package/dist/esm/storage/adapter/InMemoryStore.mjs +4 -1
- package/dist/esm/storage/adapter/InMemoryStore.mjs.map +1 -1
- package/dist/esm/storage/adapter/IndexedDBAdapter.mjs +21 -23
- package/dist/esm/storage/adapter/IndexedDBAdapter.mjs.map +1 -1
- package/dist/esm/storage/adapter/StorageAdapterBase.d.ts +2 -2
- package/dist/esm/storage/adapter/StorageAdapterBase.mjs +7 -7
- package/dist/esm/storage/adapter/StorageAdapterBase.mjs.map +1 -1
- package/dist/esm/storage/adapter/getDefaultAdapter/index.mjs +3 -1
- package/dist/esm/storage/adapter/getDefaultAdapter/index.mjs.map +1 -1
- package/dist/esm/storage/adapter/getDefaultAdapter/index.native.mjs +1 -0
- package/dist/esm/storage/adapter/getDefaultAdapter/index.native.mjs.map +1 -1
- package/dist/esm/storage/adapter/index.d.ts +1 -1
- package/dist/esm/storage/relationship.mjs +1 -0
- package/dist/esm/storage/relationship.mjs.map +1 -1
- package/dist/esm/storage/storage.d.ts +11 -11
- package/dist/esm/storage/storage.mjs +10 -10
- package/dist/esm/storage/storage.mjs.map +1 -1
- package/dist/esm/sync/datastoreConnectivity.d.ts +2 -2
- package/dist/esm/sync/datastoreConnectivity.mjs +0 -3
- package/dist/esm/sync/datastoreConnectivity.mjs.map +1 -1
- package/dist/esm/sync/index.d.ts +3 -3
- package/dist/esm/sync/index.mjs +47 -43
- package/dist/esm/sync/index.mjs.map +1 -1
- package/dist/esm/sync/merger.d.ts +1 -1
- package/dist/esm/sync/merger.mjs +1 -1
- package/dist/esm/sync/merger.mjs.map +1 -1
- package/dist/esm/sync/outbox.d.ts +4 -4
- package/dist/esm/sync/outbox.mjs +19 -17
- package/dist/esm/sync/outbox.mjs.map +1 -1
- package/dist/esm/sync/processors/errorMaps.mjs.map +1 -1
- package/dist/esm/sync/processors/mutation.d.ts +5 -5
- package/dist/esm/sync/processors/mutation.mjs +31 -28
- package/dist/esm/sync/processors/mutation.mjs.map +1 -1
- package/dist/esm/sync/processors/subscription.d.ts +1 -1
- package/dist/esm/sync/processors/subscription.mjs +21 -12
- package/dist/esm/sync/processors/subscription.mjs.map +1 -1
- package/dist/esm/sync/processors/sync.d.ts +3 -3
- package/dist/esm/sync/processors/sync.mjs +13 -9
- package/dist/esm/sync/processors/sync.mjs.map +1 -1
- package/dist/esm/sync/utils.d.ts +11 -13
- package/dist/esm/sync/utils.mjs +20 -21
- package/dist/esm/sync/utils.mjs.map +1 -1
- package/dist/esm/types.d.ts +121 -131
- package/dist/esm/types.mjs +3 -2
- package/dist/esm/types.mjs.map +1 -1
- package/dist/esm/util.d.ts +5 -5
- package/dist/esm/util.mjs +46 -25
- package/dist/esm/util.mjs.map +1 -1
- package/package.json +74 -73
- package/src/authModeStrategies/multiAuthStrategy.ts +8 -4
- package/src/datastore/datastore.ts +145 -123
- package/src/index.ts +10 -8
- package/src/predicates/index.ts +10 -2
- package/src/predicates/next.ts +39 -28
- package/src/predicates/sort.ts +23 -26
- package/src/storage/adapter/AsyncStorageAdapter.ts +43 -25
- package/src/storage/adapter/AsyncStorageDatabase.ts +18 -10
- package/src/storage/adapter/InMemoryStore.ts +5 -1
- package/src/storage/adapter/IndexedDBAdapter.ts +50 -37
- package/src/storage/adapter/StorageAdapterBase.ts +24 -18
- package/src/storage/adapter/getDefaultAdapter/index.native.ts +1 -0
- package/src/storage/adapter/getDefaultAdapter/index.ts +4 -1
- package/src/storage/adapter/index.ts +2 -2
- package/src/storage/relationship.ts +5 -1
- package/src/storage/storage.ts +38 -30
- package/src/sync/datastoreConnectivity.ts +4 -6
- package/src/sync/index.ts +228 -230
- package/src/sync/merger.ts +4 -3
- package/src/sync/outbox.ts +26 -24
- package/src/sync/processors/errorMaps.ts +5 -0
- package/src/sync/processors/mutation.ts +80 -72
- package/src/sync/processors/subscription.ts +69 -53
- package/src/sync/processors/sync.ts +49 -37
- package/src/sync/utils.ts +60 -41
- package/src/types.ts +165 -165
- package/src/util.ts +89 -53
package/src/sync/index.ts
CHANGED
|
@@ -1,37 +1,40 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { BackgroundProcessManager } from '@aws-amplify/core/internals/utils';
|
|
4
|
-
import {
|
|
4
|
+
import { ConsoleLogger, Hub } from '@aws-amplify/core';
|
|
5
|
+
import { Observable, SubscriptionLike, filter, of } from 'rxjs';
|
|
6
|
+
import {
|
|
7
|
+
ConnectionState,
|
|
8
|
+
CONNECTION_STATE_CHANGE as PUBSUB_CONNECTION_STATE_CHANGE,
|
|
9
|
+
CONTROL_MSG as PUBSUB_CONTROL_MSG,
|
|
10
|
+
} from '@aws-amplify/api-graphql';
|
|
5
11
|
|
|
6
|
-
import { filter, Observable, of, SubscriptionLike } from 'rxjs';
|
|
7
12
|
import { ModelInstanceCreator } from '../datastore/datastore';
|
|
8
13
|
import { ModelPredicateCreator } from '../predicates';
|
|
9
14
|
import { ExclusiveStorage as Storage } from '../storage/storage';
|
|
10
15
|
import {
|
|
16
|
+
AmplifyContext,
|
|
17
|
+
AuthModeStrategy,
|
|
11
18
|
ConflictHandler,
|
|
12
19
|
ControlMessageType,
|
|
13
20
|
ErrorHandler,
|
|
14
21
|
InternalSchema,
|
|
22
|
+
ManagedIdentifier,
|
|
15
23
|
ModelInit,
|
|
16
24
|
ModelInstanceMetadata,
|
|
25
|
+
ModelPredicate,
|
|
17
26
|
MutableModel,
|
|
18
27
|
NamespaceResolver,
|
|
19
28
|
OpType,
|
|
20
|
-
|
|
29
|
+
OptionallyManagedIdentifier,
|
|
21
30
|
PersistentModelConstructor,
|
|
22
31
|
SchemaModel,
|
|
23
32
|
SchemaNamespace,
|
|
24
33
|
TypeConstructorMap,
|
|
25
|
-
ModelPredicate,
|
|
26
|
-
AuthModeStrategy,
|
|
27
|
-
ManagedIdentifier,
|
|
28
|
-
OptionallyManagedIdentifier,
|
|
29
|
-
AmplifyContext,
|
|
30
34
|
} from '../types';
|
|
31
|
-
// tslint:disable:no-duplicate-imports
|
|
32
35
|
import type { __modelMeta__ } from '../types';
|
|
36
|
+
import { SYNC, USER, getNow } from '../util';
|
|
33
37
|
|
|
34
|
-
import { getNow, SYNC, USER } from '../util';
|
|
35
38
|
import DataStoreConnectivity from './datastoreConnectivity';
|
|
36
39
|
import { ModelMerger } from './merger';
|
|
37
40
|
import { MutationEventOutbox } from './outbox';
|
|
@@ -39,30 +42,25 @@ import { MutationProcessor } from './processors/mutation';
|
|
|
39
42
|
import { CONTROL_MSG, SubscriptionProcessor } from './processors/subscription';
|
|
40
43
|
import { SyncProcessor } from './processors/sync';
|
|
41
44
|
import {
|
|
45
|
+
TransformerMutationType,
|
|
42
46
|
createMutationInstanceFromModelOperation,
|
|
43
47
|
getIdentifierValue,
|
|
44
48
|
predicateToGraphQLCondition,
|
|
45
|
-
TransformerMutationType,
|
|
46
49
|
} from './utils';
|
|
47
50
|
|
|
48
|
-
import {
|
|
49
|
-
CONTROL_MSG as PUBSUB_CONTROL_MSG,
|
|
50
|
-
ConnectionState,
|
|
51
|
-
CONNECTION_STATE_CHANGE as PUBSUB_CONNECTION_STATE_CHANGE,
|
|
52
|
-
} from '@aws-amplify/api-graphql';
|
|
53
|
-
|
|
54
51
|
const logger = new ConsoleLogger('DataStore');
|
|
55
52
|
|
|
56
53
|
const ownSymbol = Symbol('sync');
|
|
57
54
|
|
|
58
|
-
|
|
55
|
+
interface StartParams {
|
|
59
56
|
fullSyncInterval: number;
|
|
60
|
-
}
|
|
57
|
+
}
|
|
61
58
|
|
|
62
59
|
export declare class MutationEvent {
|
|
63
60
|
readonly [__modelMeta__]: {
|
|
64
61
|
identifier: OptionallyManagedIdentifier<MutationEvent, 'id'>;
|
|
65
62
|
};
|
|
63
|
+
|
|
66
64
|
public readonly id: string;
|
|
67
65
|
public readonly model: string;
|
|
68
66
|
public readonly operation: TransformerMutationType;
|
|
@@ -80,6 +78,7 @@ export declare class ModelMetadata {
|
|
|
80
78
|
readonly [__modelMeta__]: {
|
|
81
79
|
identifier: ManagedIdentifier<ModelMetadata, 'id'>;
|
|
82
80
|
};
|
|
81
|
+
|
|
83
82
|
public readonly id: string;
|
|
84
83
|
public readonly namespace: string;
|
|
85
84
|
public readonly model: string;
|
|
@@ -116,15 +115,17 @@ export class SyncEngine {
|
|
|
116
115
|
private readonly modelMerger: ModelMerger;
|
|
117
116
|
private readonly outbox: MutationEventOutbox;
|
|
118
117
|
private readonly datastoreConnectivity: DataStoreConnectivity;
|
|
119
|
-
private readonly modelSyncedStatus
|
|
118
|
+
private readonly modelSyncedStatus = new WeakMap<
|
|
120
119
|
PersistentModelConstructor<any>,
|
|
121
120
|
boolean
|
|
122
|
-
>
|
|
121
|
+
>();
|
|
122
|
+
|
|
123
123
|
private unsleepSyncQueriesObservable: (() => void) | null;
|
|
124
124
|
private waitForSleepState: Promise<void>;
|
|
125
125
|
private syncQueriesObservableStartSleeping: (
|
|
126
126
|
value?: void | PromiseLike<void>,
|
|
127
127
|
) => void;
|
|
128
|
+
|
|
128
129
|
private stopDisruptionListener: () => void;
|
|
129
130
|
private connectionDisrupted = false;
|
|
130
131
|
|
|
@@ -159,13 +160,12 @@ export class SyncEngine {
|
|
|
159
160
|
this.syncQueriesObservableStartSleeping = resolve;
|
|
160
161
|
});
|
|
161
162
|
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
] as PersistentModelConstructor<MutationEvent>;
|
|
163
|
+
const MutationEventCtor = this.modelClasses
|
|
164
|
+
.MutationEvent as PersistentModelConstructor<MutationEvent>;
|
|
165
165
|
|
|
166
166
|
this.outbox = new MutationEventOutbox(
|
|
167
167
|
this.schema,
|
|
168
|
-
|
|
168
|
+
MutationEventCtor,
|
|
169
169
|
modelInstanceCreator,
|
|
170
170
|
ownSymbol,
|
|
171
171
|
);
|
|
@@ -196,7 +196,7 @@ export class SyncEngine {
|
|
|
196
196
|
this.userModelClasses,
|
|
197
197
|
this.outbox,
|
|
198
198
|
this.modelInstanceCreator,
|
|
199
|
-
|
|
199
|
+
MutationEventCtor,
|
|
200
200
|
this.amplifyConfig,
|
|
201
201
|
this.authModeStrategy,
|
|
202
202
|
errorHandler,
|
|
@@ -219,203 +219,200 @@ export class SyncEngine {
|
|
|
219
219
|
await this.setupModels(params);
|
|
220
220
|
} catch (err) {
|
|
221
221
|
observer.error(err);
|
|
222
|
+
|
|
222
223
|
return;
|
|
223
224
|
}
|
|
224
225
|
|
|
225
226
|
// this is awaited at the bottom. so, we don't need to register
|
|
226
227
|
// this explicitly with the context. it's already contained.
|
|
227
|
-
const startPromise = new Promise<void>(
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
228
|
+
const startPromise = new Promise<void>((resolve, reject) => {
|
|
229
|
+
const doneStarting = resolve;
|
|
230
|
+
const failedStarting = reject;
|
|
231
|
+
|
|
232
|
+
this.datastoreConnectivity.status().subscribe(
|
|
233
|
+
async ({ online }) =>
|
|
234
|
+
this.runningProcesses.isOpen &&
|
|
235
|
+
this.runningProcesses.add(async onTerminate => {
|
|
236
|
+
// From offline to online
|
|
237
|
+
if (online && !this.online) {
|
|
238
|
+
this.online = online;
|
|
239
|
+
|
|
240
|
+
observer.next({
|
|
241
|
+
type: ControlMessage.SYNC_ENGINE_NETWORK_STATUS,
|
|
242
|
+
data: {
|
|
243
|
+
active: this.online,
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
this.stopDisruptionListener = this.startDisruptionListener();
|
|
248
|
+
// #region GraphQL Subscriptions
|
|
249
|
+
const [ctlSubsObservable, dataSubsObservable] =
|
|
250
|
+
this.subscriptionsProcessor.start();
|
|
251
|
+
|
|
252
|
+
try {
|
|
253
|
+
await new Promise<void>((_resolve, _reject) => {
|
|
254
|
+
onTerminate.then(_reject);
|
|
255
|
+
const ctlSubsSubscription = ctlSubsObservable.subscribe({
|
|
256
|
+
next: msg => {
|
|
257
|
+
if (msg === CONTROL_MSG.CONNECTED) {
|
|
258
|
+
_resolve();
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
error: err => {
|
|
262
|
+
_reject(err);
|
|
263
|
+
const handleDisconnect = this.disconnectionHandler();
|
|
264
|
+
handleDisconnect(err);
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
subscriptions.push(ctlSubsSubscription);
|
|
242
269
|
});
|
|
270
|
+
} catch (err) {
|
|
271
|
+
observer.error(err);
|
|
272
|
+
failedStarting();
|
|
243
273
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
[TransformerMutationType, SchemaModel, PersistentModel]
|
|
247
|
-
>;
|
|
248
|
-
|
|
249
|
-
this.stopDisruptionListener =
|
|
250
|
-
this.startDisruptionListener();
|
|
251
|
-
//#region GraphQL Subscriptions
|
|
252
|
-
[ctlSubsObservable, dataSubsObservable] =
|
|
253
|
-
this.subscriptionsProcessor.start();
|
|
254
|
-
|
|
255
|
-
try {
|
|
256
|
-
await new Promise<void>((resolve, reject) => {
|
|
257
|
-
onTerminate.then(reject);
|
|
258
|
-
const ctlSubsSubscription = ctlSubsObservable.subscribe(
|
|
259
|
-
{
|
|
260
|
-
next: msg => {
|
|
261
|
-
if (msg === CONTROL_MSG.CONNECTED) {
|
|
262
|
-
resolve();
|
|
263
|
-
}
|
|
264
|
-
},
|
|
265
|
-
error: err => {
|
|
266
|
-
reject(err);
|
|
267
|
-
const handleDisconnect =
|
|
268
|
-
this.disconnectionHandler();
|
|
269
|
-
handleDisconnect(err);
|
|
270
|
-
},
|
|
271
|
-
},
|
|
272
|
-
);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
273
276
|
|
|
274
|
-
|
|
275
|
-
});
|
|
276
|
-
} catch (err) {
|
|
277
|
-
observer.error(err);
|
|
278
|
-
failedStarting();
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
277
|
+
logger.log('Realtime ready');
|
|
281
278
|
|
|
282
|
-
|
|
279
|
+
observer.next({
|
|
280
|
+
type: ControlMessage.SYNC_ENGINE_SUBSCRIPTIONS_ESTABLISHED,
|
|
281
|
+
});
|
|
283
282
|
|
|
284
|
-
|
|
285
|
-
type: ControlMessage.SYNC_ENGINE_SUBSCRIPTIONS_ESTABLISHED,
|
|
286
|
-
});
|
|
283
|
+
// #endregion
|
|
287
284
|
|
|
288
|
-
|
|
285
|
+
// #region Base & Sync queries
|
|
286
|
+
try {
|
|
287
|
+
await new Promise<void>((_resolve, _reject) => {
|
|
288
|
+
const syncQuerySubscription =
|
|
289
|
+
this.syncQueriesObservable().subscribe({
|
|
290
|
+
next: message => {
|
|
291
|
+
const { type } = message;
|
|
289
292
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
const { type } = message;
|
|
293
|
+
if (
|
|
294
|
+
type ===
|
|
295
|
+
ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY
|
|
296
|
+
) {
|
|
297
|
+
_resolve();
|
|
298
|
+
}
|
|
297
299
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
300
|
+
observer.next(message);
|
|
301
|
+
},
|
|
302
|
+
complete: () => {
|
|
303
|
+
_resolve();
|
|
304
|
+
},
|
|
305
|
+
error: error => {
|
|
306
|
+
_reject(error);
|
|
307
|
+
},
|
|
308
|
+
});
|
|
304
309
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
310
|
+
if (syncQuerySubscription) {
|
|
311
|
+
subscriptions.push(syncQuerySubscription);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
} catch (error) {
|
|
315
|
+
observer.error(error);
|
|
316
|
+
failedStarting();
|
|
317
|
+
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
// #endregion
|
|
321
|
+
|
|
322
|
+
// #region process mutations (outbox)
|
|
323
|
+
subscriptions.push(
|
|
324
|
+
this.mutationsProcessor
|
|
325
|
+
.start()
|
|
326
|
+
.subscribe(({ modelDefinition, model: item, hasMore }) =>
|
|
327
|
+
this.runningProcesses.add(async () => {
|
|
328
|
+
const modelConstructor = this.userModelClasses[
|
|
329
|
+
modelDefinition.name
|
|
330
|
+
] as PersistentModelConstructor<any>;
|
|
331
|
+
|
|
332
|
+
const model = this.modelInstanceCreator(
|
|
333
|
+
modelConstructor,
|
|
334
|
+
item,
|
|
335
|
+
);
|
|
336
|
+
|
|
337
|
+
await this.storage.runExclusive(storage =>
|
|
338
|
+
this.modelMerger.merge(
|
|
339
|
+
storage,
|
|
340
|
+
model,
|
|
341
|
+
modelDefinition,
|
|
342
|
+
),
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
observer.next({
|
|
346
|
+
type: ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_PROCESSED,
|
|
347
|
+
data: {
|
|
348
|
+
model: modelConstructor,
|
|
349
|
+
element: model,
|
|
312
350
|
},
|
|
313
351
|
});
|
|
314
352
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
return;
|
|
323
|
-
}
|
|
324
|
-
//#endregion
|
|
325
|
-
|
|
326
|
-
//#region process mutations (outbox)
|
|
327
|
-
subscriptions.push(
|
|
328
|
-
this.mutationsProcessor
|
|
329
|
-
.start()
|
|
330
|
-
.subscribe(
|
|
331
|
-
({ modelDefinition, model: item, hasMore }) =>
|
|
332
|
-
this.runningProcesses.add(async () => {
|
|
333
|
-
const modelConstructor = this.userModelClasses[
|
|
334
|
-
modelDefinition.name
|
|
335
|
-
] as PersistentModelConstructor<any>;
|
|
336
|
-
|
|
337
|
-
const model = this.modelInstanceCreator(
|
|
338
|
-
modelConstructor,
|
|
339
|
-
item,
|
|
340
|
-
);
|
|
341
|
-
|
|
342
|
-
await this.storage.runExclusive(storage =>
|
|
343
|
-
this.modelMerger.merge(
|
|
344
|
-
storage,
|
|
345
|
-
model,
|
|
346
|
-
modelDefinition,
|
|
347
|
-
),
|
|
348
|
-
);
|
|
349
|
-
|
|
350
|
-
observer.next({
|
|
351
|
-
type: ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_PROCESSED,
|
|
352
|
-
data: {
|
|
353
|
-
model: modelConstructor,
|
|
354
|
-
element: model,
|
|
355
|
-
},
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
observer.next({
|
|
359
|
-
type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
|
|
360
|
-
data: {
|
|
361
|
-
isEmpty: !hasMore,
|
|
362
|
-
},
|
|
363
|
-
});
|
|
364
|
-
}, 'mutation processor event'),
|
|
365
|
-
),
|
|
366
|
-
);
|
|
367
|
-
//#endregion
|
|
368
|
-
|
|
369
|
-
//#region Merge subscriptions buffer
|
|
370
|
-
subscriptions.push(
|
|
371
|
-
dataSubsObservable!.subscribe(
|
|
372
|
-
([_transformerMutationType, modelDefinition, item]) =>
|
|
373
|
-
this.runningProcesses.add(async () => {
|
|
374
|
-
const modelConstructor = this.userModelClasses[
|
|
375
|
-
modelDefinition.name
|
|
376
|
-
] as PersistentModelConstructor<any>;
|
|
377
|
-
|
|
378
|
-
const model = this.modelInstanceCreator(
|
|
379
|
-
modelConstructor,
|
|
380
|
-
item,
|
|
381
|
-
);
|
|
382
|
-
|
|
383
|
-
await this.storage.runExclusive(storage =>
|
|
384
|
-
this.modelMerger.merge(
|
|
385
|
-
storage,
|
|
386
|
-
model,
|
|
387
|
-
modelDefinition,
|
|
388
|
-
),
|
|
389
|
-
);
|
|
390
|
-
}, 'subscription dataSubsObservable event'),
|
|
353
|
+
observer.next({
|
|
354
|
+
type: ControlMessage.SYNC_ENGINE_OUTBOX_STATUS,
|
|
355
|
+
data: {
|
|
356
|
+
isEmpty: !hasMore,
|
|
357
|
+
},
|
|
358
|
+
});
|
|
359
|
+
}, 'mutation processor event'),
|
|
391
360
|
),
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
361
|
+
);
|
|
362
|
+
// #endregion
|
|
363
|
+
|
|
364
|
+
// #region Merge subscriptions buffer
|
|
365
|
+
subscriptions.push(
|
|
366
|
+
dataSubsObservable!.subscribe(
|
|
367
|
+
([_transformerMutationType, modelDefinition, item]) =>
|
|
368
|
+
this.runningProcesses.add(async () => {
|
|
369
|
+
const modelConstructor = this.userModelClasses[
|
|
370
|
+
modelDefinition.name
|
|
371
|
+
] as PersistentModelConstructor<any>;
|
|
372
|
+
|
|
373
|
+
const model = this.modelInstanceCreator(
|
|
374
|
+
modelConstructor,
|
|
375
|
+
item,
|
|
376
|
+
);
|
|
377
|
+
|
|
378
|
+
await this.storage.runExclusive(storage =>
|
|
379
|
+
this.modelMerger.merge(
|
|
380
|
+
storage,
|
|
381
|
+
model,
|
|
382
|
+
modelDefinition,
|
|
383
|
+
),
|
|
384
|
+
);
|
|
385
|
+
}, 'subscription dataSubsObservable event'),
|
|
386
|
+
),
|
|
387
|
+
);
|
|
388
|
+
// #endregion
|
|
389
|
+
} else if (!online) {
|
|
390
|
+
this.online = online;
|
|
391
|
+
|
|
392
|
+
observer.next({
|
|
393
|
+
type: ControlMessage.SYNC_ENGINE_NETWORK_STATUS,
|
|
394
|
+
data: {
|
|
395
|
+
active: this.online,
|
|
396
|
+
},
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
subscriptions.forEach(sub => {
|
|
400
|
+
sub.unsubscribe();
|
|
401
|
+
});
|
|
402
|
+
subscriptions = [];
|
|
403
|
+
}
|
|
407
404
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
);
|
|
405
|
+
doneStarting();
|
|
406
|
+
}, 'datastore connectivity event'),
|
|
407
|
+
);
|
|
408
|
+
});
|
|
413
409
|
|
|
414
410
|
this.storage
|
|
415
411
|
.observe(null, null, ownSymbol)
|
|
416
412
|
.pipe(
|
|
417
413
|
filter(({ model }) => {
|
|
418
414
|
const modelDefinition = this.getModelDefinition(model);
|
|
415
|
+
|
|
419
416
|
return modelDefinition.syncable === true;
|
|
420
417
|
}),
|
|
421
418
|
)
|
|
@@ -424,9 +421,8 @@ export class SyncEngine {
|
|
|
424
421
|
this.runningProcesses.add(async () => {
|
|
425
422
|
const namespace =
|
|
426
423
|
this.schema.namespaces[this.namespaceResolver(model)];
|
|
427
|
-
const MutationEventConstructor = this.modelClasses
|
|
428
|
-
|
|
429
|
-
] as PersistentModelConstructor<MutationEvent>;
|
|
424
|
+
const MutationEventConstructor = this.modelClasses
|
|
425
|
+
.MutationEvent as PersistentModelConstructor<MutationEvent>;
|
|
430
426
|
const modelDefinition = this.getModelDefinition(model);
|
|
431
427
|
const graphQLCondition = predicateToGraphQLCondition(
|
|
432
428
|
condition!,
|
|
@@ -494,21 +490,14 @@ export class SyncEngine {
|
|
|
494
490
|
private async getModelsMetadataWithNextFullSync(
|
|
495
491
|
currentTimeStamp: number,
|
|
496
492
|
): Promise<Map<SchemaModel, [string, number]>> {
|
|
497
|
-
const modelLastSync
|
|
493
|
+
const modelLastSync = new Map<SchemaModel, [string, number]>(
|
|
498
494
|
(
|
|
499
495
|
await this.runningProcesses.add(
|
|
500
496
|
() => this.getModelsMetadata(),
|
|
501
497
|
'sync/index getModelsMetadataWithNextFullSync',
|
|
502
498
|
)
|
|
503
499
|
).map(
|
|
504
|
-
({
|
|
505
|
-
namespace,
|
|
506
|
-
model,
|
|
507
|
-
lastSync,
|
|
508
|
-
lastFullSync,
|
|
509
|
-
fullSyncInterval,
|
|
510
|
-
lastSyncPredicate,
|
|
511
|
-
}) => {
|
|
500
|
+
({ namespace, model, lastSync, lastFullSync, fullSyncInterval }) => {
|
|
512
501
|
const nextFullSync = lastFullSync! + fullSyncInterval;
|
|
513
502
|
const syncFrom =
|
|
514
503
|
!lastFullSync || nextFullSync < currentTimeStamp
|
|
@@ -541,14 +530,14 @@ export class SyncEngine {
|
|
|
541
530
|
let terminated = false;
|
|
542
531
|
|
|
543
532
|
while (!observer.closed && !terminated) {
|
|
544
|
-
const count
|
|
533
|
+
const count = new WeakMap<
|
|
545
534
|
PersistentModelConstructor<any>,
|
|
546
535
|
{
|
|
547
536
|
new: number;
|
|
548
537
|
updated: number;
|
|
549
538
|
deleted: number;
|
|
550
539
|
}
|
|
551
|
-
>
|
|
540
|
+
>();
|
|
552
541
|
|
|
553
542
|
const modelLastSync = await this.getModelsMetadataWithNextFullSync(
|
|
554
543
|
Date.now(),
|
|
@@ -561,9 +550,11 @@ export class SyncEngine {
|
|
|
561
550
|
let start: number;
|
|
562
551
|
let syncDuration: number;
|
|
563
552
|
let lastStartedAt: number;
|
|
564
|
-
await new Promise<void>((resolve,
|
|
553
|
+
await new Promise<void>((resolve, _reject) => {
|
|
565
554
|
if (!this.runningProcesses.isOpen) resolve();
|
|
566
|
-
onTerminate.then(() =>
|
|
555
|
+
onTerminate.then(() => {
|
|
556
|
+
resolve();
|
|
557
|
+
});
|
|
567
558
|
syncQueriesSubscription = this.syncQueriesProcessor
|
|
568
559
|
.start(modelLastSync)
|
|
569
560
|
.subscribe({
|
|
@@ -613,6 +604,7 @@ export class SyncEngine {
|
|
|
613
604
|
}
|
|
614
605
|
|
|
615
606
|
oneByOne.push(item);
|
|
607
|
+
|
|
616
608
|
return false;
|
|
617
609
|
});
|
|
618
610
|
|
|
@@ -661,7 +653,7 @@ export class SyncEngine {
|
|
|
661
653
|
if (done) {
|
|
662
654
|
const { name: modelName } = modelDefinition;
|
|
663
655
|
|
|
664
|
-
|
|
656
|
+
// #region update last sync for type
|
|
665
657
|
let modelMetadata = await this.getModelMetadata(
|
|
666
658
|
namespace,
|
|
667
659
|
modelName,
|
|
@@ -694,7 +686,7 @@ export class SyncEngine {
|
|
|
694
686
|
undefined,
|
|
695
687
|
ownSymbol,
|
|
696
688
|
);
|
|
697
|
-
|
|
689
|
+
// #endregion
|
|
698
690
|
|
|
699
691
|
const counts = count.get(modelConstructor);
|
|
700
692
|
|
|
@@ -768,16 +760,16 @@ export class SyncEngine {
|
|
|
768
760
|
// TLDR; this is a lot of complexity here for a sleep(),
|
|
769
761
|
// but, it's not clear to me yet how to support an
|
|
770
762
|
// extensible, centralized cancelable `sleep()` elegantly.
|
|
771
|
-
await this.runningProcesses.add(async
|
|
772
|
-
let
|
|
763
|
+
await this.runningProcesses.add(async onRunningProcessTerminate => {
|
|
764
|
+
let _sleepTimer;
|
|
773
765
|
let unsleep;
|
|
774
766
|
|
|
775
|
-
const sleep = new Promise(
|
|
776
|
-
unsleep =
|
|
777
|
-
|
|
767
|
+
const sleep = new Promise(resolve => {
|
|
768
|
+
unsleep = resolve;
|
|
769
|
+
_sleepTimer = setTimeout(unsleep, msNextFullSync);
|
|
778
770
|
});
|
|
779
771
|
|
|
780
|
-
|
|
772
|
+
onRunningProcessTerminate.then(() => {
|
|
781
773
|
terminated = true;
|
|
782
774
|
this.syncQueriesObservableStartSleeping();
|
|
783
775
|
unsleep();
|
|
@@ -785,6 +777,7 @@ export class SyncEngine {
|
|
|
785
777
|
|
|
786
778
|
this.unsleepSyncQueriesObservable = unsleep;
|
|
787
779
|
this.syncQueriesObservableStartSleeping();
|
|
780
|
+
|
|
788
781
|
return sleep;
|
|
789
782
|
}, 'syncQueriesObservable sleep');
|
|
790
783
|
|
|
@@ -927,10 +920,10 @@ export class SyncEngine {
|
|
|
927
920
|
}
|
|
928
921
|
|
|
929
922
|
private async getModelsMetadata(): Promise<ModelMetadata[]> {
|
|
930
|
-
const
|
|
923
|
+
const ModelMetadataCtor = this.modelClasses
|
|
931
924
|
.ModelMetadata as PersistentModelConstructor<ModelMetadata>;
|
|
932
925
|
|
|
933
|
-
const modelsMetadata = await this.storage.query(
|
|
926
|
+
const modelsMetadata = await this.storage.query(ModelMetadataCtor);
|
|
934
927
|
|
|
935
928
|
return modelsMetadata;
|
|
936
929
|
}
|
|
@@ -939,18 +932,22 @@ export class SyncEngine {
|
|
|
939
932
|
namespace: string,
|
|
940
933
|
model: string,
|
|
941
934
|
): Promise<ModelMetadata> {
|
|
942
|
-
const
|
|
935
|
+
const ModelMetadataCtor = this.modelClasses
|
|
943
936
|
.ModelMetadata as PersistentModelConstructor<ModelMetadata>;
|
|
944
937
|
|
|
945
938
|
const predicate = ModelPredicateCreator.createFromAST<ModelMetadata>(
|
|
946
|
-
this.schema.namespaces[SYNC].models[
|
|
939
|
+
this.schema.namespaces[SYNC].models[ModelMetadataCtor.name],
|
|
947
940
|
{ and: [{ namespace: { eq: namespace } }, { model: { eq: model } }] },
|
|
948
941
|
);
|
|
949
942
|
|
|
950
|
-
const [modelMetadata] = await this.storage.query(
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
943
|
+
const [modelMetadata] = await this.storage.query(
|
|
944
|
+
ModelMetadataCtor,
|
|
945
|
+
predicate,
|
|
946
|
+
{
|
|
947
|
+
page: 0,
|
|
948
|
+
limit: 1,
|
|
949
|
+
},
|
|
950
|
+
);
|
|
954
951
|
|
|
955
952
|
return modelMetadata;
|
|
956
953
|
}
|
|
@@ -1074,6 +1071,7 @@ export class SyncEngine {
|
|
|
1074
1071
|
},
|
|
1075
1072
|
},
|
|
1076
1073
|
};
|
|
1074
|
+
|
|
1077
1075
|
return namespace;
|
|
1078
1076
|
}
|
|
1079
1077
|
|