-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create files app #239
Merged
Merged
Create files app #239
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,15 @@ | ||
import { fetchState } from "../../actions/releases/fork.ts"; | ||
import { State } from "../../types.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(); | ||
|
||
return decofile; | ||
}; | ||
|
||
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should fetchstate return the revision as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let's see how the implementation outside localhost works. Maybe we'll need to change this again