This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
setting.js
139 lines (121 loc) · 3.12 KB
/
setting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
define(function(require, exports, module) {
'use strict';
var Abstract = require('./abstract');
var denodeifyAll = require('promise').denodeifyAll;
var nextTick = require('next_tick');
function Setting() {
Abstract.apply(this, arguments);
denodeifyAll(this, [
'getValue',
'set'
]);
}
module.exports = Setting;
Setting.prototype = {
__proto__: Abstract.prototype,
_store: 'settings',
/**
* Default option values.
*/
defaults: {
standardAlarmDefault: -300,
alldayAlarmDefault: 32400,
syncFrequency: 15,
syncAlarm: {
alarmId: null,
start: null,
end: null
}
},
/** disable caching */
_addToCache: function() {},
_removeFromCache: function() {},
/**
* This method also will use the internal cache to ensure
* callers are in a consistent state and don't require round
* trips to the database. When the value does not exist defaults
* are used where possible...
*
*
* settings.getValue('syncFrequency', function(err, value) {
* // ...
* });
*
*
* @param {String} key name of setting.
* @param {Function} callback usual [err, value] does not include metadata.
*/
getValue: function(key, callback) {
var self = this;
if (key in this._cached) {
nextTick(function handleCached() {
callback(null, self._cached[key].value);
});
// we have cached value exit...
return;
}
this.get(key, function handleStored(err, value) {
if (err) {
return callback(err);
}
if (value === undefined && self.defaults[key] !== undefined) {
value = { value: self.defaults[key] };
}
self._cached[key] = value;
callback(null, value.value);
});
},
/**
* Persist a setting change.
*
* In addition to updating the value of the setting
* it will also update the updatedAt & createdAt properties
* of the record.
*
* Calling this function will also emit a 'change' event
* prior to fully persisting the record to the database.
*
* Example:
*
* var settingStore;
*
* settingStore.set('syncFrequency', 15, function() {
* // done
* });
*
* // somewhere else in the app:
*
* settingStore.on('syncFrequencyChange', function(value) {
* // value === 15
* });
*
* @param {String} key name of setting.
* @param {Object} value any object that IndexedDb can store.
* @param {IDBTransaction} [trans] idb transaction optional.
* @param {Function} [callback] optional callback.
*/
set: function(key, value, trans, callback) {
if (typeof(trans) === 'function') {
callback = trans;
trans = null;
}
var cached = this._cached[key];
var record;
if (cached && cached._id) {
cached.value = value;
cached.updatedAt = new Date();
record = cached;
} else {
var created = new Date();
this._cached[key] = record = {
_id: key,
createdAt: created,
updatedAt: created,
value: value
};
}
this.emit(key + 'Change', value, record);
this.persist(record, trans, callback);
}
};
});