From d5b4983cfa73b4af9557254c1ef0a82aad6291c3 Mon Sep 17 00:00:00 2001 From: edolix Date: Mon, 3 Apr 2023 18:27:55 +0200 Subject: [PATCH] start removing redux --- packages/core/src/redux/connect.test.ts | 33 +---------------- packages/core/src/redux/connect.ts | 48 ++----------------------- packages/core/src/redux/index.ts | 35 +++--------------- packages/core/src/redux/swStore.ts | 7 +++- 4 files changed, 13 insertions(+), 110 deletions(-) diff --git a/packages/core/src/redux/connect.test.ts b/packages/core/src/redux/connect.test.ts index 767baf8b1d..17c0e64ec8 100644 --- a/packages/core/src/redux/connect.test.ts +++ b/packages/core/src/redux/connect.test.ts @@ -1,7 +1,7 @@ import { configureJestStore } from '../testUtils' import { BaseComponent } from '../BaseComponent' import { connect } from './connect' -import { componentActions, sessionActions } from './features' +import { componentActions } from './features' import { EventEmitter } from '../utils/EventEmitter' import { SDKStore } from './' @@ -10,34 +10,16 @@ describe('Connect', () => { let instance: any let updateStateAction: any let updateRemoteSDPAction: any - const mockOnRemoteSDP = jest.fn() - - Object.defineProperties(BaseComponent.prototype, { - onAuth: { - value: jest.fn(), - }, - checkRaceOne: { - value: jest.fn(), - }, - checkRaceTwo: { - value: jest.fn(), - }, - }) beforeEach(() => { store = configureJestStore() instance = connect({ store, - sessionListeners: { - authStatus: 'onAuth', - }, Component: BaseComponent, })({ emitter: new EventEmitter(), }) - instance.onAuth.mockClear() instance.emit = jest.fn() - mockOnRemoteSDP.mockClear() updateStateAction = componentActions.upsert({ id: instance.__uuid, @@ -64,17 +46,4 @@ describe('Connect', () => { store.dispatch(updateRemoteSDPAction) expect(instance.emit).not.toHaveBeenCalled() }) - - it('should invoke the function within sessionListeners', () => { - store.dispatch(sessionActions.authStatus('authorized')) - - expect(instance.onAuth).toHaveBeenCalledTimes(1) - expect(instance.onAuth).toHaveBeenCalledWith({ - protocol: '', - iceServers: [], - authStatus: 'authorized', - authError: undefined, - authCount: 0, - }) - }) }) diff --git a/packages/core/src/redux/connect.ts b/packages/core/src/redux/connect.ts index 18cca12cb7..4dd486dfbc 100644 --- a/packages/core/src/redux/connect.ts +++ b/packages/core/src/redux/connect.ts @@ -1,19 +1,13 @@ -import { SessionState, CustomSaga } from './interfaces' +import { CustomSaga } from './interfaces' import { SDKStore } from './' -import { getSession } from './features/session/sessionSelectors' import type { BaseComponent } from '../BaseComponent' import { EventEmitter } from '../utils/EventEmitter' -type SessionEventHandler = (session: SessionState) => unknown interface Connect { - sessionListeners?: Partial< - Record - > store: SDKStore Component: new (o: any) => T customSagas?: Array> } -type ReduxSessionKeys = keyof SessionState export const connect = < EventTypes extends EventEmitter.ValidEventTypes, @@ -28,44 +22,10 @@ export const connect = < >( options: Connect ) => { - const { sessionListeners = {}, store, Component, customSagas = [] } = options - const sessionKeys = Object.keys(sessionListeners) as ReduxSessionKeys[] + const { store, Component, customSagas = [] } = options return (userOptions: any): TargetType => { const instance = new Component({ ...userOptions, store }) - const cacheMap = new Map() - /** - * Stop the execution of the redux listeners if `destroyer` - * below was called in the meantime. - */ - let run = true - - const storeUnsubscribe = store.subscribe(() => { - const state = store.getState() - - const session = getSession(state) - for (const reduxKey of sessionKeys) { - if (run === false) { - return - } - const cacheKey = `session.${reduxKey}` - const current = cacheMap.get(cacheKey) - const updatedValue = session[reduxKey] - - if (updatedValue !== undefined && current !== updatedValue) { - cacheMap.set(cacheKey, updatedValue) - const fnName = sessionListeners[reduxKey] - - if (typeof fnName === 'string') { - // FIXME: proper types for fnName - // @ts-ignore - instance[fnName](session) - } else if (typeof fnName === 'function') { - fnName(session) - } - } - } - }) // Run all the custom sagas const taskList = customSagas?.map((saga) => { @@ -73,10 +33,6 @@ export const connect = < }) instance.destroyer = () => { - run = false - storeUnsubscribe() - cacheMap.clear() - // Cancel all the custom sagas if (taskList?.length) { taskList.forEach((task) => task.cancel()) diff --git a/packages/core/src/redux/index.ts b/packages/core/src/redux/index.ts index 3f5ab7e461..35cc586470 100644 --- a/packages/core/src/redux/index.ts +++ b/packages/core/src/redux/index.ts @@ -1,12 +1,5 @@ -import { Store } from 'redux' -import createSagaMiddleware, { - channel, - multicastChannel, - Saga, - Task, -} from '@redux-saga/core' -import { configureStore as rtConfigureStore } from './toolkit' -import { rootReducer } from './rootReducer' +import { channel, multicastChannel, Saga, Task } from '@redux-saga/core' +// import { configureStore as rtConfigureStore } from './toolkit' import rootSaga from './rootSaga' import { PubSubChannel, @@ -44,7 +37,6 @@ const configureStore = (options: ConfigureStoreOptions) => { preloadedState = {}, runSagaMiddleware = true, } = options - const sagaMiddleware = createSagaMiddleware() const rootChannel: PubSubChannel = multicastChannel() const pubSubChannel: PubSubChannel = multicastChannel() const swEventChannel: SwEventChannel = multicastChannel() @@ -59,20 +51,8 @@ const configureStore = (options: ConfigureStoreOptions) => { swEventChannel, sessionChannel, } - const store = rtConfigureStore({ - devTools: userOptions?.devTools ?? true, - reducer: rootReducer, - preloadedState, - middleware: (getDefaultMiddleware) => - // It is preferrable to use the chainable .concat(...) and - // .prepend(...) methods of the returned MiddlewareArray instead - // of the array spread operator, as the latter can lose valuable - // type information under some circumstances. - // @see https://redux-toolkit.js.org/api/getDefaultMiddleware#intended-usage - getDefaultMiddleware().concat(sagaMiddleware), - }) as Store - const swStore = createSWStore({ channels }) + const swStore = createSWStore({ channels, preloadedState }) let session: BaseSession const initSession = () => { @@ -121,16 +101,9 @@ const configureStore = (options: ConfigureStoreOptions) => { } return { - ...store, + ...swStore, runSaga, channels, - dispatch: (action: any) => { - return swStore.dispatch(action) - }, - getState: () => { - return swStore.getState() - // return store.getState() - }, } } diff --git a/packages/core/src/redux/swStore.ts b/packages/core/src/redux/swStore.ts index 4590e3f62d..b858e27d02 100644 --- a/packages/core/src/redux/swStore.ts +++ b/packages/core/src/redux/swStore.ts @@ -7,9 +7,13 @@ import { AnyAction } from './toolkit' interface CreateSwStoreParams { channels: InternalChannels + preloadedState: Partial } -export const createSWStore = ({ channels }: CreateSwStoreParams) => { +export const createSWStore = ({ + channels, + preloadedState, +}: CreateSwStoreParams) => { const logger = getLogger() let rootTask: Task const state: SDKState = { @@ -24,6 +28,7 @@ export const createSWStore = ({ channels }: CreateSwStoreParams) => { authError: undefined, authCount: 0, }, + ...preloadedState, } const getState = () => {