getState
This commit is contained in:
parent
0de8b07e29
commit
8f7fc5d36b
|
|
@ -1,10 +1,10 @@
|
||||||
import { apps } from "@/webapp"
|
import { apps } from "@/webapp"
|
||||||
import { Thread } from "@/shell"
|
import { getState } from "@/state"
|
||||||
|
|
||||||
export default function (project: string) {
|
export default function (project: string) {
|
||||||
const state = Thread.getStore()
|
const state = getState()
|
||||||
|
|
||||||
if (apps().includes(project) && state) {
|
if (state && apps().includes(project)) {
|
||||||
state.project = project
|
state.project = project
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
import { readdirSync } from "fs"
|
import { readdirSync } from "fs"
|
||||||
import { NOSE_WWW } from "@/config"
|
import { NOSE_WWW } from "@/config"
|
||||||
import { Thread } from "@/shell"
|
import { getState } from "@/state"
|
||||||
import { appPath } from "@/webapp"
|
import { appPath } from "@/webapp"
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
const state = Thread.getStore()
|
const state = getState()
|
||||||
if (!state) return { error: "no state" }
|
if (!state) return { error: "no state" }
|
||||||
|
|
||||||
const project = state.project
|
const project = state.project
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { Thread } from "@/shell"
|
import { getState } from "@/state"
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
const state = Thread.getStore()
|
const state = getState()
|
||||||
if (!state) return { error: "no state" }
|
if (!state) return { error: "no state" }
|
||||||
|
|
||||||
return state?.project || "none"
|
return state?.project || "none"
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { apps } from "@/webapp"
|
import { apps } from "@/webapp"
|
||||||
import { Thread } from "@/shell"
|
import { getState } from "@/state"
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
const state = Thread.getStore()
|
const state = getState()
|
||||||
if (!state) return { error: "no state" }
|
if (!state) return { error: "no state" }
|
||||||
|
|
||||||
return { html: apps().map(app => app === state.project ? `<b class="cyan">${app}</b>` : app).join(" ") }
|
return { html: apps().map(app => app === state.project ? `<b class="cyan">${app}</b>` : app).join(" ") }
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { Glob } from "bun"
|
import { Glob } from "bun"
|
||||||
import { watch } from "fs"
|
import { watch } from "fs"
|
||||||
import { NOSE_SYS_BIN, NOSE_BIN } from "./config"
|
|
||||||
import { sendAll } from "./websocket"
|
import { sendAll } from "./websocket"
|
||||||
import { expectDir } from "./utils"
|
import { expectDir } from "./utils"
|
||||||
|
import { NOSE_SYS_BIN, NOSE_BIN } from "./config"
|
||||||
|
|
||||||
const sysCmdWatcher = watch(NOSE_SYS_BIN, async (event, filename) =>
|
const sysCmdWatcher = watch(NOSE_SYS_BIN, async (event, filename) =>
|
||||||
sendAll({ type: "commands", data: await commands() })
|
sendAll({ type: "commands", data: await commands() })
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import { transpile, isFile, tilde } from "./utils"
|
||||||
import { apps, serveApp, publishDNS } from "./webapp"
|
import { apps, serveApp, publishDNS } from "./webapp"
|
||||||
import { runCommand } from "./shell"
|
import { runCommand } from "./shell"
|
||||||
import { commands } from "./commands"
|
import { commands } from "./commands"
|
||||||
import { send, addWebsocket, removeWebsocket, closeWebsockets, websockets } from "./websocket"
|
import { send, addWebsocket, removeWebsocket, closeWebsockets } from "./websocket"
|
||||||
|
|
||||||
import { Layout } from "./components/layout"
|
import { Layout } from "./components/layout"
|
||||||
import { Terminal } from "./components/terminal"
|
import { Terminal } from "./components/terminal"
|
||||||
|
|
|
||||||
15
src/shell.ts
15
src/shell.ts
|
|
@ -2,20 +2,11 @@
|
||||||
// runs commands and such.
|
// runs commands and such.
|
||||||
|
|
||||||
import type { CommandResult, CommandOutput } from "./shared/types"
|
import type { CommandResult, CommandOutput } from "./shared/types"
|
||||||
|
import type { State } from "./state"
|
||||||
import { join } from "path"
|
import { join } from "path"
|
||||||
import { NOSE_SYS_BIN, NOSE_BIN } from "./config"
|
import { NOSE_SYS_BIN, NOSE_BIN } from "./config"
|
||||||
import { isFile } from "./utils"
|
import { isFile } from "./utils"
|
||||||
import { AsyncLocalStorage } from "async_hooks"
|
import { ALS } from "./state"
|
||||||
|
|
||||||
// Ensure "Thread" lives between bun's hot reloads
|
|
||||||
const g = globalThis as typeof globalThis & { __thread?: AsyncLocalStorage<State> }
|
|
||||||
export const Thread = g.__thread ??= new AsyncLocalStorage<State>()
|
|
||||||
|
|
||||||
type State = {
|
|
||||||
id?: string
|
|
||||||
session?: string
|
|
||||||
project?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
const sessions: Map<string, State> = new Map()
|
const sessions: Map<string, State> = new Map()
|
||||||
|
|
||||||
|
|
@ -31,7 +22,7 @@ export async function runCommand(session: string, id: string, input: string): Pr
|
||||||
const state = getState(session, id)
|
const state = getState(session, id)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
[status, output] = await Thread.run(state, async () => await exec(cmd, args))
|
[status, output] = await ALS.run(state, async () => await exec(cmd, args))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
status = "error"
|
status = "error"
|
||||||
output = errorMessage(err)
|
output = errorMessage(err)
|
||||||
|
|
|
||||||
15
src/state.ts
Normal file
15
src/state.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { AsyncLocalStorage } from "async_hooks"
|
||||||
|
|
||||||
|
export type State = {
|
||||||
|
id?: string
|
||||||
|
session?: string
|
||||||
|
project?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure "ALS" lives between bun's hot reloads
|
||||||
|
const g = globalThis as typeof globalThis & { __thread?: AsyncLocalStorage<State> }
|
||||||
|
export const ALS = g.__thread ??= new AsyncLocalStorage<State>()
|
||||||
|
|
||||||
|
export function getState(): State | undefined {
|
||||||
|
return ALS.getStore()
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
import { type Context, Hono } from "hono"
|
|
||||||
import type { Child } from "hono/jsx"
|
import type { Child } from "hono/jsx"
|
||||||
|
import { type Context, Hono } from "hono"
|
||||||
import { renderToString } from "hono/jsx/dom/server"
|
import { renderToString } from "hono/jsx/dom/server"
|
||||||
import { join, dirname } from "path"
|
import { join, dirname } from "path"
|
||||||
import { readdirSync, watch } from "fs"
|
import { readdirSync, watch } from "fs"
|
||||||
|
|
||||||
import { NOSE_WWW } from "./config"
|
import { NOSE_WWW } from "./config"
|
||||||
import { expectDir, isFile } from "./utils"
|
import { expectDir, isFile } from "./utils"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user