-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle real time events for player events
Refs: #26
- Loading branch information
Showing
8 changed files
with
105 additions
and
9 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
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
16 changes: 16 additions & 0 deletions
16
src/client/src/app/+state/player-events/actions/player-event-removed.action.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,16 @@ | ||
import { createAction, on, props } from '@ngrx/store'; | ||
|
||
import { Reducers } from '../../utils'; | ||
import { PLAYER_EVENTS_ACTION_SCOPE } from '../consts'; | ||
import { playerEventEntityAdapter, PlayerEventsFeatureState } from '../player-events.state'; | ||
|
||
export const playerEventRemovedAction = createAction( | ||
`[${PLAYER_EVENTS_ACTION_SCOPE}] Player Event Removed`, | ||
props<{ eventId: string }>() | ||
); | ||
|
||
export const playerEventRemovedReducers: Reducers<PlayerEventsFeatureState> = [ | ||
on(playerEventRemovedAction, (state, { eventId }) => | ||
playerEventEntityAdapter.removeOne(eventId, state) | ||
), | ||
]; |
19 changes: 19 additions & 0 deletions
19
src/client/src/app/+state/player-events/actions/reset-player-events.action.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,19 @@ | ||
import { createAction, on } from '@ngrx/store'; | ||
import { produce } from 'immer'; | ||
|
||
import { initialActionState } from '../../action-state'; | ||
import { Reducers } from '../../utils'; | ||
import { PLAYER_EVENTS_ACTION_SCOPE } from '../consts'; | ||
import { playerEventEntityAdapter, PlayerEventsFeatureState } from '../player-events.state'; | ||
|
||
export const resetPlayerEventsAction = createAction(`[${PLAYER_EVENTS_ACTION_SCOPE}] Reset`); | ||
|
||
export const resetPlayerEventsReducers: Reducers<PlayerEventsFeatureState> = [ | ||
on(resetPlayerEventsAction, state => | ||
playerEventEntityAdapter.removeAll( | ||
produce(state, draft => { | ||
draft.actionStates.load = initialActionState; | ||
}) | ||
) | ||
), | ||
]; |
2 changes: 2 additions & 0 deletions
2
src/client/src/app/+state/player-events/player-events.actions.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,3 +1,5 @@ | ||
export { loadPlayerEventAction } from './actions/load-player-event.action'; | ||
export { loadPlayerEventsAction } from './actions/load-player-events.action'; | ||
export { playerEventRemovedAction } from './actions/player-event-removed.action'; | ||
export { resetPlayerEventsAction } from './actions/reset-player-events.action'; | ||
export { updateEventRegistrationAction } from './actions/update-event-registration.action'; |
48 changes: 47 additions & 1 deletion
48
src/client/src/app/+state/player-events/player-events.effects.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,10 +1,56 @@ | ||
import { loadPlayerEventEffects } from './actions/load-player-event.action'; | ||
import { inject } from '@angular/core'; | ||
import { toObservable } from '@angular/core/rxjs-interop'; | ||
import { Store } from '@ngrx/store'; | ||
import { EMPTY, filter, map, mergeMap, of, skip, withLatestFrom } from 'rxjs'; | ||
|
||
import { loadPlayerEventAction, loadPlayerEventEffects } from './actions/load-player-event.action'; | ||
import { loadPlayerEventsEffects } from './actions/load-player-events.action'; | ||
import { updateEventRegistrationEffects } from './actions/update-event-registration.action'; | ||
import { playerEventRemovedAction, resetPlayerEventsAction } from './player-events.actions'; | ||
import { playerEventSelectors } from './player-events.selectors'; | ||
import { RealtimeEventsService } from '../../services/realtime-events.service'; | ||
import { createFunctionalEffect } from '../functional-effect'; | ||
import { Effects } from '../utils'; | ||
|
||
export const PlayerEventsFeatureEffects: Effects[] = [ | ||
loadPlayerEventsEffects, | ||
loadPlayerEventEffects, | ||
updateEventRegistrationEffects, | ||
{ | ||
playerEventUpdated$: createFunctionalEffect.dispatching((store = inject(Store)) => | ||
inject(RealtimeEventsService).playerEventChanged.pipe( | ||
withLatestFrom(store.select(playerEventSelectors.selectEntities)), | ||
mergeMap(([{ eventId, changeType }, entities]) => { | ||
if (changeType === 'updated') { | ||
return eventId in entities | ||
? of(loadPlayerEventAction({ eventId, reload: true, silent: true })) | ||
: EMPTY; | ||
} else if (changeType === 'created') { | ||
return of(loadPlayerEventAction({ eventId, reload: true, silent: true })); | ||
} else if (changeType === 'deleted') { | ||
return of(playerEventRemovedAction({ eventId })); | ||
} | ||
return EMPTY; | ||
}) | ||
) | ||
), | ||
playerEventRegistrationUpdated$: createFunctionalEffect.dispatching((store = inject(Store)) => | ||
inject(RealtimeEventsService).playerEventRegistrationChanged.pipe( | ||
withLatestFrom(store.select(playerEventSelectors.selectEntities)), | ||
mergeMap(([{ eventId }, entities]) => | ||
eventId in entities | ||
? of(loadPlayerEventAction({ eventId, reload: true, silent: true })) | ||
: EMPTY | ||
) | ||
) | ||
), | ||
|
||
onServerReconnect$: createFunctionalEffect.dispatching(() => | ||
toObservable(inject(RealtimeEventsService).isConnected).pipe( | ||
skip(1), | ||
filter(x => x), | ||
map(() => resetPlayerEventsAction()) | ||
) | ||
), | ||
}, | ||
]; |
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