import assert from "node:assert"; import { describe, test } from "node:test"; import { debounce, dedupe, Delay, Duration, Make, synchronize, throttle } from "../src/index.js"; class Sensitive { public syncDebounceCounts = new Array(); public asyncDebounceCounts = new Array(); public syncDebounceWithDelayCounts = new Array(); public asyncDebounceWithDelayCounts = new Array(); public syncThrottleCounts = new Array(); public asyncThrottleCounts = new Array(); public syncThrottleWithDelayCounts = new Array(); public asyncThrottleWithDelayCounts = new Array(); public syncDedupeCounts = new Array(); public asyncDedupeCounts = new Array(); public syncDedupeWithDelayCounts = new Array(); public asyncDedupeWithDelayCounts = new Array(); public syncSynchronizeCounts = new Array(); public asyncSynchronizeCounts = new Array(); public syncSynchronizeWithDelayCounts = new Array(); public asyncSynchronizeWithDelayCounts = new Array(); @debounce public syncDebounce(index: number): void { this.syncDebounceCounts.push(index); } @debounce public async asyncDebounce(index: number): Promise { await Delay.milliseconds(500); this.asyncDebounceCounts.push(index); } @debounce(Duration.fromMilliSeconds(1000)) public syncDebounceWithDelay(index: number): void { this.syncDebounceWithDelayCounts.push(index); } @debounce(Duration.fromMilliSeconds(1000)) public async asyncDebounceWithDelay(index: number): Promise { await Delay.milliseconds(500); this.asyncDebounceWithDelayCounts.push(index); } @throttle public syncThrottle(index: number): void { this.syncThrottleCounts.push(index); } @throttle public async asyncThrottle(index: number): Promise { await Delay.milliseconds(500); this.asyncThrottleCounts.push(index); } @throttle(Duration.fromMilliSeconds(1000)) public syncThrottleWithDelay(index: number): void { this.syncThrottleWithDelayCounts.push(index); } @throttle(Duration.fromMilliSeconds(1000)) public async asyncThrottleWithDelay(index: number): Promise { await Delay.milliseconds(500); this.asyncThrottleWithDelayCounts.push(index); } @dedupe public syncDedupe(index: number): void { this.syncDedupeCounts.push(index); } @dedupe public async asyncDedupe(index: number): Promise { await Delay.milliseconds(500); this.asyncDedupeCounts.push(index); } @dedupe(Duration.fromMilliSeconds(1000)) public syncDedupeWithDelay(index: number): void { this.syncDedupeWithDelayCounts.push(index); } @dedupe(Duration.fromMilliSeconds(1000)) public async asyncDedupeWithDelay(index: number): Promise { await Delay.milliseconds(500); this.asyncDedupeWithDelayCounts.push(index); } @synchronize public syncSynchronize(index: number): void { this.syncSynchronizeCounts.push(index); } @synchronize public async asyncSynchronize(index: number): Promise { await Delay.milliseconds(500); this.asyncSynchronizeCounts.push(index); } @synchronize(Duration.fromMilliSeconds(1000)) public syncSynchronizeWithDelay(index: number): void { this.syncSynchronizeWithDelayCounts.push(index); } @synchronize(Duration.fromMilliSeconds(1000)) public async asyncSynchronizeWithDelay(index: number): Promise { await Delay.milliseconds(500); this.asyncSynchronizeWithDelayCounts.push(index); } } await describe("Decorator Tests", async () => { await test(`syncDebounce`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); Make.loop((index) => { index = index + 1; instance1.syncDebounce(index); instance2.syncDebounce(index); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; instance1.syncDebounce(index); instance2.syncDebounce(index); }, 5); await Delay.milliseconds(10); assert.strictEqual(instance1.syncDebounceCounts.length, 4); assert.deepStrictEqual(instance1.syncDebounceCounts, [1, 5, 6, 10]); assert.strictEqual(instance2.syncDebounceCounts.length, 4); assert.deepStrictEqual(instance2.syncDebounceCounts, [1, 5, 6, 10]); }); await test(`asyncDebounce`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); const promises = new Array>(); Make.loop((index) => { index = index + 1; promises.push(instance1.asyncDebounce(index)); promises.push(instance2.asyncDebounce(index)); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; promises.push(instance1.asyncDebounce(index)); promises.push(instance2.asyncDebounce(index)); }, 5); await Promise.all(promises); assert.strictEqual(instance1.asyncDebounceCounts.length, 2); assert.deepStrictEqual(instance1.asyncDebounceCounts, [1, 10]); assert.strictEqual(instance2.asyncDebounceCounts.length, 2); assert.deepStrictEqual(instance2.asyncDebounceCounts, [1, 10]); }); await test(`syncDebounceWithDelay`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); Make.loop((index) => { index = index + 1; instance1.syncDebounceWithDelay(index); instance2.syncDebounceWithDelay(index); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; instance1.syncDebounceWithDelay(index); instance2.syncDebounceWithDelay(index); }, 5); await Delay.milliseconds(5000); assert.strictEqual(instance1.syncDebounceWithDelayCounts.length, 1); assert.deepStrictEqual(instance1.syncDebounceWithDelayCounts, [10]); assert.strictEqual(instance2.syncDebounceWithDelayCounts.length, 1); assert.deepStrictEqual(instance2.syncDebounceWithDelayCounts, [10]); }); await test(`asyncDebounceWithDelay`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); const promises = new Array>(); Make.loop((index) => { index = index + 1; promises.push(instance1.asyncDebounceWithDelay(index)); promises.push(instance2.asyncDebounceWithDelay(index)); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; promises.push(instance1.asyncDebounceWithDelay(index)); promises.push(instance2.asyncDebounceWithDelay(index)); }, 5); await Promise.all(promises); assert.strictEqual(instance1.asyncDebounceWithDelayCounts.length, 1); assert.deepStrictEqual(instance1.asyncDebounceWithDelayCounts, [10]); assert.strictEqual(instance2.asyncDebounceWithDelayCounts.length, 1); assert.deepStrictEqual(instance2.asyncDebounceWithDelayCounts, [10]); }); await test(`syncThrottle`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); Make.loop((index) => { index = index + 1; instance1.syncThrottle(index); instance2.syncThrottle(index); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; instance1.syncThrottle(index); instance2.syncThrottle(index); }, 5); await Delay.milliseconds(10); assert.strictEqual(instance1.syncThrottleCounts.length, 4); assert.deepStrictEqual(instance1.syncThrottleCounts, [1, 5, 6, 10]); assert.strictEqual(instance2.syncThrottleCounts.length, 4); assert.deepStrictEqual(instance2.syncThrottleCounts, [1, 5, 6, 10]); }); await test(`asyncThrottle`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); const promises = new Array>(); Make.loop((index) => { index = index + 1; promises.push(instance1.asyncThrottle(index)); promises.push(instance2.asyncThrottle(index)); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; promises.push(instance1.asyncThrottle(index)); promises.push(instance2.asyncThrottle(index)); }, 5); await Promise.all(promises); assert.strictEqual(instance1.asyncThrottleCounts.length, 2); assert.deepStrictEqual(instance1.asyncThrottleCounts, [1, 10]); assert.strictEqual(instance2.asyncThrottleCounts.length, 2); assert.deepStrictEqual(instance2.asyncThrottleCounts, [1, 10]); }); await test(`syncThrottleWithDelay`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); Make.loop((index) => { index = index + 1; instance1.syncThrottleWithDelay(index); instance2.syncThrottleWithDelay(index); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; instance1.syncThrottleWithDelay(index); instance2.syncThrottleWithDelay(index); }, 5); await Delay.milliseconds(5000); assert.strictEqual(instance1.syncThrottleWithDelayCounts.length, 2); assert.deepStrictEqual(instance1.syncThrottleWithDelayCounts, [1, 10]); assert.strictEqual(instance2.syncThrottleWithDelayCounts.length, 2); assert.deepStrictEqual(instance2.syncThrottleWithDelayCounts, [1, 10]); }); await test(`asyncThrottleWithDelay`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); const promises = new Array>(); Make.loop((index) => { index = index + 1; promises.push(instance1.asyncThrottleWithDelay(index)); promises.push(instance2.asyncThrottleWithDelay(index)); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; promises.push(instance1.asyncThrottleWithDelay(index)); promises.push(instance2.asyncThrottleWithDelay(index)); }, 5); await Promise.all(promises); assert.strictEqual(instance1.asyncThrottleWithDelayCounts.length, 2); assert.deepStrictEqual(instance1.asyncThrottleWithDelayCounts, [1, 10]); assert.strictEqual(instance2.asyncThrottleWithDelayCounts.length, 2); assert.deepStrictEqual(instance2.asyncThrottleWithDelayCounts, [1, 10]); }); await test(`syncDedupe`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); Make.loop((index) => { index = index + 1; instance1.syncDedupe(index); instance2.syncDedupe(index); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; instance1.syncDedupe(index); instance2.syncDedupe(index); }, 5); await Delay.milliseconds(10); assert.strictEqual(instance1.syncDedupeCounts.length, 2); assert.deepStrictEqual(instance1.syncDedupeCounts, [1, 6]); assert.strictEqual(instance2.syncDedupeCounts.length, 2); assert.deepStrictEqual(instance2.syncDedupeCounts, [1, 6]); }); await test(`asyncDedupe`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); const promises = new Array>(); Make.loop((index) => { index = index + 1; promises.push(instance1.asyncDedupe(index)); promises.push(instance2.asyncDedupe(index)); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; promises.push(instance1.asyncDedupe(index)); promises.push(instance2.asyncDedupe(index)); }, 5); await Promise.all(promises); assert.strictEqual(instance1.asyncDedupeCounts.length, 1); assert.deepStrictEqual(instance1.asyncDedupeCounts, [1]); assert.strictEqual(instance2.asyncDedupeCounts.length, 1); assert.deepStrictEqual(instance2.asyncDedupeCounts, [1]); }); await test(`syncDedupeWithDelay`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); Make.loop((index) => { index = index + 1; instance1.syncDedupeWithDelay(index); instance2.syncDedupeWithDelay(index); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; instance1.syncDedupeWithDelay(index); instance2.syncDedupeWithDelay(index); }, 5); await Delay.milliseconds(5000); assert.strictEqual(instance1.syncDedupeWithDelayCounts.length, 1); assert.deepStrictEqual(instance1.syncDedupeWithDelayCounts, [1]); assert.strictEqual(instance2.syncDedupeWithDelayCounts.length, 1); assert.deepStrictEqual(instance2.syncDedupeWithDelayCounts, [1]); }); await test(`asyncDedupeWithDelay`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); const promises = new Array>(); Make.loop((index) => { index = index + 1; promises.push(instance1.asyncDedupeWithDelay(index)); promises.push(instance2.asyncDedupeWithDelay(index)); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; promises.push(instance1.asyncDedupeWithDelay(index)); promises.push(instance2.asyncDedupeWithDelay(index)); }, 5); await Promise.all(promises); assert.strictEqual(instance1.asyncDedupeWithDelayCounts.length, 1); assert.deepStrictEqual(instance1.asyncDedupeWithDelayCounts, [1]); assert.strictEqual(instance2.asyncDedupeWithDelayCounts.length, 1); assert.deepStrictEqual(instance2.asyncDedupeWithDelayCounts, [1]); }); await test(`syncSynchronize`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); Make.loop((index) => { index = index + 1; instance1.syncSynchronize(index); instance2.syncSynchronize(index); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; instance1.syncSynchronize(index); instance2.syncSynchronize(index); }, 5); await Delay.milliseconds(10); assert.strictEqual(instance1.syncSynchronizeCounts.length, 10); assert.deepStrictEqual(instance1.syncSynchronizeCounts, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); assert.strictEqual(instance2.syncSynchronizeCounts.length, 10); assert.deepStrictEqual(instance2.syncSynchronizeCounts, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); await test(`asyncSynchronize`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); const promises = new Array>(); Make.loop((index) => { index = index + 1; promises.push(instance1.asyncSynchronize(index)); promises.push(instance2.asyncSynchronize(index)); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; promises.push(instance1.asyncSynchronize(index)); promises.push(instance2.asyncSynchronize(index)); }, 5); await Promise.all(promises); assert.strictEqual(instance1.asyncSynchronizeCounts.length, 10); assert.deepStrictEqual(instance1.asyncSynchronizeCounts, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); assert.strictEqual(instance2.asyncSynchronizeCounts.length, 10); assert.deepStrictEqual(instance2.asyncSynchronizeCounts, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); await test(`syncSynchronizeWithDelay`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); Make.loop((index) => { index = index + 1; instance1.syncSynchronizeWithDelay(index); instance2.syncSynchronizeWithDelay(index); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; instance1.syncSynchronizeWithDelay(index); instance2.syncSynchronizeWithDelay(index); }, 5); await Delay.milliseconds(10000); assert.strictEqual(instance1.syncSynchronizeWithDelayCounts.length, 10); assert.deepStrictEqual(instance1.syncSynchronizeWithDelayCounts, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); assert.strictEqual(instance2.syncSynchronizeWithDelayCounts.length, 10); assert.deepStrictEqual(instance2.syncSynchronizeWithDelayCounts, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); await test(`asyncSynchronizeWithDelay`, async () => { const instance1 = new Sensitive(); const instance2 = new Sensitive(); const promises = new Array>(); Make.loop((index) => { index = index + 1; promises.push(instance1.asyncSynchronizeWithDelay(index)); promises.push(instance2.asyncSynchronizeWithDelay(index)); }, 5); await Delay.milliseconds(10); Make.loop((index) => { index = index + 6; promises.push(instance1.asyncSynchronizeWithDelay(index)); promises.push(instance2.asyncSynchronizeWithDelay(index)); }, 5); await Promise.all(promises); assert.strictEqual(instance1.asyncSynchronizeWithDelayCounts.length, 10); assert.deepStrictEqual(instance1.asyncSynchronizeWithDelayCounts, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); assert.strictEqual(instance2.asyncSynchronizeWithDelayCounts.length, 10); assert.deepStrictEqual(instance2.asyncSynchronizeWithDelayCounts, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); });