Files
event-store/tests/mocks/user-posts-reducer.ts
2025-04-25 22:44:38 +00:00

33 lines
717 B
TypeScript

import { makeReducer } from "../../libraries/reducer.ts";
import { EventStoreFactory } from "./events.ts";
export const userPostReducer = makeReducer<EventStoreFactory, UserPostState>(
(state, event) => {
switch (event.type) {
case "post:created": {
state.posts.push({ id: event.stream, author: event.meta.auditor });
state.count += 1;
break;
}
case "post:removed": {
state.posts = state.posts.filter(({ id }) => id !== event.stream);
state.count -= 1;
break;
}
}
return state;
},
() => ({
posts: [],
count: 0,
}),
);
type UserPostState = {
posts: {
id: string;
author: string;
}[];
count: number;
};