@suey/rxp-meta 0.1.1 → 0.1.3

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]);
@@ -97,6 +90,8 @@ class MetadataManager {
97
90
  unsubscribe: void 0,
98
91
  needSync: false
99
92
  });
93
+ if (!normalState.current.data)
94
+ normalState.current.data = this.getMetadata(metadataKey);
100
95
  const refreshComponent = (0, react_1.useCallback)(() => {
101
96
  if (!normalState.current.isMounted) {
102
97
  normalState.current.needSync = true;
@@ -116,10 +111,12 @@ class MetadataManager {
116
111
  }
117
112
  });
118
113
  }
119
- (0, react_1.useLayoutEffect)(() => {
120
- normalState.current.isMounted = true;
121
- }, []);
122
114
  (0, react_1.useEffect)(() => {
115
+ normalState.current.isMounted = true;
116
+ const data = this.getMetadata(metadataKey);
117
+ if (data !== normalState.current.data) {
118
+ normalState.current.data = data;
119
+ }
123
120
  if (normalState.current.needSync)
124
121
  setState(() => ({}));
125
122
  return () => {
@@ -152,6 +149,12 @@ class MetadataManager {
152
149
  hasMetadata(metadataKey) {
153
150
  return this.metadataMap.has(metadataKey);
154
151
  }
152
+ hasMetadataInVector(metadataKey, metadata) {
153
+ const vec = this.metadataMap.get(metadataKey);
154
+ if (!Array.isArray(vec))
155
+ return false;
156
+ return vec.some(value => value === metadata);
157
+ }
155
158
  useFollowMetadata(metadataKey, metadata) {
156
159
  (0, react_1.useEffect)(() => {
157
160
  const destroy = this.defineMetadata(metadataKey, metadata);
@@ -1,4 +1,4 @@
1
- import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
1
+ import { useCallback, 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]);
@@ -94,6 +87,8 @@ export class MetadataManager {
94
87
  unsubscribe: void 0,
95
88
  needSync: false
96
89
  });
90
+ if (!normalState.current.data)
91
+ normalState.current.data = this.getMetadata(metadataKey);
97
92
  const refreshComponent = useCallback(() => {
98
93
  if (!normalState.current.isMounted) {
99
94
  normalState.current.needSync = true;
@@ -113,10 +108,12 @@ export class MetadataManager {
113
108
  }
114
109
  });
115
110
  }
116
- useLayoutEffect(() => {
117
- normalState.current.isMounted = true;
118
- }, []);
119
111
  useEffect(() => {
112
+ normalState.current.isMounted = true;
113
+ const data = this.getMetadata(metadataKey);
114
+ if (data !== normalState.current.data) {
115
+ normalState.current.data = data;
116
+ }
120
117
  if (normalState.current.needSync)
121
118
  setState(() => ({}));
122
119
  return () => {
@@ -149,6 +146,12 @@ export class MetadataManager {
149
146
  hasMetadata(metadataKey) {
150
147
  return this.metadataMap.has(metadataKey);
151
148
  }
149
+ hasMetadataInVector(metadataKey, metadata) {
150
+ const vec = this.metadataMap.get(metadataKey);
151
+ if (!Array.isArray(vec))
152
+ return false;
153
+ return vec.some(value => value === metadata);
154
+ }
152
155
  useFollowMetadata(metadataKey, metadata) {
153
156
  useEffect(() => {
154
157
  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,18 +1,23 @@
1
1
  {
2
2
  "name": "@suey/rxp-meta",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "author": "suey",
5
5
  "private": false,
6
6
  "description": "扩展包",
7
- "main": "./index.ts",
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
  },
19
+ "module": "./dist/esm/index.js",
20
+ "types": "./dist/lib/index.js",
16
21
  "exports": {
17
22
  ".": {
18
23
  "import": "./dist/esm/index.js",