Skip to content

Commit

Permalink
Merge pull request #284 from Jujulego/fix/qref-reads-undefined-if-que…
Browse files Browse the repository at this point in the history
…ry-fails

Fix QRef reads undefined if query fails
  • Loading branch information
julien-capellari authored Apr 25, 2023
2 parents b314b2c + 71cb9e7 commit 534e14f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/aegis/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jujulego/aegis",
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.3",
"license": "MIT",
"author": "Julien Capellari <julien.capellari@google.com>",
"repository": {
Expand Down Expand Up @@ -33,7 +33,7 @@
"publish": "yarn npm publish --access public --tolerate-republish"
},
"dependencies": {
"@jujulego/event-tree": "^2.6.1",
"@jujulego/event-tree": "^2.6.2",
"@jujulego/utils": "^1.3.2"
},
"devDependencies": {
Expand Down
19 changes: 17 additions & 2 deletions packages/aegis/src/query/q-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export type QRefEventMap<D> = {
failed: QueryStateFailed;
};

export interface QRefReadOpts {
/**
* Should throw if current query fails ?
*/
throws?: boolean;
}

// Class
export class QRef<D> implements IObservable<QueryState<D>>, IListenable<QRefEventMap<D>> {
// Attributes
Expand Down Expand Up @@ -82,11 +89,19 @@ export class QRef<D> implements IObservable<QueryState<D>>, IListenable<QRefEven
/**
* Resolves to loaded data. Will return immediately if the current Query is done.
*/
async read(): Promise<D> {
async read(opts: QRefReadOpts = {}): Promise<D> {
let state = this._query?.state;

if (state?.status !== 'done') {
state = await waitFor(this._events, 'done');
state = await new Promise<QueryStateDone<D>>((resolve, reject) => {
const off = offGroup();

once(this._events, 'done', resolve, { off });

if (opts.throws) {
once(this._events, 'failed', ({ error }) => reject(error), { off });
}
});
}

return state.data;
Expand Down
11 changes: 10 additions & 1 deletion packages/aegis/tests/queries/q-ref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,13 @@ describe('QRef.read', () => {

await expect(qref.read()).resolves.toBe(42);
});
});

it('should reject when current query fails', async () => {
const query = new Query<number>();
qref.refresh(() => query, 'keep');

setTimeout(() => query.fail(new Error('Failed !')), 0);

await expect(qref.read({ throws: true })).rejects.toEqual(new Error('Failed !'));
});
});
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@jujulego/aegis@workspace:packages/aegis"
dependencies:
"@jujulego/event-tree": ^2.6.1
"@jujulego/event-tree": ^2.6.2
"@jujulego/flow": 1.1.0
"@jujulego/utils": ^1.3.2
"@swc/cli": 0.1.62
Expand Down Expand Up @@ -1363,10 +1363,10 @@ __metadata:
languageName: node
linkType: hard

"@jujulego/event-tree@npm:^2.6.1":
version: 2.6.1
resolution: "@jujulego/event-tree@npm:2.6.1"
checksum: 04a59f0ad2c7402e98a110918a0ebd00ea13c1a330ea4f0c7a811635dc7631d5ad1c9b76003263984f986555fd9124b48f8a1584e8b88acc0a601d21fc3409d5
"@jujulego/event-tree@npm:^2.6.2":
version: 2.6.2
resolution: "@jujulego/event-tree@npm:2.6.2"
checksum: 71ff291a6d5580f649fe37136fbb409322705734f7df0b37265a78c14f52de50d910727aae530ae93cef9cf9a5e99f4b607c8a0fd85dcc41321876f8f8610a86
languageName: node
linkType: hard

Expand Down

0 comments on commit 534e14f

Please sign in to comment.