UNPKG

1.14 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.removeFromCollection = exports.addAndRemoveFromCollection = void 0;
4/**
5 * Add an item to a collection, return a function that can then be used to
6 * remove the item from the collection. Optionally accepting a callback that is
7 * invoked when the item is removed from the collection.
8 *
9 * @internal
10 */
11function addAndRemoveFromCollection(collection, item, then) {
12 collection.push(item);
13 return function () {
14 return removeFromCollection(collection, item, then);
15 };
16}
17exports.addAndRemoveFromCollection = addAndRemoveFromCollection;
18/**
19 * Remove the item from the collection. Optionally accepting a callback that is
20 * invoked when the item is removed from the collection.
21 *
22 * @internal
23 */
24function removeFromCollection(collection, item, then) {
25 var idx = collection.findIndex(function (i) { return i === item; });
26 if (idx >= 0) {
27 collection.splice(idx, 1);
28 if (then) {
29 then(item);
30 }
31 return true;
32 }
33 return false;
34}
35exports.removeFromCollection = removeFromCollection;