license import { Action } from "./Action" ; import { isPlainObject } from "./internal/utils/isPlainObject" ; import { Reducer } from "./Reducer" ; import { Store } from "./Store" ; import { StoreEnhancer } from "./StoreEnhancer" ; import { _ObservableLike } from "./_observableCompat" ; interface Listener { ( ) : void ; } /**
 * Behaviorally the same function as core Redux createStore().
 * Unlike the Redux variant,
 *
 * - This function requires initial state, instead of making it optional.
 * - This function applies fewer runtime checks for usage correctness w.r.t. type disagreements.
 * - This function makes no attempt to integrate cleanly with Observables.
 *
 * If you do want an expanded feature set, please do consider using the stock Redux.createStore().
 */ export function createStore < S > ( reducer , initialState , enhancer ? ) { if ( typeof enhancer !== "undefined" ) { return enhancer < S > ( createStore ) ( reducer , initialState ) ; } let currentReducer = reducer ; let currentState = initialState ; let currentListeners = [ ] ; let nextListeners = currentListeners ; let isDispatching = false ; function ensureCanMutateNextListeners ( ) { if ( nextListeners === currentListeners ) { nextListeners = currentListeners . slice ( ) ; } } function getState ( ) { return currentState ; } function outerSubscribe ( listener ) { let isSubscribed = true ; ensureCanMutateNextListeners ( ) ; nextListeners . push ( listener ) ; return function unsubscribe ( ) { if ( ! isSubscribed ) { return ; } isSubscribed = false ; ensureCanMutateNextListeners ( ) ; const index = nextListeners . indexOf ( listener ) ; nextListeners . splice ( index , 1 ) ; } ; } function dispatch < A extends Action > ( action ) { if ( ! isPlainObject ( action ) ) { throw new Error ( "Actions must be plain objects. " + "Use custom middleware for async actions." ) ; } if ( isDispatching ) { throw new Error ( "Reducers may not dispatch actions." ) ; } try { isDispatching = true ; currentState = currentReducer ( currentState , action ) ; } finally { isDispatching = false ; } const listeners = ( currentListeners = nextListeners ) ; for ( let i = 0 ; i < listeners . length ; i ++ ) { listeners [ i ] ( ) ; } return action ; } function replaceReducer ( nextReducer ) { currentReducer = nextReducer ; } const observable = { subscribe ( observer ) { const unsubscribe = outerSubscribe ( ( ) => { observer . next ?. ( currentState ) ; } ) ; return { unsubscribe , } ; } , [ Symbol . observable ] ( ) { return observable ; } } ; return { dispatch , subscribe : outerSubscribe , getState , replaceReducer , [ Symbol . observable ] ( ) { return observable ; } , } ; }