Skip to content

Commit

Permalink
feat(@aws-amplify/datastore): configurable sync pagination limit (#5181)
Browse files Browse the repository at this point in the history
* feat(@aws-amplify/datastore): configurable sync pagination limit

* better name for sync paginationLimit variable

Co-authored-by: shpetimshala <shpetim.sha@gmail.com>
Co-authored-by: Manuel Iglesias <6154160+manueliglesias@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 27, 2020
1 parent c9c020d commit a4f518b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ let amplifyConfig: Record<string, any> = {};
let conflictHandler: ConflictHandler;
let errorHandler: (error: SyncError) => void;
let maxRecordsToSync: number;
let syncPageSize: number;
let fullSyncInterval: number;

function configure(config: DataStoreConfig = {}) {
Expand All @@ -653,6 +654,7 @@ function configure(config: DataStoreConfig = {}) {
conflictHandler: configConflictHandler,
errorHandler: configErrorHandler,
maxRecordsToSync: configMaxRecordsToSync,
syncPageSize: configSyncPageSize,
fullSyncInterval: configFullSyncInterval,
...configFromAmplify
} = config;
Expand All @@ -676,6 +678,11 @@ function configure(config: DataStoreConfig = {}) {
maxRecordsToSync ||
config.maxRecordsToSync;

syncPageSize =
(configDataStore && configDataStore.syncPageSize) ||
syncPageSize ||
config.syncPageSize;

fullSyncInterval =
(configDataStore && configDataStore.fullSyncInterval) ||
configFullSyncInterval ||
Expand Down Expand Up @@ -797,6 +804,7 @@ async function start(): Promise<void> {
storage,
modelInstanceCreator,
maxRecordsToSync,
syncPageSize,
conflictHandler,
errorHandler
);
Expand Down
4 changes: 3 additions & 1 deletion packages/datastore/src/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class SyncEngine {
private readonly storage: Storage,
private readonly modelInstanceCreator: ModelInstanceCreator,
private readonly maxRecordsToSync: number,
private readonly syncPageSize: number,
conflictHandler: ConflictHandler,
errorHandler: ErrorHandler
) {
Expand All @@ -103,7 +104,8 @@ export class SyncEngine {

this.syncQueriesProcessor = new SyncProcessor(
this.schema,
maxRecordsToSync
maxRecordsToSync,
syncPageSize
);
this.subscriptionsProcessor = new SubscriptionProcessor(this.schema);
this.mutationsProcessor = new MutationProcessor(
Expand Down
10 changes: 8 additions & 2 deletions packages/datastore/src/sync/processors/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class SyncProcessor {

constructor(
private readonly schema: InternalSchema,
private readonly maxRecordsToSync: number = DEFAULT_MAX_RECORDS_TO_SYNC
private readonly maxRecordsToSync: number = DEFAULT_MAX_RECORDS_TO_SYNC,
private readonly syncPageSize: number = DEFAULT_PAGINATION_LIMIT
) {
this.generateQueries();
}
Expand Down Expand Up @@ -104,6 +105,11 @@ class SyncProcessor {
? this.maxRecordsToSync
: DEFAULT_MAX_RECORDS_TO_SYNC;

const syncPageSize =
this.syncPageSize !== undefined
? this.syncPageSize
: DEFAULT_PAGINATION_LIMIT;

const parentPromises = new Map<string, Promise<void>>();

const observable = new Observable<SyncModelPage>(observer => {
Expand Down Expand Up @@ -147,7 +153,7 @@ class SyncProcessor {

const limit = Math.min(
maxRecordsToSync - recordsReceived,
DEFAULT_PAGINATION_LIMIT
syncPageSize
);

({ items, nextToken, startedAt } = await this.retrievePage(
Expand Down
2 changes: 2 additions & 0 deletions packages/datastore/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,13 @@ export type DataStoreConfig = {
conflictHandler?: ConflictHandler; // default : retry until client wins up to x times
errorHandler?: (error: SyncError) => void; // default : logger.warn
maxRecordsToSync?: number; // merge
syncPageSize?: number;
fullSyncInterval?: number;
};
conflictHandler?: ConflictHandler; // default : retry until client wins up to x times
errorHandler?: (error: SyncError) => void; // default : logger.warn
maxRecordsToSync?: number; // merge
syncPageSize?: number;
fullSyncInterval?: number;
};

Expand Down

0 comments on commit a4f518b

Please sign in to comment.