-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(redux-store): add mediation store
Signed-off-by: Timo Glastra <[email protected]>
- Loading branch information
1 parent
430d965
commit 8a1f39e
Showing
8 changed files
with
151 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { mediationSlice } from './mediationSlice' | ||
export { MediationThunks } from './mediationThunks' | ||
export { MediationSelectors } from './mediationSelectors' | ||
export { startMediationListener } from './mediationListener' |
28 changes: 28 additions & 0 deletions
28
packages/redux-store/src/slices/mediation/mediationListener.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,28 @@ | ||
import type { Agent, MediationStateChangedEvent } from '@aries-framework/core' | ||
import type { EnhancedStore } from '@reduxjs/toolkit' | ||
|
||
import { RoutingEventTypes } from '@aries-framework/core' | ||
|
||
import { mediationSlice } from './mediationSlice' | ||
|
||
/** | ||
* Starts an EventListener that listens for MediationRecords state changes | ||
* and updates the store accordingly. | ||
* | ||
* This function **must** be called if you're working with MediationRecords. | ||
* If you don't, the store won't be updated. | ||
*/ | ||
const startMediationListener = (agent: Agent, store: EnhancedStore) => { | ||
const listener = (event: MediationStateChangedEvent) => { | ||
const record = event.payload.mediationRecord | ||
store.dispatch(mediationSlice.actions.updateOrAdd(record)) | ||
} | ||
|
||
agent.events.on(RoutingEventTypes.MediationStateChanged, listener) | ||
|
||
return () => { | ||
agent.events.off(RoutingEventTypes.MediationStateChanged, listener) | ||
} | ||
} | ||
|
||
export { startMediationListener } |
35 changes: 35 additions & 0 deletions
35
packages/redux-store/src/slices/mediation/mediationSelectors.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,35 @@ | ||
import type { MediationState } from './mediationSlice' | ||
import type { MediationState as MediationRecordState } from '@aries-framework/core' | ||
|
||
interface PartialMediationState { | ||
mediation: MediationState | ||
} | ||
|
||
/** | ||
* Namespace that holds all MediationRecord related selectors. | ||
*/ | ||
const MediationSelectors = { | ||
/** | ||
* Selector that retrieves the entire **mediation** store object. | ||
*/ | ||
mediationStateSelector: (state: PartialMediationState) => state.mediation.mediation, | ||
|
||
/** | ||
* Selector that retrieves all MediationRecord from the state. | ||
*/ | ||
mediationRecordsSelector: (state: PartialMediationState) => state.mediation.mediation.records, | ||
|
||
/** | ||
* Selector that retrieves all MediationRecord from the store by specified state. | ||
*/ | ||
mediationRecordsByStateSelector: (mediationState: MediationRecordState) => (state: PartialMediationState) => | ||
state.mediation.mediation.records.filter((record) => record.state === mediationState), | ||
|
||
/** | ||
* Selector that fetches a MediationRecord by id from the state. | ||
*/ | ||
mediationRecordByIdSelector: (mediationRecordId: string) => (state: PartialMediationState) => | ||
state.mediation.mediation.records.find((x) => x.id === mediationRecordId), | ||
} | ||
|
||
export { MediationSelectors } |
61 changes: 61 additions & 0 deletions
61
packages/redux-store/src/slices/mediation/mediationSlice.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,61 @@ | ||
import type { MediationRecord } from '@aries-framework/core' | ||
import type { PayloadAction, SerializedError } from '@reduxjs/toolkit' | ||
|
||
import { createSlice } from '@reduxjs/toolkit' | ||
|
||
import { MediationThunks } from './mediationThunks' | ||
|
||
interface MediationState { | ||
mediation: { | ||
records: MediationRecord[] | ||
isLoading: boolean | ||
} | ||
error: null | SerializedError | ||
} | ||
|
||
const initialState: MediationState = { | ||
mediation: { | ||
records: [], | ||
isLoading: false, | ||
}, | ||
error: null, | ||
} | ||
|
||
const mediationSlice = createSlice({ | ||
name: 'mediation', | ||
initialState, | ||
reducers: { | ||
updateOrAdd: (state, action: PayloadAction<MediationRecord>) => { | ||
const index = state.mediation.records.findIndex((record) => record.id == action.payload.id) | ||
|
||
if (index == -1) { | ||
// records doesn't exist, add it | ||
state.mediation.records.push(action.payload) | ||
return state | ||
} | ||
|
||
// record does exist, update it | ||
state.mediation.records[index] = action.payload | ||
return state | ||
}, | ||
}, | ||
extraReducers: (builder) => { | ||
builder | ||
// getAllMediators | ||
.addCase(MediationThunks.getAllMediationRecords.pending, (state) => { | ||
state.mediation.isLoading = true | ||
}) | ||
.addCase(MediationThunks.getAllMediationRecords.rejected, (state, action) => { | ||
state.mediation.isLoading = false | ||
state.error = action.error | ||
}) | ||
.addCase(MediationThunks.getAllMediationRecords.fulfilled, (state, action) => { | ||
state.mediation.isLoading = false | ||
state.mediation.records = action.payload | ||
}) | ||
}, | ||
}) | ||
|
||
export { mediationSlice } | ||
|
||
export type { MediationState } |
15 changes: 15 additions & 0 deletions
15
packages/redux-store/src/slices/mediation/mediationThunks.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,15 @@ | ||
import { createAsyncAgentThunk } from '../../utils' | ||
|
||
/** | ||
* Namespace containing all **mediation** related actions. | ||
*/ | ||
const MediationThunks = { | ||
/** | ||
* Retrieve all Mediation records | ||
*/ | ||
getAllMediationRecords: createAsyncAgentThunk('mediation/getAll', async (_, thunkApi) => { | ||
return thunkApi.extra.agent.mediationRecipient.getMediators() | ||
}), | ||
} | ||
|
||
export { MediationThunks } |
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