-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove initial component subscribe, update component slice execute/failure reducers to allow non-existent ids * add basic cleanup saga and reducer * move logic to determine which components to clean to the saga to avoid dispatching too much * update connect to make the existence of the component optional * remove unneeded tests * fix tests * move logic to selector * extends tests utils to be able to pass a initial state * add test for getComponentsToCleanup * add basic test for the componentSaga * update saga test * add changeset * set cleanup delay to 1h Co-authored-by: Edoardo Gallo <[email protected]>
- Loading branch information
Showing
12 changed files
with
173 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@signalwire/core': patch | ||
--- | ||
|
||
[internal] Add ability to cleanup unused components from the store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/core/src/redux/features/component/componentSaga.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Store } from 'redux' | ||
import { expectSaga } from 'redux-saga-test-plan' | ||
import { configureJestStore } from '../../../testUtils' | ||
import { componentCleanupSagaWorker } from './componentSaga' | ||
import { rootReducer } from '../../rootReducer' | ||
|
||
describe('componentCleanupSaga', () => { | ||
let store: Store | ||
|
||
beforeEach(() => { | ||
store = configureJestStore({ | ||
preloadedState: { | ||
components: { | ||
byId: { | ||
'268b4cf8-a3c5-4003-8666-3b7a4f0a5af9': { | ||
id: '268b4cf8-a3c5-4003-8666-3b7a4f0a5af9', | ||
}, | ||
'faa63915-3a64-4c39-acbb-06dac0758f8a': { | ||
id: 'faa63915-3a64-4c39-acbb-06dac0758f8a', | ||
responses: {}, | ||
}, | ||
'zfaa63915-3a64-4c39-acbb-06dac0758f8a': { | ||
id: 'zfaa63915-3a64-4c39-acbb-06dac0758f8a', | ||
errors: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
}) | ||
it('should cleanup unused components from the store', () => { | ||
return expectSaga(componentCleanupSagaWorker) | ||
.withReducer(rootReducer, store.getState()) | ||
.hasFinalState({ | ||
components: { | ||
byId: { | ||
'268b4cf8-a3c5-4003-8666-3b7a4f0a5af9': { | ||
id: '268b4cf8-a3c5-4003-8666-3b7a4f0a5af9', | ||
}, | ||
}, | ||
}, | ||
session: { | ||
protocol: '', | ||
iceServers: [], | ||
authStatus: 'unknown', | ||
authError: undefined, | ||
authCount: 0, | ||
}, | ||
executeQueue: { queue: [] }, | ||
}) | ||
.run() | ||
}) | ||
}) |
23 changes: 23 additions & 0 deletions
23
packages/core/src/redux/features/component/componentSaga.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { SagaIterator } from '@redux-saga/core' | ||
import { delay, fork, put, select } from '@redux-saga/core/effects' | ||
import { componentActions } from '..' | ||
import { getComponentsToCleanup } from './componentSelectors' | ||
|
||
export function* componentCleanupSagaWorker(): SagaIterator { | ||
const toCleanup = yield select(getComponentsToCleanup) | ||
|
||
if (toCleanup.length) { | ||
yield put( | ||
componentActions.cleanup({ | ||
ids: toCleanup, | ||
}) | ||
) | ||
} | ||
} | ||
|
||
export function* componentCleanupSaga(): SagaIterator { | ||
while (true) { | ||
yield delay(3600_000) // 1 hour | ||
yield fork(componentCleanupSagaWorker) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/core/src/redux/features/component/componentSelectors.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Store } from 'redux' | ||
import { configureJestStore } from '../../../testUtils' | ||
import { getComponentsToCleanup } from './componentSelectors' | ||
|
||
describe('componentSelectors', () => { | ||
let store: Store | ||
|
||
beforeEach(() => { | ||
store = configureJestStore({ | ||
preloadedState: { | ||
components: { | ||
byId: { | ||
'268b4cf8-a3c5-4003-8666-3b7a4f0a5af9': { | ||
id: '268b4cf8-a3c5-4003-8666-3b7a4f0a5af9', | ||
}, | ||
'faa63915-3a64-4c39-acbb-06dac0758f8a': { | ||
id: 'faa63915-3a64-4c39-acbb-06dac0758f8a', | ||
responses: {}, | ||
}, | ||
'zfaa63915-3a64-4c39-acbb-06dac0758f8a': { | ||
id: 'zfaa63915-3a64-4c39-acbb-06dac0758f8a', | ||
errors: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
describe('getComponentsToCleanup', () => { | ||
it('should return the list of components to cleanup', () => { | ||
expect(getComponentsToCleanup(store.getState())).toEqual([ | ||
'faa63915-3a64-4c39-acbb-06dac0758f8a', | ||
'zfaa63915-3a64-4c39-acbb-06dac0758f8a', | ||
]) | ||
}) | ||
}) | ||
}) |
19 changes: 18 additions & 1 deletion
19
packages/core/src/redux/features/component/componentSelectors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
import { SDKState } from '../../interfaces' | ||
import { ReduxComponent, SDKState } from '../../interfaces' | ||
|
||
export const getComponent = ({ components }: SDKState, id: string) => { | ||
return components.byId?.[id] | ||
} | ||
|
||
export const getComponentsById = ({ components }: SDKState) => { | ||
return components.byId | ||
} | ||
|
||
export const getComponentsToCleanup = (state: SDKState) => { | ||
const components = getComponentsById(state) | ||
|
||
let toCleanup: Array<ReduxComponent['id']> = [] | ||
Object.keys(components).forEach((id) => { | ||
if (components[id].responses || components[id].errors) { | ||
toCleanup.push(id) | ||
} | ||
}) | ||
|
||
return toCleanup | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters