-
-
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.
✨ server-sent events for story updates
- Loading branch information
Showing
24 changed files
with
319 additions
and
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"prettier.prettierPath": "./node_modules/prettier", | ||
"prettier.useTabs": true, | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"eslint.format.enable": true, | ||
"prettier.enable": true, | ||
"prettier.documentSelectors": ["!**/*.ts"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,14 @@ | ||
import { Application } from "express"; | ||
import glitchWebhook from "./routes/glitch-webhook"; | ||
import reveal from "./routes/reveal"; | ||
import { storySSE } from "./routes/story-sse"; | ||
import vote from "./routes/vote"; | ||
|
||
const routes = async (app: Application) => { | ||
glitchWebhook(app); | ||
reveal(app); | ||
storySSE(app); | ||
vote(app); | ||
}; | ||
|
||
export default routes; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Application } from "express"; | ||
import { remult } from "remult"; | ||
import { Story } from "../../models/story"; | ||
import server from "../server"; | ||
import { updateStory } from "./story-sse"; | ||
|
||
const reveal = (app: Application) => | ||
app.post("/reveal/:story", server.withRemult, async (r, s) => { | ||
const story = r.params.story; | ||
|
||
console.log(`Revealing ${story}`); | ||
await remult.repo(Story).update(story, { revealed: true }); | ||
s.sendStatus(202); | ||
|
||
const storyObject = await remult.repo(Story).findId(story); | ||
|
||
if (!storyObject) { | ||
throw new Error("No story"); | ||
} | ||
|
||
updateStory(storyObject); | ||
}); | ||
|
||
export default reveal; |
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,50 @@ | ||
import { Application, Response } from "express"; | ||
import { v4 as uuid } from "uuid"; | ||
import { Story } from "../../models/story"; | ||
|
||
interface NarfClient { | ||
id: string; | ||
response: Response; | ||
} | ||
|
||
const clients = new Map<string, NarfClient[]>(); | ||
|
||
/** Server-sent events endpoint for story updates */ | ||
export const storySSE = (app: Application) => { | ||
app.get("/story/:story/events", async (r, s) => { | ||
s.set({ | ||
"Cache-Control": "no-cache", | ||
"Content-Type": "text/event-stream", | ||
Connection: "keep-alive", | ||
}); | ||
s.flushHeaders(); | ||
|
||
const story = r.params.story; | ||
const client: NarfClient = { id: uuid(), response: s }; | ||
|
||
console.log(`Client ${client.id} connected`); | ||
|
||
if (clients.has(story)) { | ||
clients.set(story, (clients.get(story) ?? []).concat(client)); | ||
} else { | ||
clients.set(story, [client]); | ||
} | ||
|
||
r.on("close", () => { | ||
console.log(`Client ${client.id} disconnected`); | ||
clients.set( | ||
story, | ||
(clients.get(story) ?? []).filter((c) => c.id !== client.id) | ||
); | ||
}); | ||
}); | ||
}; | ||
|
||
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: ${JSON.stringify(story)}\n\n`); | ||
c.response.flush(); | ||
}); | ||
}; |
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,48 @@ | ||
import { Application } from "express"; | ||
import { remult } from "remult"; | ||
import { Story } from "../../models/story"; | ||
import { Vote } from "../../models/vote"; | ||
import server from "../server"; | ||
import { updateStory } from "./story-sse"; | ||
|
||
/** Route for accepting a vote submission for a story */ | ||
const vote = (app: Application) => { | ||
app.put("/vote/:story", server.withRemult, async (r, s) => { | ||
console.log(`Incoming vote for ${r.params.story}`); | ||
|
||
const vote = r.body; | ||
const story = await remult.repo(Story).findId(r.params.story); | ||
|
||
if (!story) { | ||
throw new Error("No such story"); | ||
} | ||
|
||
const votes: Vote[] = []; | ||
|
||
if (!story._votes) { | ||
votes.push(vote); | ||
} else { | ||
let updated = false; | ||
|
||
for (const v of story._votes) { | ||
if (v.participant?.id === vote.participant?.id) { | ||
updated = true; | ||
votes.push(vote); | ||
} else { | ||
votes.push(v); | ||
} | ||
} | ||
|
||
if (!updated) { | ||
votes.push(vote); | ||
} | ||
} | ||
|
||
story._votes = votes; | ||
await remult.repo(Story).update(r.params.story, story); | ||
s.sendStatus(201); | ||
updateStory(story); | ||
}); | ||
}; | ||
|
||
export default vote; |
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,28 @@ | ||
import { remultExpress } from "remult/remult-express"; | ||
import { Story } from "../models/story"; | ||
import { FIBONACCI } from "../scales"; | ||
|
||
const server = remultExpress({ | ||
entities: [Story], | ||
async initApi(remult) { | ||
const storyRepo = remult.repo(Story); | ||
|
||
if ((await storyRepo.count()) === 0) { | ||
await storyRepo.insert({ | ||
id: "1", | ||
title: "Testing this thing", | ||
_votes: [ | ||
{ | ||
participant: { | ||
id: "test", | ||
name: "Test Participant", | ||
}, | ||
vote: FIBONACCI[0], | ||
}, | ||
], | ||
}); | ||
} | ||
}, | ||
}); | ||
|
||
export default server; |
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,8 +1,8 @@ | ||
import { Store } from "vuex"; | ||
import { storeState } from "../front-end/scripts/types"; | ||
import { StoreState } from "../front-end/scripts/types"; | ||
|
||
declare module "@vue/runtime-core" { | ||
interface ComponentCustomProperties { | ||
$store: Store<storeState>; | ||
$store: Store<StoreState>; | ||
} | ||
} |
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
Oops, something went wrong.