UNPKG

3.63 kBJavaScriptView Raw
1define(["require", "exports", "./dom/getWindow"], function (require, exports, getWindow_1) {
2 "use strict";
3 Object.defineProperty(exports, "__esModule", { value: true });
4 /**
5 * Storing global state in local module variables has issues when more than one copy
6 * if the module gets loaded on the page (due to a bundling error or simply by consuming
7 * a prebundled script.)
8 *
9 * This file contains helpers to deal with the getting and setting local state, and allows
10 * callers to get called back when it mutates.
11 */
12 var GLOBAL_SETTINGS_PROP_NAME = '__globalSettings__';
13 var CALLBACK_STATE_PROP_NAME = '__callbacks__';
14 var _counter = 0;
15 /**
16 * Global settings helper, which stores settings in the global (window) namespace.
17 * If window is not provided, it will store settings in module scope. Provides a
18 * way to observe changes as well when their values change.
19 *
20 * @public
21 * {@docCategory GlobalSettings}
22 */
23 var GlobalSettings = /** @class */ (function () {
24 function GlobalSettings() {
25 }
26 GlobalSettings.getValue = function (key, defaultValue) {
27 var globalSettings = _getGlobalSettings();
28 if (globalSettings[key] === undefined) {
29 globalSettings[key] = typeof defaultValue === 'function' ? defaultValue() : defaultValue;
30 }
31 return globalSettings[key];
32 };
33 GlobalSettings.setValue = function (key, value) {
34 var globalSettings = _getGlobalSettings();
35 var callbacks = globalSettings[CALLBACK_STATE_PROP_NAME];
36 var oldValue = globalSettings[key];
37 if (value !== oldValue) {
38 globalSettings[key] = value;
39 var changeDescription = {
40 oldValue: oldValue,
41 value: value,
42 key: key,
43 };
44 for (var id in callbacks) {
45 if (callbacks.hasOwnProperty(id)) {
46 callbacks[id](changeDescription);
47 }
48 }
49 }
50 return value;
51 };
52 GlobalSettings.addChangeListener = function (cb) {
53 // Note: we use generated ids on the callbacks to create a map of the callbacks, which optimizes removal.
54 // (It's faster to delete a key than it is to look up the index of an object and splice an array.)
55 var id = cb.__id__;
56 var callbacks = _getCallbacks();
57 if (!id) {
58 id = cb.__id__ = String(_counter++);
59 }
60 callbacks[id] = cb;
61 };
62 GlobalSettings.removeChangeListener = function (cb) {
63 var callbacks = _getCallbacks();
64 delete callbacks[cb.__id__];
65 };
66 return GlobalSettings;
67 }());
68 exports.GlobalSettings = GlobalSettings;
69 // eslint-disable-next-line @typescript-eslint/no-explicit-any
70 function _getGlobalSettings() {
71 var _a;
72 var win = getWindow_1.getWindow();
73 // eslint-disable-next-line @typescript-eslint/no-explicit-any
74 var globalObj = win || {};
75 if (!globalObj[GLOBAL_SETTINGS_PROP_NAME]) {
76 globalObj[GLOBAL_SETTINGS_PROP_NAME] = (_a = {},
77 _a[CALLBACK_STATE_PROP_NAME] = {},
78 _a);
79 }
80 return globalObj[GLOBAL_SETTINGS_PROP_NAME];
81 }
82 function _getCallbacks() {
83 var globalSettings = _getGlobalSettings();
84 return globalSettings[CALLBACK_STATE_PROP_NAME];
85 }
86});
87//# sourceMappingURL=GlobalSettings.js.map
\No newline at end of file