Skip to content

Instantly share code, notes, and snippets.

@alexeyraspopov
Created December 21, 2021 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeyraspopov/a0dae19c04330164cf6e6cef27557e92 to your computer and use it in GitHub Desktop.
Save alexeyraspopov/a0dae19c04330164cf6e6cef27557e92 to your computer and use it in GitHub Desktop.
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