Skip to content

Commit

Permalink
test: improve createTestRoomClient
Browse files Browse the repository at this point in the history
  • Loading branch information
kizahasi committed Feb 15, 2025
1 parent 58bed99 commit c123a8d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
26 changes: 4 additions & 22 deletions apps/web-server/src/hooks/useSetupStorybook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,10 @@ export const useSetupStorybook = ({
]);

React.useEffect(() => {
let roomState = room;
function next() {
testRoomClient.source.roomState.next({
type: 'joined',
state: room,
setState,
setStateByApply,
});
}
function setState(action: SetAction<RoomState>) {
roomState = typeof action === 'function' ? action(roomState) : action;
next();
}
function setStateByApply(operation: UpOperation) {
const r = $apply({ state: roomState, operation });
if (r.isError) {
throw toOtError(r.error);
}
roomState = r.value;
next();
}
next();
testRoomClient.source.roomState.next({
type: 'joined',
state: room,
});
}, [testRoomClient.source.roomState, room]);

React.useEffect(() => {
Expand Down
44 changes: 43 additions & 1 deletion packages/sdk/src/internal/createTestRoomClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { State, UpOperation, apply, roomTemplate, toOtError } from '@flocon-trpg/core';
import {
WritingMessageStatusInputType,
WritingMessageStatusType,
Expand All @@ -7,10 +8,14 @@ import { RoomClient } from './createRoomClient';
import { GraphQLStatusEventEmitter } from './roomClient/graphqlClient';
import { RoomConnectionsManager } from './roomClient/roomConnections';
import { GetMessagesQueryStatus } from './roomClient/roomMessages';
import { RoomState } from './roomClient/roomState';
import { RoomState, SetAction } from './roomClient/roomState';
import { BehaviorEvent } from './rxjs/behaviorEvent';
import { ReadonlyBehaviorEvent } from './rxjs/readonlyBehaviorEvent';

type JoinedRoomState = State<typeof roomTemplate>;
type JoinedRoomStateUpOperation = UpOperation<typeof roomTemplate>;
const applyJoinedRoomState = apply(roomTemplate);

const createTestRoomClientSource = <TCustomMessage, TGraphQLError>() => {
const roomMessageClient = new RoomMessagesClient<TCustomMessage>();
const queryStatus = new BehaviorEvent<GetMessagesQueryStatus<TGraphQLError>>({
Expand Down Expand Up @@ -64,10 +69,47 @@ export const createTestRoomClient = <TCustomMessage, TGraphQLError>(callback: {
unsubscribe: () => callback.unsubscribe && callback.unsubscribe(source),
};

type RoomStateForTestRoomClient =
| Exclude<RoomState<TGraphQLError>, { type: 'joined' }>
| {
type: 'joined';
state: JoinedRoomState;
};

return {
roomClient,
source: {
...source,
roomState: {
next(newState: RoomStateForTestRoomClient) {
if (newState.type !== 'joined') {
source.roomState.next(newState);
return;
}
let currentState = newState.state;
function next() {
source.roomState.next({
type: 'joined',
state: currentState,
setState,
setStateByApply,
});
}
function setState(action: SetAction<JoinedRoomState>) {
currentState = typeof action === 'function' ? action(currentState) : action;
next();
}
function setStateByApply(operation: JoinedRoomStateUpOperation) {
const r = applyJoinedRoomState({ state: currentState, operation });
if (r.isError) {
throw toOtError(r.error);
}
currentState = r.value;
next();
}
next();
},
},
clientStatus: {
next: (update: Parameters<typeof source.clientStatus.next>[0]) =>
source.clientStatus.next(update),
Expand Down

0 comments on commit c123a8d

Please sign in to comment.