Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: real time updates #90

Merged
merged 8 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "C#: Host Debug",
"type": "dotnet",
"request": "launch",
"projectPath": "${workspaceFolder}/src/server/host/MinigolfFriday.Host.csproj"
}
]
}
14 changes: 12 additions & 2 deletions src/client/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,24 @@
"inject": false
}
],
"scripts": []
"scripts": [],
"externalDependencies": ["http", "https", "url", "util", "net"],
"allowedCommonJsDependencies": [
"copy-to-clipboard",
"tough-cookie",
"node-fetch",
"fetch-cookie",
"abort-controller",
"ws",
"eventsource"
]
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "600kb",
"maximumWarning": "800kb",
"maximumError": "1mb"
},
{
Expand Down
1 change: 1 addition & 0 deletions src/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@angular/pwa": "^18.0.3",
"@angular/router": "^18.0.2",
"@angular/service-worker": "^18.0.2",
"@microsoft/signalr": "^8.0.0",
"@ngneers/easy-ngrx-distinct-selector": "^0.1.1",
"@ngrx/effects": "18.0.0-rc.0",
"@ngrx/entity": "18.0.0-rc.0",
Expand Down
65 changes: 65 additions & 0 deletions src/client/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 22 additions & 9 deletions src/client/src/app/+state/action-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
error: _ActionCreatorWithResponse<`[${TScope}] [Error] ${TName}`, TProps, unknown>;
};

type x = HttpActionCreator<'a', 'b', { a: string }, { b: number }>;

Check warning on line 72 in src/client/src/app/+state/action-state.ts

View workflow job for this annotation

GitHub Actions / Build

'x' is defined but never used. Allowed unused vars must match /^_/u

export function createHttpAction<TProps extends object, TSuccess = unknown>() {
return <TScope extends string, TName extends string>(scope: TScope, name: TName) => {
Expand Down Expand Up @@ -97,13 +97,16 @@
TActionStateName extends TInferredState extends { actionStates: Record<string, ActionState> }
? keyof TInferredState['actionStates']
: never,
TAction extends HttpActionCreator<string, string, any, any>,

Check warning on line 100 in src/client/src/app/+state/action-state.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected any. Specify a different type

Check warning on line 100 in src/client/src/app/+state/action-state.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected any. Specify a different type
TProps = Parameters<TAction>[0],
TInferredState = TState,
>(
actionStateName: TActionStateName,
action: TAction,
startCondition: (state: TInferredState, props: TProps) => boolean = () => true
options?: {
condition?: (state: TInferredState, props: TProps) => boolean;
startCondition?: (state: TInferredState, props: TProps) => boolean;
}
): ReducerTypes<
TInferredState,
[TAction, TAction['starting'], TAction['success'], TAction['error']]
Expand All @@ -114,11 +117,15 @@
action.success,
action.error,
produce((draft, props) => {
if (options?.condition && !options.condition(draft as TInferredState, props.props)) {
return;
}

const actionStates = (draft as TState).actionStates;
if (props.type === action.type) {
if (
!isActionBusy(actionStates[actionStateName as string]) &&
startCondition(draft as TInferredState, props.props)
(!options?.startCondition || options.startCondition(draft as TInferredState, props.props))
) {
actionStates[actionStateName as string] = startingActionState;
}
Expand All @@ -127,22 +134,28 @@
} else if (props.type === action.success.type) {
actionStates[actionStateName as string] = successActionState;
} else if (props.type === action.error.type) {
actionStates[actionStateName as string] = errorActionState((props as any).response);

Check warning on line 137 in src/client/src/app/+state/action-state.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected any. Specify a different type
}
})
);
}

export function onHttpAction<T extends HttpActionCreator<string, string, any, any>>(

Check warning on line 143 in src/client/src/app/+state/action-state.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected any. Specify a different type

Check warning on line 143 in src/client/src/app/+state/action-state.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected any. Specify a different type
action: T,
actionStateSelector: Selector<object, ActionState>
actionStateSelector?: Selector<object, ActionState>,
disableFilterCondition?: (p: ReturnType<T>) => boolean
) {
return inject(Actions).pipe(
ofType(action),
withLatestFrom(inject(Store).select(actionStateSelector)),
filter(([, actionState]) => actionState.state === 'starting'),
map(([props]) => props)
);
if (actionStateSelector) {
return inject(Actions).pipe(
ofType(action),
withLatestFrom(inject(Store).select(actionStateSelector)),
filter(
([p, actionState]) => !!disableFilterCondition?.(p) || actionState.state === 'starting'
),
map(([props]) => props)
);
}
return inject(Actions).pipe(ofType(action));
}

