The connection that starts twice
A shared source starts again when a startup callback subscribes another observer.
What’s going wrong
A shared event source must have exactly one live connection while at least one observer is subscribed, and none when the last observer leaves. Stdin is {"initial":scalar,"observers":[{"id":string,"spawn"?:[ids],"takeOne"?:boolean}],"actions":[["subscribe",id],["unsubscribe",id],["emit",scalar],...]}. Observer ids are unique and every referenced id is declared. A first subscribe starts a connection, which synchronously broadcasts initial to a snapshot of currently subscribed observers in insertion order. A later subscriber joins without replay. Every delivery logs [id,value]. On an id's first delivery during the whole simulation, subscribe its spawn ids in order; then, if takeOne is true, unsubscribe that id after the callback. Registering an already active id and removing an inactive id are no-ops. Emission snapshots exclude observers added during that emission, and skip observers already removed. An explicit emit broadcasts only when a live connection exists. Once the last observer leaves, dispose the connection exactly once; a later subscription starts a fresh connection. Print {"starts":count,"stops":count,"liveConnections":count,"subscribers":[activeIds],"events":[deliveries]}. Scalars are strings, finite numbers, booleans or null.
Read one JSON value from stdin and print the specified JSON result to stdout. No actual network or external package is used.
Expected behavior
Publish lifecycle ownership before invoking synchronous callbacks, and preserve reference-count cleanup when those callbacks reenter or unsubscribe.
Reproduction
{"initial":0,"observers":[{"id":"A","spawn":["B"]},{"id":"B"}],"actions":[["subscribe","A"],["emit",1],["unsubscribe","A"],["unsubscribe","B"]]}{"starts":1,"stops":1,"liveConnections":0,"subscribers":[],"events":[["A",0],["A",1],["B",1]]}{"initial":0,"observers":[{"id":"A"}],"actions":[["subscribe","A"],["emit","x"],["unsubscribe","A"]]}{"starts":1,"stops":1,"liveConnections":0,"subscribers":[],"events":[["A",0],["A","x"]]}{"initial":0,"observers":[{"id":"A"}],"actions":[]}{"starts":0,"stops":0,"liveConnections":0,"subscribers":[],"events":[]}Before you begin
- Combine stable state identity with the fact that a callback may run before its calling function returns.
- TypeScript unions, arrays, objects and functions
- Trace runtime behavior even when the program type-checks
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.