Skip to content

Commit

Permalink
Merge pull request #677 from orbitjs/async-await-sources
Browse files Browse the repository at this point in the history
Convert source interface implementations to use async/await
  • Loading branch information
dgeb committed Jul 13, 2019
2 parents fc1e170 + aff3ed2 commit aad7e1c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 82 deletions.
26 changes: 13 additions & 13 deletions packages/@orbit/data/src/source-interfaces/pullable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ export default function pullable(Klass: SourceClass): void {
return this._enqueueRequest('pull', query);
};

proto.__pull__ = function(query: Query): Promise<Transform[]> {
const hints: any = {};
return fulfillInSeries(this, 'beforePull', query, hints)
.then(() => this._pull(query, hints))
.then(result => this._transformed(result))
.then(result => {
return settleInSeries(this, 'pull', query, result).then(() => result);
})
.catch((error: Error) => {
return settleInSeries(this, 'pullFail', query, error).then(() => {
throw error;
});
});
proto.__pull__ = async function(query: Query): Promise<Transform[]> {
try {
const hints: any = {};

await fulfillInSeries(this, 'beforePull', query, hints);
let result = await this._pull(query, hints);
await this._transformed(result);
await settleInSeries(this, 'pull', query, result);
return result;
} catch (error) {
await settleInSeries(this, 'pullFail', query, error);
throw error;
}
};
}
38 changes: 18 additions & 20 deletions packages/@orbit/data/src/source-interfaces/pushable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,26 @@ export default function pushable(Klass: SourceClass): void {
return this._enqueueRequest('push', transform);
};

proto.__push__ = function(transform: Transform): Promise<Transform[]> {
proto.__push__ = async function(transform: Transform): Promise<Transform[]> {
if (this.transformLog.contains(transform.id)) {
return Promise.resolve([]);
return [];
}

const hints: any = {};
return fulfillInSeries(this, 'beforePush', transform, hints)
.then(() => {
if (this.transformLog.contains(transform.id)) {
return Promise.resolve([]);
} else {
return this._push(transform, hints).then((result: Transform[]) => {
return this._transformed(result)
.then(() => settleInSeries(this, 'push', transform, result))
.then(() => result);
});
}
})
.catch(error => {
return settleInSeries(this, 'pushFail', transform, error).then(() => {
throw error;
});
});
try {
const hints: any = {};

await fulfillInSeries(this, 'beforePush', transform, hints);
if (this.transformLog.contains(transform.id)) {
return [];
} else {
let result = await this._push(transform, hints);
await this._transformed(result);
await settleInSeries(this, 'push', transform, result);
return result;
}
} catch (error) {
await settleInSeries(this, 'pushFail', transform, error);
throw error;
}
};
}
23 changes: 11 additions & 12 deletions packages/@orbit/data/src/source-interfaces/queryable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,16 @@ export default function queryable(Klass: SourceClass): void {
return this._enqueueRequest('query', query);
};

proto.__query__ = function(query: Query): Promise<any> {
const hints: any = {};
return fulfillInSeries(this, 'beforeQuery', query, hints)
.then(() => this._query(query, hints))
.then((result: any) => {
return settleInSeries(this, 'query', query, result).then(() => result);
})
.catch((error: Error) => {
return settleInSeries(this, 'queryFail', query, error).then(() => {
throw error;
});
});
proto.__query__ = async function(query: Query): Promise<any> {
try {
const hints: any = {};

await fulfillInSeries(this, 'beforeQuery', query, hints);
let result = await this._query(query, hints);
return settleInSeries(this, 'query', query, result).then(() => result);
} catch (error) {
await settleInSeries(this, 'queryFail', query, error);
throw error;
}
};
}
32 changes: 15 additions & 17 deletions packages/@orbit/data/src/source-interfaces/syncable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,23 @@ export default function syncable(Klass: SourceClass): void {
}
};

proto.__sync__ = function(transform: Transform): Promise<void> {
proto.__sync__ = async function(transform: Transform): Promise<void> {
if (this.transformLog.contains(transform.id)) {
return Promise.resolve();
return;
}

return fulfillInSeries(this, 'beforeSync', transform)
.then(() => {
if (this.transformLog.contains(transform.id)) {
return Promise.resolve();
} else {
return this._sync(transform)
.then(() => this._transformed([transform]))
.then(() => settleInSeries(this, 'sync', transform));
}
})
.catch((error: Error) => {
return settleInSeries(this, 'syncFail', transform, error).then(() => {
throw error;
});
});
try {
await fulfillInSeries(this, 'beforeSync', transform);
if (this.transformLog.contains(transform.id)) {
return;
} else {
await this._sync(transform);
await this._transformed([transform]);
await settleInSeries(this, 'sync', transform);
}
} catch (error) {
await settleInSeries(this, 'syncFail', transform, error);
throw error;
}
};
}
37 changes: 17 additions & 20 deletions packages/@orbit/data/src/source-interfaces/updatable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,25 @@ export default function updatable(Klass: SourceClass): void {
return this._enqueueRequest('update', transform);
};

proto.__update__ = function(transform: Transform): Promise<any> {
proto.__update__ = async function(transform: Transform): Promise<any> {
if (this.transformLog.contains(transform.id)) {
return Promise.resolve();
return;
}

const hints: any = {};
return fulfillInSeries(this, 'beforeUpdate', transform, hints)
.then(() => {
if (this.transformLog.contains(transform.id)) {
return Promise.resolve();
} else {
return this._update(transform, hints).then((result: any) => {
return this._transformed([transform])
.then(() => settleInSeries(this, 'update', transform, result))
.then(() => result);
});
}
})
.catch((error: Error) => {
return settleInSeries(this, 'updateFail', transform, error).then(() => {
throw error;
});
});
try {
const hints: any = {};
await fulfillInSeries(this, 'beforeUpdate', transform, hints);
if (this.transformLog.contains(transform.id)) {
return;
} else {
let result = await this._update(transform, hints);
await this._transformed([transform]);
await settleInSeries(this, 'update', transform, result);
return result;
}
} catch (error) {
await settleInSeries(this, 'updateFail', transform, error);
throw error;
}
};
}

0 comments on commit aad7e1c

Please sign in to comment.