-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
172 additions
and
182 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { fjp } from "../../deps.ts"; | ||
import { storage } from "../../fsStorage.ts"; | ||
import { Acked, Commands, Events, State } from "../../types.ts"; | ||
|
||
export interface Props { | ||
/** Environment name to connect to */ | ||
name: string; | ||
} | ||
|
||
const subscribers: WebSocket[] = []; | ||
|
||
export const fetchState = async (): Promise<State> => ({ | ||
decofile: await storage.state({ forceFresh: true }), | ||
}); | ||
|
||
const saveState = ({ decofile }: State): Promise<void> => | ||
storage.update(decofile); | ||
|
||
// Apply patch and save state ATOMICALLY! | ||
// This is easily done on play. On production, however, we probably | ||
// need a distributed queue | ||
let queue = Promise.resolve(); | ||
const patchState = (ops: fjp.Operation[]) => { | ||
queue = queue.catch(() => null).then(async () => | ||
saveState(ops.reduce(fjp.applyReducer, await fetchState())) | ||
); | ||
|
||
return queue; | ||
}; | ||
|
||
const action = (_props: Props, req: Request) => { | ||
const { socket, response } = Deno.upgradeWebSocket(req); | ||
|
||
const broadcast = (event: Acked<Events>) => { | ||
const message = JSON.stringify(event); | ||
subscribers.forEach((s) => s.send(message)); | ||
}; | ||
const send = (event: Acked<Events>) => socket.send(JSON.stringify(event)); | ||
const parse = (event: MessageEvent<string>): Acked<Commands> => | ||
JSON.parse(event.data); | ||
|
||
const open = () => subscribers.push(socket); | ||
const close = () => subscribers.splice(subscribers.indexOf(socket), 1); | ||
const message = async (event: MessageEvent<string>) => { | ||
const data = parse(event); | ||
|
||
const { ack } = data; | ||
|
||
if (data.type === "patch-state") { | ||
try { | ||
const { payload: operations } = data; | ||
|
||
await patchState(operations); | ||
|
||
// Broadcast changes | ||
broadcast({ | ||
type: "state-patched", | ||
payload: operations, | ||
etag: await storage.revision(), | ||
metadata: {}, // TODO: add metadataß | ||
ack, | ||
}); | ||
} catch ({ name, operation }) { | ||
console.error({ name, operation }); | ||
} | ||
} else if (data.type === "fetch-state") { | ||
send({ | ||
type: "state-fetched", | ||
payload: await fetchState(), | ||
etag: await storage.revision(), | ||
ack, | ||
}); | ||
} else { | ||
console.error("UNKNOWN EVENT", event); | ||
} | ||
}; | ||
|
||
/** | ||
* Handles the WebSocket connection on open event. | ||
*/ | ||
socket.onopen = open; | ||
/** | ||
* Handles the WebSocket connection on close event. | ||
*/ | ||
socket.onclose = close; | ||
|
||
/** | ||
* Handles the WebSocket connection on message event. | ||
* @param {MessageEvent} event - The WebSocket message event. | ||
*/ | ||
socket.onmessage = (e) => message(e).catch(() => {}); | ||
|
||
return response; | ||
}; | ||
|
||
export default action; |
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,18 @@ | ||
import { fetchState } from "./connect.ts"; | ||
import { State } from "../../types.ts"; | ||
import { storage } from "../../fsStorage.ts"; | ||
|
||
interface Props { | ||
name: string; | ||
} | ||
|
||
/** TODO(@gimenes): Implement fetching the state from the proper environment name */ | ||
const action = async (_props: Props): Promise<State["decofile"]> => { | ||
const { decofile } = await fetchState(); | ||
|
||
console.log("serving revision", await storage.revision()); | ||
|
||
return decofile; | ||
}; | ||
|
||
export default action; |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,41 @@ | ||
import { type Resolvable } from "deco/engine/core/resolver.ts"; | ||
import { type fjp } from "./deps.ts"; | ||
|
||
export interface Pagination<T> { | ||
data: T[]; | ||
page: number; | ||
pageSize: number; | ||
total: number; | ||
} | ||
|
||
export interface PatchState { | ||
type: "patch-state"; | ||
payload: fjp.Operation[]; | ||
} | ||
|
||
export interface FetchState { | ||
type: "fetch-state"; | ||
} | ||
|
||
export interface StatePatched { | ||
type: "state-patched"; | ||
payload: fjp.Operation[]; | ||
etag: string; | ||
// Maybe add data and user info in here | ||
metadata?: unknown; | ||
} | ||
|
||
export interface StateFetched { | ||
type: "state-fetched"; | ||
payload: State; | ||
etag: string; | ||
} | ||
|
||
export type Acked<T> = T & { ack: string }; | ||
|
||
export interface State { | ||
decofile: Record<string, Resolvable>; | ||
} | ||
|
||
export type Commands = PatchState | FetchState; | ||
export type Events = StatePatched | StateFetched; |
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