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
1 parent
888d065
commit 3134c8b
Showing
6 changed files
with
70 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
export { assertEquals, assertThrowsAsync } from "testing/asserts.ts"; | ||
/** | ||
* assert() - Expects a boolean value, throws if the value is false. | ||
* assertEquals() - Uses the equal comparison and throws if the actual and expected are not equal. | ||
* assertStrictEq() - Compares actual and expected strictly, therefore for non-primitives the values must reference the same | ||
*/ | ||
|
||
export { assert, assertEquals, assertStrictEq } from "testing/asserts.ts"; |
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,18 @@ | ||
// https://www.sitepoint.com/javascript-design-patterns-observer-pattern/ | ||
export class EventObserver { | ||
constructor(public observers = [] as any) {} | ||
|
||
subscribe(fn: any) { | ||
this.observers.push(fn); | ||
} | ||
|
||
unsubscribe(fn: any) { | ||
this.observers = this.observers.filter( | ||
(subscriber: any) => subscriber !== fn | ||
); | ||
} | ||
|
||
broadcast(data: any) { | ||
this.observers.forEach((subscriber: any) => subscriber(data)); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,40 @@ | ||
import { assertEquals, assert } from "../deps.ts"; | ||
import { EventObserver } from "../src/EventObserver.ts"; | ||
/** | ||
* SUBSCRIBE UNSUBSCRIBE | ||
*/ | ||
Deno.test({ | ||
name: "SUBSCRIBE UNSUBSCRIBE", | ||
fn(): void { | ||
// Arrange | ||
const observer = new EventObserver(); | ||
const fn = () => {}; | ||
observer.subscribe(fn); | ||
|
||
// Act | ||
observer.unsubscribe(fn); | ||
|
||
// Assert | ||
assertEquals(observer.observers.length, 0); | ||
} | ||
}); | ||
|
||
/** | ||
* BROADCAST | ||
*/ | ||
Deno.test({ | ||
name: "BROADCAST", | ||
fn(): void { | ||
// Arrange | ||
const observer = new EventObserver(); | ||
let subscriberHasBeenCalled = false; | ||
const fn = data => (subscriberHasBeenCalled = data); | ||
observer.subscribe(fn); | ||
|
||
// Act | ||
observer.broadcast(true); | ||
|
||
// Assert | ||
assert(subscriberHasBeenCalled); | ||
} | ||
}); |
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