/// /** * Test Pattern: Cross-Charm Stream Server * * Exposes a Stream that increments a counter when invoked from another charm. * Used with test-cross-charm-client.tsx to verify cross-charm stream invocation. * * IMPORTANT: The #cross-charm-test-server tag is in the JSDoc on the Output * interface below (not here) - that's where wish() looks for tags. * * SETUP: After deploying, you must FAVORITE this charm for wish() to find it. */ import { Cell, Default, handler, NAME, pattern, Stream, UI } from "commontools"; interface Input { // Counter value that increments each time the stream is invoked counter: Default; // Log of invocation timestamps invocationLog: Default; } /** A #cross-charm-test-server that exposes a stream for testing cross-charm invocation. */ interface Output { counter: number; invocationLog: string[]; // Stream that can be invoked from another charm incrementCounter: Stream; } // Handler that increments the counter and logs the invocation const incrementHandler = handler< unknown, { counter: Cell; invocationLog: Cell } >((_event, { counter, invocationLog }) => { // Increment counter counter.set(counter.get() + 1); // Add timestamp to log const timestamp = new Date().toISOString(); const log = invocationLog.get(); invocationLog.set([...log, `Invoked at ${timestamp}`]); }); export default pattern(({ counter, invocationLog }) => { return { [NAME]: "Cross-Charm Test Server", [UI]: (

Cross-Charm Test Server

Tag: #cross-charm-test-server

Counter Value: {counter}

This counter increments when the incrementCounter stream is invoked from another charm.

Invocation Log:

{invocationLog.length === 0 ?

No invocations yet

: (
    {invocationLog.map((logEntry, i) => (
  • {logEntry}
  • ))}
)}

How it works: This charm exposes an{" "} incrementCounter{" "} stream. When another charm wishes for this charm and invokes that stream, the counter increments and a log entry is added.

), counter, invocationLog, incrementCounter: incrementHandler({ counter, invocationLog, }), }; });