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

optimize hydrate & process function in offline store #77

Merged
merged 1 commit into from
Jan 27, 2021
Merged
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
38 changes: 19 additions & 19 deletions packages/offline-first/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const defaultOfflineOptions = {

export class OfflineFirst<T> {
private offlineStore: ICache;
private busy = false;
private busy: Promise<void> | undefined;
private offlineOptions: OfflineFirstOptions<T> = defaultOfflineOptions;
private online = isServer;
private rehydrated = isServer;
Expand Down Expand Up @@ -97,25 +97,25 @@ export class OfflineFirst<T> {
if (!this.promisesRestore) {
this.promisesRestore = Promise.all([NetInfo.fetch(), this.offlineStore.restore()])
.then((result) => {
this.disposeListener = NetInfo.addEventListener((state) => {
const { isConnected } = state;
if (this.online !== isConnected && isConnected && !this.isManualExecution()) {
this.process();
}
this.online = isConnected;
});
const finalize = (): boolean => {
this.disposeListener = NetInfo.addEventListener((state) => {
const { isConnected } = state;
if (this.online !== isConnected && isConnected && !this.isManualExecution()) {
this.process();
}
this.online = isConnected;
});
this.notify();
this.rehydrated = true;
return true;
};

const { isConnected } = result[0];
this.online = isConnected;
if (isConnected && !this.isManualExecution()) {
return this.process().then(() => {
this.notify();
this.rehydrated = true;
return true;
});
return this.process().then(finalize);
}
this.notify();
this.rehydrated = true;
return true;
return finalize();
})
.catch((error) => {
this.rehydrated = false;
Expand Down Expand Up @@ -162,11 +162,10 @@ export class OfflineFirst<T> {

public process(): Promise<void> {
if (!this.busy) {
this.busy = true;
const { start, finish, onExecute } = this.offlineOptions;
const listMutation: Array<OfflineRecordCache<T>> = this.getListMutation();
let parallelPromises = [];
return start(listMutation).then(async (startMutations) => {
this.busy = start(listMutation).then(async (startMutations) => {
try {
for (const mutation of startMutations) {
const processMutation = await onExecute(mutation);
Expand All @@ -191,10 +190,11 @@ export class OfflineFirst<T> {
} catch (error) {
return finish(this.getListMutation(), error);
} finally {
this.busy = false;
this.busy = undefined;
}
});
}
return this.busy;
}

public executeMutation(offlineRecord: OfflineRecordCache<T>): Promise<void> {
Expand Down