Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore): correctly apply config values #9542

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ||
Comment on lines +1354 to +1360
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the swapped order make a difference here (same question with the configSyncExpressions || this.syncExpressions on line 1330/1331)? Asking out of curiosity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The changed order will favor values coming from the Amplify.configure and DataStore.configure calls over the defaults.

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