/** * Test Pattern: Cross-Piece Stream Server * * Exposes a Stream that increments a counter when invoked from another piece. * Used with test-cross-piece-client.tsx to verify cross-piece stream invocation. * * IMPORTANT: The #crossPieceTestServer 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 piece for wish() to find it. */ import { Default, handler, NAME, pattern, Stream, UI, Writable, } from "commonfabric"; interface Input { // Counter value that increments each time the stream is invoked counter: number | Default<0>; // Log of invocation timestamps invocationLog: string[] | Default<[]>; } /** A #crossPieceTestServer that exposes a stream for testing cross-piece invocation. */ export interface Output { counter: number; invocationLog: string[]; // Stream that can be invoked from another piece incrementCounter: Stream; } // Handler that increments the counter and logs the invocation const incrementHandler = handler< void, { counter: Writable; invocationLog: Writable } >((_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-Piece Test Server", [UI]: (

Cross-Piece Test Server

Tag: #crossPieceTestServer

Counter Value: {counter}

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

Invocation Log:

{invocationLog.length === 0 ?

No invocations yet

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

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

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