@byojs/scheduler
Advanced tools
Comparing version 0.9.0 to 0.9.1
{ | ||
"name": "@byojs/scheduler", | ||
"description": "Throttle/debounce scheduler", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"exports": { | ||
@@ -6,0 +6,0 @@ "./": "./dist/scheduler.mjs" |
@@ -106,2 +106,4 @@ # Scheduler | ||
```js | ||
import Scheduler from ".."; | ||
// leading unbounded debounce | ||
@@ -121,2 +123,4 @@ var debouncer1 = Scheduler(250); | ||
```js | ||
import Scheduler from ".."; | ||
// leading bounded (400ms) debounce | ||
@@ -134,2 +138,4 @@ var debouncer3 = Scheduler(250,400); | ||
```js | ||
import Scheduler from ".."; | ||
// leading throttle | ||
@@ -162,3 +168,3 @@ var throttler1 = Scheduler(250,250); | ||
You can share the same scheduler instance can for debouncing/throttling as many different functions as desired, assuming the same timing settings should apply for each of them. | ||
You *can* share the same scheduler instance can for debouncing/throttling as many different functions as desired, assuming the same timing settings should apply for each of them. | ||
@@ -169,12 +175,12 @@ **Warning:** The internal tracking of repeated and async scheduled calls is based on function reference identity. If you pass an inline function expression (such as an `=>` arrow), the function reference will be different each time, and will be treated as entirely separate functions -- thereby defeating the debouncing/throttling. Make sure to use the same stable function reference for all scheduling-related invocations of the scheduler instance function. | ||
The scheduler instance (e.g., `debouncer1` from above) returns yet another function, which is a *canceler*: | ||
The scheduler instance (e.g., `debouncer1` from above) returns yet another function, which is a *canceller*: | ||
```js | ||
var canceler = debouncer1(someTask); | ||
var canceller = debouncer1(someTask); | ||
// later (but within 250ms of previous call) | ||
canceler(); | ||
canceller(); | ||
``` | ||
If `canceler()` (as shown) is called before the debounced `someTask()` function is actually called, that debounced scheduling will be canceled. The same *canceler* will be returned for all subsequent `debouncer1()` calls **within the same interval**: | ||
If `canceller()` (as shown) is called before the debounced `someTask()` function is actually called, that debounced scheduling will be canceled. The same *canceller* will be returned for all subsequent `debouncer1()` calls **within the same interval**: | ||
@@ -185,3 +191,3 @@ ```js | ||
Since the *canceler* function is stable (within the same interval), it's safe to preserve a reference to that function, to use at any time during that interval. Once the interval transpires, the function becomes *dead* (no-op), and should be discarded. | ||
Since the *canceller* function is stable (within the same interval), it's safe to preserve a reference to that function, to use at any time during that interval. Once the interval transpires, the function becomes *dead* (no-op), and should be discarded. | ||
@@ -188,0 +194,0 @@ ## Re-building `dist/*` |
@@ -50,3 +50,3 @@ #!/usr/bin/env node | ||
// build src/* to bundlers/* | ||
// build src/* to dist/* | ||
await buildFiles( | ||
@@ -53,0 +53,0 @@ recursiveReadDir(SRC_DIR), |
@@ -79,33 +79,40 @@ // note: this module specifier comes from the import-map | ||
var results = []; | ||
var waiter = Scheduler(100,500,/*leading=*/false); | ||
testResultsEl.innerHTML = "Running... please wait."; | ||
waiter(logResult); | ||
await timeout(110); | ||
waiter(logResult); | ||
waiter(logResult); | ||
await timeout(50); | ||
waiter(logResult); | ||
waiter(logResult); | ||
waiter(logResult); | ||
await timeout(500); | ||
try { | ||
let waiter = Scheduler(100,500,/*leading=*/false); | ||
for (let i = 0; i < 30; i++) { | ||
waiter(logResult); | ||
await timeout(60); | ||
} | ||
await timeout(110); | ||
waiter(logResult); | ||
waiter(logResult); | ||
await timeout(50); | ||
waiter(logResult); | ||
waiter(logResult); | ||
waiter(logResult); | ||
await timeout(500); | ||
await timeout(500); | ||
for (let i = 0; i < 30; i++) { | ||
waiter(logResult); | ||
await timeout(60); | ||
} | ||
var EXPECTED = 6; | ||
var FOUND = results.length; | ||
return ( | ||
`(Trailing) ${ | ||
results.length == EXPECTED ? | ||
"PASSED." : | ||
`FAILED: expected ${EXPECTED}, found ${FOUND}` | ||
}` | ||
); | ||
await timeout(500); | ||
let EXPECTED = 6; | ||
let FOUND = results.length; | ||
return ( | ||
`(Trailing) ${ | ||
results.length == EXPECTED ? | ||
"PASSED." : | ||
`FAILED: expected ${EXPECTED}, found ${FOUND}` | ||
}` | ||
); | ||
} | ||
catch (err) { | ||
logError(err); | ||
console.log("results",results); | ||
testResultsEl.innerHTML = "FAILED (see console)"; | ||
} | ||
// *********************** | ||
@@ -112,0 +119,0 @@ |
Sorry, the diff of this file is not supported yet
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
24682
391
222