react-ui-basics 0.0.217 → 0.0.219

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-ui-basics",
3
- "version": "0.0.217",
3
+ "version": "0.0.219",
4
4
  "description": "",
5
5
  "module": "index.js",
6
6
  "_main": "index.cjs.js",
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, {useEffect, useReducer, useRef} from 'react';
2
2
 
3
3
  const EMPTY = {};
4
4
  export const props: <P>(that: React.Component<P>) => P = that => that.props;
@@ -29,6 +29,24 @@ export const stateGSs = (that, num) => {
29
29
  return result;
30
30
  };
31
31
 
32
+ const useSyncExternalStoreShim = (subscribe, getSnapshot) => {
33
+ const [_, forceUpdate] = useReducer(x => x + 1, 0);
34
+ const cb = useRef();
35
+ cb.current = getSnapshot;
36
+ let value = getSnapshot()
37
+
38
+ useEffect(() => subscribe(() => {
39
+ let next = cb.current()
40
+ if (value !== next) {
41
+ value = next
42
+ forceUpdate()
43
+ }
44
+ }), []);
45
+ return value
46
+ }
47
+
48
+ export const useSyncExternalStore = React.useSyncExternalStore || useSyncExternalStoreShim
49
+
32
50
  export const componentDidMount = 'componentDidMount';
33
51
  export const componentWillUnmount = 'componentWillUnmount';
34
52
  export const componentDidUpdate = 'componentDidUpdate';
@@ -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 1', () => {
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('test 2', () => {
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
- // react executes functional-component after first setState with the same value
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(3);
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
  });
@@ -1,6 +1,7 @@
1
- import {useEffect, useState} from "react";
1
+ import {useEffect, useReducer} from "react";
2
2
  import {createProxy} from "./ProxyTools";
3
3
  import {isFunction, isObject} from "../Tools";
4
+ import {useSyncExternalStore} from "../ReactConstants";
4
5
 
5
6
  const STORES = new Map<Symbol, Store<any>>()
6
7
 
@@ -77,24 +78,7 @@ export function getGlobalState() {
77
78
 
78
79
  export type Selector<T, R> = (t: T) => R;
79
80
 
80
- export const defaultSelector = <T>(store: T): T => store;
81
-
82
- export function useStore<T, R>(store: Store<T>, selector: Selector<T, R>, selectorDependencies: ReadonlyArray<any>): R
83
- export function useStore<T, R>(store: Store<T>, selector: Selector<T, R>, selectorDependencies?: undefined): R
84
- export function useStore<T>(store: Store<T>, selector?: undefined, selectorDependencies?: undefined): T
85
- export function useStore<T, R>(store: Store<T>, selector?: Selector<T, R>, selectorDependencies: ReadonlyArray<any> = []): R {
86
- if (!selector)
87
- selector = (defaultSelector as Selector<T, R>)
88
-
89
- const [state, setState] = useState(() => selector(store.get()))
90
-
91
- useEffect(() => {
92
- const updateState = () => {
93
- setState(selector(store.get()))
94
- };
95
- updateState()
96
- return store.subscribe(updateState)
97
- }, selectorDependencies)
98
-
99
- return state
100
- }
81
+ export function useStore<T, R>(store: Store<T>, selector: Selector<T, R>): R
82
+ export function useStore<T>(store: Store<T>, selector?: undefined): T
83
+ export const useStore = <T, R>(store: Store<T>, selector?: Selector<T, R>): R =>
84
+ useSyncExternalStore(store.subscribe, selector ? () => selector(store.get()) : store.get);
package/store/Store.d.ts CHANGED
@@ -19,7 +19,6 @@ export declare class Store<T> extends AbstractStore<T> {
19
19
  export default Store;
20
20
  export declare function getGlobalState(): Map<Symbol, Store<any>>;
21
21
  export declare type Selector<T, R> = (t: T) => R;
22
- export declare const defaultSelector: <T>(store: T) => T;
23
- export declare function useStore<T, R>(store: Store<T>, selector: Selector<T, R>, selectorDependencies: ReadonlyArray<any>): R;
24
- export declare function useStore<T, R>(store: Store<T>, selector: Selector<T, R>, selectorDependencies?: undefined): R;
25
- export declare function useStore<T>(store: Store<T>, selector?: undefined, selectorDependencies?: undefined): T;
22
+ export declare function useStore<T, R>(store: Store<T>, selector: Selector<T, R>): R;
23
+ export declare function useStore<T>(store: Store<T>, selector?: undefined): T;
24
+ export declare const useStore: <T, R>(store: Store<T>, selector?: Selector<T, R>) => R;
package/store/Store.js CHANGED
@@ -1,6 +1,6 @@
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 { useState, useEffect } from 'react';
2
- import { createProxy } from './ProxyTools.js';
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 { createProxy } from './ProxyTools.js';
3
2
  import { isFunction, isObject } from '../Tools.js';
3
+ import { useSyncExternalStore } from '../ReactConstants.js';
4
4
 
5
5
  const STORES = new Map();
6
6
  class AbstractStore {
@@ -52,20 +52,7 @@ class Store extends AbstractStore {
52
52
  function getGlobalState() {
53
53
  return STORES;
54
54
  }
55
- const defaultSelector = (store) => store;
56
- function useStore(store, selector, selectorDependencies = []) {
57
- if (!selector)
58
- selector = defaultSelector;
59
- const [state, setState] = useState(() => selector(store.get()));
60
- useEffect(() => {
61
- const updateState = () => {
62
- setState(selector(store.get()));
63
- };
64
- updateState();
65
- return store.subscribe(updateState);
66
- }, selectorDependencies);
67
- return state;
68
- }
55
+ const useStore = (store, selector) => useSyncExternalStore(store.subscribe, selector ? () => selector(store.get()) : store.get);
69
56
 
70
- export { Store, Store as default, defaultSelector, getGlobalState, useStore };
57
+ export { Store, Store as default, getGlobalState, useStore };
71
58
  //# sourceMappingURL=Store.js.map
@@ -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,MAAM,CAAC;AACX,YAAA,IAAI,UAAU,CAAC,MAAM,CAAC,EAAE;AACpB,gBAAA,MAAM,KAAK,GAAG,WAAW,CAAC,KAAe,CAAC,CAAC;AAC3C,gBAAA,MAAM,GAAI,MAAoB,CAAC,KAAK,CAAC,CAAC;AACtC,gBAAA,IAAI,CAAC,MAAM,IAAI,MAAM,KAAK,KAAK;AAC3B,oBAAA,MAAM,GAAG,KAAK,CAAC,IAAI,EAAE,CAAA;AAC5B,aAAA;AAAM,iBAAA,IAAI,QAAQ,CAAC,MAAM,CAAC,EAAE;AACzB,gBAAA,MAAM,GAAO,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,EAAA,EAAA,KAAK,CAAK,E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