@jsenv/abort
Advanced tools
Comparing version 3.1.0 to 3.1.1
{ | ||
"name": "@jsenv/abort", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "Help to write code compatible with abort signals", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
export const createCallbackListCalledOnce = () => { | ||
let callbacks = [] | ||
let status = "pending" | ||
let currentCallbackIndex = -1 | ||
@@ -31,6 +32,22 @@ const add = (callback) => { | ||
} | ||
const index = callbacks.indexOf(callback) | ||
if (index > -1) { | ||
callbacks.splice(index, 1) | ||
if (index === -1) { | ||
return | ||
} | ||
if (status === "calling") { | ||
if (index <= currentCallbackIndex) { | ||
// The callback was already called (or is the current callback) | ||
// We don't want to mutate the callbacks array | ||
// or it would alter the looping done in "call" and the next callback | ||
// would be skipped | ||
return | ||
} | ||
// Callback is part of the next callback to call, | ||
// we mutate the callbacks array to prevent this callback to be called | ||
} | ||
callbacks.splice(index, 1) | ||
} | ||
@@ -45,3 +62,4 @@ } | ||
status = "calling" | ||
const values = callbacks.map((callback) => { | ||
const values = callbacks.map((callback, index) => { | ||
currentCallbackIndex = index | ||
return callback(param) | ||
@@ -53,2 +71,3 @@ }) | ||
callbacks = null | ||
currentCallbackIndex = -1 | ||
@@ -55,0 +74,0 @@ return values |
@@ -6,3 +6,3 @@ /* | ||
export const raceCallbacks = (raceDescription, winnerCallback) => { | ||
const cleanCallbacks = [] | ||
let cleanCallbacks = [] | ||
let done = false | ||
@@ -12,7 +12,6 @@ | ||
done = true | ||
const cleanCallbacksCopy = cleanCallbacks.slice() | ||
cleanCallbacks.length = 0 | ||
cleanCallbacksCopy.forEach((clean) => { | ||
cleanCallbacks.forEach((clean) => { | ||
clean() | ||
}) | ||
cleanCallbacks = null | ||
} | ||
@@ -19,0 +18,0 @@ |
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
18749
397