Skip to content

Instantly share code, notes, and snippets.

@amb26
Created November 30, 2025 09:31
Show Gist options
  • Select an option

  • Save amb26/845cd434f27f573594c53d4e0caff3b7 to your computer and use it in GitHub Desktop.

Select an option

Save amb26/845cd434f27f573594c53d4e0caff3b7 to your computer and use it in GitHub Desktop.
/**
* Bind injections from the specified SFC layer to the supplied document.
* @param {String} layerName - The name of the layer for which injections are to be bound
* @param {Document} dokkument - The document object into which any injection directives will be injected
* @return {Signal<Boolean>} - A signal which resolves to `true` when injections are resolved
*/
fluid.subscribeDocToInjections = function (layerName, dokkument) {
const rec = fluid.readSFC(layerName);
// TODO: One day clear these out
let docEffect = rec.docInjectEffects.get(dokkument);
if (!docEffect) {
/** @type {Object<String, InjectRecord>} **/
let oldInjRecs = {};
const injDone = signal(fluid.unavailable("Injection in progress"));
injDone.layerName = layerName;
injDone.dokkument = dokkument;
const newDocEffect = fluid.effect(async function docInjectEffect(injRecs) {
fluid.startBufferDefs();
try {
/** @type {DocInjectionRecord[]} */
const docInjRecs = Object.values(injRecs).map(injRec =>
fluid.decodeInjectSFCElement(dokkument, injRec, rec.url, oldInjRecs,
injRec.variety === "script" ? fluid.scriptInjectionStyles : fluid.cssInjectionStyles)
).filter(injRec => injRec);
const injsDone = docInjRecs.map(docInjRec => fluid.doInjectSFCElement(docInjRec));
const injsDoneSignal = fluid.signalsToAvailable(injsDone);
const res = fluid.signalToPromise(injsDoneSignal);
await res;
console.log(`Await for ${layerName} is done`);
} finally {
console.log(`Finally for ${layerName} is starting`);
// Don't actually register layers until all the rest of the code has executed
fluid.endBufferDefs();
injDone.value = true;
}
Object.keys(oldInjRecs).forEach(nodeId => {
const newRec = injRecs[nodeId];
if (!newRec) {
fluid.clearInjRec(nodeId);
}
});
oldInjRecs = injRecs;
}, [rec.injRecsSignal]);
newDocEffect.injDone = injDone;
rec.docInjectEffects.set(dokkument, newDocEffect);
fluid.noteLayerInjectionInProgress(injDone);
docEffect = newDocEffect;
}
return docEffect.injDone;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment