UNPKG

@enact/ui

Version:

A collection of simplified unstyled cross-platform UI components for Enact

53 lines (51 loc) 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.Provider = void 0; function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var GlobalId = 0; var ID_KEY = '$$ID$$'; var Provider = exports.Provider = /*#__PURE__*/function () { function Provider(prefix) { var _this = this; _classCallCheck(this, Provider); this.generate = function () { var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ID_KEY; var idPrefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this.prefix; var onUnmount = arguments.length > 2 ? arguments[2] : undefined; // if an id has been generated for the key, return it if (key in _this.ids) { return _this.ids[key].id; } // otherwise generate a new id (with an optional prefix), cache it, and return it var id = "".concat(idPrefix).concat(++GlobalId); _this.ids[typeof key === 'undefined' ? "generated-".concat(id) : key] = { id: id, onUnmount: onUnmount }; return id; }; this.prefix = prefix; this.ids = {}; } return _createClass(Provider, [{ key: "unload", value: function unload() { // Call the onUnmount handler for each generated id (note: not the key) for (var key in this.ids) { var _this$ids$key = this.ids[key], id = _this$ids$key.id, onUnmount = _this$ids$key.onUnmount; if (typeof onUnmount === 'function') { onUnmount(id); } } } }]); }(); var _default = exports["default"] = Provider;