Skip to content

Commit

Permalink
feat(Resolvable): Add .value(): returns value (WAIT) or promise (NO…
Browse files Browse the repository at this point in the history
…WAIT)
  • Loading branch information
christopherthielen committed Jun 17, 2017
1 parent 5b11ce0 commit 8769449
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/resolve/resolvable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ export class Resolvable implements ResolvableLiteral {
return this.promise || this.resolve(resolveContext, trans);
}

/**
* Gets the result of the resolvable.
*
* The return value depends on `policy.async`:
*
* * `WAIT`: Returns the resolved value of the promise, or undefined if it has not yet resolved
* * `NOWAIT`: Returns the promise for the result
*/
result() {
return this.policy.async === 'NOWAIT' ? this.promise : this.data;
}

toString() {
return `Resolvable(token: ${stringify(this.token)}, requires: [${this.deps.map(stringify)}])`;
}
Expand Down
52 changes: 50 additions & 2 deletions test/resolveSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ describe('Resolvables system:', function () {
});
});

describe('NOWAIT Resolve Policy', () => {
it('should allow a transition to complete before the resolve is settled', async (done) => {
describe('Resolve Policy', () => {
it('NOWAIT should allow a transition to complete before the resolve is settled', async (done) => {
let resolve, resolvePromise = new Promise(_resolve => { resolve = _resolve; });

$registry.register({
Expand Down Expand Up @@ -461,6 +461,54 @@ describe('Resolvables system:', function () {

$state.go('nowait');
});

fit('NOWAIT should return a promise from .result()', async (done) => {
let promiseResolveFn, resolvePromise = new Promise(resolve => { promiseResolveFn = resolve; });

$registry.register({
name: 'nowait',
resolve: [
{ token: 'nowait', policy: { async: 'NOWAIT' }, resolveFn: () => resolvePromise },
{ token: 'wait', policy: { async: 'WAIT' }, resolveFn: () => new Promise(resolve => resolve('should wait')) },
],
});

$transitions.onSuccess({ }, trans => {
let resolvable = tail(trans.treeChanges('to')).resolvables[0];
let result = resolvable.result();
expect(result instanceof Promise).toBeTruthy();
result.then(val => {
expect(val).toBe('foobar');
done();
});

promiseResolveFn('foobar');
});

$state.go('nowait');
});

fit('WAIT should return the resolved value from .result()', async (done) => {
let promiseResolveFn, resolvePromise = new Promise(resolve => { promiseResolveFn = resolve; });

$registry.register({
name: 'nowait',
resolve: [
{ token: 'nowait', policy: { async: 'NOWAIT' }, resolveFn: () => resolvePromise },
{ token: 'wait', policy: { async: 'WAIT' }, resolveFn: () => new Promise(resolve => resolve('should wait')) },
],
});

$transitions.onSuccess({ }, trans => {
let resolvable = tail(trans.treeChanges('to')).resolvables[1];
let result = resolvable.result();
expect(result).toBe('should wait');
done();
});

$state.go('nowait');
});

});
});

Expand Down

0 comments on commit 8769449

Please sign in to comment.