michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["GlobalState"]; michael@0: michael@0: const EXPORTED_METHODS = ["getState", "clear", "get", "set", "delete", "setFromState"]; michael@0: /** michael@0: * Module that contains global session data. michael@0: */ michael@0: function GlobalState() { michael@0: let internal = new GlobalStateInternal(); michael@0: let external = {}; michael@0: for (let method of EXPORTED_METHODS) { michael@0: external[method] = internal[method].bind(internal); michael@0: } michael@0: return Object.freeze(external); michael@0: } michael@0: michael@0: function GlobalStateInternal() { michael@0: // Storage for global state. michael@0: this.state = {}; michael@0: } michael@0: michael@0: GlobalStateInternal.prototype = { michael@0: /** michael@0: * Get all value from the global state. michael@0: */ michael@0: getState: function() { michael@0: return this.state; michael@0: }, michael@0: michael@0: /** michael@0: * Clear all currently stored global state. michael@0: */ michael@0: clear: function() { michael@0: this.state = {}; michael@0: }, michael@0: michael@0: /** michael@0: * Retrieve a value from the global state. michael@0: * michael@0: * @param aKey michael@0: * A key the value is stored under. michael@0: * @return The value stored at aKey, or an empty string if no value is set. michael@0: */ michael@0: get: function(aKey) { michael@0: return this.state[aKey] || ""; michael@0: }, michael@0: michael@0: /** michael@0: * Set a global value. michael@0: * michael@0: * @param aKey michael@0: * A key to store the value under. michael@0: */ michael@0: set: function(aKey, aStringValue) { michael@0: this.state[aKey] = aStringValue; michael@0: }, michael@0: michael@0: /** michael@0: * Delete a global value. michael@0: * michael@0: * @param aKey michael@0: * A key to delete the value for. michael@0: */ michael@0: delete: function(aKey) { michael@0: delete this.state[aKey]; michael@0: }, michael@0: michael@0: /** michael@0: * Set the current global state from a state object. Any previous global michael@0: * state will be removed, even if the new state does not contain a matching michael@0: * key. michael@0: * michael@0: * @param aState michael@0: * A state object to extract global state from to be set. michael@0: */ michael@0: setFromState: function (aState) { michael@0: this.state = (aState && aState.global) || {}; michael@0: } michael@0: };