-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
describe('ko.when', function() { | ||
it('Runs callback when predicate function becomes true, but only once', function() { | ||
var x = ko.observable(3), | ||
called = 0; | ||
|
||
ko.when(function () { return x() === 4; }, function () { called++; }); | ||
|
||
x(5); | ||
expect(called).toBe(0); | ||
expect(x.getSubscriptionsCount()).toBe(1); | ||
|
||
x(4); | ||
expect(called).toBe(1); | ||
expect(x.getSubscriptionsCount()).toBe(0); | ||
|
||
x(3); | ||
x(4); | ||
expect(called).toBe(1); | ||
expect(x.getSubscriptionsCount()).toBe(0); | ||
}); | ||
|
||
it('Runs callback if predicate function is already true', function() { | ||
var x = ko.observable(4), | ||
called = 0; | ||
|
||
ko.when(function () { return x() === 4; }, function () { called++; }); | ||
|
||
expect(called).toBe(1); | ||
expect(x.getSubscriptionsCount()).toBe(0); | ||
|
||
x(3); | ||
x(4); | ||
expect(called).toBe(1); | ||
expect(x.getSubscriptionsCount()).toBe(0); | ||
}); | ||
|
||
it('Accepts an observable as the predicate', function() { | ||
var x = ko.observable(false), | ||
called = 0; | ||
|
||
ko.when(x, function () { called++; }); | ||
|
||
expect(called).toBe(0); | ||
expect(x.getSubscriptionsCount()).toBe(1); | ||
|
||
x(true); | ||
expect(called).toBe(1); | ||
expect(x.getSubscriptionsCount()).toBe(0); | ||
}); | ||
|
||
it('Returns an object with a dispose function that cancels the notification', function() { | ||
var x = ko.observable(false), | ||
called = 0; | ||
|
||
var handle = ko.when(x, function () { called++; }); | ||
|
||
expect(called).toBe(0); | ||
expect(x.getSubscriptionsCount()).toBe(1); | ||
|
||
handle.dispose(); | ||
expect(x.getSubscriptionsCount()).toBe(0); | ||
|
||
x(true); | ||
expect(called).toBe(0); | ||
}); | ||
|
||
it('Will call callback function only once even if value is updated during callback', function() { | ||
var x = ko.observable(false), | ||
called = 0; | ||
|
||
ko.when(x, function () { | ||
called++; | ||
x(false); | ||
x(true); | ||
}); | ||
|
||
expect(called).toBe(0); | ||
expect(x.getSubscriptionsCount()).toBe(1); | ||
|
||
x(true); | ||
expect(called).toBe(1); | ||
}); | ||
|
||
it('Should be able to specify a \'this\' pointer for the callback', function () { | ||
var model = { | ||
someProperty: 123, | ||
myCallback: function () { expect(this.someProperty).toEqual(123); } | ||
}; | ||
ko.when(ko.observable(true), model.myCallback, model); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
ko.when = function(predicate, callback, context) { | ||
var observable = ko.pureComputed(predicate).extend({notify:'always'}); | ||
var subscription = observable.subscribe(function(value) { | ||
if (value) { | ||
subscription.dispose(); | ||
callback.call(context); | ||
} | ||
}); | ||
// In case the initial value is true, process it right away | ||
observable['notifySubscribers'](observable.peek()); | ||
|
||
return subscription; | ||
}; | ||
|
||
ko.exportSymbol('when', ko.when); |
4540c88
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome.
And why not getting the predicate returned value as a callback 1st parameter? Consider ...
naïve question: why is it not necessary to dispose the observable built in ko.when?
4540c88
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to that. Would you like to submit a pull request?
4540c88
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once the subscription is disposed, the
pureComputed
will go to sleep.