From 57396cd5682b88aebcadad7ab5e45f9db4a3df5b Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Thu, 3 Jul 2025 20:45:55 -0700 Subject: [PATCH] Simplify and harden tick indicator Relying on physical timing is flaky (e.g., it will fail if executing on a slow machine). Sending a simple `tick` increment into the task queue is more reliable. --- examples/data/ex3_2-out.csv | 16 ++++++++-------- examples/ex3_2.js | 13 +++---------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/examples/data/ex3_2-out.csv b/examples/data/ex3_2-out.csv index de20589..334d7d3 100644 --- a/examples/data/ex3_2-out.csv +++ b/examples/data/ex3_2-out.csv @@ -1,9 +1,9 @@ tick number,concurrent group -1,a -1,a -1,a -1,a -1,a -2,b -2,b -2,b +0,a +0,a +0,a +0,a +0,a +5,b +5,b +5,b diff --git a/examples/ex3_2.js b/examples/ex3_2.js index bf341b6..7f83dc6 100644 --- a/examples/ex3_2.js +++ b/examples/ex3_2.js @@ -7,19 +7,12 @@ // takes very long, all row processing is blocked on the 1 transformation even if all others in the group have already // resolved. -import { setTimeout } from 'node:timers/promises'; import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js'; -// Returns how many 100ms increments have elapsed since this program began -const start = performance.now(); -function tick() { - const ms = performance.now() - start; - return Math.floor(ms / 100); -} - +let tick = 0; async function concurrent(row) { - await setTimeout(100); // Simulate some slow task, e.g. a network request - return [tick(), ...row]; + setTimeout(() => tick++, 0); // Tick will only be incremented after the concurrent group is done + return [tick, ...row]; } await new CSVReader(new URL('./data/ex3_2-in.csv', import.meta.url)) -- 2.47.3