Skip to content

Commit

Permalink
fix(datastore): correctly apply config values (#9542)
Browse files Browse the repository at this point in the history
  • Loading branch information
iartemiev authored Feb 1, 2022
1 parent 472122e commit 3f8b838
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
12 changes: 6 additions & 6 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,6 @@ class DataStore {
userClasses,
this.storage,
modelInstanceCreator,
this.maxRecordsToSync,
this.syncPageSize,
this.conflictHandler,
this.errorHandler,
this.syncPredicates,
Expand Down Expand Up @@ -1329,12 +1327,13 @@ class DataStore {

this.syncExpressions =
(configDataStore && configDataStore.syncExpressions) ||
this.syncExpressions ||
configSyncExpressions;
configSyncExpressions ||
this.syncExpressions;

this.maxRecordsToSync =
(configDataStore && configDataStore.maxRecordsToSync) ||
configMaxRecordsToSync ||
this.maxRecordsToSync ||
10000;

// store on config object, so that Sync, Subscription, and Mutation processors can have access
Expand All @@ -1343,21 +1342,22 @@ class DataStore {
this.syncPageSize =
(configDataStore && configDataStore.syncPageSize) ||
configSyncPageSize ||
this.syncPageSize ||
1000;

// store on config object, so that Sync, Subscription, and Mutation processors can have access
this.amplifyConfig.syncPageSize = this.syncPageSize;

this.fullSyncInterval =
(configDataStore && configDataStore.fullSyncInterval) ||
this.fullSyncInterval ||
configFullSyncInterval ||
this.fullSyncInterval ||
24 * 60; // 1 day

this.storageAdapter =
(configDataStore && configDataStore.storageAdapter) ||
this.storageAdapter ||
configStorageAdapter ||
this.storageAdapter ||
undefined;

this.sessionId = this.retrieveSessionId();
Expand Down
55 changes: 23 additions & 32 deletions packages/datastore/src/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ export class SyncEngine {
private readonly userModelClasses: TypeConstructorMap,
private readonly storage: Storage,
private readonly modelInstanceCreator: ModelInstanceCreator,
private readonly maxRecordsToSync: number,
private readonly syncPageSize: number,
conflictHandler: ConflictHandler,
errorHandler: ErrorHandler,
private readonly syncPredicates: WeakMap<SchemaModel, ModelPredicate<any>>,
Expand Down Expand Up @@ -189,11 +187,9 @@ export class SyncEngine {
});

let ctlSubsObservable: Observable<CONTROL_MSG>;
let dataSubsObservable: Observable<[
TransformerMutationType,
SchemaModel,
PersistentModel
]>;
let dataSubsObservable: Observable<
[TransformerMutationType, SchemaModel, PersistentModel]
>;

if (isNode) {
logger.warn(
Expand Down Expand Up @@ -244,8 +240,8 @@ export class SyncEngine {
//#region Base & Sync queries
try {
await new Promise((resolve, reject) => {
const syncQuerySubscription = this.syncQueriesObservable().subscribe(
{
const syncQuerySubscription =
this.syncQueriesObservable().subscribe({
next: message => {
const { type } = message;

Expand All @@ -263,8 +259,7 @@ export class SyncEngine {
error: error => {
reject(error);
},
}
);
});

if (syncQuerySubscription) {
subscriptions.push(syncQuerySubscription);
Expand Down Expand Up @@ -295,8 +290,7 @@ export class SyncEngine {
);

observer.next({
type:
ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_PROCESSED,
type: ControlMessage.SYNC_ENGINE_OUTBOX_MUTATION_PROCESSED,
data: {
model: modelConstructor,
element: model,
Expand Down Expand Up @@ -363,9 +357,8 @@ export class SyncEngine {
})
.subscribe({
next: async ({ opType, model, element, condition }) => {
const namespace = this.schema.namespaces[
this.namespaceResolver(model)
];
const namespace =
this.schema.namespaces[this.namespaceResolver(model)];
const MutationEventConstructor = this.modelClasses[
'MutationEvent'
] as PersistentModelConstructor<MutationEvent>;
Expand Down Expand Up @@ -603,16 +596,15 @@ export class SyncEngine {
isFullSync ? startedAt : lastFullSync
);

modelMetadata = (this.modelClasses
.ModelMetadata as PersistentModelConstructor<any>).copyOf(
modelMetadata,
draft => {
draft.lastSync = startedAt;
draft.lastFullSync = isFullSync
? startedAt
: modelMetadata.lastFullSync;
}
);
modelMetadata = (
this.modelClasses
.ModelMetadata as PersistentModelConstructor<any>
).copyOf(modelMetadata, draft => {
draft.lastSync = startedAt;
draft.lastFullSync = isFullSync
? startedAt
: modelMetadata.lastFullSync;
});

await this.storage.save(
modelMetadata,
Expand Down Expand Up @@ -757,9 +749,9 @@ export class SyncEngine {
const syncPredicateUpdated = prevSyncPredicate !== lastSyncPredicate;

[[savedModel]] = await this.storage.save(
(this.modelClasses.ModelMetadata as PersistentModelConstructor<
any
>).copyOf(modelMetadata, draft => {
(
this.modelClasses.ModelMetadata as PersistentModelConstructor<any>
).copyOf(modelMetadata, draft => {
draft.fullSyncInterval = fullSyncInterval;
// perform a base sync if the syncPredicate changed in between calls to DataStore.start
// ensures that the local store contains all the data specified by the syncExpression
Expand Down Expand Up @@ -819,9 +811,8 @@ export class SyncEngine {
): SchemaModel {
const namespaceName = this.namespaceResolver(modelConstructor);

const modelDefinition = this.schema.namespaces[namespaceName].models[
modelConstructor.name
];
const modelDefinition =
this.schema.namespaces[namespaceName].models[modelConstructor.name];

return modelDefinition;
}
Expand Down

0 comments on commit 3f8b838

Please sign in to comment.