@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.
Files changed (2) hide show
  1. package/main.js +63 -15
  2. package/package.json +1 -1
package/main.js CHANGED
@@ -395,32 +395,53 @@ export class Plugin {
395
395
  });
396
396
  }
397
397
 
398
- registerSlot(name, callback = () => null) {
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 (context) => {
401
- const result = await callback(context);
402
- if (result == null) return null;
403
- if (!(result instanceof VirtualEl)) {
404
- const description = result?.constructor?.name ?? typeof result;
405
- throw new Error(
406
- `Slot "${name}" must return a VirtualEl (or null), got ${description}`,
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 result._serialize();
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 every mounted <plugin-slot name=...> re-invoke this plugin's
420
- // registered callbacks for that slot. Useful when a slot's content depends
421
- // on data that resolves asynchronously after the initial render.
422
- refreshSlot(name) {
423
- return hostCall("refreshSlot", { name });
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@impro.social/impro-plugin",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "type": "module",
5
5
  "main": "main.js",
6
6
  "license": "0BSD",