react-ui-basics 0.0.217 → 0.0.218
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/bundle.css +573 -574
- package/package.json +1 -1
- package/src/store/Store.test.js +36 -6
- package/src/store/Store.ts +9 -5
- package/store/Store.js +9 -5
- package/store/Store.js.map +1 -1
package/package.json
CHANGED
package/src/store/Store.test.js
CHANGED
|
@@ -3,7 +3,7 @@ import {mount} from 'enzyme';
|
|
|
3
3
|
import Store, {useStore} from "./Store";
|
|
4
4
|
import {act} from 'react-dom/test-utils';
|
|
5
5
|
|
|
6
|
-
it('test
|
|
6
|
+
it('basic test, rerender on each change', () => {
|
|
7
7
|
const store = new Store({
|
|
8
8
|
count: 0,
|
|
9
9
|
value: ''
|
|
@@ -44,14 +44,14 @@ it('test 1', () => {
|
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
|
|
47
|
-
it('
|
|
47
|
+
it('no rerender if selector value doesn\'t change', () => {
|
|
48
48
|
const store = new Store({
|
|
49
49
|
count: 0,
|
|
50
50
|
value: ''
|
|
51
51
|
});
|
|
52
52
|
let renderCount = 0
|
|
53
53
|
const MyComponent = () => {
|
|
54
|
-
let count = useStore(store, it=> it.count);
|
|
54
|
+
let count = useStore(store, it => it.count);
|
|
55
55
|
renderCount++
|
|
56
56
|
return <div>{count}</div>;
|
|
57
57
|
};
|
|
@@ -73,8 +73,7 @@ it('test 2', () => {
|
|
|
73
73
|
})
|
|
74
74
|
});
|
|
75
75
|
expect(wrapper).toHaveHTML('<div>1</div>');
|
|
76
|
-
|
|
77
|
-
expect(renderCount).toBe(3);
|
|
76
|
+
expect(renderCount).toBe(2);
|
|
78
77
|
|
|
79
78
|
act(() => {
|
|
80
79
|
store.set({
|
|
@@ -82,5 +81,36 @@ it('test 2', () => {
|
|
|
82
81
|
})
|
|
83
82
|
});
|
|
84
83
|
expect(wrapper).toHaveHTML('<div>1</div>');
|
|
85
|
-
expect(renderCount).toBe(
|
|
84
|
+
expect(renderCount).toBe(2);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('update value if selector dependencies change', () => {
|
|
88
|
+
const store = new Store({
|
|
89
|
+
key: 'foo',
|
|
90
|
+
foo: 'foo',
|
|
91
|
+
bar: 'bar',
|
|
92
|
+
});
|
|
93
|
+
let renderCount = 0
|
|
94
|
+
let renderedValue = []
|
|
95
|
+
|
|
96
|
+
const MyComponent = () => {
|
|
97
|
+
let key = useStore(store, it => it.key);
|
|
98
|
+
let value = useStore(store, it => it[key], [key]);
|
|
99
|
+
renderCount++
|
|
100
|
+
renderedValue.push(value)
|
|
101
|
+
return <div>{value}</div>;
|
|
102
|
+
};
|
|
103
|
+
const wrapper = mount(<MyComponent/>);
|
|
104
|
+
expect(wrapper).toHaveHTML('<div>foo</div>');
|
|
105
|
+
expect(renderCount).toBe(1);
|
|
106
|
+
expect(renderedValue).toEqual(['foo']);
|
|
107
|
+
|
|
108
|
+
act(() => {
|
|
109
|
+
store.set(it => {
|
|
110
|
+
it.key = 'bar'
|
|
111
|
+
})
|
|
112
|
+
});
|
|
113
|
+
expect(wrapper).toHaveHTML('<div>bar</div>');
|
|
114
|
+
expect(renderCount).toBe(2);
|
|
115
|
+
expect(renderedValue).toEqual(['foo', 'bar']);
|
|
86
116
|
});
|
package/src/store/Store.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useEffect,
|
|
1
|
+
import {useEffect, useReducer} from "react";
|
|
2
2
|
import {createProxy} from "./ProxyTools";
|
|
3
3
|
import {isFunction, isObject} from "../Tools";
|
|
4
4
|
|
|
@@ -86,15 +86,19 @@ export function useStore<T, R>(store: Store<T>, selector?: Selector<T, R>, selec
|
|
|
86
86
|
if (!selector)
|
|
87
87
|
selector = (defaultSelector as Selector<T, R>)
|
|
88
88
|
|
|
89
|
-
const [
|
|
89
|
+
const [_, forceUpdate] = useReducer(x => x + 1, 0);
|
|
90
|
+
let value = selector(store.get())
|
|
90
91
|
|
|
91
92
|
useEffect(() => {
|
|
92
93
|
const updateState = () => {
|
|
93
|
-
|
|
94
|
+
let next = selector(store.get())
|
|
95
|
+
if (value !== next) {
|
|
96
|
+
value = next
|
|
97
|
+
forceUpdate()
|
|
98
|
+
}
|
|
94
99
|
};
|
|
95
|
-
updateState()
|
|
96
100
|
return store.subscribe(updateState)
|
|
97
101
|
}, selectorDependencies)
|
|
98
102
|
|
|
99
|
-
return
|
|
103
|
+
return value
|
|
100
104
|
}
|
package/store/Store.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import ReactConstants from "./../ReactConstants"; import {clientHeight as __clientHeight, clientWidth as __clientWidth, createAccessor as __createAccessor, data as __data, height as __height, left as __left, length as __length, pageX as __pageX, pageY as __pageY, props as __props, scrollLeft as __scrollLeft, scrollTop as __scrollTop, state as __state, style as __style, target as __target, top as __top, value as __value, width as __width} from "./../CommonFields"; import {toConsumableArray as __toConsumableArray} from "./../_virtual/_rollupPluginBabelHelpers"; import {extends as __extends} from "./../_virtual/_rollupPluginBabelHelpers"; import {inherits as __inherits} from "./../_virtual/_rollupPluginBabelHelpers"; import {
|
|
1
|
+
import ReactConstants from "./../ReactConstants"; import {clientHeight as __clientHeight, clientWidth as __clientWidth, createAccessor as __createAccessor, data as __data, height as __height, left as __left, length as __length, pageX as __pageX, pageY as __pageY, props as __props, scrollLeft as __scrollLeft, scrollTop as __scrollTop, state as __state, style as __style, target as __target, top as __top, value as __value, width as __width} from "./../CommonFields"; import {toConsumableArray as __toConsumableArray} from "./../_virtual/_rollupPluginBabelHelpers"; import {extends as __extends} from "./../_virtual/_rollupPluginBabelHelpers"; import {inherits as __inherits} from "./../_virtual/_rollupPluginBabelHelpers"; import { useReducer, useEffect } from 'react';
|
|
2
2
|
import { createProxy } from './ProxyTools.js';
|
|
3
3
|
import { isFunction, isObject } from '../Tools.js';
|
|
4
4
|
|
|
@@ -56,15 +56,19 @@ const defaultSelector = (store) => store;
|
|
|
56
56
|
function useStore(store, selector, selectorDependencies = []) {
|
|
57
57
|
if (!selector)
|
|
58
58
|
selector = defaultSelector;
|
|
59
|
-
const [
|
|
59
|
+
const [_, forceUpdate] = useReducer(x => x + 1, 0);
|
|
60
|
+
let value = selector(store.get());
|
|
60
61
|
useEffect(() => {
|
|
61
62
|
const updateState = () => {
|
|
62
|
-
|
|
63
|
+
let next = selector(store.get());
|
|
64
|
+
if (value !== next) {
|
|
65
|
+
value = next;
|
|
66
|
+
forceUpdate();
|
|
67
|
+
}
|
|
63
68
|
};
|
|
64
|
-
updateState();
|
|
65
69
|
return store.subscribe(updateState);
|
|
66
70
|
}, selectorDependencies);
|
|
67
|
-
return
|
|
71
|
+
return value;
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
export { Store, Store as default, defaultSelector, getGlobalState, useStore };
|
package/store/Store.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Store.js","sources":["../src/store/Store.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAIA,MAAM,MAAM,GAAG,IAAI,GAAG,EAAsB,CAAA;AAM5C,MAAe,aAAa,CAAA;AAW3B,CAAA;AAGK,MAAO,KAAS,SAAQ,aAAgB,CAAA;IAE1C,WAAY,CAAA,YAAe,EAAE,IAAa,EAAA;AACtC,QAAA,KAAK,EAAE,CAAA;QACP,IAAI,IAAI,GAAG,IAAI,CAAC;AAChB,QAAA,IAAI,SAAS,GAA4B,IAAI,GAAG,EAAsB,CAAA;QACtE,IAAI,KAAK,GAAG,YAAY,CAAC;QACzB,IAAI,YAAY,GAAG,MAAK;AACpB,YAAA,KAAK,IAAI,QAAQ,IAAI,SAAS,CAAC,MAAM,EAAE,EAAE;AACrC,gBAAA,QAAQ,EAAE,CAAA;AACb,aAAA;AACL,SAAC,CAAC;AACF,QAAA,IAAI,CAAC,GAAG,GAAG,MAAS,KAAK,CAAC;AAC1B,QAAA,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,KAAI;AAClB,YAAA,IAAI,MA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