@stackbit/utils
Advanced tools
Comparing version 0.2.13-alpha.0 to 0.2.13
@@ -111,4 +111,4 @@ import { jest, describe, expect, test } from '@jest/globals'; | ||
const result = deferred('a', 'b', 'c'); | ||
const start = Date.now(); | ||
const result = deferred('a', 'b', 'c'); | ||
await expect(result).resolves.toEqual('abc'); | ||
@@ -128,4 +128,4 @@ expect(callTimes[0]! - start).toBeWithinRange(80, 120); | ||
const result = deferred('a', 'b', 'c'); | ||
const start = Date.now(); | ||
const result = deferred('a', 'b', 'c'); | ||
await expect(result).rejects.toEqual({ error: 'abc' }); | ||
@@ -145,4 +145,4 @@ expect(callTimes[0]! - start).toBeWithinRange(80, 120); | ||
const result = deferred('a', 'b', 'c'); | ||
const start = Date.now(); | ||
const result = deferred('a', 'b', 'c'); | ||
await expect(result).rejects.toThrow('abc'); | ||
@@ -162,15 +162,22 @@ expect(callTimes[0]! - start).toBeWithinRange(80, 120); | ||
const start = Date.now(); | ||
// first call is debounced for 150ms | ||
const result1 = deferred('a'); | ||
await sleep(100); | ||
// second call is debounced for 150ms after 100ms | ||
const result2 = deferred('b'); | ||
await sleep(100); | ||
// third call is debounced for 150ms after 200ms and called at 350ms | ||
const result3 = deferred('c'); | ||
const start = Date.now(); | ||
// second call is made after 100ms, debounced for 150ms, and scheduled for 250ms (from start) | ||
const deferredResult2 = deferredPromise(); | ||
setTimeout(() => { | ||
deferredResult2.resolve(deferred('b')); | ||
}, 100); | ||
// third call is made after 200ms (from start), debounced for 150ms, and scheduled for 350ms (from start) | ||
const deferredResult3 = deferredPromise(); | ||
setTimeout(() => { | ||
deferredResult3.resolve(deferred('c')); | ||
}, 200); | ||
await expect(result1).resolves.toEqual('c'); | ||
await expect(result2).resolves.toEqual('c'); | ||
await expect(result3).resolves.toEqual('c'); | ||
await expect(deferredResult2.promise).resolves.toEqual('c'); | ||
await expect(deferredResult3.promise).resolves.toEqual('c'); | ||
expect(callTimes[0]! - start).toBeWithinRange(330, 370); | ||
@@ -193,17 +200,24 @@ expect(mockFn.mock.calls.length).toEqual(1); | ||
const start = Date.now(); | ||
// first call is debounced for 300ms | ||
const result1 = deferred('a'); | ||
await sleep(250); | ||
// second call is debounced for 150ms because max debounce delay is 400ms | ||
const result2 = deferred('b'); | ||
// the second call will be invoked after 150ms and run for 100ms | ||
await sleep(200); | ||
// after 200ms the second call will be still running, and the third call will be deferred | ||
const result3 = deferred('c'); | ||
const start = Date.now(); | ||
// second call is made after 250ms, debounced for 150ms because max debounce delay is 400ms, | ||
// and scheduled for 400ms (from start) and run for 100ms | ||
const deferredResult2 = deferredPromise(); | ||
setTimeout(() => { | ||
deferredResult2.resolve(deferred('b')); | ||
}, 250); | ||
// third call is made after 450ms (from start) while the second call is running, | ||
// so this call will be deferred until after the second call invocation completes | ||
// which will be around 500ms from the start | ||
const deferredResult3 = deferredPromise(); | ||
setTimeout(() => { | ||
deferredResult3.resolve(deferred('c')); | ||
}, 450); | ||
await expect(result1).resolves.toEqual('b'); | ||
await expect(result2).resolves.toEqual('b'); | ||
await expect(result3).resolves.toEqual('c'); | ||
await expect(deferredResult2.promise).resolves.toEqual('b'); | ||
await expect(deferredResult3.promise).resolves.toEqual('c'); | ||
expect(mockFn.mock.calls.length).toEqual(2); | ||
@@ -225,19 +239,25 @@ expect(callTimes[0]! - start).toBeWithinRange(380, 420); | ||
argsResolver: ({ nextArgs, prevArgs }) => { | ||
return prevArgs ? [prevArgs[0] + nextArgs[0], prevArgs[1] + nextArgs[1]] as [string, number] : nextArgs; | ||
return prevArgs ? ([prevArgs[0] + nextArgs[0], prevArgs[1] + nextArgs[1]] as [string, number]) : nextArgs; | ||
} | ||
}); | ||
const start = Date.now(); | ||
// first call is debounced for 100ms | ||
const result1 = deferred('a', 1); | ||
await sleep(50); | ||
// second call is debounced for 100ms after 50ms | ||
const result2 = deferred('b', 2); | ||
await sleep(50); | ||
// third call is debounced for 100ms after 100ms and called after 200ms | ||
const result3 = deferred('c', 3); | ||
const start = Date.now(); | ||
// second call is made after 50ms, debounced for 100ms, and scheduled for 150ms (from start) | ||
const deferredResult2 = deferredPromise(); | ||
setTimeout(() => { | ||
deferredResult2.resolve(deferred('b', 2)); | ||
}, 50); | ||
// third call is made after 100ms (from start), debounced for 100ms, and scheduled for 200ms (from start) | ||
const deferredResult3 = deferredPromise(); | ||
setTimeout(() => { | ||
deferredResult3.resolve(deferred('c', 3)); | ||
}, 100); | ||
await expect(result1).resolves.toEqual(['abc', 6]); | ||
await expect(result2).resolves.toEqual(['abc', 6]); | ||
await expect(result3).resolves.toEqual(['abc', 6]); | ||
await expect(deferredResult2.promise).resolves.toEqual(['abc', 6]); | ||
await expect(deferredResult3.promise).resolves.toEqual(['abc', 6]); | ||
expect(callTimes[0]! - start).toBeWithinRange(180, 220); | ||
@@ -244,0 +264,0 @@ expect(mockFn.mock.calls.length).toEqual(1); |
{ | ||
"name": "@stackbit/utils", | ||
"version": "0.2.13-alpha.0", | ||
"version": "0.2.13", | ||
"description": "Stackbit utilities", | ||
@@ -46,3 +46,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "c655a5d1f3fa7d613fb20c173369cef8e141e633" | ||
"gitHead": "a078e89fdf11b6a8f467a00a831ab1148984c722" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
161355
2954