2 * Copyright 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var when = require("when");
19 var assert = require("assert");
21 var userSettings = null;
22 var globalSettings = null;
25 var persistentSettings = {
26 init: function(settings) {
27 userSettings = settings;
29 for (var i in settings) {
30 if (settings.hasOwnProperty(i)) {
33 persistentSettings.__defineGetter__(j,function() { return userSettings[j]; });
34 persistentSettings.__defineSetter__(j,function() { throw new Error("Property '"+i+"' is read-only"); });
38 globalSettings = null;
40 load: function(_storage) {
42 return storage.getSettings().then(function(_settings) {
43 globalSettings = _settings;
47 if (userSettings.hasOwnProperty(prop)) {
48 return userSettings[prop];
50 if (globalSettings === null) {
51 throw new Error("Settings not available");
53 return globalSettings[prop];
56 set: function(prop,value) {
57 if (userSettings.hasOwnProperty(prop)) {
58 throw new Error("Property '"+prop+"' is read-only");
60 if (globalSettings === null) {
61 throw new Error("Settings not available");
63 var current = globalSettings[prop];
64 globalSettings[prop] = value;
66 assert.deepEqual(current,value);
67 return when.resolve();
69 return storage.saveSettings(globalSettings);
73 available: function() {
74 return (globalSettings !== null);
79 globalSettings = null;
84 module.exports = persistentSettings;