@shopify/app-bridge-core
Version:
**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**
33 lines (31 loc) • 906 B
JavaScript
/**
* Add an item to a collection, return a function that can then be used to
* remove the item from the collection. Optionally accepting a callback that is
* invoked when the item is removed from the collection.
*
* @internal
*/
function addAndRemoveFromCollection(collection, item, then) {
collection.push(item);
return () => {
return removeFromCollection(collection, item, then);
};
}
/**
* Remove the item from the collection. Optionally accepting a callback that is
* invoked when the item is removed from the collection.
*
* @internal
*/
function removeFromCollection(collection, item, then) {
const idx = collection.findIndex((i) => i === item);
if (idx >= 0) {
collection.splice(idx, 1);
if (then) {
then(item);
}
return true;
}
return false;
}
export { addAndRemoveFromCollection, removeFromCollection };