Skip to content

Commit

Permalink
Merge pull request #39 from mattmccray/master
Browse files Browse the repository at this point in the history
Updated .waitFor() to allow passing store instances instead of tokens.
  • Loading branch information
goatslacker committed Mar 6, 2015
2 parents 02038ef + 5e9fe47 commit 05eb618
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
16 changes: 13 additions & 3 deletions dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,21 @@ var StoreMixin = {
});
},

waitFor: function waitFor(tokens) {
if (!tokens) {
waitFor: function waitFor(sources) {
if (!sources) {
throw new ReferenceError("Dispatch tokens not provided");
}
tokens = Array.isArray(tokens) ? tokens : [tokens];

if (arguments.length === 1) {
sources = Array.isArray(sources) ? sources : [sources];
} else {
sources = Array.prototype.slice.call(arguments);
}

var tokens = sources.map(function (source) {
return source.dispatchToken || source;
});

this.dispatcher.waitFor(tokens);
}
};
Expand Down
16 changes: 13 additions & 3 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,21 @@ var StoreMixin = {
});
},

waitFor: function waitFor(tokens) {
if (!tokens) {
waitFor: function waitFor(sources) {
if (!sources) {
throw new ReferenceError("Dispatch tokens not provided");
}
tokens = Array.isArray(tokens) ? tokens : [tokens];

if (arguments.length === 1) {
sources = Array.isArray(sources) ? sources : [sources];
} else {
sources = Array.prototype.slice.call(arguments);
}

var tokens = sources.map(function (source) {
return source.dispatchToken || source;
});

this.dispatcher.waitFor(tokens);
}
};
Expand Down
16 changes: 13 additions & 3 deletions src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,21 @@ const StoreMixin = {
})
},

waitFor(tokens) {
if (!tokens) {
waitFor(sources) {
if (!sources) {
throw new ReferenceError('Dispatch tokens not provided')
}
tokens = Array.isArray(tokens) ? tokens : [tokens]

if (arguments.length === 1) {
sources = Array.isArray(sources) ? sources : [sources]
} else {
sources = Array.prototype.slice.call(arguments)
}

let tokens = sources.map((source) => {
return source.dispatchToken || source
})

this.dispatcher.waitFor(tokens)
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class MyActions {
updateThree(a, b, c) {
this.dispatch({ a, b, c })
}

}

let myActions = {}
Expand Down Expand Up @@ -208,6 +209,19 @@ class LifeCycleStore {

let lifecycleStore = alt.createStore(LifeCycleStore)

class ThirdStore {
constructor() {
this.bindAction(myActions.updateName, this.onUpdateName)
}

onUpdateName() {
this.waitFor(myStore, secondStore) // Not referencing dispatchToken!
this.name = secondStore.getState().name + '3'
}
}

let thirdStore = alt.createStore(ThirdStore)

// Alt instances...

class AltInstance extends Alt {
Expand Down Expand Up @@ -345,6 +359,7 @@ let tests = {
assert.equal(myStore.getState().name, 'bear', 'action was called, state was updated properly')
assert.equal(myStore.getState().calledInternal, false, 'internal method has not been called')
assert.equal(secondStore.getState().name, 'bear', 'second store gets its value from myStore')
assert.equal(thirdStore.getState().name, 'bear3', 'third store gets its value from secondStore, adds 3')
},

'calling internal methods'() {
Expand Down

0 comments on commit 05eb618

Please sign in to comment.