@teacss/engine 0.0.0-stage → 0.8.5-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,78 @@
1
+ // @ts-check
2
+ // The lazily created TypeScript generator behind the facade (v02.9 §3.3).
3
+
4
+ import { ENGINE_INSTANCE } from "./constants.mjs";
5
+ import { tsRequiredError } from "./errors.mjs";
6
+
7
+ /** @typedef {{ version: string, createGenerator: (config?: object, defaults?: object) => Promise<any>, extractorSplit: object }} CoreBinding */
8
+
9
+ /** Test seam that replaces the dynamic import of `@teacss/core`. */
10
+ export const coreLoader = {
11
+ /** @type {(() => Promise<CoreBinding>) | null} */
12
+ load: null,
13
+ };
14
+
15
+ /** @param {string} member @returns {Promise<CoreBinding>} rejects with `E_TS_REQUIRED` when the peer is missing */
16
+ export async function importCore(member) {
17
+ try {
18
+ // @ts-ignore optional peer
19
+ return await (coreLoader.load ? coreLoader.load() : import("@teacss/core"));
20
+ } catch {
21
+ throw tsRequiredError(member);
22
+ }
23
+ }
24
+
25
+ /**
26
+ * `engine: "ts"` goes into the defaults, which take precedence over `config.engine`, so the TS
27
+ * generator does not enter the hook again.
28
+ * @param {{ core?: CoreBinding | undefined }} host
29
+ * @param {{ userConfig: object, defaults: object }} committed the configuration committed at construction
30
+ */
31
+ export function createTsFallback(host, committed) {
32
+ /** @type {Promise<any> | null} */
33
+ let generatorPromise = null;
34
+ /** @type {any} */
35
+ let resolved = null;
36
+ /** @param {string} member */
37
+ const core = (member) => (host.core ? Promise.resolve(host.core) : importCore(member));
38
+ return {
39
+ /** Called after a successful `commitConfig`; drops the lazy instance. @param {object} userConfig @param {object} defaults */
40
+ invalidate(userConfig, defaults) {
41
+ committed = { userConfig, defaults };
42
+ generatorPromise = null;
43
+ resolved = null;
44
+ },
45
+ /** The lazy TS generator; a rejection is not cached. @param {string} [member] */
46
+ generator(member = "materialize") {
47
+ generatorPromise ??= (async () => {
48
+ const binding = await core(member);
49
+ const generator = await binding.createGenerator(committed.userConfig, { ...committed.defaults, engine: "ts" });
50
+ resolved = generator;
51
+ return generator;
52
+ })().catch((error) => {
53
+ generatorPromise = null;
54
+ throw error;
55
+ });
56
+ return generatorPromise;
57
+ },
58
+ /** The ready instance, or `null` before `generator()` resolves. */
59
+ peek() {
60
+ return resolved;
61
+ },
62
+ /**
63
+ * A TS candidate for a configuration (v02.9 §5.2), made by the TS generator's own
64
+ * `prepareConfig` so it tracks in-place edits of its config as an oracle candidate does
65
+ * (generator.ts:1986-2011). The lazy instance is untouched.
66
+ * @param {object} userConfig @param {object} defaults
67
+ */
68
+ async create(userConfig, defaults) {
69
+ const binding = await core("prepareConfig");
70
+ const tsDefaults = { ...defaults, engine: "ts" };
71
+ // The base is built from the configuration itself: Rust cannot translate it, so it is a TS
72
+ // generator even when TEACSS_ENGINE overrides `engine: "ts"`, and no Engine is created.
73
+ const base = await binding.createGenerator(userConfig, tsDefaults);
74
+ if (ENGINE_INSTANCE in Object(base) || typeof base?.prepareConfig !== "function") return base;
75
+ return (await base.prepareConfig(userConfig, tsDefaults)).generator;
76
+ },
77
+ };
78
+ }