From: Ardis Lu Date: Fri, 4 Jul 2025 03:45:55 +0000 (-0700) Subject: Simplify and harden tick indicator X-Git-Tag: v0.1.2~5 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=57396cd5682b88aebcadad7ab5e45f9db4a3df5b;p=csvio 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. --- 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))