Skip to content

Commit

Permalink
Merge pull request #8 from salsify/allow-no-callback
Browse files Browse the repository at this point in the history
Allow milestones with no callback
  • Loading branch information
dfreeman authored May 7, 2018
2 parents 62b13b9 + 3c187e9 commit d84e2f3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
9 changes: 6 additions & 3 deletions addon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ export function advanceTo(name: string): MilestoneTarget {
* When not activated, code wrapped in a milestone is immediately invoked as though
* the wrapper weren't there at all.
*/
export function milestone<T extends PromiseLike<any>>(name: string, callback: () => T): T {
export function milestone<T extends PromiseLike<any>>(name: string, callback: () => T): T;
export function milestone(name: string): PromiseLike<void>;
export function milestone(name: string, callback?: () => any): PromiseLike<any> {
let coordinator = CoordinatorImpl.forMilestone(name);
let action = callback || (() => Promise.resolve());
if (coordinator) {
debugActive('reached active milestone %s', name);
return coordinator._milestoneReached(name, callback);
return coordinator._milestoneReached(name, action);
} else {
debugInactive('skipping inactive milestone %s', name);
return callback();
return action();
}
}

Expand Down
28 changes: 27 additions & 1 deletion tests/integration/milestones-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,25 @@ module('Integration | milestones', function(hooks) {
});

module('with no milestones active', function() {
test('milestones are inert', async function(assert) {
test('milestones with callbacks are inert', async function(assert) {
let { first, second } = await program();
assert.equal(location, 'two-completed');
assert.equal(first, 1);
assert.equal(second, 2);
});

test('milestones without callbacks are inert', async function(assert) {
let program = async () => {
let first = await milestone('one');
let second = await milestone('two');
return { first, second };
};

assert.deepEqual(await program(), {
first: undefined,
second: undefined,
});
});
});

module('with milestones active', function(hooks) {
Expand Down Expand Up @@ -127,6 +140,19 @@ module('Integration | milestones', function(hooks) {
assert.equal(await program(), boom);
});

test('with no callback', async function(assert) {
let program = async () => {
let first = await milestone('one');
let second = await milestone('two');
return { first, second };
};

let programPromise = program();
await advanceTo('one').andContinue();
await advanceTo('two').andContinue();
assert.deepEqual(await programPromise, { first: undefined, second: undefined });
});

test('stepping through each location', async function(assert) {
let programPromise = program();

Expand Down

0 comments on commit d84e2f3

Please sign in to comment.