@flowgram.ai/reactive 0.1.0
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/README.md +48 -0
- package/dist/esm/index.js +518 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.mts +177 -0
- package/dist/index.d.ts +177 -0
- package/dist/index.js +553 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fork from: https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js
|
|
5
|
+
*/
|
|
6
|
+
type ICallback<ARG = void, RET = void> = (arg: ARG) => RET;
|
|
7
|
+
/**
|
|
8
|
+
* Tracker 是一套 响应式依赖追踪 库,来源于 Meteor.Tracker
|
|
9
|
+
* https://docs.meteor.com/api/Tracker.html#tracker-autorun-and-async-callbacks
|
|
10
|
+
* https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js
|
|
11
|
+
*
|
|
12
|
+
* 相关论文:https://dl.acm.org/doi/fullHtml/10.1145/3184558.3185978
|
|
13
|
+
*/
|
|
14
|
+
declare namespace Tracker {
|
|
15
|
+
interface FlushOptions {
|
|
16
|
+
finishSynchronously?: boolean;
|
|
17
|
+
throwFirstError?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/******************************** Tracker Base API ******************************************/
|
|
20
|
+
/**
|
|
21
|
+
* 函数在响应式模块中执行
|
|
22
|
+
* @param computation
|
|
23
|
+
* @param f
|
|
24
|
+
*/
|
|
25
|
+
function withComputation<T = any>(computation: Computation, f: ICallback<Computation, T>): T;
|
|
26
|
+
/**
|
|
27
|
+
* 函数在非响应式模块中执行
|
|
28
|
+
*/
|
|
29
|
+
function withoutComputation<T = any>(f: ICallback<undefined, T>): T;
|
|
30
|
+
function isActive(): boolean;
|
|
31
|
+
function getCurrentComputation(): Computation | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Run a function now and rerun it later whenever its dependencies
|
|
34
|
+
* change. Returns a Computation object that can be used to stop or observe the
|
|
35
|
+
* rerunning.
|
|
36
|
+
*/
|
|
37
|
+
function autorun<T = any>(f: IComputationCallback<T>, options?: {
|
|
38
|
+
onError: ICallback<Error>;
|
|
39
|
+
}): Computation<T>;
|
|
40
|
+
function onInvalidate(f: ICallback<Computation | undefined>): void;
|
|
41
|
+
/**
|
|
42
|
+
* 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.
|
|
43
|
+
*/
|
|
44
|
+
function inFlush(): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Process all reactive updates immediately and ensure that all invalidated computations are rerun.
|
|
47
|
+
*/
|
|
48
|
+
function flush(options?: Omit<FlushOptions, 'finishSynchronously'>): void;
|
|
49
|
+
/**
|
|
50
|
+
* 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.
|
|
51
|
+
*/
|
|
52
|
+
function afterFlush(f: ICallback): void;
|
|
53
|
+
/********************************************************************************************/
|
|
54
|
+
type IComputationCallback<V = any> = ICallback<Computation, V>;
|
|
55
|
+
/**
|
|
56
|
+
* A Computation object represents code that is repeatedly rerun
|
|
57
|
+
* in response to
|
|
58
|
+
* reactive data changes. Computations don't have return values; they just
|
|
59
|
+
* perform actions, such as rerendering a template on the screen. Computations
|
|
60
|
+
* are created using Tracker.autorun. Use stop to prevent further rerunning of a
|
|
61
|
+
* computation.
|
|
62
|
+
*/
|
|
63
|
+
class Computation<V = any> {
|
|
64
|
+
private _fn;
|
|
65
|
+
readonly parent?: Computation<any> | undefined;
|
|
66
|
+
private readonly _onError?;
|
|
67
|
+
private _onInvalidateCallbacks;
|
|
68
|
+
private _onStopCallbacks;
|
|
69
|
+
private _recomputing;
|
|
70
|
+
private _result;
|
|
71
|
+
/**
|
|
72
|
+
* 是否停止
|
|
73
|
+
*/
|
|
74
|
+
stopped: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* 未开始执行则返回 false
|
|
77
|
+
*/
|
|
78
|
+
invalidated: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* 是否第一次执行
|
|
81
|
+
*/
|
|
82
|
+
firstRun: boolean;
|
|
83
|
+
constructor(_fn: IComputationCallback<V>, parent?: Computation<any> | undefined, _onError?: ICallback<Error, void> | undefined);
|
|
84
|
+
onInvalidate(f: IComputationCallback): void;
|
|
85
|
+
/**
|
|
86
|
+
* @summary Invalidates this computation so that it will be rerun.
|
|
87
|
+
*/
|
|
88
|
+
invalidate(): void;
|
|
89
|
+
/**
|
|
90
|
+
* @summary Prevents this computation from rerunning.
|
|
91
|
+
* @locus Client
|
|
92
|
+
*/
|
|
93
|
+
stop(): void;
|
|
94
|
+
onStop(f: IComputationCallback): void;
|
|
95
|
+
private _compute;
|
|
96
|
+
_needsRecompute(): boolean;
|
|
97
|
+
_recompute(): void;
|
|
98
|
+
/**
|
|
99
|
+
* @summary Process the reactive updates for this computation immediately
|
|
100
|
+
* and ensure that the computation is rerun. The computation is rerun only
|
|
101
|
+
* if it is invalidated.
|
|
102
|
+
*/
|
|
103
|
+
flush(): void;
|
|
104
|
+
/**
|
|
105
|
+
* @summary Causes the function inside this computation to run and
|
|
106
|
+
* synchronously process all reactive updtes.
|
|
107
|
+
* @locus Client
|
|
108
|
+
*/
|
|
109
|
+
run(): void;
|
|
110
|
+
get result(): V;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* A Dependency represents an atomic unit of reactive data that a
|
|
114
|
+
* computation might depend on. Reactive data sources such as Session or
|
|
115
|
+
* Minimongo internally create different Dependency objects for different
|
|
116
|
+
* pieces of data, each of which may be depended on by multiple computations.
|
|
117
|
+
* When the data changes, the computations are invalidated.
|
|
118
|
+
*/
|
|
119
|
+
class Dependency {
|
|
120
|
+
private _dependents;
|
|
121
|
+
/**
|
|
122
|
+
* Declares that the current computation (or `fromComputation` if given) depends on `dependency`. The computation will be invalidated the next time `dependency` changes.
|
|
123
|
+
* If there is no current computation and `depend()` is called with no arguments, it does nothing and returns false.
|
|
124
|
+
* Returns true if the computation is a new dependent of `dependency` rather than an existing one.
|
|
125
|
+
*/
|
|
126
|
+
depend(computation?: Computation): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Invalidate all dependent computations immediately and remove them as dependents.
|
|
129
|
+
*/
|
|
130
|
+
changed(): void;
|
|
131
|
+
/**
|
|
132
|
+
* True if this Dependency has one or more dependent Computations, which would be invalidated if this Dependency were to change.
|
|
133
|
+
*/
|
|
134
|
+
hasDependents(): boolean;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type IStateEqual = (a: any, b: any) => boolean;
|
|
139
|
+
declare class ReactiveBaseState<V> {
|
|
140
|
+
protected _dep: Tracker.Dependency;
|
|
141
|
+
protected _value: V;
|
|
142
|
+
protected _isEqual: IStateEqual;
|
|
143
|
+
protected _addDepend(dep: Tracker.Dependency): void;
|
|
144
|
+
constructor(initialValue: V, opts?: {
|
|
145
|
+
isEqual?: IStateEqual;
|
|
146
|
+
});
|
|
147
|
+
hasDependents(): boolean;
|
|
148
|
+
get value(): V;
|
|
149
|
+
set value(newValue: V);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
declare class ReactiveState<V extends Record<string, any>> extends ReactiveBaseState<V> {
|
|
153
|
+
private _keyDeps;
|
|
154
|
+
set(key: keyof V & string, value: any): boolean;
|
|
155
|
+
get(key: keyof V & string): V[keyof V & string];
|
|
156
|
+
protected _ensureKey(key: keyof V & string): void;
|
|
157
|
+
hasDependents(): boolean;
|
|
158
|
+
keys(): string[];
|
|
159
|
+
set value(newValue: V);
|
|
160
|
+
private _proxyValue;
|
|
161
|
+
get value(): V;
|
|
162
|
+
private _proxyReadonlyValue;
|
|
163
|
+
get readonlyValue(): Readonly<V>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
declare function useReactiveState<T extends Record<string, any>>(v: ReactiveState<T> | T): T;
|
|
167
|
+
|
|
168
|
+
declare function useReadonlyReactiveState<T extends Record<string, any>>(state: ReactiveState<T>): Readonly<T>;
|
|
169
|
+
|
|
170
|
+
declare function useObserve<T extends Record<string, any>>(value: T | undefined): T;
|
|
171
|
+
|
|
172
|
+
declare function observe<T = any>(fc: React.FC<T>): React.FC<T>;
|
|
173
|
+
|
|
174
|
+
declare const Dependency: typeof Tracker.Dependency;
|
|
175
|
+
declare const Computation: typeof Tracker.Computation;
|
|
176
|
+
|
|
177
|
+
export { Computation, Dependency, ReactiveBaseState, ReactiveState, Tracker, observe, useObserve, useReactiveState, useReadonlyReactiveState };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fork from: https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js
|
|
5
|
+
*/
|
|
6
|
+
type ICallback<ARG = void, RET = void> = (arg: ARG) => RET;
|
|
7
|
+
/**
|
|
8
|
+
* Tracker 是一套 响应式依赖追踪 库,来源于 Meteor.Tracker
|
|
9
|
+
* https://docs.meteor.com/api/Tracker.html#tracker-autorun-and-async-callbacks
|
|
10
|
+
* https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js
|
|
11
|
+
*
|
|
12
|
+
* 相关论文:https://dl.acm.org/doi/fullHtml/10.1145/3184558.3185978
|
|
13
|
+
*/
|
|
14
|
+
declare namespace Tracker {
|
|
15
|
+
interface FlushOptions {
|
|
16
|
+
finishSynchronously?: boolean;
|
|
17
|
+
throwFirstError?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/******************************** Tracker Base API ******************************************/
|
|
20
|
+
/**
|
|
21
|
+
* 函数在响应式模块中执行
|
|
22
|
+
* @param computation
|
|
23
|
+
* @param f
|
|
24
|
+
*/
|
|
25
|
+
function withComputation<T = any>(computation: Computation, f: ICallback<Computation, T>): T;
|
|
26
|
+
/**
|
|
27
|
+
* 函数在非响应式模块中执行
|
|
28
|
+
*/
|
|
29
|
+
function withoutComputation<T = any>(f: ICallback<undefined, T>): T;
|
|
30
|
+
function isActive(): boolean;
|
|
31
|
+
function getCurrentComputation(): Computation | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Run a function now and rerun it later whenever its dependencies
|
|
34
|
+
* change. Returns a Computation object that can be used to stop or observe the
|
|
35
|
+
* rerunning.
|
|
36
|
+
*/
|
|
37
|
+
function autorun<T = any>(f: IComputationCallback<T>, options?: {
|
|
38
|
+
onError: ICallback<Error>;
|
|
39
|
+
}): Computation<T>;
|
|
40
|
+
function onInvalidate(f: ICallback<Computation | undefined>): void;
|
|
41
|
+
/**
|
|
42
|
+
* 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.
|
|
43
|
+
*/
|
|
44
|
+
function inFlush(): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Process all reactive updates immediately and ensure that all invalidated computations are rerun.
|
|
47
|
+
*/
|
|
48
|
+
function flush(options?: Omit<FlushOptions, 'finishSynchronously'>): void;
|
|
49
|
+
/**
|
|
50
|
+
* 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.
|
|
51
|
+
*/
|
|
52
|
+
function afterFlush(f: ICallback): void;
|
|
53
|
+
/********************************************************************************************/
|
|
54
|
+
type IComputationCallback<V = any> = ICallback<Computation, V>;
|
|
55
|
+
/**
|
|
56
|
+
* A Computation object represents code that is repeatedly rerun
|
|
57
|
+
* in response to
|
|
58
|
+
* reactive data changes. Computations don't have return values; they just
|
|
59
|
+
* perform actions, such as rerendering a template on the screen. Computations
|
|
60
|
+
* are created using Tracker.autorun. Use stop to prevent further rerunning of a
|
|
61
|
+
* computation.
|
|
62
|
+
*/
|
|
63
|
+
class Computation<V = any> {
|
|
64
|
+
private _fn;
|
|
65
|
+
readonly parent?: Computation<any> | undefined;
|
|
66
|
+
private readonly _onError?;
|
|
67
|
+
private _onInvalidateCallbacks;
|
|
68
|
+
private _onStopCallbacks;
|
|
69
|
+
private _recomputing;
|
|
70
|
+
private _result;
|
|
71
|
+
/**
|
|
72
|
+
* 是否停止
|
|
73
|
+
*/
|
|
74
|
+
stopped: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* 未开始执行则返回 false
|
|
77
|
+
*/
|
|
78
|
+
invalidated: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* 是否第一次执行
|
|
81
|
+
*/
|
|
82
|
+
firstRun: boolean;
|
|
83
|
+
constructor(_fn: IComputationCallback<V>, parent?: Computation<any> | undefined, _onError?: ICallback<Error, void> | undefined);
|
|
84
|
+
onInvalidate(f: IComputationCallback): void;
|
|
85
|
+
/**
|
|
86
|
+
* @summary Invalidates this computation so that it will be rerun.
|
|
87
|
+
*/
|
|
88
|
+
invalidate(): void;
|
|
89
|
+
/**
|
|
90
|
+
* @summary Prevents this computation from rerunning.
|
|
91
|
+
* @locus Client
|
|
92
|
+
*/
|
|
93
|
+
stop(): void;
|
|
94
|
+
onStop(f: IComputationCallback): void;
|
|
95
|
+
private _compute;
|
|
96
|
+
_needsRecompute(): boolean;
|
|
97
|
+
_recompute(): void;
|
|
98
|
+
/**
|
|
99
|
+
* @summary Process the reactive updates for this computation immediately
|
|
100
|
+
* and ensure that the computation is rerun. The computation is rerun only
|
|
101
|
+
* if it is invalidated.
|
|
102
|
+
*/
|
|
103
|
+
flush(): void;
|
|
104
|
+
/**
|
|
105
|
+
* @summary Causes the function inside this computation to run and
|
|
106
|
+
* synchronously process all reactive updtes.
|
|
107
|
+
* @locus Client
|
|
108
|
+
*/
|
|
109
|
+
run(): void;
|
|
110
|
+
get result(): V;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* A Dependency represents an atomic unit of reactive data that a
|
|
114
|
+
* computation might depend on. Reactive data sources such as Session or
|
|
115
|
+
* Minimongo internally create different Dependency objects for different
|
|
116
|
+
* pieces of data, each of which may be depended on by multiple computations.
|
|
117
|
+
* When the data changes, the computations are invalidated.
|
|
118
|
+
*/
|
|
119
|
+
class Dependency {
|
|
120
|
+
private _dependents;
|
|
121
|
+
/**
|
|
122
|
+
* Declares that the current computation (or `fromComputation` if given) depends on `dependency`. The computation will be invalidated the next time `dependency` changes.
|
|
123
|
+
* If there is no current computation and `depend()` is called with no arguments, it does nothing and returns false.
|
|
124
|
+
* Returns true if the computation is a new dependent of `dependency` rather than an existing one.
|
|
125
|
+
*/
|
|
126
|
+
depend(computation?: Computation): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Invalidate all dependent computations immediately and remove them as dependents.
|
|
129
|
+
*/
|
|
130
|
+
changed(): void;
|
|
131
|
+
/**
|
|
132
|
+
* True if this Dependency has one or more dependent Computations, which would be invalidated if this Dependency were to change.
|
|
133
|
+
*/
|
|
134
|
+
hasDependents(): boolean;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type IStateEqual = (a: any, b: any) => boolean;
|
|
139
|
+
declare class ReactiveBaseState<V> {
|
|
140
|
+
protected _dep: Tracker.Dependency;
|
|
141
|
+
protected _value: V;
|
|
142
|
+
protected _isEqual: IStateEqual;
|
|
143
|
+
protected _addDepend(dep: Tracker.Dependency): void;
|
|
144
|
+
constructor(initialValue: V, opts?: {
|
|
145
|
+
isEqual?: IStateEqual;
|
|
146
|
+
});
|
|
147
|
+
hasDependents(): boolean;
|
|
148
|
+
get value(): V;
|
|
149
|
+
set value(newValue: V);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
declare class ReactiveState<V extends Record<string, any>> extends ReactiveBaseState<V> {
|
|
153
|
+
private _keyDeps;
|
|
154
|
+
set(key: keyof V & string, value: any): boolean;
|
|
155
|
+
get(key: keyof V & string): V[keyof V & string];
|
|
156
|
+
protected _ensureKey(key: keyof V & string): void;
|
|
157
|
+
hasDependents(): boolean;
|
|
158
|
+
keys(): string[];
|
|
159
|
+
set value(newValue: V);
|
|
160
|
+
private _proxyValue;
|
|
161
|
+
get value(): V;
|
|
162
|
+
private _proxyReadonlyValue;
|
|
163
|
+
get readonlyValue(): Readonly<V>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
declare function useReactiveState<T extends Record<string, any>>(v: ReactiveState<T> | T): T;
|
|
167
|
+
|
|
168
|
+
declare function useReadonlyReactiveState<T extends Record<string, any>>(state: ReactiveState<T>): Readonly<T>;
|
|
169
|
+
|
|
170
|
+
declare function useObserve<T extends Record<string, any>>(value: T | undefined): T;
|
|
171
|
+
|
|
172
|
+
declare function observe<T = any>(fc: React.FC<T>): React.FC<T>;
|
|
173
|
+
|
|
174
|
+
declare const Dependency: typeof Tracker.Dependency;
|
|
175
|
+
declare const Computation: typeof Tracker.Computation;
|
|
176
|
+
|
|
177
|
+
export { Computation, Dependency, ReactiveBaseState, ReactiveState, Tracker, observe, useObserve, useReactiveState, useReadonlyReactiveState };
|