Skip to content

Commit

Permalink
feat: add observe method to Observable class
Browse files Browse the repository at this point in the history
Create an observer using less boilerplate than previously possible.
  • Loading branch information
aleclarson committed Jul 19, 2020
1 parent f1c1a69 commit aa94cb7
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ export class Observable<T extends object = any> extends Map<
}
return observers
}

/** Create an observer for the given key. */
observe(key: ObservedKey, onChange: (change: Change) => void) {
const observers = this.get(key)
const observer = { onChange, dispose: () => observers.delete(observer) }
observers.add(observer)
return observer
}
}

/** Mutable state with an associated observable */
Expand Down

0 comments on commit aa94cb7

Please sign in to comment.