@flowgram.ai/reactive 0.1.0-alpha.6 → 0.1.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.js +6 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/index.js +6 -4
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/esm/index.js
CHANGED
|
@@ -100,7 +100,7 @@ var Tracker;
|
|
|
100
100
|
}
|
|
101
101
|
Tracker2.getCurrentComputation = getCurrentComputation;
|
|
102
102
|
function autorun(f, options) {
|
|
103
|
-
var c = new Computation2(f, _currentComputation, options
|
|
103
|
+
var c = new Computation2(f, _currentComputation, options == null ? void 0 : options.onError);
|
|
104
104
|
if (isActive())
|
|
105
105
|
Tracker2.onInvalidate(function() {
|
|
106
106
|
c.stop();
|
|
@@ -309,7 +309,7 @@ var ReactiveBaseState = class {
|
|
|
309
309
|
this._dep = new Tracker.Dependency();
|
|
310
310
|
this._isEqual = (a, b) => a == b;
|
|
311
311
|
this._value = initialValue;
|
|
312
|
-
if (opts
|
|
312
|
+
if (opts == null ? void 0 : opts.isEqual) {
|
|
313
313
|
this._isEqual = opts.isEqual;
|
|
314
314
|
}
|
|
315
315
|
}
|
|
@@ -481,10 +481,11 @@ import { useRef, useEffect as useEffect2 } from "react";
|
|
|
481
481
|
import { useRefresh as useRefresh2 } from "@flowgram.ai/utils";
|
|
482
482
|
function observe(fc) {
|
|
483
483
|
return function ReactiveObserver(props) {
|
|
484
|
+
var _a;
|
|
484
485
|
const childrenRef = useRef();
|
|
485
486
|
const computationRef = useRef();
|
|
486
487
|
const refresh = useRefresh2();
|
|
487
|
-
computationRef.current
|
|
488
|
+
(_a = computationRef.current) == null ? void 0 : _a.stop();
|
|
488
489
|
computationRef.current = new Tracker.Computation((c) => {
|
|
489
490
|
if (c.firstRun) {
|
|
490
491
|
childrenRef.current = fc(props);
|
|
@@ -494,7 +495,8 @@ function observe(fc) {
|
|
|
494
495
|
});
|
|
495
496
|
useEffect2(
|
|
496
497
|
() => () => {
|
|
497
|
-
|
|
498
|
+
var _a2;
|
|
499
|
+
(_a2 = computationRef.current) == null ? void 0 : _a2.stop();
|
|
498
500
|
},
|
|
499
501
|
[]
|
|
500
502
|
);
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/core/tracker.ts","../../src/core/reactive-base-state.ts","../../src/utils/create-proxy.ts","../../src/core/reactive-state.ts","../../src/hooks/use-reactive-state.ts","../../src/hooks/use-observe.ts","../../src/hooks/use-readonly-reactive-state.ts","../../src/react/observe.tsx","../../src/index.ts"],"sourcesContent":["/**\n * Fork from: https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js\n */\ntype ICallback<ARG = void, RET = void> = (arg: ARG) => RET;\n\n/**\n * Tracker 是一套 响应式依赖追踪 库,来源于 Meteor.Tracker\n * https://docs.meteor.com/api/Tracker.html#tracker-autorun-and-async-callbacks\n * https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js\n *\n * 相关论文:https://dl.acm.org/doi/fullHtml/10.1145/3184558.3185978\n */\nexport namespace Tracker {\n const _pendingComputations: Computation[] = [];\n const _afterFlushCallbacks: ICallback[] = [];\n // `true` if a Tracker.flush is scheduled, or if we are in Tracker.flush now\n let _willFlush = false;\n // `true` if we are in Tracker.flush now\n let _inFlush = false;\n // `true` if we are computing a computation now, either first time\n // or recompute. This matches Tracker.active unless we are inside\n // Tracker.nonreactive, which nullfies currentComputation even though\n // an enclosing computation may still be running.\n let _inCompute = false;\n let _currentComputation: Computation | undefined = undefined;\n // `true` if the `_throwFirstError` option was passed in to the call\n // to Tracker.flush that we are in. When set, throw rather than log the\n // first error encountered while flushing. Before throwing the error,\n // finish flushing (from a finally block), logging any subsequent\n // errors.\n let _throwFirstError = false;\n\n export interface FlushOptions {\n finishSynchronously?: boolean;\n throwFirstError?: boolean;\n }\n\n function _throwOrLog(msg: string, e: any) {\n if (_throwFirstError) {\n throw e;\n } else {\n console.error(`[Tracker error] ${msg}`, e);\n }\n }\n\n // Run all pending computations and afterFlush callbacks. If we were not called\n // directly via Tracker.flush, this may return before they're all done to allow\n // the event loop to run a little before continuing.\n function _runFlush(options?: FlushOptions) {\n // Nested flush could plausibly happen if, say, a flush causes\n // DOM mutation, which causes a \"blur\" event, which runs an\n // app event handler that calls Tracker.flush. At the moment\n // Spark blocks event handlers during DOM mutation anyway,\n // because the LiveRange tree isn't valid. And we don't have\n // any useful notion of a nested flush.\n if (inFlush()) throw new Error(\"Can't call Tracker.flush while flushing\");\n\n if (_inCompute) throw new Error(\"Can't flush inside Tracker.autorun\");\n\n options = options || {};\n\n _inFlush = true;\n _willFlush = true;\n _throwFirstError = !!options.throwFirstError;\n\n var recomputedCount = 0;\n var finishedTry = false;\n try {\n while (_pendingComputations.length || _afterFlushCallbacks.length) {\n // recompute all pending computations\n while (_pendingComputations.length) {\n var comp = _pendingComputations.shift()!;\n comp._recompute();\n if (comp._needsRecompute()) {\n _pendingComputations.unshift(comp);\n }\n\n if (!options.finishSynchronously && ++recomputedCount > 100) {\n finishedTry = true;\n return;\n }\n }\n\n if (_afterFlushCallbacks.length) {\n // call one afterFlush callback, which may\n // invalidate more computations\n var func = _afterFlushCallbacks.shift()!;\n try {\n func();\n } catch (e: any) {\n _throwOrLog('afterFlush', e);\n }\n }\n }\n finishedTry = true;\n } finally {\n if (!finishedTry) {\n // we're erroring due to throwFirstError being true.\n _inFlush = false; // needed before calling `Tracker.flush()` again\n // finish flushing\n _runFlush({\n finishSynchronously: options.finishSynchronously,\n throwFirstError: false,\n });\n }\n _willFlush = false;\n _inFlush = false;\n if (_pendingComputations.length || _afterFlushCallbacks.length) {\n // We're yielding because we ran a bunch of computations and we aren't\n // required to finish synchronously, so we'd like to give the event loop a\n // chance. We should flush again soon.\n if (options.finishSynchronously) {\n throw new Error('still have more to do?'); // shouldn't happen\n }\n setTimeout(_requireFlush, 10);\n }\n }\n }\n\n function _requireFlush() {\n if (!_willFlush) {\n setTimeout(_runFlush, 0);\n _willFlush = true;\n }\n }\n\n /******************************** Tracker Base API ******************************************/\n\n /**\n * 函数在响应式模块中执行\n * @param computation\n * @param f\n */\n export function withComputation<T = any>(\n computation: Computation,\n f: ICallback<Computation, T>,\n ): T {\n let previousComputation = _currentComputation;\n _currentComputation = computation;\n try {\n return f.call(null, computation);\n } finally {\n _currentComputation = previousComputation;\n }\n }\n\n /**\n * 函数在非响应式模块中执行\n */\n export function withoutComputation<T = any>(f: ICallback<undefined, T>): T {\n let previousComputation = _currentComputation;\n _currentComputation = undefined;\n try {\n return f(undefined);\n } finally {\n _currentComputation = previousComputation;\n }\n }\n\n export function isActive(): boolean {\n return !!_currentComputation;\n }\n\n export function getCurrentComputation(): Computation | undefined {\n return _currentComputation;\n }\n\n /**\n * Run a function now and rerun it later whenever its dependencies\n * change. Returns a Computation object that can be used to stop or observe the\n * rerunning.\n */\n export function autorun<T = any>(\n f: IComputationCallback<T>,\n options?: { onError: ICallback<Error> },\n ): Computation<T> {\n var c = new Computation<T>(f, _currentComputation, options?.onError);\n\n if (isActive())\n Tracker.onInvalidate(function () {\n c.stop();\n });\n\n return c;\n }\n\n export function onInvalidate(f: ICallback<Computation | undefined>) {\n if (!_currentComputation) {\n throw new Error('Tracker.onInvalidate requires a currentComputation');\n }\n _currentComputation.onInvalidate(f);\n }\n\n /**\n * True if we are computing a computation now, either first time or recompute. This matches Tracker.active unless we are inside Tracker.nonreactive, which nullfies currentComputation even though an enclosing computation may still be running.\n */\n export function inFlush(): boolean {\n return _inFlush;\n }\n\n /**\n * Process all reactive updates immediately and ensure that all invalidated computations are rerun.\n */\n export function flush(options?: Omit<FlushOptions, 'finishSynchronously'>) {\n _runFlush({\n finishSynchronously: true,\n throwFirstError: options && options.throwFirstError,\n });\n }\n\n /**\n * Schedules a function to be called during the next flush, or later in the current flush if one is in progress, after all invalidated computations have been rerun. The function will be run once and not on subsequent flushes unless `afterFlush` is called again.\n */\n export function afterFlush(f: ICallback) {\n _afterFlushCallbacks.push(f);\n _requireFlush();\n }\n\n /********************************************************************************************/\n\n export type IComputationCallback<V = any> = ICallback<Computation, V>;\n\n /**\n * A Computation object represents code that is repeatedly rerun\n * in response to\n * reactive data changes. Computations don't have return values; they just\n * perform actions, such as rerendering a template on the screen. Computations\n * are created using Tracker.autorun. Use stop to prevent further rerunning of a\n * computation.\n */\n export class Computation<V = any> {\n private _onInvalidateCallbacks: IComputationCallback[] = [];\n\n private _onStopCallbacks: IComputationCallback[] = [];\n\n private _recomputing = false;\n\n private _result: V;\n\n /**\n * 是否停止\n */\n public stopped = false;\n\n /**\n * 未开始执行则返回 false\n */\n public invalidated = false;\n\n /**\n * 是否第一次执行\n */\n public firstRun = true;\n\n constructor(\n private _fn: IComputationCallback<V>,\n public readonly parent?: Computation,\n private readonly _onError?: ICallback<Error>,\n ) {\n let hasError = true;\n try {\n this._compute();\n hasError = false;\n } finally {\n this.firstRun = false;\n if (hasError) {\n this.stop();\n }\n }\n }\n\n onInvalidate(f: IComputationCallback): void {\n if (this.invalidated) {\n withoutComputation(f.bind(null, this));\n } else {\n this._onInvalidateCallbacks.push(f);\n }\n }\n\n /**\n * @summary Invalidates this computation so that it will be rerun.\n */\n invalidate() {\n if (!this.invalidated) {\n // if we're currently in _recompute(), don't enqueue\n // ourselves, since we'll rerun immediately anyway.\n if (!this._recomputing && !this.stopped) {\n _requireFlush();\n _pendingComputations.push(this);\n }\n\n this.invalidated = true;\n\n // callbacks can't add callbacks, because\n // this.invalidated === true.\n for (var i = 0, f: IComputationCallback; (f = this._onInvalidateCallbacks[i]); i++) {\n withoutComputation(f.bind(null, this));\n }\n this._onInvalidateCallbacks = [];\n }\n }\n\n /**\n * @summary Prevents this computation from rerunning.\n * @locus Client\n */\n stop() {\n if (!this.stopped) {\n this.stopped = true;\n this.invalidate();\n for (let i = 0, f: IComputationCallback; (f = this._onStopCallbacks[i]); i++) {\n withoutComputation(f.bind(null, this));\n }\n this._onStopCallbacks = [];\n }\n }\n\n onStop(f: IComputationCallback): void {\n if (this.stopped) {\n withoutComputation(f.bind(null, this));\n } else {\n this._onStopCallbacks.push(f);\n }\n }\n\n private _compute(): void {\n this.invalidated = false;\n\n var previousInCompute = _inCompute;\n _inCompute = true;\n try {\n this._result = Tracker.withComputation<V>(this, this._fn);\n } finally {\n _inCompute = previousInCompute;\n }\n }\n\n _needsRecompute() {\n return this.invalidated && !this.stopped;\n }\n\n _recompute() {\n this._recomputing = true;\n try {\n if (this._needsRecompute()) {\n try {\n this._compute();\n } catch (e: any) {\n if (this._onError) {\n this._onError(e);\n } else {\n _throwOrLog('recompute', e);\n }\n }\n }\n } finally {\n this._recomputing = false;\n }\n }\n\n /**\n * @summary Process the reactive updates for this computation immediately\n * and ensure that the computation is rerun. The computation is rerun only\n * if it is invalidated.\n */\n flush() {\n if (this._recomputing) return;\n\n this._recompute();\n }\n\n /**\n * @summary Causes the function inside this computation to run and\n * synchronously process all reactive updtes.\n * @locus Client\n */\n run() {\n this.invalidate();\n this.flush();\n }\n\n get result(): V {\n return this._result;\n }\n }\n\n /**\n * A Dependency represents an atomic unit of reactive data that a\n * computation might depend on. Reactive data sources such as Session or\n * Minimongo internally create different Dependency objects for different\n * pieces of data, each of which may be depended on by multiple computations.\n * When the data changes, the computations are invalidated.\n */\n export class Dependency {\n private _dependents: Set<Computation> = new Set<Computation>();\n\n /**\n * Declares that the current computation (or `fromComputation` if given) depends on `dependency`. The computation will be invalidated the next time `dependency` changes.\n * If there is no current computation and `depend()` is called with no arguments, it does nothing and returns false.\n * Returns true if the computation is a new dependent of `dependency` rather than an existing one.\n */\n depend(computation?: Computation): boolean {\n if (!computation) {\n if (!isActive()) {\n return false;\n }\n computation = _currentComputation;\n }\n if (!this._dependents.has(computation!)) {\n this._dependents.add(computation!);\n computation!.onInvalidate(() => {\n this._dependents.delete(computation!);\n });\n return true;\n }\n return false;\n }\n\n /**\n * Invalidate all dependent computations immediately and remove them as dependents.\n */\n changed() {\n for (const dep of this._dependents) {\n dep.invalidate();\n }\n }\n\n /**\n * True if this Dependency has one or more dependent Computations, which would be invalidated if this Dependency were to change.\n */\n hasDependents() {\n return this._dependents.size !== 0;\n }\n }\n}\n","import { Tracker } from './tracker';\n\ntype IStateEqual = (a: any, b: any) => boolean;\n\nexport class ReactiveBaseState<V> {\n protected _dep = new Tracker.Dependency();\n\n protected _value: V;\n\n protected _isEqual: IStateEqual = (a: any, b: any) => a == b;\n\n protected _addDepend(dep: Tracker.Dependency): void {\n if (Tracker.isActive()) {\n dep.depend();\n }\n }\n\n constructor(initialValue: V, opts?: { isEqual?: IStateEqual }) {\n this._value = initialValue;\n if (opts?.isEqual) {\n this._isEqual = opts.isEqual;\n }\n }\n\n hasDependents(): boolean {\n return this._dep.hasDependents();\n }\n\n get value(): V {\n this._addDepend(this._dep);\n return this._value;\n }\n\n set value(newValue: V) {\n if (!this._isEqual(this._value, newValue)) {\n this._value = newValue;\n this._dep.changed();\n }\n }\n}\n","interface ProxyOptions<V> {\n get?: (target: V, key: string) => any;\n set?: (target: V, key: string, newValue: any) => boolean;\n}\n\nexport function createProxy<V extends Record<string, any>>(target: V, opts: ProxyOptions<V>): V {\n let useProxy = 'Proxy' in window;\n if (process.env.NODE_ENV === 'test') {\n if ((global as any).__ignoreProxy) {\n useProxy = false;\n }\n }\n if (useProxy) {\n return new Proxy<V>(target, opts);\n }\n const result: V = {} as V;\n for (const key in target) {\n Object.defineProperty(result, key, {\n enumerable: true,\n get: opts.get ? () => opts.get!(target, key) : undefined,\n set: opts.set ? (newValue: any) => opts.set!(target, key, newValue) : undefined,\n });\n }\n return result;\n}\n","import { Tracker } from './tracker';\n\nimport Dependency = Tracker.Dependency;\n\nimport { ReactiveBaseState } from './reactive-base-state';\nimport { createProxy } from '../utils/create-proxy';\n\nexport class ReactiveState<V extends Record<string, any>> extends ReactiveBaseState<V> {\n private _keyDeps: Map<string, Dependency> = new Map();\n\n set(key: keyof V & string, value: any): boolean {\n this._ensureKey(key);\n const oldValue = this._value[key];\n if (!this._isEqual(oldValue, value)) {\n this._value[key] = value;\n this._keyDeps.get(key)!.changed();\n return true;\n }\n return false;\n }\n\n get(key: keyof V & string) {\n this._ensureKey(key);\n this._addDepend(this._keyDeps.get(key)!);\n return this._value[key];\n }\n\n protected _ensureKey(key: keyof V & string) {\n if (!this._keyDeps.has(key)) {\n this._keyDeps.set(key, new Dependency());\n }\n }\n\n hasDependents(): boolean {\n if (this._dep.hasDependents()) return true;\n for (const dep of this._keyDeps.values()) {\n if (dep.hasDependents()) return true;\n }\n return false;\n }\n\n keys(): string[] {\n return Object.keys(this._value);\n }\n\n set value(newValue: V) {\n if (!this._isEqual(this._value, newValue)) {\n this._value = newValue;\n this._keyDeps.clear();\n this._dep.changed();\n }\n }\n\n private _proxyValue: V;\n\n get value(): V {\n this._addDepend(this._dep);\n if (!this._proxyValue) {\n this._proxyValue = createProxy<V>(this._value, {\n get: (target, key: string) => this.get(key),\n set: (target, key: string, newValue) => {\n this.set(key, newValue);\n return true;\n },\n });\n }\n return this._proxyValue;\n }\n\n private _proxyReadonlyValue: V;\n\n get readonlyValue(): Readonly<V> {\n this._addDepend(this._dep);\n if (!this._proxyReadonlyValue) {\n this._proxyReadonlyValue = createProxy(this._value, {\n get: (target, key: string) => this.get(key),\n set: (newValue, key: string) => {\n throw new Error(`[ReactiveState] Cannnot set readonly field \"${key}\"`);\n },\n });\n }\n return this._proxyReadonlyValue;\n }\n}\n","import { useMemo } from 'react';\n\nimport { ReactiveState } from '../core/reactive-state';\nimport { useObserve } from './use-observe';\n\nexport function useReactiveState<T extends Record<string, any>>(v: ReactiveState<T> | T): T {\n const state = useMemo<ReactiveState<T>>(\n () => (v instanceof ReactiveState ? v : new ReactiveState(v)),\n [],\n );\n return useObserve<T>(state.value);\n}\n","import { useCallback, useEffect, useMemo } from 'react';\n\nimport { useRefresh } from '@flowgram.ai/utils';\n\nimport { createProxy } from '../utils/create-proxy';\nimport { Tracker } from '../core/tracker';\n\nimport Computation = Tracker.Computation;\n\nexport function useObserve<T extends Record<string, any>>(value: T | undefined): T {\n const refresh = useRefresh();\n const computationMap = useMemo<Map<string, Computation>>(() => new Map(), []);\n const clear = useCallback(() => {\n computationMap.forEach((comp) => comp.stop());\n computationMap.clear();\n }, []);\n useEffect(() => clear, []);\n // 重新渲染需要清空依赖\n clear();\n return useMemo(() => {\n if (value === undefined) return {} as T;\n return createProxy(value, {\n get(target, key: string) {\n let computation = computationMap.get(key);\n if (!computation) {\n computation = new Tracker.Computation((c) => {\n if (!c.firstRun) {\n refresh();\n return;\n }\n return value[key];\n });\n computationMap.set(key, computation);\n }\n return value[key];\n },\n });\n }, [value]);\n}\n","import { ReactiveState } from '../core/reactive-state';\nimport { useObserve } from './use-observe';\n\nexport function useReadonlyReactiveState<T extends Record<string, any>>(\n state: ReactiveState<T>,\n): Readonly<T> {\n return useObserve<T>(state.readonlyValue);\n}\n","import React, { useRef, useEffect } from 'react';\n\nimport { useRefresh } from '@flowgram.ai/utils';\n\nimport { Tracker } from '../core/tracker';\n\nimport Computation = Tracker.Computation;\n\nexport function observe<T = any>(fc: React.FC<T>): React.FC<T> {\n return function ReactiveObserver(props: T) {\n const childrenRef = useRef<React.ReactElement<any, any> | null>();\n const computationRef = useRef<Computation | undefined>();\n const refresh = useRefresh();\n computationRef.current?.stop();\n computationRef.current = new Tracker.Computation(c => {\n if (c.firstRun) {\n childrenRef.current = fc(props);\n } else {\n refresh();\n }\n });\n useEffect(\n () => () => {\n computationRef.current?.stop();\n },\n [],\n );\n return childrenRef.current!;\n };\n}\n","import { Tracker } from './core/tracker';\n\nexport { Tracker } from './core/tracker';\nexport { ReactiveState } from './core/reactive-state';\nexport { ReactiveBaseState } from './core/reactive-base-state';\nexport { useReactiveState } from './hooks/use-reactive-state';\nexport { useReadonlyReactiveState } from './hooks/use-readonly-reactive-state';\nexport { useObserve } from './hooks/use-observe';\nexport { observe } from './react/observe';\nexport const { Dependency, Computation } = Tracker;\n"],"mappings":";AAYO,IAAU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AAQ,YAAY,OAAK;AACpD,UAAI,EAAE,UAAU;AACd,oBAAY,UAAU,GAAG,KAAK;AAAA,MAChC,OAAO;AACL,gBAAQ;AAAA,MACV;AAAA,IACF,CAAC;AACD,IAAAC;AAAA,MACE,MAAM,MAAM;AACV,uBAAe,SAAS,KAAK;AAAA,MAC/B;AAAA,MACA,CAAC;AAAA,IACH;AACA,WAAO,YAAY;AAAA,EACrB;AACF;;;ACpBO,IAAM,EAAE,YAAAC,aAAY,YAAY,IAAI;","names":["Tracker","Computation","Dependency","useMemo","useMemo","useEffect","useRefresh","useRefresh","useEffect","Dependency"]}
|
|
1
|
+
{"version":3,"sources":["../../src/core/tracker.ts","../../src/core/reactive-base-state.ts","../../src/utils/create-proxy.ts","../../src/core/reactive-state.ts","../../src/hooks/use-reactive-state.ts","../../src/hooks/use-observe.ts","../../src/hooks/use-readonly-reactive-state.ts","../../src/react/observe.tsx","../../src/index.ts"],"sourcesContent":["/**\n * Fork from: https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js\n */\ntype ICallback<ARG = void, RET = void> = (arg: ARG) => RET;\n\n/**\n * Tracker 是一套 响应式依赖追踪 库,来源于 Meteor.Tracker\n * https://docs.meteor.com/api/Tracker.html#tracker-autorun-and-async-callbacks\n * https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js\n *\n * 相关论文:https://dl.acm.org/doi/fullHtml/10.1145/3184558.3185978\n */\nexport namespace Tracker {\n const _pendingComputations: Computation[] = [];\n const _afterFlushCallbacks: ICallback[] = [];\n // `true` if a Tracker.flush is scheduled, or if we are in Tracker.flush now\n let _willFlush = false;\n // `true` if we are in Tracker.flush now\n let _inFlush = false;\n // `true` if we are computing a computation now, either first time\n // or recompute. This matches Tracker.active unless we are inside\n // Tracker.nonreactive, which nullfies currentComputation even though\n // an enclosing computation may still be running.\n let _inCompute = false;\n let _currentComputation: Computation | undefined = undefined;\n // `true` if the `_throwFirstError` option was passed in to the call\n // to Tracker.flush that we are in. When set, throw rather than log the\n // first error encountered while flushing. Before throwing the error,\n // finish flushing (from a finally block), logging any subsequent\n // errors.\n let _throwFirstError = false;\n\n export interface FlushOptions {\n finishSynchronously?: boolean;\n throwFirstError?: boolean;\n }\n\n function _throwOrLog(msg: string, e: any) {\n if (_throwFirstError) {\n throw e;\n } else {\n console.error(`[Tracker error] ${msg}`, e);\n }\n }\n\n // Run all pending computations and afterFlush callbacks. If we were not called\n // directly via Tracker.flush, this may return before they're all done to allow\n // the event loop to run a little before continuing.\n function _runFlush(options?: FlushOptions) {\n // Nested flush could plausibly happen if, say, a flush causes\n // DOM mutation, which causes a \"blur\" event, which runs an\n // app event handler that calls Tracker.flush. At the moment\n // Spark blocks event handlers during DOM mutation anyway,\n // because the LiveRange tree isn't valid. And we don't have\n // any useful notion of a nested flush.\n if (inFlush()) throw new Error(\"Can't call Tracker.flush while flushing\");\n\n if (_inCompute) throw new Error(\"Can't flush inside Tracker.autorun\");\n\n options = options || {};\n\n _inFlush = true;\n _willFlush = true;\n _throwFirstError = !!options.throwFirstError;\n\n var recomputedCount = 0;\n var finishedTry = false;\n try {\n while (_pendingComputations.length || _afterFlushCallbacks.length) {\n // recompute all pending computations\n while (_pendingComputations.length) {\n var comp = _pendingComputations.shift()!;\n comp._recompute();\n if (comp._needsRecompute()) {\n _pendingComputations.unshift(comp);\n }\n\n if (!options.finishSynchronously && ++recomputedCount > 100) {\n finishedTry = true;\n return;\n }\n }\n\n if (_afterFlushCallbacks.length) {\n // call one afterFlush callback, which may\n // invalidate more computations\n var func = _afterFlushCallbacks.shift()!;\n try {\n func();\n } catch (e: any) {\n _throwOrLog('afterFlush', e);\n }\n }\n }\n finishedTry = true;\n } finally {\n if (!finishedTry) {\n // we're erroring due to throwFirstError being true.\n _inFlush = false; // needed before calling `Tracker.flush()` again\n // finish flushing\n _runFlush({\n finishSynchronously: options.finishSynchronously,\n throwFirstError: false,\n });\n }\n _willFlush = false;\n _inFlush = false;\n if (_pendingComputations.length || _afterFlushCallbacks.length) {\n // We're yielding because we ran a bunch of computations and we aren't\n // required to finish synchronously, so we'd like to give the event loop a\n // chance. We should flush again soon.\n if (options.finishSynchronously) {\n throw new Error('still have more to do?'); // shouldn't happen\n }\n setTimeout(_requireFlush, 10);\n }\n }\n }\n\n function _requireFlush() {\n if (!_willFlush) {\n setTimeout(_runFlush, 0);\n _willFlush = true;\n }\n }\n\n /******************************** Tracker Base API ******************************************/\n\n /**\n * 函数在响应式模块中执行\n * @param computation\n * @param f\n */\n export function withComputation<T = any>(\n computation: Computation,\n f: ICallback<Computation, T>,\n ): T {\n let previousComputation = _currentComputation;\n _currentComputation = computation;\n try {\n return f.call(null, computation);\n } finally {\n _currentComputation = previousComputation;\n }\n }\n\n /**\n * 函数在非响应式模块中执行\n */\n export function withoutComputation<T = any>(f: ICallback<undefined, T>): T {\n let previousComputation = _currentComputation;\n _currentComputation = undefined;\n try {\n return f(undefined);\n } finally {\n _currentComputation = previousComputation;\n }\n }\n\n export function isActive(): boolean {\n return !!_currentComputation;\n }\n\n export function getCurrentComputation(): Computation | undefined {\n return _currentComputation;\n }\n\n /**\n * Run a function now and rerun it later whenever its dependencies\n * change. Returns a Computation object that can be used to stop or observe the\n * rerunning.\n */\n export function autorun<T = any>(\n f: IComputationCallback<T>,\n options?: { onError: ICallback<Error> },\n ): Computation<T> {\n var c = new Computation<T>(f, _currentComputation, options?.onError);\n\n if (isActive())\n Tracker.onInvalidate(function () {\n c.stop();\n });\n\n return c;\n }\n\n export function onInvalidate(f: ICallback<Computation | undefined>) {\n if (!_currentComputation) {\n throw new Error('Tracker.onInvalidate requires a currentComputation');\n }\n _currentComputation.onInvalidate(f);\n }\n\n /**\n * True if we are computing a computation now, either first time or recompute. This matches Tracker.active unless we are inside Tracker.nonreactive, which nullfies currentComputation even though an enclosing computation may still be running.\n */\n export function inFlush(): boolean {\n return _inFlush;\n }\n\n /**\n * Process all reactive updates immediately and ensure that all invalidated computations are rerun.\n */\n export function flush(options?: Omit<FlushOptions, 'finishSynchronously'>) {\n _runFlush({\n finishSynchronously: true,\n throwFirstError: options && options.throwFirstError,\n });\n }\n\n /**\n * Schedules a function to be called during the next flush, or later in the current flush if one is in progress, after all invalidated computations have been rerun. The function will be run once and not on subsequent flushes unless `afterFlush` is called again.\n */\n export function afterFlush(f: ICallback) {\n _afterFlushCallbacks.push(f);\n _requireFlush();\n }\n\n /********************************************************************************************/\n\n export type IComputationCallback<V = any> = ICallback<Computation, V>;\n\n /**\n * A Computation object represents code that is repeatedly rerun\n * in response to\n * reactive data changes. Computations don't have return values; they just\n * perform actions, such as rerendering a template on the screen. Computations\n * are created using Tracker.autorun. Use stop to prevent further rerunning of a\n * computation.\n */\n export class Computation<V = any> {\n private _onInvalidateCallbacks: IComputationCallback[] = [];\n\n private _onStopCallbacks: IComputationCallback[] = [];\n\n private _recomputing = false;\n\n private _result: V;\n\n /**\n * 是否停止\n */\n public stopped = false;\n\n /**\n * 未开始执行则返回 false\n */\n public invalidated = false;\n\n /**\n * 是否第一次执行\n */\n public firstRun = true;\n\n constructor(\n private _fn: IComputationCallback<V>,\n public readonly parent?: Computation,\n private readonly _onError?: ICallback<Error>,\n ) {\n let hasError = true;\n try {\n this._compute();\n hasError = false;\n } finally {\n this.firstRun = false;\n if (hasError) {\n this.stop();\n }\n }\n }\n\n onInvalidate(f: IComputationCallback): void {\n if (this.invalidated) {\n withoutComputation(f.bind(null, this));\n } else {\n this._onInvalidateCallbacks.push(f);\n }\n }\n\n /**\n * @summary Invalidates this computation so that it will be rerun.\n */\n invalidate() {\n if (!this.invalidated) {\n // if we're currently in _recompute(), don't enqueue\n // ourselves, since we'll rerun immediately anyway.\n if (!this._recomputing && !this.stopped) {\n _requireFlush();\n _pendingComputations.push(this);\n }\n\n this.invalidated = true;\n\n // callbacks can't add callbacks, because\n // this.invalidated === true.\n for (var i = 0, f: IComputationCallback; (f = this._onInvalidateCallbacks[i]); i++) {\n withoutComputation(f.bind(null, this));\n }\n this._onInvalidateCallbacks = [];\n }\n }\n\n /**\n * @summary Prevents this computation from rerunning.\n * @locus Client\n */\n stop() {\n if (!this.stopped) {\n this.stopped = true;\n this.invalidate();\n for (let i = 0, f: IComputationCallback; (f = this._onStopCallbacks[i]); i++) {\n withoutComputation(f.bind(null, this));\n }\n this._onStopCallbacks = [];\n }\n }\n\n onStop(f: IComputationCallback): void {\n if (this.stopped) {\n withoutComputation(f.bind(null, this));\n } else {\n this._onStopCallbacks.push(f);\n }\n }\n\n private _compute(): void {\n this.invalidated = false;\n\n var previousInCompute = _inCompute;\n _inCompute = true;\n try {\n this._result = Tracker.withComputation<V>(this, this._fn);\n } finally {\n _inCompute = previousInCompute;\n }\n }\n\n _needsRecompute() {\n return this.invalidated && !this.stopped;\n }\n\n _recompute() {\n this._recomputing = true;\n try {\n if (this._needsRecompute()) {\n try {\n this._compute();\n } catch (e: any) {\n if (this._onError) {\n this._onError(e);\n } else {\n _throwOrLog('recompute', e);\n }\n }\n }\n } finally {\n this._recomputing = false;\n }\n }\n\n /**\n * @summary Process the reactive updates for this computation immediately\n * and ensure that the computation is rerun. The computation is rerun only\n * if it is invalidated.\n */\n flush() {\n if (this._recomputing) return;\n\n this._recompute();\n }\n\n /**\n * @summary Causes the function inside this computation to run and\n * synchronously process all reactive updtes.\n * @locus Client\n */\n run() {\n this.invalidate();\n this.flush();\n }\n\n get result(): V {\n return this._result;\n }\n }\n\n /**\n * A Dependency represents an atomic unit of reactive data that a\n * computation might depend on. Reactive data sources such as Session or\n * Minimongo internally create different Dependency objects for different\n * pieces of data, each of which may be depended on by multiple computations.\n * When the data changes, the computations are invalidated.\n */\n export class Dependency {\n private _dependents: Set<Computation> = new Set<Computation>();\n\n /**\n * Declares that the current computation (or `fromComputation` if given) depends on `dependency`. The computation will be invalidated the next time `dependency` changes.\n * If there is no current computation and `depend()` is called with no arguments, it does nothing and returns false.\n * Returns true if the computation is a new dependent of `dependency` rather than an existing one.\n */\n depend(computation?: Computation): boolean {\n if (!computation) {\n if (!isActive()) {\n return false;\n }\n computation = _currentComputation;\n }\n if (!this._dependents.has(computation!)) {\n this._dependents.add(computation!);\n computation!.onInvalidate(() => {\n this._dependents.delete(computation!);\n });\n return true;\n }\n return false;\n }\n\n /**\n * Invalidate all dependent computations immediately and remove them as dependents.\n */\n changed() {\n for (const dep of this._dependents) {\n dep.invalidate();\n }\n }\n\n /**\n * True if this Dependency has one or more dependent Computations, which would be invalidated if this Dependency were to change.\n */\n hasDependents() {\n return this._dependents.size !== 0;\n }\n }\n}\n","import { Tracker } from './tracker';\n\ntype IStateEqual = (a: any, b: any) => boolean;\n\nexport class ReactiveBaseState<V> {\n protected _dep = new Tracker.Dependency();\n\n protected _value: V;\n\n protected _isEqual: IStateEqual = (a: any, b: any) => a == b;\n\n protected _addDepend(dep: Tracker.Dependency): void {\n if (Tracker.isActive()) {\n dep.depend();\n }\n }\n\n constructor(initialValue: V, opts?: { isEqual?: IStateEqual }) {\n this._value = initialValue;\n if (opts?.isEqual) {\n this._isEqual = opts.isEqual;\n }\n }\n\n hasDependents(): boolean {\n return this._dep.hasDependents();\n }\n\n get value(): V {\n this._addDepend(this._dep);\n return this._value;\n }\n\n set value(newValue: V) {\n if (!this._isEqual(this._value, newValue)) {\n this._value = newValue;\n this._dep.changed();\n }\n }\n}\n","interface ProxyOptions<V> {\n get?: (target: V, key: string) => any;\n set?: (target: V, key: string, newValue: any) => boolean;\n}\n\nexport function createProxy<V extends Record<string, any>>(target: V, opts: ProxyOptions<V>): V {\n let useProxy = 'Proxy' in window;\n if (process.env.NODE_ENV === 'test') {\n if ((global as any).__ignoreProxy) {\n useProxy = false;\n }\n }\n if (useProxy) {\n return new Proxy<V>(target, opts);\n }\n const result: V = {} as V;\n for (const key in target) {\n Object.defineProperty(result, key, {\n enumerable: true,\n get: opts.get ? () => opts.get!(target, key) : undefined,\n set: opts.set ? (newValue: any) => opts.set!(target, key, newValue) : undefined,\n });\n }\n return result;\n}\n","import { Tracker } from './tracker';\n\nimport Dependency = Tracker.Dependency;\n\nimport { ReactiveBaseState } from './reactive-base-state';\nimport { createProxy } from '../utils/create-proxy';\n\nexport class ReactiveState<V extends Record<string, any>> extends ReactiveBaseState<V> {\n private _keyDeps: Map<string, Dependency> = new Map();\n\n set(key: keyof V & string, value: any): boolean {\n this._ensureKey(key);\n const oldValue = this._value[key];\n if (!this._isEqual(oldValue, value)) {\n this._value[key] = value;\n this._keyDeps.get(key)!.changed();\n return true;\n }\n return false;\n }\n\n get(key: keyof V & string) {\n this._ensureKey(key);\n this._addDepend(this._keyDeps.get(key)!);\n return this._value[key];\n }\n\n protected _ensureKey(key: keyof V & string) {\n if (!this._keyDeps.has(key)) {\n this._keyDeps.set(key, new Dependency());\n }\n }\n\n hasDependents(): boolean {\n if (this._dep.hasDependents()) return true;\n for (const dep of this._keyDeps.values()) {\n if (dep.hasDependents()) return true;\n }\n return false;\n }\n\n keys(): string[] {\n return Object.keys(this._value);\n }\n\n set value(newValue: V) {\n if (!this._isEqual(this._value, newValue)) {\n this._value = newValue;\n this._keyDeps.clear();\n this._dep.changed();\n }\n }\n\n private _proxyValue: V;\n\n get value(): V {\n this._addDepend(this._dep);\n if (!this._proxyValue) {\n this._proxyValue = createProxy<V>(this._value, {\n get: (target, key: string) => this.get(key),\n set: (target, key: string, newValue) => {\n this.set(key, newValue);\n return true;\n },\n });\n }\n return this._proxyValue;\n }\n\n private _proxyReadonlyValue: V;\n\n get readonlyValue(): Readonly<V> {\n this._addDepend(this._dep);\n if (!this._proxyReadonlyValue) {\n this._proxyReadonlyValue = createProxy(this._value, {\n get: (target, key: string) => this.get(key),\n set: (newValue, key: string) => {\n throw new Error(`[ReactiveState] Cannnot set readonly field \"${key}\"`);\n },\n });\n }\n return this._proxyReadonlyValue;\n }\n}\n","import { useMemo } from 'react';\n\nimport { ReactiveState } from '../core/reactive-state';\nimport { useObserve } from './use-observe';\n\nexport function useReactiveState<T extends Record<string, any>>(v: ReactiveState<T> | T): T {\n const state = useMemo<ReactiveState<T>>(\n () => (v instanceof ReactiveState ? v : new ReactiveState(v)),\n [],\n );\n return useObserve<T>(state.value);\n}\n","import { useCallback, useEffect, useMemo } from 'react';\n\nimport { useRefresh } from '@flowgram.ai/utils';\n\nimport { createProxy } from '../utils/create-proxy';\nimport { Tracker } from '../core/tracker';\n\nimport Computation = Tracker.Computation;\n\nexport function useObserve<T extends Record<string, any>>(value: T | undefined): T {\n const refresh = useRefresh();\n const computationMap = useMemo<Map<string, Computation>>(() => new Map(), []);\n const clear = useCallback(() => {\n computationMap.forEach((comp) => comp.stop());\n computationMap.clear();\n }, []);\n useEffect(() => clear, []);\n // 重新渲染需要清空依赖\n clear();\n return useMemo(() => {\n if (value === undefined) return {} as T;\n return createProxy(value, {\n get(target, key: string) {\n let computation = computationMap.get(key);\n if (!computation) {\n computation = new Tracker.Computation((c) => {\n if (!c.firstRun) {\n refresh();\n return;\n }\n return value[key];\n });\n computationMap.set(key, computation);\n }\n return value[key];\n },\n });\n }, [value]);\n}\n","import { ReactiveState } from '../core/reactive-state';\nimport { useObserve } from './use-observe';\n\nexport function useReadonlyReactiveState<T extends Record<string, any>>(\n state: ReactiveState<T>,\n): Readonly<T> {\n return useObserve<T>(state.readonlyValue);\n}\n","import React, { useRef, useEffect } from 'react';\n\nimport { useRefresh } from '@flowgram.ai/utils';\n\nimport { Tracker } from '../core/tracker';\n\nimport Computation = Tracker.Computation;\n\nexport function observe<T = any>(fc: React.FC<T>): React.FC<T> {\n return function ReactiveObserver(props: T) {\n const childrenRef = useRef<React.ReactElement<any, any> | null>();\n const computationRef = useRef<Computation | undefined>();\n const refresh = useRefresh();\n computationRef.current?.stop();\n computationRef.current = new Tracker.Computation(c => {\n if (c.firstRun) {\n childrenRef.current = fc(props);\n } else {\n refresh();\n }\n });\n useEffect(\n () => () => {\n computationRef.current?.stop();\n },\n [],\n );\n return childrenRef.current!;\n };\n}\n","import { Tracker } from './core/tracker';\n\nexport { Tracker } from './core/tracker';\nexport { ReactiveState } from './core/reactive-state';\nexport { ReactiveBaseState } from './core/reactive-base-state';\nexport { useReactiveState } from './hooks/use-reactive-state';\nexport { useReadonlyReactiveState } from './hooks/use-readonly-reactive-state';\nexport { useObserve } from './hooks/use-observe';\nexport { observe } from './react/observe';\nexport const { Dependency, Computation } = Tracker;\n"],"mappings":";AAYO,IAAU;AAAA,CAAV,CAAUA,aAAV;AACL,QAAM,uBAAsC,CAAC;AAC7C,QAAM,uBAAoC,CAAC;AAE3C,MAAI,aAAa;AAEjB,MAAI,WAAW;AAKf,MAAI,aAAa;AACjB,MAAI,sBAA+C;AAMnD,MAAI,mBAAmB;AAOvB,WAAS,YAAY,KAAa,GAAQ;AACxC,QAAI,kBAAkB;AACpB,YAAM;AAAA,IACR,OAAO;AACL,cAAQ,MAAM,mBAAmB,GAAG,IAAI,CAAC;AAAA,IAC3C;AAAA,EACF;AAKA,WAAS,UAAU,SAAwB;AAOzC,QAAI,QAAQ,EAAG,OAAM,IAAI,MAAM,yCAAyC;AAExE,QAAI,WAAY,OAAM,IAAI,MAAM,oCAAoC;AAEpE,cAAU,WAAW,CAAC;AAEtB,eAAW;AACX,iBAAa;AACb,uBAAmB,CAAC,CAAC,QAAQ;AAE7B,QAAI,kBAAkB;AACtB,QAAI,cAAc;AAClB,QAAI;AACF,aAAO,qBAAqB,UAAU,qBAAqB,QAAQ;AAEjE,eAAO,qBAAqB,QAAQ;AAClC,cAAI,OAAO,qBAAqB,MAAM;AACtC,eAAK,WAAW;AAChB,cAAI,KAAK,gBAAgB,GAAG;AAC1B,iCAAqB,QAAQ,IAAI;AAAA,UACnC;AAEA,cAAI,CAAC,QAAQ,uBAAuB,EAAE,kBAAkB,KAAK;AAC3D,0BAAc;AACd;AAAA,UACF;AAAA,QACF;AAEA,YAAI,qBAAqB,QAAQ;AAG/B,cAAI,OAAO,qBAAqB,MAAM;AACtC,cAAI;AACF,iBAAK;AAAA,UACP,SAAS,GAAQ;AACf,wBAAY,cAAc,CAAC;AAAA,UAC7B;AAAA,QACF;AAAA,MACF;AACA,oBAAc;AAAA,IAChB,U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AAC7D,SAAO,SAAS,iBAAiB,OAAU;AAT7C;AAUI,UAAM,cAAc,OAA4C;AAChE,UAAM,iBAAiB,OAAgC;AACvD,UAAM,UAAUC,YAAW;AAC3B,yBAAe,YAAf,mBAAwB;AACxB,mBAAe,UAAU,IAAI,QAAQ,YAAY,OAAK;AACpD,UAAI,EAAE,UAAU;AACd,oBAAY,UAAU,GAAG,KAAK;AAAA,MAChC,OAAO;AACL,gBAAQ;AAAA,MACV;AAAA,IACF,CAAC;AACD,IAAAC;AAAA,MACE,MAAM,MAAM;AAtBlB,YAAAC;AAuBQ,SAAAA,MAAA,eAAe,YAAf,gBAAAA,IAAwB;AAAA,MAC1B;AAAA,MACA,CAAC;AAAA,IACH;AACA,WAAO,YAAY;AAAA,EACrB;AACF;;;ACpBO,IAAM,EAAE,YAAAC,aAAY,YAAY,IAAI;","names":["Tracker","Computation","Dependency","useMemo","useMemo","useEffect","useRefresh","useRefresh","useEffect","_a","Dependency"]}
|
package/dist/index.js
CHANGED
|
@@ -134,7 +134,7 @@ var Tracker;
|
|
|
134
134
|
}
|
|
135
135
|
Tracker2.getCurrentComputation = getCurrentComputation;
|
|
136
136
|
function autorun(f, options) {
|
|
137
|
-
var c = new Computation2(f, _currentComputation, options
|
|
137
|
+
var c = new Computation2(f, _currentComputation, options == null ? void 0 : options.onError);
|
|
138
138
|
if (isActive())
|
|
139
139
|
Tracker2.onInvalidate(function() {
|
|
140
140
|
c.stop();
|
|
@@ -343,7 +343,7 @@ var ReactiveBaseState = class {
|
|
|
343
343
|
this._dep = new Tracker.Dependency();
|
|
344
344
|
this._isEqual = (a, b) => a == b;
|
|
345
345
|
this._value = initialValue;
|
|
346
|
-
if (opts
|
|
346
|
+
if (opts == null ? void 0 : opts.isEqual) {
|
|
347
347
|
this._isEqual = opts.isEqual;
|
|
348
348
|
}
|
|
349
349
|
}
|
|
@@ -515,10 +515,11 @@ var import_react3 = require("react");
|
|
|
515
515
|
var import_utils2 = require("@flowgram.ai/utils");
|
|
516
516
|
function observe(fc) {
|
|
517
517
|
return function ReactiveObserver(props) {
|
|
518
|
+
var _a;
|
|
518
519
|
const childrenRef = (0, import_react3.useRef)();
|
|
519
520
|
const computationRef = (0, import_react3.useRef)();
|
|
520
521
|
const refresh = (0, import_utils2.useRefresh)();
|
|
521
|
-
computationRef.current
|
|
522
|
+
(_a = computationRef.current) == null ? void 0 : _a.stop();
|
|
522
523
|
computationRef.current = new Tracker.Computation((c) => {
|
|
523
524
|
if (c.firstRun) {
|
|
524
525
|
childrenRef.current = fc(props);
|
|
@@ -528,7 +529,8 @@ function observe(fc) {
|
|
|
528
529
|
});
|
|
529
530
|
(0, import_react3.useEffect)(
|
|
530
531
|
() => () => {
|
|
531
|
-
|
|
532
|
+
var _a2;
|
|
533
|
+
(_a2 = computationRef.current) == null ? void 0 : _a2.stop();
|
|
532
534
|
},
|
|
533
535
|
[]
|
|
534
536
|
);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/core/tracker.ts","../src/core/reactive-base-state.ts","../src/utils/create-proxy.ts","../src/core/reactive-state.ts","../src/hooks/use-reactive-state.ts","../src/hooks/use-observe.ts","../src/hooks/use-readonly-reactive-state.ts","../src/react/observe.tsx"],"sourcesContent":["import { Tracker } from './core/tracker';\n\nexport { Tracker } from './core/tracker';\nexport { ReactiveState } from './core/reactive-state';\nexport { ReactiveBaseState } from './core/reactive-base-state';\nexport { useReactiveState } from './hooks/use-reactive-state';\nexport { useReadonlyReactiveState } from './hooks/use-readonly-reactive-state';\nexport { useObserve } from './hooks/use-observe';\nexport { observe } from './react/observe';\nexport const { Dependency, Computation } = Tracker;\n","/**\n * Fork from: https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js\n */\ntype ICallback<ARG = void, RET = void> = (arg: ARG) => RET;\n\n/**\n * Tracker 是一套 响应式依赖追踪 库,来源于 Meteor.Tracker\n * https://docs.meteor.com/api/Tracker.html#tracker-autorun-and-async-callbacks\n * https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js\n *\n * 相关论文:https://dl.acm.org/doi/fullHtml/10.1145/3184558.3185978\n */\nexport namespace Tracker {\n const _pendingComputations: Computation[] = [];\n const _afterFlushCallbacks: ICallback[] = [];\n // `true` if a Tracker.flush is scheduled, or if we are in Tracker.flush now\n let _willFlush = false;\n // `true` if we are in Tracker.flush now\n let _inFlush = false;\n // `true` if we are computing a computation now, either first time\n // or recompute. This matches Tracker.active unless we are inside\n // Tracker.nonreactive, which nullfies currentComputation even though\n // an enclosing computation may still be running.\n let _inCompute = false;\n let _currentComputation: Computation | undefined = undefined;\n // `true` if the `_throwFirstError` option was passed in to the call\n // to Tracker.flush that we are in. When set, throw rather than log the\n // first error encountered while flushing. Before throwing the error,\n // finish flushing (from a finally block), logging any subsequent\n // errors.\n let _throwFirstError = false;\n\n export interface FlushOptions {\n finishSynchronously?: boolean;\n throwFirstError?: boolean;\n }\n\n function _throwOrLog(msg: string, e: any) {\n if (_throwFirstError) {\n throw e;\n } else {\n console.error(`[Tracker error] ${msg}`, e);\n }\n }\n\n // Run all pending computations and afterFlush callbacks. If we were not called\n // directly via Tracker.flush, this may return before they're all done to allow\n // the event loop to run a little before continuing.\n function _runFlush(options?: FlushOptions) {\n // Nested flush could plausibly happen if, say, a flush causes\n // DOM mutation, which causes a \"blur\" event, which runs an\n // app event handler that calls Tracker.flush. At the moment\n // Spark blocks event handlers during DOM mutation anyway,\n // because the LiveRange tree isn't valid. And we don't have\n // any useful notion of a nested flush.\n if (inFlush()) throw new Error(\"Can't call Tracker.flush while flushing\");\n\n if (_inCompute) throw new Error(\"Can't flush inside Tracker.autorun\");\n\n options = options || {};\n\n _inFlush = true;\n _willFlush = true;\n _throwFirstError = !!options.throwFirstError;\n\n var recomputedCount = 0;\n var finishedTry = false;\n try {\n while (_pendingComputations.length || _afterFlushCallbacks.length) {\n // recompute all pending computations\n while (_pendingComputations.length) {\n var comp = _pendingComputations.shift()!;\n comp._recompute();\n if (comp._needsRecompute()) {\n _pendingComputations.unshift(comp);\n }\n\n if (!options.finishSynchronously && ++recomputedCount > 100) {\n finishedTry = true;\n return;\n }\n }\n\n if (_afterFlushCallbacks.length) {\n // call one afterFlush callback, which may\n // invalidate more computations\n var func = _afterFlushCallbacks.shift()!;\n try {\n func();\n } catch (e: any) {\n _throwOrLog('afterFlush', e);\n }\n }\n }\n finishedTry = true;\n } finally {\n if (!finishedTry) {\n // we're erroring due to throwFirstError being true.\n _inFlush = false; // needed before calling `Tracker.flush()` again\n // finish flushing\n _runFlush({\n finishSynchronously: options.finishSynchronously,\n throwFirstError: false,\n });\n }\n _willFlush = false;\n _inFlush = false;\n if (_pendingComputations.length || _afterFlushCallbacks.length) {\n // We're yielding because we ran a bunch of computations and we aren't\n // required to finish synchronously, so we'd like to give the event loop a\n // chance. We should flush again soon.\n if (options.finishSynchronously) {\n throw new Error('still have more to do?'); // shouldn't happen\n }\n setTimeout(_requireFlush, 10);\n }\n }\n }\n\n function _requireFlush() {\n if (!_willFlush) {\n setTimeout(_runFlush, 0);\n _willFlush = true;\n }\n }\n\n /******************************** Tracker Base API ******************************************/\n\n /**\n * 函数在响应式模块中执行\n * @param computation\n * @param f\n */\n export function withComputation<T = any>(\n computation: Computation,\n f: ICallback<Computation, T>,\n ): T {\n let previousComputation = _currentComputation;\n _currentComputation = computation;\n try {\n return f.call(null, computation);\n } finally {\n _currentComputation = previousComputation;\n }\n }\n\n /**\n * 函数在非响应式模块中执行\n */\n export function withoutComputation<T = any>(f: ICallback<undefined, T>): T {\n let previousComputation = _currentComputation;\n _currentComputation = undefined;\n try {\n return f(undefined);\n } finally {\n _currentComputation = previousComputation;\n }\n }\n\n export function isActive(): boolean {\n return !!_currentComputation;\n }\n\n export function getCurrentComputation(): Computation | undefined {\n return _currentComputation;\n }\n\n /**\n * Run a function now and rerun it later whenever its dependencies\n * change. Returns a Computation object that can be used to stop or observe the\n * rerunning.\n */\n export function autorun<T = any>(\n f: IComputationCallback<T>,\n options?: { onError: ICallback<Error> },\n ): Computation<T> {\n var c = new Computation<T>(f, _currentComputation, options?.onError);\n\n if (isActive())\n Tracker.onInvalidate(function () {\n c.stop();\n });\n\n return c;\n }\n\n export function onInvalidate(f: ICallback<Computation | undefined>) {\n if (!_currentComputation) {\n throw new Error('Tracker.onInvalidate requires a currentComputation');\n }\n _currentComputation.onInvalidate(f);\n }\n\n /**\n * True if we are computing a computation now, either first time or recompute. This matches Tracker.active unless we are inside Tracker.nonreactive, which nullfies currentComputation even though an enclosing computation may still be running.\n */\n export function inFlush(): boolean {\n return _inFlush;\n }\n\n /**\n * Process all reactive updates immediately and ensure that all invalidated computations are rerun.\n */\n export function flush(options?: Omit<FlushOptions, 'finishSynchronously'>) {\n _runFlush({\n finishSynchronously: true,\n throwFirstError: options && options.throwFirstError,\n });\n }\n\n /**\n * Schedules a function to be called during the next flush, or later in the current flush if one is in progress, after all invalidated computations have been rerun. The function will be run once and not on subsequent flushes unless `afterFlush` is called again.\n */\n export function afterFlush(f: ICallback) {\n _afterFlushCallbacks.push(f);\n _requireFlush();\n }\n\n /********************************************************************************************/\n\n export type IComputationCallback<V = any> = ICallback<Computation, V>;\n\n /**\n * A Computation object represents code that is repeatedly rerun\n * in response to\n * reactive data changes. Computations don't have return values; they just\n * perform actions, such as rerendering a template on the screen. Computations\n * are created using Tracker.autorun. Use stop to prevent further rerunning of a\n * computation.\n */\n export class Computation<V = any> {\n private _onInvalidateCallbacks: IComputationCallback[] = [];\n\n private _onStopCallbacks: IComputationCallback[] = [];\n\n private _recomputing = false;\n\n private _result: V;\n\n /**\n * 是否停止\n */\n public stopped = false;\n\n /**\n * 未开始执行则返回 false\n */\n public invalidated = false;\n\n /**\n * 是否第一次执行\n */\n public firstRun = true;\n\n constructor(\n private _fn: IComputationCallback<V>,\n public readonly parent?: Computation,\n private readonly _onError?: ICallback<Error>,\n ) {\n let hasError = true;\n try {\n this._compute();\n hasError = false;\n } finally {\n this.firstRun = false;\n if (hasError) {\n this.stop();\n }\n }\n }\n\n onInvalidate(f: IComputationCallback): void {\n if (this.invalidated) {\n withoutComputation(f.bind(null, this));\n } else {\n this._onInvalidateCallbacks.push(f);\n }\n }\n\n /**\n * @summary Invalidates this computation so that it will be rerun.\n */\n invalidate() {\n if (!this.invalidated) {\n // if we're currently in _recompute(), don't enqueue\n // ourselves, since we'll rerun immediately anyway.\n if (!this._recomputing && !this.stopped) {\n _requireFlush();\n _pendingComputations.push(this);\n }\n\n this.invalidated = true;\n\n // callbacks can't add callbacks, because\n // this.invalidated === true.\n for (var i = 0, f: IComputationCallback; (f = this._onInvalidateCallbacks[i]); i++) {\n withoutComputation(f.bind(null, this));\n }\n this._onInvalidateCallbacks = [];\n }\n }\n\n /**\n * @summary Prevents this computation from rerunning.\n * @locus Client\n */\n stop() {\n if (!this.stopped) {\n this.stopped = true;\n this.invalidate();\n for (let i = 0, f: IComputationCallback; (f = this._onStopCallbacks[i]); i++) {\n withoutComputation(f.bind(null, this));\n }\n this._onStopCallbacks = [];\n }\n }\n\n onStop(f: IComputationCallback): void {\n if (this.stopped) {\n withoutComputation(f.bind(null, this));\n } else {\n this._onStopCallbacks.push(f);\n }\n }\n\n private _compute(): void {\n this.invalidated = false;\n\n var previousInCompute = _inCompute;\n _inCompute = true;\n try {\n this._result = Tracker.withComputation<V>(this, this._fn);\n } finally {\n _inCompute = previousInCompute;\n }\n }\n\n _needsRecompute() {\n return this.invalidated && !this.stopped;\n }\n\n _recompute() {\n this._recomputing = true;\n try {\n if (this._needsRecompute()) {\n try {\n this._compute();\n } catch (e: any) {\n if (this._onError) {\n this._onError(e);\n } else {\n _throwOrLog('recompute', e);\n }\n }\n }\n } finally {\n this._recomputing = false;\n }\n }\n\n /**\n * @summary Process the reactive updates for this computation immediately\n * and ensure that the computation is rerun. The computation is rerun only\n * if it is invalidated.\n */\n flush() {\n if (this._recomputing) return;\n\n this._recompute();\n }\n\n /**\n * @summary Causes the function inside this computation to run and\n * synchronously process all reactive updtes.\n * @locus Client\n */\n run() {\n this.invalidate();\n this.flush();\n }\n\n get result(): V {\n return this._result;\n }\n }\n\n /**\n * A Dependency represents an atomic unit of reactive data that a\n * computation might depend on. Reactive data sources such as Session or\n * Minimongo internally create different Dependency objects for different\n * pieces of data, each of which may be depended on by multiple computations.\n * When the data changes, the computations are invalidated.\n */\n export class Dependency {\n private _dependents: Set<Computation> = new Set<Computation>();\n\n /**\n * Declares that the current computation (or `fromComputation` if given) depends on `dependency`. The computation will be invalidated the next time `dependency` changes.\n * If there is no current computation and `depend()` is called with no arguments, it does nothing and returns false.\n * Returns true if the computation is a new dependent of `dependency` rather than an existing one.\n */\n depend(computation?: Computation): boolean {\n if (!computation) {\n if (!isActive()) {\n return false;\n }\n computation = _currentComputation;\n }\n if (!this._dependents.has(computation!)) {\n this._dependents.add(computation!);\n computation!.onInvalidate(() => {\n this._dependents.delete(computation!);\n });\n return true;\n }\n return false;\n }\n\n /**\n * Invalidate all dependent computations immediately and remove them as dependents.\n */\n changed() {\n for (const dep of this._dependents) {\n dep.invalidate();\n }\n }\n\n /**\n * True if this Dependency has one or more dependent Computations, which would be invalidated if this Dependency were to change.\n */\n hasDependents() {\n return this._dependents.size !== 0;\n }\n }\n}\n","import { Tracker } from './tracker';\n\ntype IStateEqual = (a: any, b: any) => boolean;\n\nexport class ReactiveBaseState<V> {\n protected _dep = new Tracker.Dependency();\n\n protected _value: V;\n\n protected _isEqual: IStateEqual = (a: any, b: any) => a == b;\n\n protected _addDepend(dep: Tracker.Dependency): void {\n if (Tracker.isActive()) {\n dep.depend();\n }\n }\n\n constructor(initialValue: V, opts?: { isEqual?: IStateEqual }) {\n this._value = initialValue;\n if (opts?.isEqual) {\n this._isEqual = opts.isEqual;\n }\n }\n\n hasDependents(): boolean {\n return this._dep.hasDependents();\n }\n\n get value(): V {\n this._addDepend(this._dep);\n return this._value;\n }\n\n set value(newValue: V) {\n if (!this._isEqual(this._value, newValue)) {\n this._value = newValue;\n this._dep.changed();\n }\n }\n}\n","interface ProxyOptions<V> {\n get?: (target: V, key: string) => any;\n set?: (target: V, key: string, newValue: any) => boolean;\n}\n\nexport function createProxy<V extends Record<string, any>>(target: V, opts: ProxyOptions<V>): V {\n let useProxy = 'Proxy' in window;\n if (process.env.NODE_ENV === 'test') {\n if ((global as any).__ignoreProxy) {\n useProxy = false;\n }\n }\n if (useProxy) {\n return new Proxy<V>(target, opts);\n }\n const result: V = {} as V;\n for (const key in target) {\n Object.defineProperty(result, key, {\n enumerable: true,\n get: opts.get ? () => opts.get!(target, key) : undefined,\n set: opts.set ? (newValue: any) => opts.set!(target, key, newValue) : undefined,\n });\n }\n return result;\n}\n","import { Tracker } from './tracker';\n\nimport Dependency = Tracker.Dependency;\n\nimport { ReactiveBaseState } from './reactive-base-state';\nimport { createProxy } from '../utils/create-proxy';\n\nexport class ReactiveState<V extends Record<string, any>> extends ReactiveBaseState<V> {\n private _keyDeps: Map<string, Dependency> = new Map();\n\n set(key: keyof V & string, value: any): boolean {\n this._ensureKey(key);\n const oldValue = this._value[key];\n if (!this._isEqual(oldValue, value)) {\n this._value[key] = value;\n this._keyDeps.get(key)!.changed();\n return true;\n }\n return false;\n }\n\n get(key: keyof V & string) {\n this._ensureKey(key);\n this._addDepend(this._keyDeps.get(key)!);\n return this._value[key];\n }\n\n protected _ensureKey(key: keyof V & string) {\n if (!this._keyDeps.has(key)) {\n this._keyDeps.set(key, new Dependency());\n }\n }\n\n hasDependents(): boolean {\n if (this._dep.hasDependents()) return true;\n for (const dep of this._keyDeps.values()) {\n if (dep.hasDependents()) return true;\n }\n return false;\n }\n\n keys(): string[] {\n return Object.keys(this._value);\n }\n\n set value(newValue: V) {\n if (!this._isEqual(this._value, newValue)) {\n this._value = newValue;\n this._keyDeps.clear();\n this._dep.changed();\n }\n }\n\n private _proxyValue: V;\n\n get value(): V {\n this._addDepend(this._dep);\n if (!this._proxyValue) {\n this._proxyValue = createProxy<V>(this._value, {\n get: (target, key: string) => this.get(key),\n set: (target, key: string, newValue) => {\n this.set(key, newValue);\n return true;\n },\n });\n }\n return this._proxyValue;\n }\n\n private _proxyReadonlyValue: V;\n\n get readonlyValue(): Readonly<V> {\n this._addDepend(this._dep);\n if (!this._proxyReadonlyValue) {\n this._proxyReadonlyValue = createProxy(this._value, {\n get: (target, key: string) => this.get(key),\n set: (newValue, key: string) => {\n throw new Error(`[ReactiveState] Cannnot set readonly field \"${key}\"`);\n },\n });\n }\n return this._proxyReadonlyValue;\n }\n}\n","import { useMemo } from 'react';\n\nimport { ReactiveState } from '../core/reactive-state';\nimport { useObserve } from './use-observe';\n\nexport function useReactiveState<T extends Record<string, any>>(v: ReactiveState<T> | T): T {\n const state = useMemo<ReactiveState<T>>(\n () => (v instanceof ReactiveState ? v : new ReactiveState(v)),\n [],\n );\n return useObserve<T>(state.value);\n}\n","import { useCallback, useEffect, useMemo } from 'react';\n\nimport { useRefresh } from '@flowgram.ai/utils';\n\nimport { createProxy } from '../utils/create-proxy';\nimport { Tracker } from '../core/tracker';\n\nimport Computation = Tracker.Computation;\n\nexport function useObserve<T extends Record<string, any>>(value: T | undefined): T {\n const refresh = useRefresh();\n const computationMap = useMemo<Map<string, Computation>>(() => new Map(), []);\n const clear = useCallback(() => {\n computationMap.forEach((comp) => comp.stop());\n computationMap.clear();\n }, []);\n useEffect(() => clear, []);\n // 重新渲染需要清空依赖\n clear();\n return useMemo(() => {\n if (value === undefined) return {} as T;\n return createProxy(value, {\n get(target, key: string) {\n let computation = computationMap.get(key);\n if (!computation) {\n computation = new Tracker.Computation((c) => {\n if (!c.firstRun) {\n refresh();\n return;\n }\n return value[key];\n });\n computationMap.set(key, computation);\n }\n return value[key];\n },\n });\n }, [value]);\n}\n","import { ReactiveState } from '../core/reactive-state';\nimport { useObserve } from './use-observe';\n\nexport function useReadonlyReactiveState<T extends Record<string, any>>(\n state: ReactiveState<T>,\n): Readonly<T> {\n return useObserve<T>(state.readonlyValue);\n}\n","import React, { useRef, useEffect } from 'react';\n\nimport { useRefresh } from '@flowgram.ai/utils';\n\nimport { Tracker } from '../core/tracker';\n\nimport Computation = Tracker.Computation;\n\nexport function observe<T = any>(fc: React.FC<T>): React.FC<T> {\n return function ReactiveObserver(props: T) {\n const childrenRef = useRef<React.ReactElement<any, any> | null>();\n const computationRef = useRef<Computation | undefined>();\n const refresh = useRefresh();\n computationRef.current?.stop();\n computationRef.current = new Tracker.Computation(c => {\n if (c.firstRun) {\n childrenRef.current = fc(props);\n } else {\n refresh();\n }\n });\n useEffect(\n () => () => {\n computationRef.current?.stop();\n },\n [],\n );\n return childrenRef.current!;\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA,oBAAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACYO,IAAU;AAAA,CAAV,CAAUC,aAAV;AACL,QAAM,uBAAsC,CAAC;AAC7C,QAAM,uBAAoC,CAAC;AAE3C,MAAI,aAAa;AAEjB,MAAI,WAAW;AAKf,MAAI,aAAa;AACjB,MAAI,sBAA+C;AAMnD,MAAI,mBAAmB;AAOvB,WAAS,YAAY,KAAa,GAAQ;AACxC,QAAI,kBAAkB;AACpB,YAAM;AAAA,IACR,OAAO;AACL,cAAQ,MAAM,mBAAmB,GAAG,IAAI,CAAC;AAAA,IAC3C;AAAA,EACF;AAKA,WAAS,UAAU,SAAwB;AAOzC,QAAI,QAAQ,EAAG,OAAM,IAAI,MAAM,yCAAyC;AAExE,QAAI,WAAY,OAAM,IAAI,MAAM,oCAAoC;AAEpE,cAAU,WAAW,CAAC;AAEtB,eAAW;AACX,iBAAa;AACb,uBAAmB,CAAC,CAAC,QAAQ;AAE7B,QAAI,kBAAkB;AACtB,QAAI,cAAc;AAClB,QAAI;AACF,aAAO,qBAAqB,UAAU,qBAAqB,QAAQ;AAEjE,eAAO,qBAAqB,QAAQ;AAClC,cAAI,OAAO,qBAAqB,MAAM;AACtC,eAAK,WAAW;AAChB,cAAI,KAAK,gBAAgB,GAAG;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D,GAA4B;AAC1F,QAAM,YAAQ;AAAA,IACZ,MAAO,aAAa,gBAAgB,IAAI,IAAI,cAAc,CAAC;AAAA,IAC3D,CAAC;AAAA,EACH;AACA,SAAO,WAAc,MAAM,KAAK;AAClC;;;AERO,SAAS,yBACd,OACa;AACb,SAAO,WAAc,MAAM,aAAa;AAC1C;;;ACPA,IAAAC,gBAAyC;AAEzC,IAAAC,gBAA2B;AAMpB,SAAS,QAAiB,IAA8B;AAC7D,SAAO,SAAS,iBAAiB,OAAU;AACzC,UAAM,kBAAc,sBAA4C;AAChE,UAAM,qBAAiB,sBAAgC;AACvD,UAAM,cAAU,0BAAW;AAC3B,mBAAe,SAAS,KAAK;AAC7B,mBAAe,UAAU,IAAI,QAAQ,YAAY,OAAK;AACpD,UAAI,EAAE,UAAU;AACd,oBAAY,UAAU,GAAG,KAAK;AAAA,MAChC,OAAO;AACL,gBAAQ;AAAA,MACV;AAAA,IACF,CAAC;AACD;AAAA,MACE,MAAM,MAAM;AACV,uBAAe,SAAS,KAAK;AAAA,MAC/B;AAAA,MACA,CAAC;AAAA,IACH;AACA,WAAO,YAAY;AAAA,EACrB;AACF;;;ARpBO,IAAM,EAAE,YAAAC,aAAY,YAAY,IAAI;","names":["Dependency","Tracker","Computation","Dependency","import_react","import_react","import_utils","Dependency"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/core/tracker.ts","../src/core/reactive-base-state.ts","../src/utils/create-proxy.ts","../src/core/reactive-state.ts","../src/hooks/use-reactive-state.ts","../src/hooks/use-observe.ts","../src/hooks/use-readonly-reactive-state.ts","../src/react/observe.tsx"],"sourcesContent":["import { Tracker } from './core/tracker';\n\nexport { Tracker } from './core/tracker';\nexport { ReactiveState } from './core/reactive-state';\nexport { ReactiveBaseState } from './core/reactive-base-state';\nexport { useReactiveState } from './hooks/use-reactive-state';\nexport { useReadonlyReactiveState } from './hooks/use-readonly-reactive-state';\nexport { useObserve } from './hooks/use-observe';\nexport { observe } from './react/observe';\nexport const { Dependency, Computation } = Tracker;\n","/**\n * Fork from: https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js\n */\ntype ICallback<ARG = void, RET = void> = (arg: ARG) => RET;\n\n/**\n * Tracker 是一套 响应式依赖追踪 库,来源于 Meteor.Tracker\n * https://docs.meteor.com/api/Tracker.html#tracker-autorun-and-async-callbacks\n * https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js\n *\n * 相关论文:https://dl.acm.org/doi/fullHtml/10.1145/3184558.3185978\n */\nexport namespace Tracker {\n const _pendingComputations: Computation[] = [];\n const _afterFlushCallbacks: ICallback[] = [];\n // `true` if a Tracker.flush is scheduled, or if we are in Tracker.flush now\n let _willFlush = false;\n // `true` if we are in Tracker.flush now\n let _inFlush = false;\n // `true` if we are computing a computation now, either first time\n // or recompute. This matches Tracker.active unless we are inside\n // Tracker.nonreactive, which nullfies currentComputation even though\n // an enclosing computation may still be running.\n let _inCompute = false;\n let _currentComputation: Computation | undefined = undefined;\n // `true` if the `_throwFirstError` option was passed in to the call\n // to Tracker.flush that we are in. When set, throw rather than log the\n // first error encountered while flushing. Before throwing the error,\n // finish flushing (from a finally block), logging any subsequent\n // errors.\n let _throwFirstError = false;\n\n export interface FlushOptions {\n finishSynchronously?: boolean;\n throwFirstError?: boolean;\n }\n\n function _throwOrLog(msg: string, e: any) {\n if (_throwFirstError) {\n throw e;\n } else {\n console.error(`[Tracker error] ${msg}`, e);\n }\n }\n\n // Run all pending computations and afterFlush callbacks. If we were not called\n // directly via Tracker.flush, this may return before they're all done to allow\n // the event loop to run a little before continuing.\n function _runFlush(options?: FlushOptions) {\n // Nested flush could plausibly happen if, say, a flush causes\n // DOM mutation, which causes a \"blur\" event, which runs an\n // app event handler that calls Tracker.flush. At the moment\n // Spark blocks event handlers during DOM mutation anyway,\n // because the LiveRange tree isn't valid. And we don't have\n // any useful notion of a nested flush.\n if (inFlush()) throw new Error(\"Can't call Tracker.flush while flushing\");\n\n if (_inCompute) throw new Error(\"Can't flush inside Tracker.autorun\");\n\n options = options || {};\n\n _inFlush = true;\n _willFlush = true;\n _throwFirstError = !!options.throwFirstError;\n\n var recomputedCount = 0;\n var finishedTry = false;\n try {\n while (_pendingComputations.length || _afterFlushCallbacks.length) {\n // recompute all pending computations\n while (_pendingComputations.length) {\n var comp = _pendingComputations.shift()!;\n comp._recompute();\n if (comp._needsRecompute()) {\n _pendingComputations.unshift(comp);\n }\n\n if (!options.finishSynchronously && ++recomputedCount > 100) {\n finishedTry = true;\n return;\n }\n }\n\n if (_afterFlushCallbacks.length) {\n // call one afterFlush callback, which may\n // invalidate more computations\n var func = _afterFlushCallbacks.shift()!;\n try {\n func();\n } catch (e: any) {\n _throwOrLog('afterFlush', e);\n }\n }\n }\n finishedTry = true;\n } finally {\n if (!finishedTry) {\n // we're erroring due to throwFirstError being true.\n _inFlush = false; // needed before calling `Tracker.flush()` again\n // finish flushing\n _runFlush({\n finishSynchronously: options.finishSynchronously,\n throwFirstError: false,\n });\n }\n _willFlush = false;\n _inFlush = false;\n if (_pendingComputations.length || _afterFlushCallbacks.length) {\n // We're yielding because we ran a bunch of computations and we aren't\n // required to finish synchronously, so we'd like to give the event loop a\n // chance. We should flush again soon.\n if (options.finishSynchronously) {\n throw new Error('still have more to do?'); // shouldn't happen\n }\n setTimeout(_requireFlush, 10);\n }\n }\n }\n\n function _requireFlush() {\n if (!_willFlush) {\n setTimeout(_runFlush, 0);\n _willFlush = true;\n }\n }\n\n /******************************** Tracker Base API ******************************************/\n\n /**\n * 函数在响应式模块中执行\n * @param computation\n * @param f\n */\n export function withComputation<T = any>(\n computation: Computation,\n f: ICallback<Computation, T>,\n ): T {\n let previousComputation = _currentComputation;\n _currentComputation = computation;\n try {\n return f.call(null, computation);\n } finally {\n _currentComputation = previousComputation;\n }\n }\n\n /**\n * 函数在非响应式模块中执行\n */\n export function withoutComputation<T = any>(f: ICallback<undefined, T>): T {\n let previousComputation = _currentComputation;\n _currentComputation = undefined;\n try {\n return f(undefined);\n } finally {\n _currentComputation = previousComputation;\n }\n }\n\n export function isActive(): boolean {\n return !!_currentComputation;\n }\n\n export function getCurrentComputation(): Computation | undefined {\n return _currentComputation;\n }\n\n /**\n * Run a function now and rerun it later whenever its dependencies\n * change. Returns a Computation object that can be used to stop or observe the\n * rerunning.\n */\n export function autorun<T = any>(\n f: IComputationCallback<T>,\n options?: { onError: ICallback<Error> },\n ): Computation<T> {\n var c = new Computation<T>(f, _currentComputation, options?.onError);\n\n if (isActive())\n Tracker.onInvalidate(function () {\n c.stop();\n });\n\n return c;\n }\n\n export function onInvalidate(f: ICallback<Computation | undefined>) {\n if (!_currentComputation) {\n throw new Error('Tracker.onInvalidate requires a currentComputation');\n }\n _currentComputation.onInvalidate(f);\n }\n\n /**\n * True if we are computing a computation now, either first time or recompute. This matches Tracker.active unless we are inside Tracker.nonreactive, which nullfies currentComputation even though an enclosing computation may still be running.\n */\n export function inFlush(): boolean {\n return _inFlush;\n }\n\n /**\n * Process all reactive updates immediately and ensure that all invalidated computations are rerun.\n */\n export function flush(options?: Omit<FlushOptions, 'finishSynchronously'>) {\n _runFlush({\n finishSynchronously: true,\n throwFirstError: options && options.throwFirstError,\n });\n }\n\n /**\n * Schedules a function to be called during the next flush, or later in the current flush if one is in progress, after all invalidated computations have been rerun. The function will be run once and not on subsequent flushes unless `afterFlush` is called again.\n */\n export function afterFlush(f: ICallback) {\n _afterFlushCallbacks.push(f);\n _requireFlush();\n }\n\n /********************************************************************************************/\n\n export type IComputationCallback<V = any> = ICallback<Computation, V>;\n\n /**\n * A Computation object represents code that is repeatedly rerun\n * in response to\n * reactive data changes. Computations don't have return values; they just\n * perform actions, such as rerendering a template on the screen. Computations\n * are created using Tracker.autorun. Use stop to prevent further rerunning of a\n * computation.\n */\n export class Computation<V = any> {\n private _onInvalidateCallbacks: IComputationCallback[] = [];\n\n private _onStopCallbacks: IComputationCallback[] = [];\n\n private _recomputing = false;\n\n private _result: V;\n\n /**\n * 是否停止\n */\n public stopped = false;\n\n /**\n * 未开始执行则返回 false\n */\n public invalidated = false;\n\n /**\n * 是否第一次执行\n */\n public firstRun = true;\n\n constructor(\n private _fn: IComputationCallback<V>,\n public readonly parent?: Computation,\n private readonly _onError?: ICallback<Error>,\n ) {\n let hasError = true;\n try {\n this._compute();\n hasError = false;\n } finally {\n this.firstRun = false;\n if (hasError) {\n this.stop();\n }\n }\n }\n\n onInvalidate(f: IComputationCallback): void {\n if (this.invalidated) {\n withoutComputation(f.bind(null, this));\n } else {\n this._onInvalidateCallbacks.push(f);\n }\n }\n\n /**\n * @summary Invalidates this computation so that it will be rerun.\n */\n invalidate() {\n if (!this.invalidated) {\n // if we're currently in _recompute(), don't enqueue\n // ourselves, since we'll rerun immediately anyway.\n if (!this._recomputing && !this.stopped) {\n _requireFlush();\n _pendingComputations.push(this);\n }\n\n this.invalidated = true;\n\n // callbacks can't add callbacks, because\n // this.invalidated === true.\n for (var i = 0, f: IComputationCallback; (f = this._onInvalidateCallbacks[i]); i++) {\n withoutComputation(f.bind(null, this));\n }\n this._onInvalidateCallbacks = [];\n }\n }\n\n /**\n * @summary Prevents this computation from rerunning.\n * @locus Client\n */\n stop() {\n if (!this.stopped) {\n this.stopped = true;\n this.invalidate();\n for (let i = 0, f: IComputationCallback; (f = this._onStopCallbacks[i]); i++) {\n withoutComputation(f.bind(null, this));\n }\n this._onStopCallbacks = [];\n }\n }\n\n onStop(f: IComputationCallback): void {\n if (this.stopped) {\n withoutComputation(f.bind(null, this));\n } else {\n this._onStopCallbacks.push(f);\n }\n }\n\n private _compute(): void {\n this.invalidated = false;\n\n var previousInCompute = _inCompute;\n _inCompute = true;\n try {\n this._result = Tracker.withComputation<V>(this, this._fn);\n } finally {\n _inCompute = previousInCompute;\n }\n }\n\n _needsRecompute() {\n return this.invalidated && !this.stopped;\n }\n\n _recompute() {\n this._recomputing = true;\n try {\n if (this._needsRecompute()) {\n try {\n this._compute();\n } catch (e: any) {\n if (this._onError) {\n this._onError(e);\n } else {\n _throwOrLog('recompute', e);\n }\n }\n }\n } finally {\n this._recomputing = false;\n }\n }\n\n /**\n * @summary Process the reactive updates for this computation immediately\n * and ensure that the computation is rerun. The computation is rerun only\n * if it is invalidated.\n */\n flush() {\n if (this._recomputing) return;\n\n this._recompute();\n }\n\n /**\n * @summary Causes the function inside this computation to run and\n * synchronously process all reactive updtes.\n * @locus Client\n */\n run() {\n this.invalidate();\n this.flush();\n }\n\n get result(): V {\n return this._result;\n }\n }\n\n /**\n * A Dependency represents an atomic unit of reactive data that a\n * computation might depend on. Reactive data sources such as Session or\n * Minimongo internally create different Dependency objects for different\n * pieces of data, each of which may be depended on by multiple computations.\n * When the data changes, the computations are invalidated.\n */\n export class Dependency {\n private _dependents: Set<Computation> = new Set<Computation>();\n\n /**\n * Declares that the current computation (or `fromComputation` if given) depends on `dependency`. The computation will be invalidated the next time `dependency` changes.\n * If there is no current computation and `depend()` is called with no arguments, it does nothing and returns false.\n * Returns true if the computation is a new dependent of `dependency` rather than an existing one.\n */\n depend(computation?: Computation): boolean {\n if (!computation) {\n if (!isActive()) {\n return false;\n }\n computation = _currentComputation;\n }\n if (!this._dependents.has(computation!)) {\n this._dependents.add(computation!);\n computation!.onInvalidate(() => {\n this._dependents.delete(computation!);\n });\n return true;\n }\n return false;\n }\n\n /**\n * Invalidate all dependent computations immediately and remove them as dependents.\n */\n changed() {\n for (const dep of this._dependents) {\n dep.invalidate();\n }\n }\n\n /**\n * True if this Dependency has one or more dependent Computations, which would be invalidated if this Dependency were to change.\n */\n hasDependents() {\n return this._dependents.size !== 0;\n }\n }\n}\n","import { Tracker } from './tracker';\n\ntype IStateEqual = (a: any, b: any) => boolean;\n\nexport class ReactiveBaseState<V> {\n protected _dep = new Tracker.Dependency();\n\n protected _value: V;\n\n protected _isEqual: IStateEqual = (a: any, b: any) => a == b;\n\n protected _addDepend(dep: Tracker.Dependency): void {\n if (Tracker.isActive()) {\n dep.depend();\n }\n }\n\n constructor(initialValue: V, opts?: { isEqual?: IStateEqual }) {\n this._value = initialValue;\n if (opts?.isEqual) {\n this._isEqual = opts.isEqual;\n }\n }\n\n hasDependents(): boolean {\n return this._dep.hasDependents();\n }\n\n get value(): V {\n this._addDepend(this._dep);\n return this._value;\n }\n\n set value(newValue: V) {\n if (!this._isEqual(this._value, newValue)) {\n this._value = newValue;\n this._dep.changed();\n }\n }\n}\n","interface ProxyOptions<V> {\n get?: (target: V, key: string) => any;\n set?: (target: V, key: string, newValue: any) => boolean;\n}\n\nexport function createProxy<V extends Record<string, any>>(target: V, opts: ProxyOptions<V>): V {\n let useProxy = 'Proxy' in window;\n if (process.env.NODE_ENV === 'test') {\n if ((global as any).__ignoreProxy) {\n useProxy = false;\n }\n }\n if (useProxy) {\n return new Proxy<V>(target, opts);\n }\n const result: V = {} as V;\n for (const key in target) {\n Object.defineProperty(result, key, {\n enumerable: true,\n get: opts.get ? () => opts.get!(target, key) : undefined,\n set: opts.set ? (newValue: any) => opts.set!(target, key, newValue) : undefined,\n });\n }\n return result;\n}\n","import { Tracker } from './tracker';\n\nimport Dependency = Tracker.Dependency;\n\nimport { ReactiveBaseState } from './reactive-base-state';\nimport { createProxy } from '../utils/create-proxy';\n\nexport class ReactiveState<V extends Record<string, any>> extends ReactiveBaseState<V> {\n private _keyDeps: Map<string, Dependency> = new Map();\n\n set(key: keyof V & string, value: any): boolean {\n this._ensureKey(key);\n const oldValue = this._value[key];\n if (!this._isEqual(oldValue, value)) {\n this._value[key] = value;\n this._keyDeps.get(key)!.changed();\n return true;\n }\n return false;\n }\n\n get(key: keyof V & string) {\n this._ensureKey(key);\n this._addDepend(this._keyDeps.get(key)!);\n return this._value[key];\n }\n\n protected _ensureKey(key: keyof V & string) {\n if (!this._keyDeps.has(key)) {\n this._keyDeps.set(key, new Dependency());\n }\n }\n\n hasDependents(): boolean {\n if (this._dep.hasDependents()) return true;\n for (const dep of this._keyDeps.values()) {\n if (dep.hasDependents()) return true;\n }\n return false;\n }\n\n keys(): string[] {\n return Object.keys(this._value);\n }\n\n set value(newValue: V) {\n if (!this._isEqual(this._value, newValue)) {\n this._value = newValue;\n this._keyDeps.clear();\n this._dep.changed();\n }\n }\n\n private _proxyValue: V;\n\n get value(): V {\n this._addDepend(this._dep);\n if (!this._proxyValue) {\n this._proxyValue = createProxy<V>(this._value, {\n get: (target, key: string) => this.get(key),\n set: (target, key: string, newValue) => {\n this.set(key, newValue);\n return true;\n },\n });\n }\n return this._proxyValue;\n }\n\n private _proxyReadonlyValue: V;\n\n get readonlyValue(): Readonly<V> {\n this._addDepend(this._dep);\n if (!this._proxyReadonlyValue) {\n this._proxyReadonlyValue = createProxy(this._value, {\n get: (target, key: string) => this.get(key),\n set: (newValue, key: string) => {\n throw new Error(`[ReactiveState] Cannnot set readonly field \"${key}\"`);\n },\n });\n }\n return this._proxyReadonlyValue;\n }\n}\n","import { useMemo } from 'react';\n\nimport { ReactiveState } from '../core/reactive-state';\nimport { useObserve } from './use-observe';\n\nexport function useReactiveState<T extends Record<string, any>>(v: ReactiveState<T> | T): T {\n const state = useMemo<ReactiveState<T>>(\n () => (v instanceof ReactiveState ? v : new ReactiveState(v)),\n [],\n );\n return useObserve<T>(state.value);\n}\n","import { useCallback, useEffect, useMemo } from 'react';\n\nimport { useRefresh } from '@flowgram.ai/utils';\n\nimport { createProxy } from '../utils/create-proxy';\nimport { Tracker } from '../core/tracker';\n\nimport Computation = Tracker.Computation;\n\nexport function useObserve<T extends Record<string, any>>(value: T | undefined): T {\n const refresh = useRefresh();\n const computationMap = useMemo<Map<string, Computation>>(() => new Map(), []);\n const clear = useCallback(() => {\n computationMap.forEach((comp) => comp.stop());\n computationMap.clear();\n }, []);\n useEffect(() => clear, []);\n // 重新渲染需要清空依赖\n clear();\n return useMemo(() => {\n if (value === undefined) return {} as T;\n return createProxy(value, {\n get(target, key: string) {\n let computation = computationMap.get(key);\n if (!computation) {\n computation = new Tracker.Computation((c) => {\n if (!c.firstRun) {\n refresh();\n return;\n }\n return value[key];\n });\n computationMap.set(key, computation);\n }\n return value[key];\n },\n });\n }, [value]);\n}\n","import { ReactiveState } from '../core/reactive-state';\nimport { useObserve } from './use-observe';\n\nexport function useReadonlyReactiveState<T extends Record<string, any>>(\n state: ReactiveState<T>,\n): Readonly<T> {\n return useObserve<T>(state.readonlyValue);\n}\n","import React, { useRef, useEffect } from 'react';\n\nimport { useRefresh } from '@flowgram.ai/utils';\n\nimport { Tracker } from '../core/tracker';\n\nimport Computation = Tracker.Computation;\n\nexport function observe<T = any>(fc: React.FC<T>): React.FC<T> {\n return function ReactiveObserver(props: T) {\n const childrenRef = useRef<React.ReactElement<any, any> | null>();\n const computationRef = useRef<Computation | undefined>();\n const refresh = useRefresh();\n computationRef.current?.stop();\n computationRef.current = new Tracker.Computation(c => {\n if (c.firstRun) {\n childrenRef.current = fc(props);\n } else {\n refresh();\n }\n });\n useEffect(\n () => () => {\n computationRef.current?.stop();\n },\n [],\n );\n return childrenRef.current!;\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA,oBAAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACYO,IAAU;AAAA,CAAV,CAAUC,aAAV;AACL,QAAM,uBAAsC,CAAC;AAC7C,QAAM,uBAAoC,CAAC;AAE3C,MAAI,aAAa;AAEjB,MAAI,WAAW;AAKf,MAAI,aAAa;AACjB,MAAI,sBAA+C;AAMnD,MAAI,mBAAmB;AAOvB,WAAS,YAAY,KAAa,GAAQ;AACxC,QAAI,kBAAkB;AACpB,YAAM;AAAA,IACR,OAAO;AACL,cAAQ,MAAM,mBAAmB,GAAG,IAAI,CAAC;AAAA,IAC3C;AAAA,EACF;AAKA,WAAS,UAAU,SAAwB;AAOzC,QAAI,QAAQ,EAAG,OAAM,IAAI,MAAM,yCAAyC;AAExE,QAAI,WAAY,OAAM,IAAI,MAAM,oCAAoC;AAEpE,cAAU,WAAW,CAAC;AAEtB,eAAW;AACX,iBAAa;AACb,uBAAmB,CAAC,CAAC,QA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R;AAAA,YACF;AACA,mBAAO,MAAM,GAAG;AAAA,UAClB,CAAC;AACD,yBAAe,IAAI,KAAK,WAAW;AAAA,QACrC;AACA,eAAO,MAAM,GAAG;AAAA,MAClB;AAAA,IACF,CAAC;AAAA,EACH,GAAG,CAAC,KAAK,CAAC;AACZ;;;ADjCO,SAAS,iBAAgD,GAA4B;AAC1F,QAAM,YAAQ;AAAA,IACZ,MAAO,aAAa,gBAAgB,IAAI,IAAI,cAAc,CAAC;AAAA,IAC3D,CAAC;AAAA,EACH;AACA,SAAO,WAAc,MAAM,KAAK;AAClC;;;AERO,SAAS,yBACd,OACa;AACb,SAAO,WAAc,MAAM,aAAa;AAC1C;;;ACPA,IAAAC,gBAAyC;AAEzC,IAAAC,gBAA2B;AAMpB,SAAS,QAAiB,IAA8B;AAC7D,SAAO,SAAS,iBAAiB,OAAU;AAT7C;AAUI,UAAM,kBAAc,sBAA4C;AAChE,UAAM,qBAAiB,sBAAgC;AACvD,UAAM,cAAU,0BAAW;AAC3B,yBAAe,YAAf,mBAAwB;AACxB,mBAAe,UAAU,IAAI,QAAQ,YAAY,OAAK;AACpD,UAAI,EAAE,UAAU;AACd,oBAAY,UAAU,GAAG,KAAK;AAAA,MAChC,OAAO;AACL,gBAAQ;AAAA,MACV;AAAA,IACF,CAAC;AACD;AAAA,MACE,MAAM,MAAM;AAtBlB,YAAAC;AAuBQ,SAAAA,MAAA,eAAe,YAAf,gBAAAA,IAAwB;AAAA,MAC1B;AAAA,MACA,CAAC;AAAA,IACH;AACA,WAAO,YAAY;AAAA,EACrB;AACF;;;ARpBO,IAAM,EAAE,YAAAC,aAAY,YAAY,IAAI;","names":["Dependency","Tracker","Computation","Dependency","import_react","import_react","import_utils","_a","Dependency"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flowgram.ai/reactive",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.8",
|
|
4
4
|
"homepage": "https://flowgram.ai/",
|
|
5
5
|
"repository": "https://github.com/bytedance/flowgram.ai",
|
|
6
6
|
"license": "MIT",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@flowgram.ai/utils": "0.1.0-alpha.
|
|
19
|
+
"@flowgram.ai/utils": "0.1.0-alpha.8"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@testing-library/react": "^12",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"tsup": "^8.0.1",
|
|
32
32
|
"typescript": "^5.0.4",
|
|
33
33
|
"vitest": "^0.34.6",
|
|
34
|
-
"@flowgram.ai/ts-config": "0.1.0-alpha.
|
|
35
|
-
"@flowgram.ai/eslint-config": "0.1.0-alpha.
|
|
34
|
+
"@flowgram.ai/ts-config": "0.1.0-alpha.8",
|
|
35
|
+
"@flowgram.ai/eslint-config": "0.1.0-alpha.8"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"react": ">=
|
|
39
|
-
"react-dom": ">=
|
|
38
|
+
"react": ">=16.8",
|
|
39
|
+
"react-dom": ">=16.8"
|
|
40
40
|
},
|
|
41
41
|
"publishConfig": {
|
|
42
42
|
"access": "public",
|