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