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

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,88 @@
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
+ /**
39
+ * A TypeScript generator. The hook's `createTsGenerator` bypasses it; `createGenerator` (an older
40
+ * core, or `@teacss/core` imported without a hook) re-enters it when TEACSS_ENGINE overrides
41
+ * `engine: "ts"`, and returns a facade for a configuration Rust can serve.
42
+ * @param {any} binding @param {object} userConfig @param {object} defaults
43
+ */
44
+ const createTs = (binding, userConfig, defaults) => {
45
+ const tsDefaults = { ...defaults, engine: "ts" };
46
+ return typeof binding.createTsGenerator === "function" ? binding.createTsGenerator(userConfig, tsDefaults) : binding.createGenerator(userConfig, tsDefaults);
47
+ };
48
+ return {
49
+ /** Called after a successful `commitConfig`; drops the lazy instance. @param {object} userConfig @param {object} defaults */
50
+ invalidate(userConfig, defaults) {
51
+ committed = { userConfig, defaults };
52
+ generatorPromise = null;
53
+ resolved = null;
54
+ },
55
+ /** The lazy TS generator; a rejection is not cached. @param {string} [member] */
56
+ generator(member = "materialize") {
57
+ generatorPromise ??= (async () => {
58
+ const binding = await core(member);
59
+ const generator = await createTs(binding, committed.userConfig, committed.defaults);
60
+ resolved = generator;
61
+ return generator;
62
+ })().catch((error) => {
63
+ generatorPromise = null;
64
+ throw error;
65
+ });
66
+ return generatorPromise;
67
+ },
68
+ /** The ready instance, or `null` before `generator()` resolves. */
69
+ peek() {
70
+ return resolved;
71
+ },
72
+ /**
73
+ * A TS candidate for a configuration (v02.9 §5.2), made by the TS generator's own
74
+ * `prepareConfig` so it tracks in-place edits of its config as an oracle candidate does
75
+ * (generator.ts:1986-2011). The lazy instance is untouched.
76
+ * @param {object} userConfig @param {object} defaults
77
+ */
78
+ async create(userConfig, defaults) {
79
+ const binding = await core("prepareConfig");
80
+ const tsDefaults = { ...defaults, engine: "ts" };
81
+ // Without `createTsGenerator` the base is built from the configuration itself: Rust cannot
82
+ // translate it, so `createGenerator` resolves to TS and creates no Engine.
83
+ const base = await createTs(binding, typeof binding.createTsGenerator === "function" ? {} : userConfig, defaults);
84
+ if (ENGINE_INSTANCE in Object(base) || typeof base?.prepareConfig !== "function") return base;
85
+ return (await base.prepareConfig(userConfig, tsDefaults)).generator;
86
+ },
87
+ };
88
+ }