-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(observable): Implement observable .once (#5309)
Node has a handy one-liner method on its EventEmitter called "once". It is similar to "on", but fires a single time and automatically unsubscribes.
- Loading branch information
1 parent
9d7f0e5
commit 2166d1e
Showing
3 changed files
with
78 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,61 @@ | ||
import { Observable } from "tns-core-modules/data/observable"; | ||
import { assert } from "chai"; | ||
|
||
describe("observable", () => { | ||
describe("once", () => { | ||
|
||
let observable: Observable; | ||
let handler: () => void; | ||
let callCount: number = 0; | ||
|
||
beforeEach("create handlers", () => { | ||
handler = function() { | ||
callCount++; | ||
} | ||
observable = new Observable(); | ||
observable.once("test", handler); | ||
}); | ||
afterEach("reset handlers", () => { | ||
callCount = 0; | ||
handler = null; | ||
observable = null; | ||
}); | ||
|
||
function notify() { | ||
observable.notify({ eventName: "test", object: observable }); | ||
} | ||
function notifyWrong() { | ||
observable.notify({ eventName: "test2", object: observable }); | ||
} | ||
|
||
it("fires just once", () => { | ||
notify(); | ||
notify(); | ||
assert.equal(callCount, 1, "Expected the handler to be called exactly once"); | ||
}); | ||
it("does not fire for other events", () => { | ||
notifyWrong(); | ||
assert.equal(callCount, 0, "Expected the handler to not be called, when other events fire"); | ||
}); | ||
}); | ||
|
||
describe("once", () => { | ||
it("fire once when fired recursively", () => { | ||
const observable = new Observable(); | ||
let callCount1 = 0; | ||
let callCount2 = 0; | ||
const handler2 = function () { | ||
callCount2++; | ||
} | ||
const handler1 = function () { | ||
callCount1++; | ||
observable.once("test", handler2); | ||
observable.notify({ eventName: "test", object: observable }); | ||
} | ||
observable.once("test", handler1); | ||
observable.notify({ eventName: "test", object: observable }); | ||
assert.equal(callCount1, 1, "Expected the first handler to unsubscribe before being fired and to notify just once"); | ||
assert.equal(callCount2, 1, "Expected the second handler to be fired once when recursively notified by the first handler"); | ||
}); | ||
}); | ||
}); |