Skip to content

Commit

Permalink
Added unbindAll method that removes all listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
duart38 committed Sep 4, 2020
1 parent d5062c2 commit 62a8f39
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Observe.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ System.register("Observe", [], function (exports_1, context_1) {
this.currentEvent = new CustomEvent("");
this.maxHistorySize = 1000;
this.lastNestedBound = () => { };
this.boundCallbacks = [];
this.eventID = "Observed_" +
crypto.getRandomValues(new Uint32Array(2)).toString().replace(",", "_");
this.history.push(defaultValue);
Expand All @@ -127,10 +128,19 @@ System.register("Observe", [], function (exports_1, context_1) {
bind(callback, once = false) {
let func = (e) => callback(e.detail);
addEventListener(this.eventID, func, { once });
this.boundCallbacks.push(func);
return func;
}
unBind(callback) {
removeEventListener(this.eventID, callback);
this.boundCallbacks.splice(this.boundCallbacks.findIndex((x) => x === callback));
return this;
}
unBindAll() {
this.boundCallbacks.forEach(cb => {
removeEventListener(this.eventID, cb);
});
this.boundCallbacks = [];
return this;
}
setValue(...value) {
Expand Down
14 changes: 14 additions & 0 deletions Observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class Observe<T> {
*/
public maxHistorySize = 1000;
private lastNestedBound: EventListener | EventListenerObject = () => {};
public boundCallbacks: Array<(e: Event) =>void> = [];
/**
* Observes a value for changes and updates all the listeners. also keeps a track of the change history.
* @param defaultValue
Expand Down Expand Up @@ -54,6 +55,7 @@ export default class Observe<T> {
): (e: Event) => void {
let func = (e: Event) => callback((<CustomEvent> e).detail);
addEventListener(this.eventID, func, { once });
this.boundCallbacks.push(func);
return func;
}
/**
Expand All @@ -62,6 +64,18 @@ export default class Observe<T> {
*/
public unBind(callback: EventListener | EventListenerObject): this {
removeEventListener(this.eventID, callback);
this.boundCallbacks.splice(this.boundCallbacks.findIndex((x)=>x === callback));
return this;
}

/**
* Unbinds ALL previously bound EventListener or EventListenerObject.
*/
public unBindAll(): this{
this.boundCallbacks.forEach(cb => {
removeEventListener(this.eventID, cb);
});
this.boundCallbacks = [];
return this;
}

Expand Down
13 changes: 13 additions & 0 deletions sandbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Observe from "./Observe.ts";

let obs = new Observe("initial value");

function test(d: any) {
console.log("hello " + d);
}

let first = obs.bind(test);
let second = obs.bind((x)=>console.log("----------"))
obs.unBind(first);

obs.setValue("test");
8 changes: 8 additions & 0 deletions test_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,13 @@ export function testBaseClass() {
val.setValue("lorem");
assertEquals(val.stop(), val);
});
Deno.test("unbinding All removes all listeners", (): void => {
let val = new Observe("init");
let t = 0;
val.bind(()=>t++);
val.bind(()=>t++);
val.unBindAll();
assertEquals(t, 0);
});
console.log("\n[+] -- DONE: Testing main class -- \n");
}
8 changes: 8 additions & 0 deletions test_bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,13 @@ export function testBundled() {
val.setValue("lorem");
assertEquals(val.stop(), val);
});
Deno.test("unbinding All removes all listeners", (): void => {
let val = new Observe("init");
let t = 0;
val.bind(()=>t++);
val.bind(()=>t++);
val.unBindAll();
assertEquals(t, 0);
});
console.log("\n[+] -- DONE: Testing Bundled version -- \n");
}

0 comments on commit 62a8f39

Please sign in to comment.