@blankgrimoire/runtime 0.0.0-stage → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +59 -2
- package/dist/css.d.ts +10 -0
- package/dist/css.js +21 -0
- package/dist/dom.d.ts +39 -0
- package/dist/dom.js +95 -0
- package/dist/engine.d.ts +35 -0
- package/dist/engine.js +311 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/prelude.d.ts +27 -0
- package/dist/prelude.js +96 -0
- package/dist/runtime.js +790 -0
- package/dist/strategies.d.ts +49 -0
- package/dist/strategies.js +282 -0
- package/package.json +37 -3
package/dist/prelude.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
import { API_VERSION } from '@blankgrimoire/types';
|
|
3
|
+
export const __apiVersion = API_VERSION;
|
|
4
|
+
function nativeHost() {
|
|
5
|
+
return globalThis.__native;
|
|
6
|
+
}
|
|
7
|
+
const timers = new Map();
|
|
8
|
+
const timerHandles = new Map();
|
|
9
|
+
let timerIdCounter = 0;
|
|
10
|
+
/**
|
|
11
|
+
* Register a timer. When the host exposes `__native.setTimeout`, the callback
|
|
12
|
+
* is scheduled natively after `ms`; otherwise the host must call
|
|
13
|
+
* `__timerFire(id)`.
|
|
14
|
+
*/
|
|
15
|
+
export function setTimeout(cb, ms) {
|
|
16
|
+
const id = ++timerIdCounter;
|
|
17
|
+
timers.set(id, cb);
|
|
18
|
+
const host = nativeHost();
|
|
19
|
+
if (host && typeof host.setTimeout === 'function') {
|
|
20
|
+
const handle = host.setTimeout(() => __timerFire(id), ms);
|
|
21
|
+
timerHandles.set(id, handle);
|
|
22
|
+
}
|
|
23
|
+
return id;
|
|
24
|
+
}
|
|
25
|
+
export function clearTimeout(id) {
|
|
26
|
+
timers.delete(id);
|
|
27
|
+
const host = nativeHost();
|
|
28
|
+
const handle = timerHandles.get(id);
|
|
29
|
+
if (handle !== undefined && host && typeof host.clearTimeout === 'function') {
|
|
30
|
+
host.clearTimeout(handle);
|
|
31
|
+
}
|
|
32
|
+
timerHandles.delete(id);
|
|
33
|
+
}
|
|
34
|
+
export function __timerFire(id) {
|
|
35
|
+
const cb = timers.get(id);
|
|
36
|
+
if (cb) {
|
|
37
|
+
timers.delete(id);
|
|
38
|
+
timerHandles.delete(id);
|
|
39
|
+
cb();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Call a native host function by name. */
|
|
43
|
+
export function __nativeInvoke(fn, ...args) {
|
|
44
|
+
const host = nativeHost();
|
|
45
|
+
if (!host || typeof host[fn] !== 'function') {
|
|
46
|
+
throw new Error(`Host function not implemented: ${fn}`);
|
|
47
|
+
}
|
|
48
|
+
return host[fn](...args);
|
|
49
|
+
}
|
|
50
|
+
/** Perform a native HTTP request. */
|
|
51
|
+
export function __http(request) {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
const host = nativeHost();
|
|
54
|
+
if (!host || typeof host.request !== 'function') {
|
|
55
|
+
reject(new Error('Host function not implemented: request'));
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
Promise.resolve(host.request(request)).then(resolve, reject);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
reject(error);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Parse HTML through the native bridge. In Node tests the host can inject a
|
|
68
|
+
* `parseHTML` implementation; jsdom documents are also accepted as a fallback.
|
|
69
|
+
*/
|
|
70
|
+
export function __parseHTML(html) {
|
|
71
|
+
const host = nativeHost();
|
|
72
|
+
if (host && typeof host.parseHTML === 'function') {
|
|
73
|
+
return host.parseHTML(html);
|
|
74
|
+
}
|
|
75
|
+
const document = globalThis.document;
|
|
76
|
+
if (document?.implementation?.createHTMLDocument) {
|
|
77
|
+
const parsed = document.implementation.createHTMLDocument('');
|
|
78
|
+
if (parsed.documentElement) {
|
|
79
|
+
parsed.documentElement.innerHTML = html;
|
|
80
|
+
}
|
|
81
|
+
return parsed;
|
|
82
|
+
}
|
|
83
|
+
throw new Error('Host function not implemented: parseHTML');
|
|
84
|
+
}
|
|
85
|
+
const console = {
|
|
86
|
+
log: (...args) => {
|
|
87
|
+
nativeHost()?.log?.(...args);
|
|
88
|
+
},
|
|
89
|
+
error: (...args) => {
|
|
90
|
+
nativeHost()?.error?.(...args);
|
|
91
|
+
},
|
|
92
|
+
warn: (...args) => {
|
|
93
|
+
nativeHost()?.warn?.(...args);
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
export { console };
|