State -> Session

This commit is contained in:
Chris Wanstrath 2025-09-26 09:47:45 -07:00
parent ef282e6df7
commit acabf8c4c6
8 changed files with 20 additions and 20 deletions

View File

@ -4,7 +4,7 @@ import { join, extname } from "path"
import type { CommandOutput } from "app/src/shared/types" import type { CommandOutput } from "app/src/shared/types"
import { NOSE_WWW } from "app/src/config" import { NOSE_WWW } from "app/src/config"
import { getState } from "app/src/state" import { getState } from "@/session"
import { appPath } from "app/src/webapp" import { appPath } from "app/src/webapp"
import { isBinaryFile } from "app/src/utils" import { isBinaryFile } from "app/src/utils"
import { highlight } from "../lib/highlight" import { highlight } from "../lib/highlight"

View File

@ -4,7 +4,7 @@ import { join, extname } from "path"
import type { CommandOutput } from "app/src/shared/types" import type { CommandOutput } from "app/src/shared/types"
import { NOSE_WWW } from "app/src/config" import { NOSE_WWW } from "app/src/config"
import { getState } from "app/src/state" import { getState } from "@/session"
import { appPath } from "app/src/webapp" import { appPath } from "app/src/webapp"
import { isBinaryFile } from "app/src/utils" import { isBinaryFile } from "app/src/utils"
import { countChar } from "app/src/shared/utils" import { countChar } from "app/src/shared/utils"

View File

@ -1,5 +1,5 @@
import { apps } from "app/src/webapp" import { apps } from "app/src/webapp"
import { getState } from "app/src/state" import { getState } from "@/session"
export default function (project: string) { export default function (project: string) {
const state = getState() const state = getState()

View File

@ -1,6 +1,6 @@
import { readdirSync } from "fs" import { readdirSync } from "fs"
import { NOSE_WWW } from "app/src/config" import { NOSE_WWW } from "app/src/config"
import { getState } from "app/src/state" import { getState } from "@/session"
import { appPath } from "app/src/webapp" import { appPath } from "app/src/webapp"
export default function () { export default function () {

View File

@ -1,4 +1,4 @@
import { getState } from "app/src/state" import { getState } from "@/session"
export default function () { export default function () {
const state = getState() const state = getState()

View File

@ -1,5 +1,5 @@
import { apps } from "app/src/webapp" import { apps } from "app/src/webapp"
import { getState } from "app/src/state" import { getState } from "@/session"
export default function () { export default function () {
const state = getState() const state = getState()

View File

@ -1,15 +1,15 @@
import { AsyncLocalStorage } from "async_hooks" import { AsyncLocalStorage } from "async_hooks"
export type State = { export type Session = {
id?: string taskId?: string
session?: string sessionId?: string
project?: string project?: string
} }
// Ensure "ALS" lives between bun's hot reloads // Ensure "ALS" lives between bun's hot reloads
const g = globalThis as typeof globalThis & { __thread?: AsyncLocalStorage<State> } const g = globalThis as typeof globalThis & { __thread?: AsyncLocalStorage<Session> }
export const ALS = g.__thread ??= new AsyncLocalStorage<State>() export const ALS = g.__thread ??= new AsyncLocalStorage<Session>()
export function getState(): State | undefined { export function getState(): Session | undefined {
return ALS.getStore() return ALS.getStore()
} }

View File

@ -3,12 +3,12 @@
import { join } from "path" import { join } from "path"
import type { CommandResult, CommandOutput } from "./shared/types" import type { CommandResult, CommandOutput } from "./shared/types"
import type { State } from "./state" import type { Session } from "./session"
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 { ALS } from "./state" import { ALS } from "./session"
const sessions: Map<string, State> = new Map() const sessions: Map<string, Session> = new Map()
export async function runCommand(session: string, id: string, input: string): Promise<CommandResult> { export async function runCommand(session: string, id: string, input: string): Promise<CommandResult> {
const [cmd = "", ...args] = input.split(" ") const [cmd = "", ...args] = input.split(" ")
@ -56,13 +56,13 @@ function processExecOutput(output: string | any): ["ok" | "error", CommandOutput
} }
} }
function getState(session: string, id: string): State { function getState(sessionId: string, taskId: string): Session {
let state = sessions.get(session) let state = sessions.get(sessionId)
if (!state) { if (!state) {
state = { session, project: "" } state = { sessionId: sessionId, project: "" }
sessions.set(session, state) sessions.set(sessionId, state)
} }
state.id = id state.taskId = taskId
return state return state
} }