export function mapToHttpAction<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const addEventTimeslotReducers: Reducers<EventsFeatureState> = [
state
);
}),
handleHttpAction('addTimeslot', addEventTimeslotAction, (s, p) => !!s.entities[p.eventId]),
handleHttpAction('addTimeslot', addEventTimeslotAction, {
startCondition: (s, p) => !!s.entities[p.eventId],
}),
];

export const addEventTimeslotEffects: Effects = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ export const addPlayerToEventPreconfigurationReducers: Reducers<EventsFeatureSta
)
: state;
}),
handleHttpAction(
'addPlayerToPreconfig',
addPlayerToEventPreconfigurationAction,
(s, p) =>
handleHttpAction('addPlayerToPreconfig', addPlayerToEventPreconfigurationAction, {
startCondition: (s, p) =>
s.entities[p.eventId]?.timeslots.some(
x => x.id === p.timeslotId && x.preconfigurations.some(y => y.id === p.preconfigId)
) === true
),
) === true,
}),
];

export const addPlayerToEventPreconfigurationEffects: Effects = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createAction, on, props } from '@ngrx/store';
import { produce } from 'immer';

import { PlayerEventTimeslotRegistrationChanged } from '../../../models/realtime-events';
import { Reducers } from '../../utils';
import { EVENTS_ACTION_SCOPE } from '../consts';
import { eventEntityAdapter, EventsFeatureState } from '../events.state';

export const eventTimeslotRegistrationChangedAction = createAction(
`[${EVENTS_ACTION_SCOPE}] Event Timeslot Registration Changed`,
props<PlayerEventTimeslotRegistrationChanged>()
);

export const eventTimeslotRegistrationChangedReducers: Reducers<EventsFeatureState> = [
on(eventTimeslotRegistrationChangedAction, (state, event) =>
eventEntityAdapter.mapOne(
{
id: event.eventId,
map: produce(draft => {
const timeslot = draft.timeslots.find(t => t.id === event.eventTimeslotId);
if (!timeslot) return;
if (event.isRegistered) {
if (!timeslot.playerIds.includes(event.userId)) {
timeslot.playerIds.push(event.userId);
}
} else {
timeslot.playerIds = timeslot.playerIds.filter(p => p !== event.userId);
}
}),
},
state
)
),
];
19 changes: 9 additions & 10 deletions src/client/src/app/+state/events/actions/load-event.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,24 @@ import { EVENTS_ACTION_SCOPE } from '../consts';
import { selectEventsActionState } from '../events.selectors';
import { EventsFeatureState, eventEntityAdapter } from '../events.state';

export const loadEventAction = createHttpAction<{ eventId: string; reload?: boolean }, Event>()(
EVENTS_ACTION_SCOPE,
'Load Event'
);
export const loadEventAction = createHttpAction<
{ eventId: string; reload?: boolean; silent?: boolean },
Event
>()(EVENTS_ACTION_SCOPE, 'Load Event');

export const loadEventReducers: Reducers<EventsFeatureState> = [
on(loadEventAction.success, (state, { response }) =>
eventEntityAdapter.upsertOne(response, state)
),
handleHttpAction(
'loadOne',
loadEventAction,
(s, p) => !s.entities[p.eventId] || p.reload === true
),
handleHttpAction('loadOne', loadEventAction, {
condition: (s, p) => p.silent !== true,
startCondition: (s, p) => !s.entities[p.eventId] || p.reload === true,
}),
];

export const loadEventEffects: Effects = {
loadEvent$: createFunctionalEffect.dispatching((api = inject(EventAdministrationService)) =>
onHttpAction(loadEventAction, selectEventsActionState('loadOne')).pipe(
onHttpAction(loadEventAction, selectEventsActionState('loadOne'), p => !!p.props.silent).pipe(
switchMap(({ props }) => toHttpAction(getEvent(api, props), loadEventAction, props))
)
),
Expand Down
Loading
Loading