mirror of
https://github.com/zhigang1992/devhub.git
synced 2026-06-18 03:58:44 +08:00
Delete unused subscriptions after deleting columns
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
import { ColumnSubscription, Omit } from '../../types'
|
||||
import { createActionCreatorCreator } from '../../utils/helpers/redux'
|
||||
|
||||
export const addColumnSubscription = createActionCreatorCreator(
|
||||
'ADD_COLUMN_SUBSCRIPTION',
|
||||
)<Omit<ColumnSubscription, 'id' | 'createdAt' | 'updatedAt'>>()
|
||||
|
||||
export const deleteColumnSubscription = createActionCreatorCreator(
|
||||
'DELETE_COLUMN_SUBSCRIPTION',
|
||||
)<string>()
|
||||
|
||||
@@ -49,6 +49,19 @@ export const columnsReducer: Reducer<State> = (
|
||||
if (draft.byId) delete draft.byId[action.payload]
|
||||
})
|
||||
|
||||
case 'DELETE_COLUMN_SUBSCRIPTION':
|
||||
return immer(state, draft => {
|
||||
if (!(draft.allIds && draft.byId)) return
|
||||
|
||||
draft.allIds.forEach(columnId => {
|
||||
if (!draft.byId![columnId].subscriptionIds) return
|
||||
|
||||
draft.byId![columnId].subscriptionIds = draft.byId![
|
||||
columnId
|
||||
].subscriptionIds.filter(id => id !== action.payload)
|
||||
})
|
||||
})
|
||||
|
||||
case 'MOVE_COLUMN':
|
||||
return immer(state, draft => {
|
||||
if (!draft.allIds) return
|
||||
|
||||
@@ -38,6 +38,14 @@ export const subscriptionsReducer: Reducer<State> = (
|
||||
)
|
||||
})
|
||||
|
||||
case 'DELETE_COLUMN_SUBSCRIPTION':
|
||||
return immer(state, draft => {
|
||||
if (draft.allIds)
|
||||
draft.allIds = draft.allIds.filter(id => id !== action.payload)
|
||||
|
||||
if (draft.byId) delete draft.byId[action.payload]
|
||||
})
|
||||
|
||||
case 'REPLACE_COLUMNS':
|
||||
return immer(state, draft => {
|
||||
draft.allIds = []
|
||||
|
||||
@@ -3,11 +3,13 @@ import { all, fork } from 'redux-saga/effects'
|
||||
import { authSagas } from './auth'
|
||||
import { columnsSagas } from './columns'
|
||||
import { configSagas } from './config'
|
||||
import { subscriptionsSagas } from './subscriptions'
|
||||
|
||||
export function* rootSaga() {
|
||||
yield all([
|
||||
yield fork(authSagas),
|
||||
yield fork(columnsSagas),
|
||||
yield fork(configSagas),
|
||||
yield fork(subscriptionsSagas),
|
||||
])
|
||||
}
|
||||
|
||||
43
packages/shared/src/redux/sagas/subscriptions.ts
Normal file
43
packages/shared/src/redux/sagas/subscriptions.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import _ from 'lodash'
|
||||
import { all, put, select, takeLatest } from 'redux-saga/effects'
|
||||
|
||||
import { Column } from '../../types'
|
||||
import * as actions from '../actions'
|
||||
import * as selectors from '../selectors'
|
||||
|
||||
function* cleanupSubscriptions() {
|
||||
const allSubscriptionIds: string[] = yield select(
|
||||
selectors.subscriptionIdsSelector,
|
||||
)
|
||||
if (!(allSubscriptionIds && allSubscriptionIds.length)) return
|
||||
|
||||
const columns: Column[] | null = yield select(selectors.columnsArrSelector)
|
||||
if (!(columns && columns.length)) return
|
||||
|
||||
const usedSubscriptionIds = _.uniq(
|
||||
columns
|
||||
.reduce(
|
||||
(result, column) => result.concat(column.subscriptionIds),
|
||||
[] as string[],
|
||||
)
|
||||
.filter(Boolean),
|
||||
)
|
||||
|
||||
const unusedSubscriptionIds = _.difference(
|
||||
allSubscriptionIds,
|
||||
usedSubscriptionIds,
|
||||
)
|
||||
|
||||
yield all(
|
||||
unusedSubscriptionIds.map(subscriptionId =>
|
||||
put(actions.deleteColumnSubscription(subscriptionId)),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
export function* subscriptionsSagas() {
|
||||
yield all([
|
||||
yield takeLatest('DELETE_COLUMN', cleanupSubscriptions),
|
||||
yield takeLatest('REPLACE_COLUMNS', cleanupSubscriptions),
|
||||
])
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createSelector } from 'reselect'
|
||||
|
||||
import { RootState } from '../../types'
|
||||
import { createSubscriptionSelector } from './subscriptions'
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { createSelector } from 'reselect'
|
||||
|
||||
import { RootState } from '../../types'
|
||||
|
||||
const s = (state: RootState) => state.subscriptions || {}
|
||||
|
||||
export const subscriptionIdsSelector = (state: RootState) => s(state).allIds
|
||||
|
||||
export const createSubscriptionSelector = () =>
|
||||
createSelector(
|
||||
(state: RootState) => s(state).byId,
|
||||
|
||||
Reference in New Issue
Block a user