UNPKG

@sanity/form-builder

Version:
46 lines (45 loc) 1.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = create; function create() { for (var _len = arguments.length, keys = new Array(_len), _key = 0; _key < _len; _key++) { keys[_key] = arguments[_key]; } var registry = {}; function validate(key) { if (!keys.includes(key)) { throw new Error("Invalid subscription key: \"".concat(key, "\". Must be one of: ").concat(keys.join(', '))); } } function add(key, subscription) { validate(key); if (registry[key]) { throw new Error("Subscription already exists for key: ".concat(key, ". Did you mean to call .replace instead?")); } registry[key] = subscription; } function unsubscribe(key) { validate(key); if (registry[key]) { registry[key].unsubscribe(); registry[key] = null; } } function replace(key, subscription) { if (registry[key]) { unsubscribe(key); } add(key, subscription); } function unsubscribeAll() { keys.forEach(key => unsubscribe(key)); } return { add: add, replace: replace, unsubscribe: unsubscribe, unsubscribeAll: unsubscribeAll }; }