inlinable-runtime
Version:
JavaScript code inlining runtime utilities
28 lines (27 loc) • 804 B
JavaScript
/** Sentinel module, only meant to be imported directly from `inlinable` */
/** Returns true if code is currently being inlined. */
export function isInlining() {
// TODO: We could add more granularity, for example exposing the path of the
// file currently being inlined as optional argument to `isInlining`.
return globalThis.__isInlining != null;
}
export async function inlining(lp, fn) {
let state = globalThis.__isInlining;
if (!state) {
state = new Set();
globalThis.__isInlining = state;
}
if (state.has(lp)) {
throw new Error(`Already inlining ${lp}`);
}
state.add(lp);
try {
await fn();
}
finally {
state.delete(lp);
if (!state.size) {
delete globalThis.__isInlining;
}
}
}