@wordpress/priority-queue
Advanced tools
Comparing version
@@ -40,5 +40,6 @@ /** | ||
* | ||
* @property {WPPriorityQueueAdd} add Add callback to queue for context. | ||
* @property {WPPriorityQueueFlush} flush Flush queue for context. | ||
* @property {WPPriorityQueueReset} reset Reset queue. | ||
* @property {WPPriorityQueueAdd} add Add callback to queue for context. | ||
* @property {WPPriorityQueueFlush} flush Flush queue for context. | ||
* @property {WPPriorityQueueFlush} cancel Clear queue for context. | ||
* @property {WPPriorityQueueReset} reset Reset queue. | ||
*/ | ||
@@ -156,2 +157,25 @@ | ||
/** | ||
* Clears the queue for a given context, cancelling the callbacks without | ||
* executing them. Returns `true` if there were scheduled callbacks to cancel, | ||
* or `false` if there was is no queue for the given context. | ||
* | ||
* @type {WPPriorityQueueFlush} | ||
* | ||
* @param {WPPriorityQueueContext} element Context object. | ||
* | ||
* @return {boolean} Whether any callbacks got cancelled. | ||
*/ | ||
const cancel = element => { | ||
if (!elementsMap.has(element)) { | ||
return false; | ||
} | ||
const index = waitingList.indexOf(element); | ||
waitingList.splice(index, 1); | ||
elementsMap.delete(element); | ||
return true; | ||
}; | ||
/** | ||
* Reset the queue without running the pending callbacks. | ||
@@ -172,2 +196,3 @@ * | ||
flush, | ||
cancel, | ||
reset | ||
@@ -174,0 +199,0 @@ }; |
@@ -35,2 +35,6 @@ export function createQueue(): WPPriorityQueue; | ||
/** | ||
* Clear queue for context. | ||
*/ | ||
cancel: WPPriorityQueueFlush; | ||
/** | ||
* Reset queue. | ||
@@ -37,0 +41,0 @@ */ |
@@ -51,5 +51,6 @@ "use strict"; | ||
* | ||
* @property {WPPriorityQueueAdd} add Add callback to queue for context. | ||
* @property {WPPriorityQueueFlush} flush Flush queue for context. | ||
* @property {WPPriorityQueueReset} reset Reset queue. | ||
* @property {WPPriorityQueueAdd} add Add callback to queue for context. | ||
* @property {WPPriorityQueueFlush} flush Flush queue for context. | ||
* @property {WPPriorityQueueFlush} cancel Clear queue for context. | ||
* @property {WPPriorityQueueReset} reset Reset queue. | ||
*/ | ||
@@ -166,2 +167,25 @@ | ||
/** | ||
* Clears the queue for a given context, cancelling the callbacks without | ||
* executing them. Returns `true` if there were scheduled callbacks to cancel, | ||
* or `false` if there was is no queue for the given context. | ||
* | ||
* @type {WPPriorityQueueFlush} | ||
* | ||
* @param {WPPriorityQueueContext} element Context object. | ||
* | ||
* @return {boolean} Whether any callbacks got cancelled. | ||
*/ | ||
const cancel = element => { | ||
if (!elementsMap.has(element)) { | ||
return false; | ||
} | ||
const index = waitingList.indexOf(element); | ||
waitingList.splice(index, 1); | ||
elementsMap.delete(element); | ||
return true; | ||
}; | ||
/** | ||
* Reset the queue without running the pending callbacks. | ||
@@ -182,2 +206,3 @@ * | ||
flush, | ||
cancel, | ||
reset | ||
@@ -184,0 +209,0 @@ }; |
@@ -5,2 +5,8 @@ <!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. --> | ||
## 2.7.0 (2022-04-21) | ||
### New features | ||
- Add a new `cancel` method that removes scheduled callbacks without executing them. | ||
## 2.6.0 (2022-04-08) | ||
@@ -7,0 +13,0 @@ |
{ | ||
"name": "@wordpress/priority-queue", | ||
"version": "2.6.1", | ||
"version": "2.7.0", | ||
"description": "Generic browser priority queue.", | ||
@@ -36,3 +36,3 @@ "author": "The WordPress Contributors", | ||
}, | ||
"gitHead": "446565ecaa40370173c18926535e975ec5652b71" | ||
"gitHead": "1ba52312b56db563df2d8d4fba5b00613fb46d8c" | ||
} |
@@ -41,5 +41,6 @@ /** | ||
* | ||
* @property {WPPriorityQueueAdd} add Add callback to queue for context. | ||
* @property {WPPriorityQueueFlush} flush Flush queue for context. | ||
* @property {WPPriorityQueueReset} reset Reset queue. | ||
* @property {WPPriorityQueueAdd} add Add callback to queue for context. | ||
* @property {WPPriorityQueueFlush} flush Flush queue for context. | ||
* @property {WPPriorityQueueFlush} cancel Clear queue for context. | ||
* @property {WPPriorityQueueReset} reset Reset queue. | ||
*/ | ||
@@ -156,2 +157,25 @@ | ||
/** | ||
* Clears the queue for a given context, cancelling the callbacks without | ||
* executing them. Returns `true` if there were scheduled callbacks to cancel, | ||
* or `false` if there was is no queue for the given context. | ||
* | ||
* @type {WPPriorityQueueFlush} | ||
* | ||
* @param {WPPriorityQueueContext} element Context object. | ||
* | ||
* @return {boolean} Whether any callbacks got cancelled. | ||
*/ | ||
const cancel = ( element ) => { | ||
if ( ! elementsMap.has( element ) ) { | ||
return false; | ||
} | ||
const index = waitingList.indexOf( element ); | ||
waitingList.splice( index, 1 ); | ||
elementsMap.delete( element ); | ||
return true; | ||
}; | ||
/** | ||
* Reset the queue without running the pending callbacks. | ||
@@ -170,4 +194,5 @@ * | ||
flush, | ||
cancel, | ||
reset, | ||
}; | ||
}; |
@@ -131,2 +131,29 @@ /** | ||
} ); | ||
describe( 'cancel', () => { | ||
it( 'removes all callbacks associated with element without executing', () => { | ||
const elementA = {}; | ||
const elementB = {}; | ||
const callbackElementA = jest.fn(); | ||
const callbackElementB = jest.fn(); | ||
queue.add( elementA, callbackElementA ); | ||
queue.add( elementB, callbackElementB ); | ||
expect( callbackElementA ).not.toHaveBeenCalled(); | ||
expect( callbackElementB ).not.toHaveBeenCalled(); | ||
expect( queue.cancel( elementA ) ).toBe( true ); | ||
// No callbacks should have been called. | ||
expect( callbackElementA ).not.toHaveBeenCalled(); | ||
expect( callbackElementB ).not.toHaveBeenCalled(); | ||
// A subsequent `flush` has nothing to remove. | ||
expect( queue.flush( elementA ) ).toBe( false ); | ||
// The queue for `elementA` remained intact and can be successfully flushed. | ||
expect( queue.flush( elementB ) ).toBe( true ); | ||
expect( callbackElementB ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
} ); | ||
} ); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
91883
6.48%764
13.52%