@suey/rxp-meta 0.1.3 → 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.
|
@@ -85,47 +85,27 @@ class MetadataManager {
|
|
|
85
85
|
useMetadata(metadataKey) {
|
|
86
86
|
const [_, setState] = (0, react_1.useState)({});
|
|
87
87
|
const normalState = (0, react_1.useRef)({
|
|
88
|
-
isMounted: false,
|
|
89
88
|
data: this.getMetadata(metadataKey),
|
|
90
89
|
unsubscribe: void 0,
|
|
91
|
-
needSync: false
|
|
92
90
|
});
|
|
93
|
-
|
|
94
|
-
normalState.current.data = this.getMetadata(metadataKey);
|
|
95
|
-
const refreshComponent = (0, react_1.useCallback)(() => {
|
|
96
|
-
if (!normalState.current.isMounted) {
|
|
97
|
-
normalState.current.needSync = true;
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
setState(() => ({}));
|
|
101
|
-
}, []);
|
|
102
|
-
if (!normalState.current.isMounted || !normalState.current.unsubscribe) {
|
|
103
|
-
if (normalState.current.unsubscribe)
|
|
104
|
-
normalState.current.unsubscribe();
|
|
105
|
-
normalState.current.data = this.getMetadata(metadataKey);
|
|
91
|
+
(0, react_1.useEffect)(() => {
|
|
106
92
|
normalState.current.unsubscribe = this.rxpInnerStore.subscribe(() => {
|
|
107
93
|
const data = this.getMetadata(metadataKey);
|
|
108
94
|
if (data !== normalState.current.data) {
|
|
109
95
|
normalState.current.data = data;
|
|
110
|
-
|
|
96
|
+
setState(() => ({}));
|
|
111
97
|
}
|
|
112
98
|
});
|
|
113
|
-
}
|
|
114
|
-
(0, react_1.useEffect)(() => {
|
|
115
|
-
normalState.current.isMounted = true;
|
|
116
99
|
const data = this.getMetadata(metadataKey);
|
|
117
100
|
if (data !== normalState.current.data) {
|
|
118
101
|
normalState.current.data = data;
|
|
119
|
-
}
|
|
120
|
-
if (normalState.current.needSync)
|
|
121
102
|
setState(() => ({}));
|
|
103
|
+
}
|
|
122
104
|
return () => {
|
|
123
|
-
normalState.current.isMounted = false;
|
|
124
105
|
if (normalState.current.unsubscribe)
|
|
125
106
|
normalState.current.unsubscribe();
|
|
126
107
|
normalState.current.unsubscribe = void 0;
|
|
127
108
|
normalState.current.data = null;
|
|
128
|
-
normalState.current.needSync = false;
|
|
129
109
|
};
|
|
130
110
|
}, []);
|
|
131
111
|
return normalState.current.data;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
2
|
import { RxpInnerStore } from '../base/index';
|
|
3
3
|
export class MetadataManager {
|
|
4
4
|
rxpInnerStore = new RxpInnerStore();
|
|
@@ -82,47 +82,27 @@ export class MetadataManager {
|
|
|
82
82
|
useMetadata(metadataKey) {
|
|
83
83
|
const [_, setState] = useState({});
|
|
84
84
|
const normalState = useRef({
|
|
85
|
-
isMounted: false,
|
|
86
85
|
data: this.getMetadata(metadataKey),
|
|
87
86
|
unsubscribe: void 0,
|
|
88
|
-
needSync: false
|
|
89
87
|
});
|
|
90
|
-
|
|
91
|
-
normalState.current.data = this.getMetadata(metadataKey);
|
|
92
|
-
const refreshComponent = useCallback(() => {
|
|
93
|
-
if (!normalState.current.isMounted) {
|
|
94
|
-
normalState.current.needSync = true;
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
setState(() => ({}));
|
|
98
|
-
}, []);
|
|
99
|
-
if (!normalState.current.isMounted || !normalState.current.unsubscribe) {
|
|
100
|
-
if (normalState.current.unsubscribe)
|
|
101
|
-
normalState.current.unsubscribe();
|
|
102
|
-
normalState.current.data = this.getMetadata(metadataKey);
|
|
88
|
+
useEffect(() => {
|
|
103
89
|
normalState.current.unsubscribe = this.rxpInnerStore.subscribe(() => {
|
|
104
90
|
const data = this.getMetadata(metadataKey);
|
|
105
91
|
if (data !== normalState.current.data) {
|
|
106
92
|
normalState.current.data = data;
|
|
107
|
-
|
|
93
|
+
setState(() => ({}));
|
|
108
94
|
}
|
|
109
95
|
});
|
|
110
|
-
}
|
|
111
|
-
useEffect(() => {
|
|
112
|
-
normalState.current.isMounted = true;
|
|
113
96
|
const data = this.getMetadata(metadataKey);
|
|
114
97
|
if (data !== normalState.current.data) {
|
|
115
98
|
normalState.current.data = data;
|
|
116
|
-
}
|
|
117
|
-
if (normalState.current.needSync)
|
|
118
99
|
setState(() => ({}));
|
|
100
|
+
}
|
|
119
101
|
return () => {
|
|
120
|
-
normalState.current.isMounted = false;
|
|
121
102
|
if (normalState.current.unsubscribe)
|
|
122
103
|
normalState.current.unsubscribe();
|
|
123
104
|
normalState.current.unsubscribe = void 0;
|
|
124
105
|
normalState.current.data = null;
|
|
125
|
-
normalState.current.needSync = false;
|
|
126
106
|
};
|
|
127
107
|
}, []);
|
|
128
108
|
return normalState.current.data;
|