Created
December 21, 2021 03:51
-
-
Save alexeyraspopov/a0dae19c04330164cf6e6cef27557e92 to your computer and use it in GitHub Desktop.
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
function Observer() { | |
let head = { prev: null, next: null } | |
let tail = { prev: null, next: null } | |
head.next = tail | |
tail.prev = head | |
function subscribe(callback) { | |
let node = { callback, prev: tail.prev, next: tail } | |
tail.prev.next = node | |
tail.prev = node | |
return { | |
dispose() { | |
node.prev.next = node.next | |
node.next.prev = node.prev | |
} | |
} | |
} | |
function publish(value) { | |
let cursor = head | |
while ((cursor = cursor.next) !== tail) { | |
let callback = cursor.callback | |
callback(value) | |
} | |
} | |
return { subscribe, publish } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment