@sigi/react
Version:
React bindings for sigi framework
33 lines • 1.58 kB
JavaScript
import { useDebugValue } from 'react';
import { identity, skip } from 'rxjs';
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector';
import { useInstance } from './injectable-context';
import { shallowEqual } from './shallow-equal';
export function useDispatchers(A) {
var effectModule = useInstance(A);
return effectModule.dispatchers;
}
function _useModuleState(store, selector, equalFn) {
if (selector === void 0) { selector = identity; }
if (equalFn === void 0) { equalFn = shallowEqual; }
var state = useSyncExternalStoreWithSelector(function (onStoreChange) {
var sub = store.state$.pipe(skip(1)).subscribe(onStoreChange);
return function () { return sub.unsubscribe(); };
}, function () { return store.state; }, function () { return store.state; }, selector, equalFn);
useDebugValue(state);
return state;
}
export function useModuleState(A, config) {
var store = useInstance(A).store;
return _useModuleState(store, config === null || config === void 0 ? void 0 : config.selector, config === null || config === void 0 ? void 0 : config.equalFn);
}
export function useModule(A, config) {
var effectModule = useInstance(A);
var store = effectModule.store;
var appState = _useModuleState(store, config === null || config === void 0 ? void 0 : config.selector);
var appDispatcher = effectModule.dispatchers;
return [appState, appDispatcher];
}
export { SSRContext } from './ssr-context';
export * from './injectable-context';
//# sourceMappingURL=index.browser.js.map