@suey/rxp-meta 0.2.3 → 0.2.4
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/commonjs/index.js +18 -0
- package/dist/commonjs/src/base/InnerZustandStoreManager.js +35 -0
- package/dist/commonjs/src/base/index.js +17 -0
- package/dist/commonjs/src/base/rxpInnerStore.js +66 -0
- package/dist/commonjs/src/constants/index.js +25 -0
- package/dist/commonjs/src/extension/At.js +113 -0
- package/dist/commonjs/src/extension/ExtensionManager.js +115 -0
- package/dist/commonjs/src/extension/ExtensionManager.old.js +156 -0
- package/dist/commonjs/src/extension/declare.js +2 -0
- package/dist/commonjs/src/extension/index.js +5 -0
- package/dist/commonjs/src/metadata/MetadataManager.js +157 -0
- package/dist/commonjs/src/metadata/MetadataManager.old.js +171 -0
- package/dist/commonjs/src/metadata/declare.js +2 -0
- package/dist/commonjs/src/metadata/index.js +5 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/src/base/InnerZustandStoreManager.js +31 -0
- package/dist/esm/src/base/index.js +1 -0
- package/dist/esm/src/base/rxpInnerStore.js +62 -0
- package/dist/esm/src/constants/index.js +22 -0
- package/dist/esm/src/extension/At.js +109 -0
- package/dist/esm/src/extension/ExtensionManager.js +110 -0
- package/dist/esm/src/extension/ExtensionManager.old.js +152 -0
- package/dist/esm/src/extension/declare.js +1 -0
- package/dist/esm/src/extension/index.js +1 -0
- package/dist/esm/src/metadata/MetadataManager.js +153 -0
- package/dist/esm/src/metadata/MetadataManager.old.js +167 -0
- package/dist/esm/src/metadata/declare.js +1 -0
- package/dist/esm/src/metadata/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { InnerZustandStoreManager } from '../base/InnerZustandStoreManager';
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
const ExtensionSymbolTag = Symbol('ExtensionSymbolTag');
|
|
4
|
+
export class ExtensionManager extends InnerZustandStoreManager {
|
|
5
|
+
extNameMapStore = new Map();
|
|
6
|
+
defineExtension(define) {
|
|
7
|
+
if (!Reflect.has(define, 'name'))
|
|
8
|
+
throw new Error(`Extension name is required`);
|
|
9
|
+
if (!(typeof define.name === 'string'))
|
|
10
|
+
throw new Error(`Extension name must be string`);
|
|
11
|
+
if (!Reflect.has(define, 'version'))
|
|
12
|
+
throw new Error(`Extension version is required`);
|
|
13
|
+
if (Reflect.has(define, 'onActivated') && !Reflect.has(define, 'onDeactivated'))
|
|
14
|
+
throw new Error(`Extension lifecycle: onActivated and onDeactivated must be set at same time`);
|
|
15
|
+
if (Reflect.has(define, 'onDeactivated') && !Reflect.has(define, 'onActivated'))
|
|
16
|
+
throw new Error(`Extension lifecycle: onActivated and onDeactivated must be set at same time`);
|
|
17
|
+
const name = define.name;
|
|
18
|
+
const version = define.version;
|
|
19
|
+
const onActivated = define.onActivated;
|
|
20
|
+
const onDeactivated = define.onDeactivated;
|
|
21
|
+
const extension = {
|
|
22
|
+
...define,
|
|
23
|
+
onActivated: async (...args) => {
|
|
24
|
+
if (!this.extNameMapStore.has(name)) {
|
|
25
|
+
console.error(`当前扩展还未注册加载, 但却错误得试图激活`);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const lifecycle = this.extNameMapStore.get(name);
|
|
29
|
+
if (!lifecycle)
|
|
30
|
+
return;
|
|
31
|
+
if (lifecycle.isActivated) {
|
|
32
|
+
console.error(`当前扩展已被激活, 但却错误得试图再次激活`);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
await onActivated?.call(extension, ...args);
|
|
36
|
+
lifecycle.isActivated = true;
|
|
37
|
+
},
|
|
38
|
+
onDeactivated: async (...args) => {
|
|
39
|
+
if (!this.extNameMapStore.has(name)) {
|
|
40
|
+
console.error(`当前扩展还未注册加载, 但却错误得试图去活`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const lifecycle = this.extNameMapStore.get(name);
|
|
44
|
+
if (!lifecycle)
|
|
45
|
+
return;
|
|
46
|
+
if (!lifecycle.isActivated) {
|
|
47
|
+
console.error(`当前扩展未被激活, 但却错误得试图去活`);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
await onDeactivated?.call(extension, ...args);
|
|
51
|
+
lifecycle.isActivated = false;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
Reflect.defineProperty(extension, '__TAG__', {
|
|
55
|
+
enumerable: false,
|
|
56
|
+
writable: false,
|
|
57
|
+
value: ExtensionSymbolTag,
|
|
58
|
+
configurable: false
|
|
59
|
+
});
|
|
60
|
+
return extension;
|
|
61
|
+
}
|
|
62
|
+
isExtension(extension) {
|
|
63
|
+
if (typeof extension !== 'object')
|
|
64
|
+
return false;
|
|
65
|
+
const hasTAG = Reflect.has(extension, '__TAG__');
|
|
66
|
+
return hasTAG;
|
|
67
|
+
}
|
|
68
|
+
hasExtension(extensionName) {
|
|
69
|
+
const has = this.extNameMapStore.has(extensionName);
|
|
70
|
+
const lifecycle = this.extNameMapStore.get(extensionName);
|
|
71
|
+
if (has && !lifecycle)
|
|
72
|
+
throw new Error(`数据状态异常`);
|
|
73
|
+
return has;
|
|
74
|
+
}
|
|
75
|
+
getExtension(extensionName) {
|
|
76
|
+
if (!this.hasExtension(extensionName))
|
|
77
|
+
return null;
|
|
78
|
+
return this.extNameMapStore.get(extensionName)?.extension ?? null;
|
|
79
|
+
}
|
|
80
|
+
registerExtension(extension) {
|
|
81
|
+
if (!this.isExtension(extension))
|
|
82
|
+
throw new Error(`参数非法, 不是一个有效的扩展`);
|
|
83
|
+
const name = extension.name;
|
|
84
|
+
if (this.hasExtension(name)) {
|
|
85
|
+
console.error(`试图重新注册一个已存在得扩展:应该先移除再注册`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const extensionLifecycle = {
|
|
89
|
+
extension: extension,
|
|
90
|
+
isActivated: false,
|
|
91
|
+
};
|
|
92
|
+
this.extNameMapStore.set(name, extensionLifecycle);
|
|
93
|
+
}
|
|
94
|
+
async activatedExtension(name, context) {
|
|
95
|
+
const lifecycle = this.extNameMapStore.get(name);
|
|
96
|
+
if (!lifecycle)
|
|
97
|
+
throw new Error(`试图激活一个不存在得扩展`);
|
|
98
|
+
if (lifecycle.isActivated)
|
|
99
|
+
return;
|
|
100
|
+
if (lifecycle.extension.onActivated)
|
|
101
|
+
await lifecycle.extension.onActivated(context);
|
|
102
|
+
lifecycle.isActivated = true;
|
|
103
|
+
}
|
|
104
|
+
async deactivatedExtension(name, context) {
|
|
105
|
+
const lifecycle = this.extNameMapStore.get(name);
|
|
106
|
+
if (!lifecycle)
|
|
107
|
+
throw new Error(`试图去活一个不存在得扩展`);
|
|
108
|
+
if (!lifecycle.isActivated)
|
|
109
|
+
return;
|
|
110
|
+
if (lifecycle.extension.onDeactivated)
|
|
111
|
+
await lifecycle.extension.onDeactivated(context);
|
|
112
|
+
lifecycle.isActivated = false;
|
|
113
|
+
}
|
|
114
|
+
async delExtension(name, context) {
|
|
115
|
+
const lifecycle = this.extNameMapStore.get(name);
|
|
116
|
+
if (!lifecycle)
|
|
117
|
+
throw new Error(`试图删除一个不存在得扩展`);
|
|
118
|
+
if (lifecycle.isActivated) {
|
|
119
|
+
if (lifecycle.extension.onDeactivated) {
|
|
120
|
+
await lifecycle.extension.onDeactivated(context);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (!lifecycle.isActivated) {
|
|
124
|
+
this.extNameMapStore.delete(name);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
useExtensionsList() {
|
|
128
|
+
const value = this.useStoreValueToRerenderComponent();
|
|
129
|
+
const [statusState] = useState(() => ({
|
|
130
|
+
value: void 0
|
|
131
|
+
}));
|
|
132
|
+
const [normalState] = useState({
|
|
133
|
+
extensions: [],
|
|
134
|
+
});
|
|
135
|
+
if (statusState.value !== value) {
|
|
136
|
+
statusState.value = value;
|
|
137
|
+
const extensions = [];
|
|
138
|
+
for (const ext of this.extNameMapStore.values()) {
|
|
139
|
+
extensions.push(ext.extension);
|
|
140
|
+
}
|
|
141
|
+
normalState.extensions = extensions;
|
|
142
|
+
}
|
|
143
|
+
return [normalState.extensions];
|
|
144
|
+
}
|
|
145
|
+
getExtensions() {
|
|
146
|
+
const extensions = [];
|
|
147
|
+
for (const ext of this.extNameMapStore.values()) {
|
|
148
|
+
extensions.push(ext.extension);
|
|
149
|
+
}
|
|
150
|
+
return extensions;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ExtensionManager } from './ExtensionManager';
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { RxpInnerStore } from '../base/index';
|
|
3
|
+
export class MetadataManager {
|
|
4
|
+
rxpInnerStore = new RxpInnerStore();
|
|
5
|
+
metadataMap = new Map();
|
|
6
|
+
constructor() {
|
|
7
|
+
this.metadataMap.clear();
|
|
8
|
+
}
|
|
9
|
+
defineMetadata(metadataKey, metadata) {
|
|
10
|
+
this.metadataMap.set(metadataKey, metadata);
|
|
11
|
+
this.rxpInnerStore.update();
|
|
12
|
+
return () => {
|
|
13
|
+
this.delMetadata(metadataKey);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
getMetadata(metadataKey) {
|
|
17
|
+
return (this.metadataMap.get(metadataKey)) ?? null;
|
|
18
|
+
}
|
|
19
|
+
getMetadataLatestInVector(metadataKey) {
|
|
20
|
+
return (this.metadataMap.get(metadataKey) ?? [])[this.metadataMap.get(metadataKey)?.length - 1] ?? null;
|
|
21
|
+
}
|
|
22
|
+
getMetadataOldestInVector(metadataKey) {
|
|
23
|
+
return (this.metadataMap.get(metadataKey) ?? [])[0] ?? null;
|
|
24
|
+
}
|
|
25
|
+
delMetadata(metadataKey) {
|
|
26
|
+
if (!this.hasMetadata(metadataKey))
|
|
27
|
+
return;
|
|
28
|
+
this.metadataMap.delete(metadataKey);
|
|
29
|
+
this.rxpInnerStore.update();
|
|
30
|
+
}
|
|
31
|
+
defineMetadataInSingle(metadataKey, metadata) {
|
|
32
|
+
this.metadataMap.set(metadataKey, metadata);
|
|
33
|
+
this.rxpInnerStore.update();
|
|
34
|
+
return () => {
|
|
35
|
+
this.delMetadata(metadataKey);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
defineMetadataInVector(metadataKey, metadata) {
|
|
39
|
+
let hasThisMetadata = false;
|
|
40
|
+
if (this.hasMetadata(metadataKey)) {
|
|
41
|
+
const vector = this.metadataMap.get(metadataKey);
|
|
42
|
+
if (!Array.isArray(vector))
|
|
43
|
+
throw new Error(`defineMetadataInVector: current metadata value is not an array`);
|
|
44
|
+
const newVector = [...vector, metadata];
|
|
45
|
+
this.metadataMap.set(metadataKey, newVector);
|
|
46
|
+
this.rxpInnerStore.update();
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.metadataMap.set(metadataKey, [metadata]);
|
|
50
|
+
this.rxpInnerStore.update();
|
|
51
|
+
}
|
|
52
|
+
return () => this.delMetadataInVector(metadataKey, metadata);
|
|
53
|
+
}
|
|
54
|
+
delMetadataInVector(metadataKey, metadata) {
|
|
55
|
+
if (!this.hasMetadata(metadataKey))
|
|
56
|
+
return;
|
|
57
|
+
let hasThisMetadata = false;
|
|
58
|
+
const vector = this.metadataMap.get(metadataKey) ?? [];
|
|
59
|
+
if (!Array.isArray(vector))
|
|
60
|
+
throw new Error(`delMetadataInVector: current metadata value is not an array`);
|
|
61
|
+
if (vector.length === 0) {
|
|
62
|
+
this.metadataMap.delete(metadataKey);
|
|
63
|
+
this.rxpInnerStore.update();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const fVector = vector.filter(v => {
|
|
67
|
+
const isThisMetadata = (v === metadata);
|
|
68
|
+
if (isThisMetadata)
|
|
69
|
+
hasThisMetadata = true;
|
|
70
|
+
return !isThisMetadata;
|
|
71
|
+
});
|
|
72
|
+
if (hasThisMetadata) {
|
|
73
|
+
if (fVector.length === 0) {
|
|
74
|
+
this.metadataMap.delete(metadataKey);
|
|
75
|
+
this.rxpInnerStore.update();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this.metadataMap.set(metadataKey, fVector);
|
|
79
|
+
this.rxpInnerStore.update();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
useMetadata(metadataKey) {
|
|
83
|
+
const [_, setState] = useState({});
|
|
84
|
+
const normalState = useRef({
|
|
85
|
+
data: this.getMetadata(metadataKey),
|
|
86
|
+
unsubscribe: void 0,
|
|
87
|
+
});
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
normalState.current.unsubscribe = this.rxpInnerStore.subscribe(() => {
|
|
90
|
+
const data = this.getMetadata(metadataKey);
|
|
91
|
+
if (data !== normalState.current.data) {
|
|
92
|
+
normalState.current.data = data;
|
|
93
|
+
setState(() => ({}));
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
const data = this.getMetadata(metadataKey);
|
|
97
|
+
if (data !== normalState.current.data) {
|
|
98
|
+
normalState.current.data = data;
|
|
99
|
+
setState(() => ({}));
|
|
100
|
+
}
|
|
101
|
+
return () => {
|
|
102
|
+
if (normalState.current.unsubscribe)
|
|
103
|
+
normalState.current.unsubscribe();
|
|
104
|
+
normalState.current.unsubscribe = void 0;
|
|
105
|
+
normalState.current.data = null;
|
|
106
|
+
};
|
|
107
|
+
}, []);
|
|
108
|
+
return normalState.current.data;
|
|
109
|
+
}
|
|
110
|
+
useOldestMetadataInVector(metadataKey) {
|
|
111
|
+
const metadata = this.useMetadata(metadataKey);
|
|
112
|
+
if (metadata && Array.isArray(metadata))
|
|
113
|
+
return metadata[0] ?? null;
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
useLatestMetadataInVector(metadataKey) {
|
|
117
|
+
const metadata = this.useMetadata(metadataKey);
|
|
118
|
+
if (metadata && Array.isArray(metadata))
|
|
119
|
+
return metadata[metadata.length - 1] ?? null;
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
useAllMetadata() {
|
|
123
|
+
this.rxpInnerStore.useValueToRenderReactComponent();
|
|
124
|
+
return this.metadataMap;
|
|
125
|
+
}
|
|
126
|
+
hasMetadata(metadataKey) {
|
|
127
|
+
return this.metadataMap.has(metadataKey);
|
|
128
|
+
}
|
|
129
|
+
hasMetadataInVector(metadataKey, metadata) {
|
|
130
|
+
const vec = this.metadataMap.get(metadataKey);
|
|
131
|
+
if (!Array.isArray(vec))
|
|
132
|
+
return false;
|
|
133
|
+
return vec.some(value => value === metadata);
|
|
134
|
+
}
|
|
135
|
+
useFollowMetadata(metadataKey, metadata) {
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
const destroy = this.defineMetadata(metadataKey, metadata);
|
|
138
|
+
return destroy;
|
|
139
|
+
}, [metadataKey, metadata]);
|
|
140
|
+
}
|
|
141
|
+
useFollowMetadataInVector(metadataKey, metadata) {
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
const destroy = this.defineMetadataInVector(metadataKey, metadata);
|
|
144
|
+
return destroy;
|
|
145
|
+
}, [metadataKey, metadata]);
|
|
146
|
+
}
|
|
147
|
+
useFollowMetadataInSingle(metadataKey, metadata) {
|
|
148
|
+
useEffect(() => {
|
|
149
|
+
const destroy = this.defineMetadataInSingle(metadataKey, metadata);
|
|
150
|
+
return destroy;
|
|
151
|
+
}, [metadataKey, metadata]);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
2
|
+
import { InnerZustandStoreManager } from '../base/InnerZustandStoreManager';
|
|
3
|
+
export class MetadataManager extends InnerZustandStoreManager {
|
|
4
|
+
metadataMap = new Map();
|
|
5
|
+
defineMetadata(metadataKey, metadata) {
|
|
6
|
+
this.metadataMap.set(metadataKey, metadata);
|
|
7
|
+
super.updateStore();
|
|
8
|
+
return () => {
|
|
9
|
+
this.delMetadata(metadataKey);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
getMetadata(metadataKey) {
|
|
13
|
+
return (this.metadataMap.get(metadataKey)) ?? null;
|
|
14
|
+
}
|
|
15
|
+
getMetadataLatestInVector(metadataKey) {
|
|
16
|
+
return (this.metadataMap.get(metadataKey) ?? [])[this.metadataMap.get(metadataKey)?.length - 1] ?? null;
|
|
17
|
+
}
|
|
18
|
+
getMetadataOldestInVector(metadataKey) {
|
|
19
|
+
return (this.metadataMap.get(metadataKey) ?? [])[0] ?? null;
|
|
20
|
+
}
|
|
21
|
+
delMetadata(metadataKey) {
|
|
22
|
+
if (!this.hasMetadata(metadataKey))
|
|
23
|
+
return;
|
|
24
|
+
this.metadataMap.delete(metadataKey);
|
|
25
|
+
super.updateStore();
|
|
26
|
+
}
|
|
27
|
+
defineMetadataInSingle(metadataKey, metadata) {
|
|
28
|
+
this.metadataMap.set(metadataKey, metadata);
|
|
29
|
+
super.updateStore();
|
|
30
|
+
return () => {
|
|
31
|
+
this.delMetadata(metadataKey);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
defineMetadataInVector(metadataKey, metadata) {
|
|
35
|
+
let hasThisMetadata = false;
|
|
36
|
+
if (this.hasMetadata(metadataKey)) {
|
|
37
|
+
const vector = this.metadataMap.get(metadataKey);
|
|
38
|
+
if (!Array.isArray(vector))
|
|
39
|
+
throw new Error(`defineMetadataInVector: current metadata value is not an array`);
|
|
40
|
+
const newVector = vector.filter(v => {
|
|
41
|
+
const isThisMetadata = (v === metadata);
|
|
42
|
+
if (isThisMetadata)
|
|
43
|
+
hasThisMetadata = true;
|
|
44
|
+
return !isThisMetadata;
|
|
45
|
+
});
|
|
46
|
+
if (!hasThisMetadata) {
|
|
47
|
+
newVector.push(metadata);
|
|
48
|
+
this.metadataMap.set(metadataKey, newVector);
|
|
49
|
+
super.updateStore();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this.metadataMap.set(metadataKey, [metadata]);
|
|
54
|
+
super.updateStore();
|
|
55
|
+
}
|
|
56
|
+
return () => this.delMetadataInVector(metadataKey, metadata);
|
|
57
|
+
}
|
|
58
|
+
delMetadataInVector(metadataKey, metadata) {
|
|
59
|
+
if (!this.hasMetadata(metadataKey))
|
|
60
|
+
return;
|
|
61
|
+
let hasThisMetadata = false;
|
|
62
|
+
const vector = this.metadataMap.get(metadataKey) ?? [];
|
|
63
|
+
if (!Array.isArray(vector))
|
|
64
|
+
throw new Error(`delMetadataInVector: current metadata value is not an array`);
|
|
65
|
+
if (vector.length === 0) {
|
|
66
|
+
this.metadataMap.delete(metadataKey);
|
|
67
|
+
super.updateStore();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const fVector = vector.filter(v => {
|
|
71
|
+
const isThisMetadata = (v === metadata);
|
|
72
|
+
if (isThisMetadata)
|
|
73
|
+
hasThisMetadata = true;
|
|
74
|
+
return !isThisMetadata;
|
|
75
|
+
});
|
|
76
|
+
if (hasThisMetadata) {
|
|
77
|
+
if (fVector.length === 0) {
|
|
78
|
+
this.metadataMap.delete(metadataKey);
|
|
79
|
+
super.updateStore();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
this.metadataMap.set(metadataKey, fVector);
|
|
83
|
+
super.updateStore();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
useMetadata(metadataKey) {
|
|
87
|
+
const [_, setState] = useState({});
|
|
88
|
+
const normalState = useRef({
|
|
89
|
+
isMounted: false,
|
|
90
|
+
data: this.getMetadata(metadataKey),
|
|
91
|
+
unsubscribe: void 0,
|
|
92
|
+
needSync: false
|
|
93
|
+
});
|
|
94
|
+
const refreshComponent = useCallback(() => {
|
|
95
|
+
if (!normalState.current.isMounted) {
|
|
96
|
+
normalState.current.needSync = true;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
setState(() => ({}));
|
|
100
|
+
}, []);
|
|
101
|
+
if (!normalState.current.isMounted || !normalState.current.unsubscribe) {
|
|
102
|
+
if (normalState.current.unsubscribe)
|
|
103
|
+
normalState.current.unsubscribe();
|
|
104
|
+
normalState.current.data = this.getMetadata(metadataKey);
|
|
105
|
+
normalState.current.unsubscribe = super.subscribe(() => {
|
|
106
|
+
const data = this.getMetadata(metadataKey);
|
|
107
|
+
if (data !== normalState.current.data) {
|
|
108
|
+
normalState.current.data = data;
|
|
109
|
+
refreshComponent();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
useLayoutEffect(() => {
|
|
114
|
+
normalState.current.isMounted = true;
|
|
115
|
+
}, []);
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (normalState.current.needSync)
|
|
118
|
+
setState(() => ({}));
|
|
119
|
+
return () => {
|
|
120
|
+
normalState.current.isMounted = false;
|
|
121
|
+
if (normalState.current.unsubscribe)
|
|
122
|
+
normalState.current.unsubscribe();
|
|
123
|
+
normalState.current.unsubscribe = void 0;
|
|
124
|
+
normalState.current.data = null;
|
|
125
|
+
normalState.current.needSync = false;
|
|
126
|
+
};
|
|
127
|
+
}, []);
|
|
128
|
+
return normalState.current.data;
|
|
129
|
+
}
|
|
130
|
+
useOldestMetadataInVector(metadataKey) {
|
|
131
|
+
const metadata = this.useMetadata(metadataKey);
|
|
132
|
+
if (metadata && Array.isArray(metadata))
|
|
133
|
+
return metadata[0] ?? null;
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
useLatestMetadataInVector(metadataKey) {
|
|
137
|
+
const metadata = this.useMetadata(metadataKey);
|
|
138
|
+
if (metadata && Array.isArray(metadata))
|
|
139
|
+
return metadata[metadata.length - 1] ?? null;
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
useAllMetadata() {
|
|
143
|
+
super.useStoreValueToRerenderComponent();
|
|
144
|
+
return this.metadataMap;
|
|
145
|
+
}
|
|
146
|
+
hasMetadata(metadataKey) {
|
|
147
|
+
return this.metadataMap.has(metadataKey);
|
|
148
|
+
}
|
|
149
|
+
useFollowMetadata(metadataKey, metadata) {
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
const destroy = this.defineMetadata(metadataKey, metadata);
|
|
152
|
+
return destroy;
|
|
153
|
+
}, [metadataKey, metadata]);
|
|
154
|
+
}
|
|
155
|
+
useFollowMetadataInVector(metadataKey, metadata) {
|
|
156
|
+
useEffect(() => {
|
|
157
|
+
const destroy = this.defineMetadataInVector(metadataKey, metadata);
|
|
158
|
+
return destroy;
|
|
159
|
+
}, [metadataKey, metadata]);
|
|
160
|
+
}
|
|
161
|
+
useFollowMetadataInSingle(metadataKey, metadata) {
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
const destroy = this.defineMetadataInSingle(metadataKey, metadata);
|
|
164
|
+
return destroy;
|
|
165
|
+
}, [metadataKey, metadata]);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MetadataManager } from './MetadataManager';
|