Comparing version 0.0.6 to 0.0.7
@@ -36,10 +36,28 @@ "use strict"; | ||
} | ||
isActivelyListening = false; | ||
_isActivelyListening = false; | ||
/** | ||
* Whether this scheduler is attached and actively listening to its parents. | ||
* @public | ||
*/ | ||
get isActivelyListening() { | ||
return this._isActivelyListening; | ||
} | ||
/** @internal */ | ||
lastTraversedEpoch = import_constants.GLOBAL_START_EPOCH; | ||
lastReactedEpoch = import_constants.GLOBAL_START_EPOCH; | ||
scheduleCount = 0; | ||
_scheduleCount = 0; | ||
/** | ||
* The number of times this effect has been scheduled. | ||
* @public | ||
*/ | ||
get scheduleCount() { | ||
return this._scheduleCount; | ||
} | ||
/** @internal */ | ||
parentEpochs = []; | ||
/** @internal */ | ||
parents = []; | ||
/** @internal */ | ||
maybeScheduleEffect() { | ||
if (!this.isActivelyListening) | ||
if (!this._isActivelyListening) | ||
return; | ||
@@ -52,3 +70,3 @@ if (this.lastReactedEpoch === import_transactions.globalEpoch) | ||
} | ||
this.scheduleCount++; | ||
this._scheduleCount++; | ||
if (this.scheduleEffect) { | ||
@@ -61,3 +79,3 @@ this.scheduleEffect(this.maybeExecute); | ||
maybeExecute = () => { | ||
if (!this.isActivelyListening) | ||
if (!this._isActivelyListening) | ||
return; | ||
@@ -73,3 +91,3 @@ this.execute(); | ||
attach() { | ||
this.isActivelyListening = true; | ||
this._isActivelyListening = true; | ||
for (let i = 0, n = this.parents.length; i < n; i++) { | ||
@@ -84,3 +102,3 @@ (0, import_helpers.attach)(this.parents[i], this); | ||
detach() { | ||
this.isActivelyListening = false; | ||
this._isActivelyListening = false; | ||
for (let i = 0, n = this.parents.length; i < n; i++) { | ||
@@ -87,0 +105,0 @@ (0, import_helpers.detach)(this.parents[i], this); |
@@ -132,5 +132,14 @@ /** | ||
*/ | ||
export declare interface Computed<Value, Diff = unknown> extends Signal<Value, Diff>, ComputedChild { | ||
export declare interface Computed<Value, Diff = unknown> extends Signal<Value, Diff> { | ||
/** | ||
* Whether this computed child is involved in an actively-running effect graph. | ||
* @public | ||
*/ | ||
readonly isActivelyListening: boolean; | ||
/* Excluded from this release type: parents */ | ||
/* Excluded from this release type: parentEpochs */ | ||
} | ||
/* Excluded from this release type: _Computed */ | ||
/** | ||
@@ -189,14 +198,2 @@ * Creates a computed signal. | ||
/** @public */ | ||
declare interface ComputedChild { | ||
/* Excluded from this release type: parents */ | ||
/* Excluded from this release type: parentEpochs */ | ||
/** | ||
* Whether this computed child is involved in an actively-running effect graph. | ||
* @public | ||
*/ | ||
isActivelyListening: boolean; | ||
/* Excluded from this release type: lastTraversedEpoch */ | ||
} | ||
/** | ||
@@ -258,14 +255,24 @@ * Computes the diff between the previous and current value. | ||
*/ | ||
export declare class EffectScheduler<Result> implements ReactingChild { | ||
export declare class EffectScheduler<Result> { | ||
readonly name: string; | ||
private readonly runEffect; | ||
private readonly scheduleEffect?; | ||
isActivelyListening: boolean; | ||
lastTraversedEpoch: number; | ||
lastReactedEpoch: number; | ||
scheduleCount: number; | ||
parentEpochs: number[]; | ||
parents: Signal<any, any>[]; | ||
private _isActivelyListening; | ||
/** | ||
* Whether this scheduler is attached and actively listening to its parents. | ||
* @public | ||
*/ | ||
get isActivelyListening(): boolean; | ||
/* Excluded from this release type: lastTraversedEpoch */ | ||
private lastReactedEpoch; | ||
private _scheduleCount; | ||
/** | ||
* The number of times this effect has been scheduled. | ||
* @public | ||
*/ | ||
get scheduleCount(): number; | ||
/* Excluded from this release type: parentEpochs */ | ||
/* Excluded from this release type: parents */ | ||
constructor(name: string, runEffect: (lastReactedEpoch: number) => Result, scheduleEffect?: ((execute: () => void) => void) | undefined); | ||
maybeScheduleEffect(): void; | ||
/* Excluded from this release type: maybeScheduleEffect */ | ||
private maybeExecute; | ||
@@ -322,2 +329,4 @@ /** | ||
/* Excluded from this release type: HistoryBuffer */ | ||
/** | ||
@@ -355,2 +364,4 @@ * Returns true if the given value is an [[Atom]]. | ||
declare type RangeTuple<Diff> = [fromEpoch: number, toEpoch: number, diff: Diff]; | ||
/** | ||
@@ -389,13 +400,2 @@ * Starts a new effect scheduler, executing the effect immediately. | ||
/** | ||
* A child that can run effects if necessary. | ||
* @public | ||
*/ | ||
declare interface ReactingChild extends ComputedChild { | ||
/** | ||
* This should check whether the child needs to run any effects, and schedule them if necessary. | ||
*/ | ||
maybeScheduleEffect(): void; | ||
} | ||
/** | ||
* The reactor is a simple interface for starting and stopping an [[EffectScheduler]]. | ||
@@ -402,0 +402,0 @@ * |
@@ -5,3 +5,3 @@ { | ||
"description": "A tiny little drawing reactive state management library.", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"author": "tldraw GB Ltd.", | ||
@@ -40,3 +40,3 @@ "homepage": "https://tldraw.dev", | ||
}, | ||
"gitHead": "3973df0f68f4205663eb7b655f5e27ac84c03dce", | ||
"gitHead": "c7db8617f56d5b11c04265718010422b98c12486", | ||
"module": "dist/index.mjs", | ||
@@ -43,0 +43,0 @@ "source": "src/index.ts", |
/* eslint-disable prefer-rest-params */ | ||
import { Child, ComputedChild, ComputeDiff, RESET_VALUE, Signal } from './types' | ||
import { Child, ComputeDiff, RESET_VALUE, Signal } from './types' | ||
@@ -108,4 +108,15 @@ import { maybeCaptureParent, startCapturingParents, stopCapturingParents } from './capture' | ||
*/ | ||
export interface Computed<Value, Diff = unknown> extends Signal<Value, Diff>, ComputedChild {} | ||
export interface Computed<Value, Diff = unknown> extends Signal<Value, Diff> { | ||
/** | ||
* Whether this computed child is involved in an actively-running effect graph. | ||
* @public | ||
*/ | ||
readonly isActivelyListening: boolean | ||
/** @internal */ | ||
readonly parents: Signal<any, any>[] | ||
/** @internal */ | ||
readonly parentEpochs: number[] | ||
} | ||
/** | ||
@@ -112,0 +123,0 @@ * @internal |
@@ -5,3 +5,3 @@ import { startCapturingParents, stopCapturingParents } from './capture' | ||
import { globalEpoch } from './transactions' | ||
import { ReactingChild, Reactor, Signal } from './types' | ||
import { Reactor, Signal } from './types' | ||
@@ -25,9 +25,28 @@ /** | ||
*/ | ||
export class EffectScheduler<Result> implements ReactingChild { | ||
isActivelyListening = false | ||
export class EffectScheduler<Result> { | ||
private _isActivelyListening = false | ||
/** | ||
* Whether this scheduler is attached and actively listening to its parents. | ||
* @public | ||
*/ | ||
get isActivelyListening() { | ||
return this._isActivelyListening | ||
} | ||
/** @internal */ | ||
lastTraversedEpoch = GLOBAL_START_EPOCH | ||
lastReactedEpoch = GLOBAL_START_EPOCH | ||
scheduleCount = 0 | ||
private lastReactedEpoch = GLOBAL_START_EPOCH | ||
private _scheduleCount = 0 | ||
/** | ||
* The number of times this effect has been scheduled. | ||
* @public | ||
*/ | ||
get scheduleCount() { | ||
return this._scheduleCount | ||
} | ||
/** @internal */ | ||
parentEpochs: number[] = [] | ||
/** @internal */ | ||
parents: Signal<any, any>[] = [] | ||
@@ -41,5 +60,6 @@ | ||
/** @internal */ | ||
maybeScheduleEffect() { | ||
// bail out if we have been cancelled by another effect | ||
if (!this.isActivelyListening) return | ||
if (!this._isActivelyListening) return | ||
// bail out if no atoms have changed since the last time we ran this effect | ||
@@ -55,3 +75,3 @@ if (this.lastReactedEpoch === globalEpoch) return | ||
this.scheduleCount++ | ||
this._scheduleCount++ | ||
if (this.scheduleEffect) { | ||
@@ -68,3 +88,3 @@ // if the efect should be deferred (e.g. until a react render), do so | ||
// bail out if we have been detached before this runs | ||
if (!this.isActivelyListening) return | ||
if (!this._isActivelyListening) return | ||
this.execute() | ||
@@ -80,3 +100,3 @@ } | ||
attach() { | ||
this.isActivelyListening = true | ||
this._isActivelyListening = true | ||
for (let i = 0, n = this.parents.length; i < n; i++) { | ||
@@ -92,3 +112,3 @@ attach(this.parents[i], this) | ||
detach() { | ||
this.isActivelyListening = false | ||
this._isActivelyListening = false | ||
for (let i = 0, n = this.parents.length; i < n; i++) { | ||
@@ -95,0 +115,0 @@ detach(this.parents[i], this) |
import { _Atom } from './Atom' | ||
import { GLOBAL_START_EPOCH } from './constants' | ||
import { Child, ReactingChild, Signal } from './types' | ||
import { EffectScheduler } from './EffectScheduler' | ||
import { Child, Signal } from './types' | ||
@@ -82,3 +83,3 @@ // The current epoch (global to all atoms). | ||
// Collect all of the visited reactors. | ||
const reactors = new Set<ReactingChild>() | ||
const reactors = new Set<EffectScheduler<unknown>>() | ||
@@ -85,0 +86,0 @@ // Visit each descendant of the atom, collecting reactors. |
import { ArraySet } from './ArraySet' | ||
import { _Computed } from './Computed' | ||
import { EffectScheduler } from './EffectScheduler' | ||
@@ -51,39 +52,4 @@ | ||
/** @public */ | ||
export interface ComputedChild { | ||
/** | ||
* Any signals this computed child dereferenced the last time it was computed. | ||
* @internal | ||
*/ | ||
parents: Signal<any, any>[] | ||
/** | ||
* The matching epochs of the parents array when this child was last computed. | ||
* @internal | ||
*/ | ||
parentEpochs: number[] | ||
/** | ||
* Whether this computed child is involved in an actively-running effect graph. | ||
* @public | ||
*/ | ||
isActivelyListening: boolean | ||
/** | ||
* The epoch when this child was last traversed during the 'commit' phase of a transation (or calling [[Atom.set]] outside of a transaction). | ||
* @internal | ||
*/ | ||
lastTraversedEpoch: number | ||
} | ||
/** | ||
* A child that can run effects if necessary. | ||
* @public | ||
*/ | ||
export interface ReactingChild extends ComputedChild { | ||
/** | ||
* This should check whether the child needs to run any effects, and schedule them if necessary. | ||
*/ | ||
maybeScheduleEffect(): void | ||
} | ||
/** @internal */ | ||
export type Child = ReactingChild | ComputedChild | ||
export type Child = EffectScheduler<any> | _Computed<any> | ||
@@ -90,0 +56,0 @@ /** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
300045
5571