UNPKG

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