Containers (#56571)

* refactor: 💡 move state containers to /common folder

* refactor: 💡 remove RecursiveReadonly type in state containers

* fix: 🐛 assume we are in production by default on server-side

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Vadim Dalecky 2020-02-03 07:04:23 -08:00 committed by GitHub
parent 1c849acdd5
commit 85988386b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 31 additions and 27 deletions

View file

@ -19,4 +19,5 @@
export * from './defer';
export * from './of';
export * from './state_containers';
export { distinctUntilChangedWithInitialValue } from './distinct_until_changed_with_initial_value';

View file

@ -19,7 +19,6 @@
import { BehaviorSubject } from 'rxjs';
import { skip } from 'rxjs/operators';
import { RecursiveReadonly } from '@kbn/utility-types';
import deepFreeze from 'deep-freeze-strict';
import {
PureTransitionsToTransitions,
@ -32,14 +31,18 @@ import {
const $$observable = (typeof Symbol === 'function' && (Symbol as any).observable) || '@@observable';
const $$setActionType = '@@SET';
const freeze: <T>(value: T) => RecursiveReadonly<T> =
process.env.NODE_ENV !== 'production'
? <T>(value: T): RecursiveReadonly<T> => {
const isFreezable = value !== null && typeof value === 'object';
if (isFreezable) return deepFreeze(value) as RecursiveReadonly<T>;
return value as RecursiveReadonly<T>;
}
: <T>(value: T) => value as RecursiveReadonly<T>;
const isProduction =
typeof window === 'object'
? process.env.NODE_ENV === 'production'
: !process.env.NODE_ENV || process.env.NODE_ENV === 'production';
const freeze: <T>(value: T) => T = isProduction
? <T>(value: T) => value as T
: <T>(value: T): T => {
const isFreezable = value !== null && typeof value === 'object';
if (isFreezable) return deepFreeze(value) as T;
return value as T;
};
export function createStateContainer<State extends BaseState>(
defaultState: State
@ -66,7 +69,7 @@ export function createStateContainer<
pureTransitions: PureTransitions = {} as PureTransitions,
pureSelectors: PureSelectors = {} as PureSelectors
): ReduxLikeStateContainer<State, PureTransitions, PureSelectors> {
const data$ = new BehaviorSubject<RecursiveReadonly<State>>(freeze(defaultState));
const data$ = new BehaviorSubject<State>(freeze(defaultState));
const state$ = data$.pipe(skip(1));
const get = () => data$.getValue();
const container: ReduxLikeStateContainer<State, PureTransitions, PureSelectors> = {
@ -101,7 +104,7 @@ export function createStateContainer<
),
addMiddleware: middleware =>
(container.dispatch = middleware(container as any)(container.dispatch)),
subscribe: (listener: (state: RecursiveReadonly<State>) => void) => {
subscribe: (listener: (state: State) => void) => {
const subscription = state$.subscribe(listener);
return () => subscription.unsubscribe();
},

View file

@ -18,7 +18,7 @@
*/
import { Observable } from 'rxjs';
import { Ensure, RecursiveReadonly } from '@kbn/utility-types';
import { Ensure } from '@kbn/utility-types';
export type BaseState = object;
export interface TransitionDescription<Type extends string = string, Args extends any[] = any[]> {
@ -27,7 +27,7 @@ export interface TransitionDescription<Type extends string = string, Args extend
}
export type Transition<State extends BaseState, Args extends any[]> = (...args: Args) => State;
export type PureTransition<State extends BaseState, Args extends any[]> = (
state: RecursiveReadonly<State>
state: State
) => Transition<State, Args>;
export type EnsurePureTransition<T> = Ensure<T, PureTransition<any, any>>;
export type PureTransitionToTransition<T extends PureTransition<any, any>> = ReturnType<T>;
@ -36,9 +36,9 @@ export type PureTransitionsToTransitions<T extends object> = {
};
export interface BaseStateContainer<State extends BaseState> {
get: () => RecursiveReadonly<State>;
get: () => State;
set: (state: State) => void;
state$: Observable<RecursiveReadonly<State>>;
state$: Observable<State>;
}
export interface StateContainer<
@ -55,12 +55,12 @@ export interface ReduxLikeStateContainer<
PureTransitions extends object = {},
PureSelectors extends object = {}
> extends StateContainer<State, PureTransitions, PureSelectors> {
getState: () => RecursiveReadonly<State>;
reducer: Reducer<RecursiveReadonly<State>>;
replaceReducer: (nextReducer: Reducer<RecursiveReadonly<State>>) => void;
getState: () => State;
reducer: Reducer<State>;
replaceReducer: (nextReducer: Reducer<State>) => void;
dispatch: (action: TransitionDescription) => void;
addMiddleware: (middleware: Middleware<RecursiveReadonly<State>>) => void;
subscribe: (listener: (state: RecursiveReadonly<State>) => void) => () => void;
addMiddleware: (middleware: Middleware<State>) => void;
subscribe: (listener: (state: State) => void) => () => void;
}
export type Dispatch<T> = (action: T) => void;

View file

@ -17,7 +17,7 @@
* under the License.
*/
import { createStateContainer } from '../../public/state_containers';
import { createStateContainer } from '../../common/state_containers';
interface State {
count: number;

View file

@ -17,7 +17,7 @@
* under the License.
*/
import { createStateContainer, PureTransition } from '../../public/state_containers';
import { createStateContainer, PureTransition } from '../../common/state_containers';
export interface TodoItem {
text: string;

View file

@ -18,7 +18,7 @@
*/
import { defaultState, pureTransitions, TodoActions, TodoState } from '../state_containers/todomvc';
import { BaseState, BaseStateContainer, createStateContainer } from '../../public/state_containers';
import { BaseState, BaseStateContainer, createStateContainer } from '../../common/state_containers';
import {
createKbnUrlStateStorage,
syncState,

View file

@ -25,7 +25,7 @@ export * from './field_wildcard';
export * from './parse';
export * from './render_complete';
export * from './resize_checker';
export * from './state_containers';
export * from '../common/state_containers';
export * from './storage';
export { hashedItemStore, HashedItemStore } from './storage/hashed_item_store';
export {

View file

@ -17,7 +17,7 @@
* under the License.
*/
import { BaseState, BaseStateContainer, createStateContainer } from '../state_containers';
import { BaseState, BaseStateContainer, createStateContainer } from '../../common/state_containers';
import {
defaultState,
pureTransitions,

View file

@ -23,7 +23,7 @@ import defaultComparator from 'fast-deep-equal';
import { IStateSyncConfig } from './types';
import { IStateStorage } from './state_sync_state_storage';
import { distinctUntilChangedWithInitialValue } from '../../common';
import { BaseState } from '../state_containers';
import { BaseState } from '../../common/state_containers';
import { applyDiff } from '../state_management/utils/diff_object';
/**

View file

@ -17,7 +17,7 @@
* under the License.
*/
import { BaseState, BaseStateContainer } from '../state_containers/types';
import { BaseState, BaseStateContainer } from '../../common/state_containers/types';
import { IStateStorage } from './state_sync_state_storage';
export interface INullableBaseStateContainer<State extends BaseState>