class WatchHub

WatchHub is a mixin class that provides an interface to register property watchers and fire mutation notifications when property values mutate, thereby causing registered watchers to be applied. Since JS6 classes do not allow multiple inheritance, defining mixin classes requires trickery; Backdraft uses this trick. Accordingly WatchHub is actually a function with the signature WatchHub(superClass) where superClass is an optional class expression (defaults to class{}). WatchHub then returns a class that implements the interface below by extending superClass.

Public Methods

watch(name, watcher){

watch(name, watcher):[handle]

name [string]

watcher [function(newValue, oldValue, thisObject)]

Registers watcher so that it is applied when the property given by name mutates.

advise(hash):[Array of handle]

hash [Object]

Syntax sugar for Reflect.ownKeys(hash).map(key => this.watch(key, hash[key]))

destroyWatch(name):[void]

name [string | undefined]

Removes all registered watchers for associated with name; if name===undefined, then removes all registered watchers.

Protected Methods

_applyWatchers(name, privateName, newValue):[boolean]

name [string]

privateName [string | symbol]

newValue [any]

Compares this[privateName] to newValue with ===. If true, returns false to indicate no mutation. Otherwise, set this[privateName] = newValue, applies the new value, the previous value, than this to each watcher registered to name, and finally returns true to indicate a mutation occurred. Code that mutates watchable properties and requires comparison operators different than === and/or other side-effect processing should mutate the private member directly and signal watchers via _applyWatchersRaw().

_applyWatchersRaw(name, oldValue, newValue):[void]

name [string]

oldValue [any]

newValue [any]

Applies (newValue, oldValue, this) to each watcher registered to name.

Private Properties

[WatchHub.ppVariables] [Object]

A hash from event name to an array of listeners registered to that name via advise().

Static Properties

ppVariables [symbol]

Defines the symbol at which the private [ppVariables] property is located; See [ppVariables].