@suey/rxp-meta 0.1.2 → 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.
@@ -44,16 +44,9 @@ class MetadataManager {
44
44
  const vector = this.metadataMap.get(metadataKey);
45
45
  if (!Array.isArray(vector))
46
46
  throw new Error(`defineMetadataInVector: current metadata value is not an array`);
47
- const newVector = vector.map(v => {
48
- if (v === metadata)
49
- hasThisMetadata = true;
50
- return v;
51
- });
52
- if (!hasThisMetadata) {
53
- newVector.push(metadata);
54
- this.metadataMap.set(metadataKey, newVector);
55
- this.rxpInnerStore.update();
56
- }
47
+ const newVector = [...vector, metadata];
48
+ this.metadataMap.set(metadataKey, newVector);
49
+ this.rxpInnerStore.update();
57
50
  }
58
51
  else {
59
52
  this.metadataMap.set(metadataKey, [metadata]);
@@ -92,43 +85,27 @@ class MetadataManager {
92
85
  useMetadata(metadataKey) {
93
86
  const [_, setState] = (0, react_1.useState)({});
94
87
  const normalState = (0, react_1.useRef)({
95
- isMounted: false,
96
88
  data: this.getMetadata(metadataKey),
97
89
  unsubscribe: void 0,
98
- needSync: false
99
90
  });
100
- const refreshComponent = (0, react_1.useCallback)(() => {
101
- if (!normalState.current.isMounted) {
102
- normalState.current.needSync = true;
103
- return;
104
- }
105
- setState(() => ({}));
106
- }, []);
107
- if (!normalState.current.isMounted || !normalState.current.unsubscribe) {
108
- if (normalState.current.unsubscribe)
109
- normalState.current.unsubscribe();
110
- normalState.current.data = this.getMetadata(metadataKey);
91
+ (0, react_1.useEffect)(() => {
111
92
  normalState.current.unsubscribe = this.rxpInnerStore.subscribe(() => {
112
93
  const data = this.getMetadata(metadataKey);
113
94
  if (data !== normalState.current.data) {
114
95
  normalState.current.data = data;
115
- refreshComponent();
96
+ setState(() => ({}));
116
97
  }
117
98
  });
118
- }
119
- (0, react_1.useLayoutEffect)(() => {
120
- normalState.current.isMounted = true;
121
- }, []);
122
- (0, react_1.useEffect)(() => {
123
- if (normalState.current.needSync)
99
+ const data = this.getMetadata(metadataKey);
100
+ if (data !== normalState.current.data) {
101
+ normalState.current.data = data;
124
102
  setState(() => ({}));
103
+ }
125
104
  return () => {
126
- normalState.current.isMounted = false;
127
105
  if (normalState.current.unsubscribe)
128
106
  normalState.current.unsubscribe();
129
107
  normalState.current.unsubscribe = void 0;
130
108
  normalState.current.data = null;
131
- normalState.current.needSync = false;
132
109
  };
133
110
  }, []);
134
111
  return normalState.current.data;
@@ -152,6 +129,12 @@ class MetadataManager {
152
129
  hasMetadata(metadataKey) {
153
130
  return this.metadataMap.has(metadataKey);
154
131
  }
132
+ hasMetadataInVector(metadataKey, metadata) {
133
+ const vec = this.metadataMap.get(metadataKey);
134
+ if (!Array.isArray(vec))
135
+ return false;
136
+ return vec.some(value => value === metadata);
137
+ }
155
138
  useFollowMetadata(metadataKey, metadata) {
156
139
  (0, react_1.useEffect)(() => {
157
140
  const destroy = this.defineMetadata(metadataKey, metadata);
@@ -1,4 +1,4 @@
1
- import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
1
+ import { useEffect, useRef, useState } from 'react';
2
2
  import { RxpInnerStore } from '../base/index';
3
3
  export class MetadataManager {
4
4
  rxpInnerStore = new RxpInnerStore();
@@ -41,16 +41,9 @@ export class MetadataManager {
41
41
  const vector = this.metadataMap.get(metadataKey);
42
42
  if (!Array.isArray(vector))
43
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;
48
- });
49
- if (!hasThisMetadata) {
50
- newVector.push(metadata);
51
- this.metadataMap.set(metadataKey, newVector);
52
- this.rxpInnerStore.update();
53
- }
44
+ const newVector = [...vector, metadata];
45
+ this.metadataMap.set(metadataKey, newVector);
46
+ this.rxpInnerStore.update();
54
47
  }
55
48
  else {
56
49
  this.metadataMap.set(metadataKey, [metadata]);
@@ -89,43 +82,27 @@ export class MetadataManager {
89
82
  useMetadata(metadataKey) {
90
83
  const [_, setState] = useState({});
91
84
  const normalState = useRef({
92
- isMounted: false,
93
85
  data: this.getMetadata(metadataKey),
94
86
  unsubscribe: void 0,
95
- needSync: false
96
87
  });
97
- const refreshComponent = useCallback(() => {
98
- if (!normalState.current.isMounted) {
99
- normalState.current.needSync = true;
100
- return;
101
- }
102
- setState(() => ({}));
103
- }, []);
104
- if (!normalState.current.isMounted || !normalState.current.unsubscribe) {
105
- if (normalState.current.unsubscribe)
106
- normalState.current.unsubscribe();
107
- normalState.current.data = this.getMetadata(metadataKey);
88
+ useEffect(() => {
108
89
  normalState.current.unsubscribe = this.rxpInnerStore.subscribe(() => {
109
90
  const data = this.getMetadata(metadataKey);
110
91
  if (data !== normalState.current.data) {
111
92
  normalState.current.data = data;
112
- refreshComponent();
93
+ setState(() => ({}));
113
94
  }
114
95
  });
115
- }
116
- useLayoutEffect(() => {
117
- normalState.current.isMounted = true;
118
- }, []);
119
- useEffect(() => {
120
- if (normalState.current.needSync)
96
+ const data = this.getMetadata(metadataKey);
97
+ if (data !== normalState.current.data) {
98
+ normalState.current.data = data;
121
99
  setState(() => ({}));
100
+ }
122
101
  return () => {
123
- normalState.current.isMounted = false;
124
102
  if (normalState.current.unsubscribe)
125
103
  normalState.current.unsubscribe();
126
104
  normalState.current.unsubscribe = void 0;
127
105
  normalState.current.data = null;
128
- normalState.current.needSync = false;
129
106
  };
130
107
  }, []);
131
108
  return normalState.current.data;
@@ -149,6 +126,12 @@ export class MetadataManager {
149
126
  hasMetadata(metadataKey) {
150
127
  return this.metadataMap.has(metadataKey);
151
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
+ }
152
135
  useFollowMetadata(metadataKey, metadata) {
153
136
  useEffect(() => {
154
137
  const destroy = this.defineMetadata(metadataKey, metadata);
@@ -147,6 +147,7 @@ export declare class MetadataManager<MetadataEntries extends Record<string, any>
147
147
  * 查看是否定义了元数据
148
148
  */
149
149
  hasMetadata<MetadataKey extends keyof MetadataEntries>(metadataKey: MetadataKey): boolean;
150
+ hasMetadataInVector<MetadataKey extends keyof ExtractVectorEntries<MetadataEntries>>(metadataKey: MetadataKey, metadata: ExtractElInArray<MetadataEntries[MetadataKey]>): boolean;
150
151
  /**
151
152
  * 在组件中注册元数据, 跟随当前组件生命周期, 组件卸载时自动删除
152
153
  */
package/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "@suey/rxp-meta",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "author": "suey",
5
5
  "private": false,
6
6
  "description": "扩展包",
7
7
  "main": "./dist/commonjs/index.js",
8
8
  "keywords": [],
9
9
  "scripts": {
10
+ "typecheck": "tsc --noEmit -p tsconfig.json",
10
11
  "build": "pnpm build-commonjs && pnpm build-esm && pnpm build-types",
11
- "build-commonjs": "tsc -p . --outDir ./dist/commonjs --module commonjs --declaration false --removeComments true",
12
- "build-esm": "tsc -p . --outDir ./dist/esm --module ESNext --declaration false --removeComments true",
13
- "build-types": "tsc -p . --outDir ./dist/lib --emitDeclarationOnly --removeComments false",
14
- "typecheck": "tsc --noEmit -p tsconfig.json"
12
+ "build-esm": "tsc -p . --outDir ./dist/esm --module ESNext --declaration false --removeComments true --incremental false",
13
+ "build-commonjs": "tsc -p . --outDir ./dist/commonjs --module commonjs --declaration false --removeComments true --incremental false",
14
+ "build-types": "tsc -p . --outDir ./dist/lib --emitDeclarationOnly --removeComments false --incremental false",
15
+ "test": "jest -o",
16
+ "test:all": "jest",
17
+ "runner": "pnpm -C ./runner dev"
15
18
  },
16
19
  "module": "./dist/esm/index.js",
17
20
  "types": "./dist/lib/index.js",