-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plumb VS Code getSession through to cell environment
- Loading branch information
Jake Donham
committed
Jun 28, 2024
1 parent
22cec49
commit f2f22d4
Showing
9 changed files
with
272 additions
and
129 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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { Cells } from "./cells"; | ||
import { executeCell } from "./executeCell"; | ||
import { Runtime } from "./runtime"; | ||
import { Rpc } from "./rpc"; | ||
|
||
function extOfLanguage(language: string): string { | ||
switch (language) { | ||
case "typescriptreact": | ||
return "tsx"; | ||
case "typescript": | ||
return "ts"; | ||
case "javascriptreact": | ||
return "jsx"; | ||
case "javascript": | ||
return "js"; | ||
default: | ||
throw new Error(`unknown language "${language}"`); | ||
} | ||
} | ||
|
||
export class Client { | ||
private rpc: Rpc; | ||
private cells: Cells; | ||
private runtime: Runtime; | ||
|
||
constructor(rpc: Rpc, cells: Cells, runtime: Runtime) { | ||
this.rpc = rpc; | ||
this.cells = cells; | ||
this.runtime = runtime; | ||
} | ||
|
||
async executeCells( | ||
cells: { | ||
path: string; | ||
cellId: string; | ||
language: string; | ||
code?: string; | ||
}[], | ||
force: boolean, | ||
executeDirtyCells: boolean | ||
) { | ||
let dirtyCells: { path: string; cellId: string; ext: string }[] = []; | ||
|
||
for (const { path, cellId, language, code } of cells) { | ||
const ext = extOfLanguage(language); | ||
const id = `${path}-cellId=${cellId}.${ext}`; | ||
if (code) { | ||
this.cells.set(id, { cellId, code, language }); | ||
} | ||
|
||
this.runtime.invalidateServerModule(id); | ||
this.runtime.handleHMRUpdate(id); | ||
this.runtime.invalidateRuntimeModule(id, dirtyCells); | ||
} | ||
|
||
dirtyCells = dirtyCells.filter( | ||
(dirtyCell) => | ||
!cells.some( | ||
(cell) => | ||
cell.path === dirtyCell.path && cell.cellId === dirtyCell.cellId | ||
) | ||
); | ||
|
||
const cellsToExecute = [ | ||
...cells.map(({ path, cellId, language }) => ({ | ||
path, | ||
cellId, | ||
ext: extOfLanguage(language), | ||
force, | ||
})), | ||
...(executeDirtyCells | ||
? dirtyCells.map((cell) => ({ ...cell, force: false })) | ||
: []), | ||
]; | ||
|
||
const executed = await Promise.all( | ||
cellsToExecute.map(({ path, cellId, ext, force }) => | ||
executeCell( | ||
this.rpc, | ||
this.cells, | ||
this.runtime, | ||
`${path}-cellId=${cellId}.${ext}`, | ||
path, | ||
cellId, | ||
force | ||
) | ||
) | ||
); | ||
|
||
const cellsToMarkDirty = cellsToExecute.filter( | ||
({ force }, i) => !force && !executed[i] | ||
); | ||
this.rpc.markCellsDirty(cellsToMarkDirty); | ||
} | ||
|
||
removeCells( | ||
cells: { | ||
path: string; | ||
cellId: string; | ||
language: string; | ||
}[] | ||
) { | ||
let dirtyCells: { path: string; cellId: string; ext: string }[] = []; | ||
|
||
for (const { path, cellId, language } of cells) { | ||
const ext = extOfLanguage(language); | ||
const id = `${path}-cellId=${cellId}.${ext}`; | ||
this.cells.delete(id); | ||
|
||
this.runtime.invalidateServerModule(id); | ||
// TODO(jaked) HMR remove? | ||
this.runtime.invalidateRuntimeModule(id, dirtyCells); | ||
} | ||
|
||
// don't mark cells dirty if they were just removed | ||
dirtyCells = dirtyCells.filter( | ||
(dirtyCell) => | ||
!cells.some( | ||
(cell) => | ||
cell.path === dirtyCell.path && cell.cellId === dirtyCell.cellId | ||
) | ||
); | ||
this.rpc.markCellsDirty(dirtyCells); | ||
} | ||
} |
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.