Path: blob/main/src/command/render/render-services.ts
3583 views
/*1* render-services.ts2*3* Copyright (C) 2020-2023 Posit Software, PBC4*/56import { kRenderServicesLifetime } from "../../config/constants.ts";7import { createNamedLifetime, getNamedLifetime } from "../../core/lifetimes.ts";8import { createTempContext } from "../../core/temp.ts";9import { createExtensionContext } from "../../extension/extension.ts";10import { NotebookContext } from "../../render/notebook/notebook-types.ts";11import { RenderServiceWithLifetime } from "./types.ts";1213export function renderServices(14notebook: NotebookContext,15): RenderServiceWithLifetime {16const temp = createTempContext();17const extension = createExtensionContext();1819if (getNamedLifetime(kRenderServicesLifetime)) {20return {21temp,22extension,23notebook,24lifetime: getNamedLifetime(kRenderServicesLifetime),25cleanup: () => {},26};27} else {28const lifetime = createNamedLifetime(kRenderServicesLifetime);29lifetime.attach(temp);30lifetime.attach(notebook);31return {32temp,33extension,34notebook,35lifetime,36cleanup: () => {37lifetime.cleanup();38},39};40}41}4243export function withRenderServices<T>(44notebook: NotebookContext,45fn: (services: RenderServiceWithLifetime) => Promise<T>,46) {47const services = renderServices(notebook);48return fn(services).finally(() => {49services.cleanup();50});51}525354