@impro.social/impro-plugin 0.0.17 → 0.0.19
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/main.js +63 -15
- package/package.json +1 -1
package/main.js
CHANGED
|
@@ -395,32 +395,53 @@ export class Plugin {
|
|
|
395
395
|
});
|
|
396
396
|
}
|
|
397
397
|
|
|
398
|
-
|
|
398
|
+
// options.cacheKey: array of context fields. If provided, the host
|
|
399
|
+
// will treat the slot content as a pure function of these fields
|
|
400
|
+
// - omitting other fields in the callback and caching return values
|
|
401
|
+
// until they're invalidated by refreshSlot(). An empty array declares
|
|
402
|
+
// that the content depends on no context at all, so one cached result
|
|
403
|
+
// serves every instance.
|
|
404
|
+
//
|
|
405
|
+
// The host batches all pending contexts of a render into one call.
|
|
406
|
+
registerSlot(name, callback = () => null, options = {}) {
|
|
399
407
|
const handlerId = uuid.create();
|
|
400
|
-
callHandlers.set(handlerId, async (
|
|
401
|
-
const
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
)
|
|
408
|
+
callHandlers.set(handlerId, async (batch) => {
|
|
409
|
+
const results = [];
|
|
410
|
+
for (const context of batch) {
|
|
411
|
+
try {
|
|
412
|
+
results.push({
|
|
413
|
+
value: await getSlotContent(name, callback, context),
|
|
414
|
+
});
|
|
415
|
+
} catch (error) {
|
|
416
|
+
results.push({ error: error?.message ?? String(error) });
|
|
417
|
+
}
|
|
408
418
|
}
|
|
409
|
-
return
|
|
419
|
+
return results;
|
|
410
420
|
});
|
|
421
|
+
const cacheKey = Array.isArray(options.cacheKey)
|
|
422
|
+
? options.cacheKey.filter((field) => typeof field === "string")
|
|
423
|
+
: null;
|
|
411
424
|
self.postMessage({
|
|
412
425
|
type: "register",
|
|
413
426
|
target: "slot",
|
|
414
427
|
name,
|
|
415
428
|
handlerId,
|
|
429
|
+
cacheKey,
|
|
430
|
+
batch: true,
|
|
416
431
|
});
|
|
417
432
|
}
|
|
418
433
|
|
|
419
|
-
// Makes
|
|
420
|
-
// registered
|
|
421
|
-
// on
|
|
422
|
-
|
|
423
|
-
|
|
434
|
+
// Makes mounted <plugin-slot name=...> instances re-invoke this plugin's
|
|
435
|
+
// registered callback for that slot, and drops any cached results. Useful
|
|
436
|
+
// when a slot's content depends on plugin state that changed after render.
|
|
437
|
+
//
|
|
438
|
+
// options.keys: array of matcher objects to be OR'd together,
|
|
439
|
+
// e.g. [{ did: "..." }] - any matching slots will be invalidated / refreshed.
|
|
440
|
+
// Omit to refresh every instance. A slot registered with a cacheKey can only
|
|
441
|
+
// be matched on those declared fields, since its output depends on nothing
|
|
442
|
+
// else.
|
|
443
|
+
refreshSlot(name, options = {}) {
|
|
444
|
+
return hostCall("refreshSlot", { name, keys: options.keys ?? null });
|
|
424
445
|
}
|
|
425
446
|
|
|
426
447
|
onload() {}
|
|
@@ -446,6 +467,18 @@ export class Plugin {
|
|
|
446
467
|
}
|
|
447
468
|
}
|
|
448
469
|
|
|
470
|
+
async function getSlotContent(name, callback, context) {
|
|
471
|
+
const result = await callback(context);
|
|
472
|
+
if (result == null) return null;
|
|
473
|
+
if (!(result instanceof VirtualEl)) {
|
|
474
|
+
const description = result?.constructor?.name ?? typeof result;
|
|
475
|
+
throw new Error(
|
|
476
|
+
`Slot "${name}" must return a VirtualEl or null, got ${description}`,
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
return result._serialize();
|
|
480
|
+
}
|
|
481
|
+
|
|
449
482
|
function serializeTransformTokens(tokens) {
|
|
450
483
|
if (!Array.isArray(tokens)) return tokens;
|
|
451
484
|
return tokens.map((token) => {
|
|
@@ -535,6 +568,21 @@ export class Modal {
|
|
|
535
568
|
});
|
|
536
569
|
}
|
|
537
570
|
|
|
571
|
+
update() {
|
|
572
|
+
if (!openModals.has(this._modalId)) return;
|
|
573
|
+
self.postMessage({
|
|
574
|
+
type: "hostCall",
|
|
575
|
+
method: "updateModal",
|
|
576
|
+
args: [
|
|
577
|
+
{
|
|
578
|
+
modalId: this._modalId,
|
|
579
|
+
title: this.titleEl._serialize(),
|
|
580
|
+
content: this.contentEl._serialize(),
|
|
581
|
+
},
|
|
582
|
+
],
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
|
|
538
586
|
close() {
|
|
539
587
|
if (!openModals.has(this._modalId)) return;
|
|
540
588
|
openModals.delete(this._modalId);
|