Skip to content

Commit

Permalink
🐛 move updateStory to controller as BackendMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Dec 16, 2024
1 parent 600b40a commit 0d6aff3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
20 changes: 12 additions & 8 deletions src/back-end/routes/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Application, Request, Response } from "express";
import { BackendMethod } from "remult";
import { v4 as uuid } from "uuid";
import { Story } from "../../models/story";

Expand Down Expand Up @@ -40,11 +41,14 @@ export const handler = async (r: Request, s: Response) => {
/** Server-sent events endpoint for story updates */
export const events = (app: Application) => app.get("/:story/events", handler);

export const updateStory = (story: Story) => {
console.log(`Sending update to story ${story.id}`);
clients.get(story.id)?.map((c) => {
console.log(`Updating client ${c.id}`);
c.response.write(`data: update\n\n`);
c.response.flush();
});
};
export class UpdateStoryController {
@BackendMethod({ allowed: true })
static updateStory(story: Story) {
console.log(`Sending update to story ${story.id}`);
clients.get(story.id)?.map((c) => {
console.log(`Updating client ${story.id} => ${c.id}`);
c.response.write(`data: update\n\n`);
c.response.flush();
});
}
}
1 change: 0 additions & 1 deletion src/front-end/app/store/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const story: Module<StoryState | Promise<StoryState>, StoreState> = {
useCache: false,
});

console.log("Story", story);
ctx.commit("story", story);

if (story?.revealed) {
Expand Down
4 changes: 2 additions & 2 deletions src/models/story.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entity, Fields, Remult, Validators } from "remult";
import { v4 } from "uuid";
import { updateStory } from "../back-end/routes/events";
import { UpdateStoryController } from "../back-end/routes/events";
import scales from "../scales";
import { Vote } from "./vote";

Expand All @@ -14,7 +14,7 @@ const generateId = () =>

@Entity<Story>("story", {
allowApiCrud: true,
saved: (r) => updateStory(r),
saved: (r) => UpdateStoryController.updateStory(r),
})
export class Story {
@Fields.string({ allowApiUpdate: false, validate: Validators.unique })
Expand Down
4 changes: 2 additions & 2 deletions src/models/vote.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, Fields } from "remult";
import { updateStory } from "../back-end/routes/events";
import { UpdateStoryController } from "../back-end/routes/events";
import scales from "../scales";
import { Story } from "./story";

Expand All @@ -12,7 +12,7 @@ import { Story } from "./story";

if (!story) throw "Invalid story";

updateStory(story);
UpdateStoryController.updateStory(story);
};
})
export class Vote {
Expand Down
5 changes: 3 additions & 2 deletions test/back-end/routes/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Request, Response } from "express";
import { MockedObject, afterEach, beforeEach, describe, it, vi } from "vitest";
import {
UpdateStoryController,
clients,
handler,
updateStory,
} from "../../../src/back-end/routes/events";

vi.mock("remult", () => ({ BackendMethod: vi.fn() }));
vi.mock("uuid", () => ({ v4: vi.fn(() => "test") }));

describe("events", () => {
Expand Down Expand Up @@ -96,7 +97,7 @@ describe("events", () => {
},
]);

updateStory(new Story());
UpdateStoryController.updateStory(new Story());

const testClients = clients.get("test");
expect(testClients).toHaveLength(1);
Expand Down

0 comments on commit 0d6aff3

Please sign in to comment.