Skip to content

Watcher

Defined in: watcher.ts:116

Watcher for tracking changes to observable properties

  • Automatically tracks @observable and @computed properties
  • Supports @watch and @watch.ref annotations
  • Can track nested objects
  • Provides change detection at both property and path levels
  • Can be temporarily disabled via unwatch()

get static isWatching(): boolean

Defined in: watcher.ts:159

Whether Watcher is enabled in the current transaction

boolean


static get<T>(target): Watcher

Defined in: watcher.ts:134

Get a watcher instance for the target object.

T extends object

T

Watcher

  • Returns existing instance if one exists for the target
  • Creates new instance if none exists
  • Instances are cached, and garbage collected with the target only if everything they observe is too

TypeError if the target is not an object.


static getSafe(target): Watcher | null

Defined in: watcher.ts:145

Get a watcher instance for the target object.

Same as Watcher.get but returns null instead of throwing an error.

any

Watcher | null

readonly id: string

Defined in: watcher.ts:117


get changed(): boolean

Defined in: watcher.ts:193

Whether changes have been made

boolean

@computed


get changedKeyPaths(): ReadonlySet<KeyPath>

Defined in: watcher.ts:216

The key paths that have changed

Keys of nested objects are included.

ReadonlySet<KeyPath>

@computed


get changedKeys(): ReadonlySet<KeyPath>

Defined in: watcher.ts:206

The keys that have changed

  • Does not include keys of nested objects
  • Cleared when reset() is called
  • Updated when properties are modified

ReadonlySet<KeyPath>

@computed


get changedTick(): bigint

Defined in: watcher.ts:187

The total number of changes processed

Observe this value to react to changes.

  • Incremented for each change and each affected key
  • Not affected by assumeChanged()
  • Reset to 0 when reset() is called

bigint


get nested(): ReadonlyMap<KeyPath, Watcher>

Defined in: watcher.ts:227

Nested watchers

ReadonlyMap<KeyPath, Watcher>


assumeChanged(): void

Defined in: watcher.ts:257

@action

Assume some changes have been made

It only changes changed to true and does not increment changedTick.

void


reset(): void

Defined in: watcher.ts:241

@action

Reset the changed state

void

  • Clears all changed keys
  • Resets changedTick to 0
  • Clears assumeChanged flag
  • Resets all nested watchers