Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/command/render/render-services.ts
3583 views
1
/*
2
* render-services.ts
3
*
4
* Copyright (C) 2020-2023 Posit Software, PBC
5
*/
6
7
import { kRenderServicesLifetime } from "../../config/constants.ts";
8
import { createNamedLifetime, getNamedLifetime } from "../../core/lifetimes.ts";
9
import { createTempContext } from "../../core/temp.ts";
10
import { createExtensionContext } from "../../extension/extension.ts";
11
import { NotebookContext } from "../../render/notebook/notebook-types.ts";
12
import { RenderServiceWithLifetime } from "./types.ts";
13
14
export function renderServices(
15
notebook: NotebookContext,
16
): RenderServiceWithLifetime {
17
const temp = createTempContext();
18
const extension = createExtensionContext();
19
20
if (getNamedLifetime(kRenderServicesLifetime)) {
21
return {
22
temp,
23
extension,
24
notebook,
25
lifetime: getNamedLifetime(kRenderServicesLifetime),
26
cleanup: () => {},
27
};
28
} else {
29
const lifetime = createNamedLifetime(kRenderServicesLifetime);
30
lifetime.attach(temp);
31
lifetime.attach(notebook);
32
return {
33
temp,
34
extension,
35
notebook,
36
lifetime,
37
cleanup: () => {
38
lifetime.cleanup();
39
},
40
};
41
}
42
}
43
44
export function withRenderServices<T>(
45
notebook: NotebookContext,
46
fn: (services: RenderServiceWithLifetime) => Promise<T>,
47
) {
48
const services = renderServices(notebook);
49
return fn(services).finally(() => {
50
services.cleanup();
51
});
52
}
53
54