Skip to content

Commit

Permalink
fix: add hook listener props to IObservable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Dec 8, 2018
1 parent 7d42f47 commit 009a482
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/api/become-observed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ function interceptHook(hook: "onBecomeObserved" | "onBecomeUnobserved", thing, a
const atom: IObservable =
typeof arg2 === "string" ? getAtom(thing, arg2) : (getAtom(thing) as any)
const cb = typeof arg2 === "string" ? arg3 : arg2
const listenersKey = `${hook}Listeners`
const listenersKey = `${hook}Listeners` as
| "onBecomeObservedListeners"
| "onBecomeUnobservedListeners"

if (!atom[listenersKey]) {
atom[listenersKey] = new Set<Lambda>([cb])
if (atom[listenersKey]) {
atom[listenersKey]!.add(cb)
} else {
;(atom[listenersKey] as Set<Lambda>).add(cb)
atom[listenersKey] = new Set<Lambda>([cb])
}

const orig = atom[hook]
if (typeof orig !== "function")
return fail(process.env.NODE_ENV !== "production" && "Not an atom that can be (un)observed")

return function() {
const hookListeners = atom[listenersKey] as Set<Lambda> | undefined
const hookListeners = atom[listenersKey]
if (hookListeners) {
hookListeners.delete(cb)
if (hookListeners.size === 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/observable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Lambda,
ComputedValue,
IDependencyTree,
IDerivation,
Expand Down Expand Up @@ -31,6 +32,9 @@ export interface IObservable extends IDepTreeNode {

onBecomeUnobserved(): void
onBecomeObserved(): void

onBecomeUnobservedListeners: Set<Lambda> | undefined
onBecomeObservedListeners: Set<Lambda> | undefined
}

export function hasObservers(observable: IObservable): boolean {
Expand Down

0 comments on commit 009a482

Please sign in to comment.