The pull that runs twice
A progress observer consumes a cold asynchronous source before the collector can read it.
What’s going wrong
Input is {mode, values}: mode is cold or promise and values is an array of at most 100 JSON values, each of nesting depth at most 5; total input is at most 16 KiB. The supplied source adapter is part of the exercise: each next() returns a native promise or a cold custom thenable. A native promise captures one next result when created; every subscription to a cold thenable consumes the next source result. Accessing a cold object’s then getter increments thenReads, subscribing increments subscriptions, and calling next increments pulls. The observer and collector must share ONE normalized result per pull, including the final done result. Preserve each value once and in order; a payload object containing done is ordinary data. Output {values,pulls,subscriptions,thenReads,observed}, where observed counts callbacks delivered to the progress observer, including the final done result. A completed traversal makes values.length+1 pulls and observed callbacks; cold mode has that many getter reads and subscriptions, promise mode has zero instrumented getter reads/subscriptions. No actual network, timers or custom input code executes.
Expected behavior
Normalize an asynchronous operation once before sharing it with observers and consumers.
Reproduction
{"mode":"cold","values":[3,8,13]}{"values":[3,8,13],"pulls":4,"subscriptions":4,"thenReads":4,"observed":4}{"mode":"promise","values":[3,8,13]}{"values":[3,8,13],"pulls":4,"subscriptions":0,"thenReads":0,"observed":4}{"mode":"promise","values":[]}{"values":[],"pulls":1,"subscriptions":0,"thenReads":0,"observed":1}Before you begin
- JavaScript functions, arrays and objects
- Carry representation invariants across two consumers: a thenable operation is not necessarily a reusable settled value.
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.