Skip to content

Commit

Permalink
feat(UIEventObserver): add UIEventObserver#subscribeOnce
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Nov 25, 2016
1 parent 20202c8 commit 4dfe592
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/UIEventObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class UIEventObserver {
* @param {Object} target target Element Node
* @param {string} eventName event name
* @param {Function} handler event handler
* @returns {Function} unsubscribe handler
* @public
*/
subscribe(target, eventName, handler) {
Expand All @@ -31,6 +32,26 @@ export default class UIEventObserver {
target.addEventListener(eventName, handler);
}
domEventEmitter.on(handler);
return () => {
this.unsubscribe(target, eventName, handler);
}
}

/**
* registers the specified `handler` on the `target` element it's called `eventName`.
* It is called at once difference from UIEventObserver#subscribe
* @param {Object} target target Element Node
* @param {string} eventName event name
* @param {Function} handler event handler
* @returns {Function} unsubscribe handler
* @public
*/
subscribeOnce(target, eventName, handler) {
const onceHandler = (event) => {
handler(event);
this.unsubscribe(target, eventName, onceHandler);
};
return this.subscribe(target, eventName, onceHandler);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions test/UIEventObserver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ describe("UIEventObserver", () => {
eventObserver.unsubscribeAll();
});
describe("#subscribe", () => {
context("return values", () => {
it("is unsubscribe function bind arguments", () => {
const called = [];
const handler = (event) => {
called.push(event);
};
const unsubscribe = eventObserver.subscribe(window, "scroll", handler);
assert(typeof unsubscribe === "function");
assert(called.length === 0);
// unsubscribe
unsubscribe();
// fire
const event = new Event("scroll");
window.dispatchEvent(event);
assert(called.length === 0);
});
});
context("target is document.body", () => {
it("add handler is called when the event is fired", (done) => {
const handler = (event) => {
Expand Down Expand Up @@ -52,6 +69,25 @@ describe("UIEventObserver", () => {
});
});
});
describe("#subscribeOnce", () => {
context("when fire it once", () => {
it("should automatically unsubscribe", () => {
const called = [];
const handler = (event) => {
called.push(event);
};
const unsubscribe = eventObserver.subscribeOnce(window, "scroll", handler);
assert(typeof unsubscribe === "function");
assert(called.length === 0);
// fire
window.dispatchEvent(new Event("scroll"));
assert(called.length === 1);
// fire twice, but is not called
window.dispatchEvent(new Event("scroll"));
assert(called.length === 1);
});
});
});
describe("#unsubscribe", () => {
it("unSubscribe all -> Actual removeEventListener", () => {
const handler = () => {
Expand Down

0 comments on commit 4dfe592

Please sign in to comment.