@flowgram-vue/reactive 0.2.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/LICENSE +22 -0
- package/README.md +38 -0
- package/dist/index.cjs +526 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +215 -0
- package/dist/index.js +517 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
- package/src/core/reactive-base-state.ts +45 -0
- package/src/core/reactive-state.ts +89 -0
- package/src/core/tracker.ts +440 -0
- package/src/hooks/use-observe.ts +45 -0
- package/src/hooks/use-reactive-state.ts +12 -0
- package/src/hooks/use-readonly-reactive-state.ts +13 -0
- package/src/index.ts +15 -0
- package/src/utils/create-proxy.ts +30 -0
- package/src/vue/observe.ts +58 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
defineComponent,
|
|
8
|
+
getCurrentInstance,
|
|
9
|
+
onBeforeUnmount,
|
|
10
|
+
shallowRef,
|
|
11
|
+
type Component,
|
|
12
|
+
type VNode,
|
|
13
|
+
} from 'vue';
|
|
14
|
+
|
|
15
|
+
import { Tracker } from '../core/tracker';
|
|
16
|
+
|
|
17
|
+
import Computation = Tracker.Computation;
|
|
18
|
+
|
|
19
|
+
export function observe<T = any>(fc: (props: T) => VNode | null | undefined): Component {
|
|
20
|
+
return defineComponent({
|
|
21
|
+
name: 'ReactiveObserver',
|
|
22
|
+
inheritAttrs: false,
|
|
23
|
+
setup(_, { attrs, slots }) {
|
|
24
|
+
const instance = getCurrentInstance();
|
|
25
|
+
const tick = shallowRef(0);
|
|
26
|
+
const childrenRef: { current: VNode | null | undefined } = { current: null };
|
|
27
|
+
const computationRef: { current: Computation | undefined } = { current: undefined };
|
|
28
|
+
const refresh = () => {
|
|
29
|
+
tick.value += 1;
|
|
30
|
+
instance?.update();
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
onBeforeUnmount(() => {
|
|
34
|
+
computationRef.current?.stop();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return () => {
|
|
38
|
+
void tick.value;
|
|
39
|
+
computationRef.current?.stop();
|
|
40
|
+
const slotChildren = slots.default?.();
|
|
41
|
+
const childrenFromSlot =
|
|
42
|
+
slotChildren && slotChildren.length === 1 ? slotChildren[0] : slotChildren;
|
|
43
|
+
const props = {
|
|
44
|
+
...attrs,
|
|
45
|
+
children: childrenFromSlot ?? (attrs as { children?: unknown }).children,
|
|
46
|
+
} as T;
|
|
47
|
+
computationRef.current = new Tracker.Computation((c) => {
|
|
48
|
+
if (c.firstRun) {
|
|
49
|
+
childrenRef.current = fc(props);
|
|
50
|
+
} else {
|
|
51
|
+
refresh();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return childrenRef.current ?? null;
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
}
|