This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathpreferences.js
503 lines (473 loc) · 15.4 KB
/
preferences.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*******************************************************************************
* @license
* Copyright (c) 2010, 2019 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors: IBM Corporation - initial API and implementation
******************************************************************************/
/*eslint-env browser, amd*/
define([
'require',
'orion/Deferred',
'orion/EventTarget',
'orion/xhr',
'orion/util'],
function(require, Deferred, _EventTarget, xhr, util) {
function _mixin(target/*, source..*/) {
var hasOwn = Object.prototype.hasOwnProperty;
for (var j = 1, len = arguments.length; j < len; j++) {
var source = arguments[j];
for (var key in source) {
if (hasOwn.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
}
function Cache(prefix, expiresSeconds) {
return {
get: function(namespace, ignoreExpires) {
if (expiresSeconds === 0) {
return null;
}
var item = util.readSetting(prefix + namespace);
if (item === null) {
return null;
}
try {
var cached = JSON.parse(item);
if (ignoreExpires || expiresSeconds === -1 || (cached._expires && cached._expires > Date.now())) {
delete cached._expires;
return cached;
}
} catch (e) {
//invalid value; remove from cache.
util.deleteSetting(prefix + namespace);
}
return null;
},
set: function(namespace, data) {
if (expiresSeconds === 0) {
return;
}
if (expiresSeconds !== -1) {
data._expires = Date.now() + 1000 * expiresSeconds;
}
if (Object.keys(data).length === 0) {
util.deleteSetting(prefix + namespace);
} else {
var jsonData = JSON.stringify(data);
util.saveSetting(prefix + namespace, jsonData);
delete data._expires;
}
},
remove: function(namespace) {
util.deleteSetting(prefix + namespace);
}
};
}
function UserPreferencesProvider(serviceRegistry) {
this._currentPromises = {};
this._cache = new Cache("/orion/preferences/user", 60*60); //$NON-NLS-0$
this._service = null;
this.available = function() {
if (!this._service) {
var references = serviceRegistry.getServiceReferences("orion.core.preference.provider"); //$NON-NLS-0$
if (references.length > 0) {
this._service = serviceRegistry.getService(references[0]);
}
}
return !!this._service;
};
}
UserPreferencesProvider.prototype = {
get: function(namespace, optForce) {
if (this._currentPromises[namespace]) {
return this._currentPromises[namespace];
}
var d = new Deferred();
var cached = null;
if (optForce) {
this._cache.remove(namespace);
} else {
cached = this._cache.get(namespace);
}
if (cached !== null) {
d.resolve(cached);
} else {
this._currentPromises[namespace] = d;
var that = this;
this._service.get(namespace).then(function(data) {
data = data || {};
that._cache.set(namespace, data);
delete that._currentPromises[namespace];
d.resolve(data);
}, function (error) {
if (error.status === 404 || error.status === 410) {
var data = {};
that._cache.set(namespace, data);
delete that._currentPromises[namespace];
d.resolve(data);
} else {
delete that._currentPromises[namespace];
d.resolve(that._cache.get(namespace, true) || {});
}
});
}
return d;
},
put: function(namespace, data) {
this._cache.set(namespace, data);
return this._service.put(namespace, data);
},
remove: function(namespace, key){
var cached = this._cache.get(namespace);
delete cached[key];
this._cache.set(namespace, cached);
return this._service.remove(namespace, key);
}
};
function DefaultPreferencesProvider(_location) {
_location = _location || "defaults.pref"; //$NON-NLS-0$
if (_location.indexOf("://") === -1) { //$NON-NLS-0$
_location = require.toUrl ? require.toUrl(_location) : _location;
}
this._location = _location;
this._currentPromise = null;
this._cache = new Cache("/orion/preferences/default", 60*60); //$NON-NLS-0$
}
DefaultPreferencesProvider.prototype = {
get: function(namespace, optForce) {
var cached = null;
var that = this;
if (this._currentPromise) {
return this._currentPromise.then(function() {
cached = that._cache.get(namespace);
if (cached === null) {
cached = {};
that._cache.set(namespace, cached);
}
return cached;
});
}
var d = new Deferred();
if (optForce) {
this._cache.remove(namespace);
} else {
cached = this._cache.get(namespace);
}
if (cached !== null) {
d.resolve(cached);
} else {
this._currentPromise = d;
xhr("GET", this._location, { //$NON-NLS-0$
headers: {
"Orion-Version": "1" //$NON-NLS-0$
},
timeout: 15000
}).then(function(result) {
var data = JSON.parse(result.response);
Object.keys(data).forEach(function(key) {
that._cache.set(key, data[key] || {});
});
cached = data[namespace];
if (!cached) {
cached = {};
that._cache.set(namespace, cached);
}
that._currentPromise = null;
d.resolve(cached);
}, function(error) {
if (error.xhr.status === 401 || error.xhr.status === 491 || error.xhr.status === 404 ) {
that._cache.set(namespace, {});
that._currentPromise = null;
d.resolve({});
} else {
that._currentPromise = null;
var data = that._cache.get(namespace, true);
if (data !== null) {
d.resolve(data[namespace] || {});
} else {
d.resolve({});
}
}
});
}
return d;
},
/**
* @callback
*/
put: function(namespace, data) {
var d = new Deferred();
d.resolve();
return d;
},
remove: function(namespace, key){
var cached = this._cache.get(namespace);
delete cached[key];
this.put(namespace, cached);
}
};
function LocalPreferencesProvider() {
this._cache = new Cache("/orion/preferences/local", -1); //$NON-NLS-0$
}
LocalPreferencesProvider.prototype = {
get: function(namespace) {
var d = new Deferred();
var cached = this._cache.get(namespace);
if (cached !== null) {
d.resolve(cached);
} else {
d.resolve({});
}
return d;
},
put: function(namespace, data) {
var d = new Deferred();
this._cache.set(namespace, data);
d.resolve();
return d;
},
remove: function(namespace, key){
var cached = this._cache.get(namespace);
delete cached[key];
this.put(namespace, cached);
}
};
function PreferencesEvent(type, namespace, scope) {
this.type = type;
this.namespace = namespace;
this.scope = scope;
}
/**
* Dispatched when a preferences node has been changed. The type of this event is <code>"changed"</code>.
* @name orion.PreferencesService#changed
* @event
*/
/**
* Constructs a new preference service. Clients should obtain a preference service
* by requesting the service <tt>orion.core.preference</tt> from the service registry.
* This service constructor is only intended to be used by page service registry
* initialization code.
* @class The preferences service manages a hierarchical set of preference
* nodes. Each node consists of preference key/value pairs.
* @name orion.preferences.PreferencesService
* @see orion.preferences.Preferences
* @borrows orion.serviceregistry.EventTarget#addEventListener as #addEventListener
* @borrows orion.serviceregistry.EventTarget#removeEventListener as #removeEventListener
*/
function PreferencesService(serviceRegistry, options) {
options = options || {};
this._changeListeners = [];
this._userProvider = options.userProvider || new UserPreferencesProvider(serviceRegistry);
this._localProvider = options.localProvider || new LocalPreferencesProvider();
this._defaultsProvider = options.defaultsProvider || new DefaultPreferencesProvider(options.defaultPreferencesLocation);
_EventTarget.attach(this);
window.addEventListener("storage", function(evt) {
var key = evt.key;
var prefix = "/orion/preferences/"; //$NON-NLS-1$
if (!key || key.indexOf(prefix) !== 0) return;
var index = key.indexOf("/", prefix.length);
var namespace = key.substring(index);
var scope = {"default": PreferencesService.DEFAULT_SCOPE, local: PreferencesService.LOCAL_SCOPE, user: PreferencesService.USER_SCOPE}[key.substring(prefix.length, index)];
this.dispatchEvent(new PreferencesEvent("changed", namespace, scope)); //$NON-NLS-1$
}.bind(this), false);
this._serviceRegistration = serviceRegistry.registerService("orion.core.preference", this); //$NON-NLS-0$
}
PreferencesService.DEFAULT_SCOPE = 1;
PreferencesService.LOCAL_SCOPE = 2;
PreferencesService.USER_SCOPE = 4;
PreferencesService.prototype = /** @lends orion.preferences.PreferencesService.prototype */ {
/**
* @private this is intended to be used by the metrics services
* @name addChangeListener
* @description description
* @function
* @param callback
* @returns returns
*/
addChangeListener: function(callback) {
if (typeof(callback) === "function") { //$NON-NLS-0$
this._changeListeners.push(callback);
}
},
/**
* @name get
* @description Gets one or more preferences from the node specified by <code>namespace</code>.
* @function
* @param {String} namespace A slash-delimited path to the preference node to get
* @param {String|Array|Object} [key=undefined] the key, or array of keys, or an object specifying the default
* values of the keys to get. An empty array or object will return an empty object. Pass in <code>null</code> or <code>undefined</code
* to get all the preferences of the specified node.
* @param options the options object
* @returns A promise that will resolve when the preferences has been retrivied. The promise result is an object with key-value mappings.
*/
get: function(namespace, key, options) {
options = options || {};
var providers = this._getProviders(options.scope);
var gets = [];
providers.reverse().forEach(function(provider) {
gets.push(provider.get(namespace, options.noCache));
});
return Deferred.all(gets).then(function(stores) {
var result = {};
if (key && typeof key !== "string" && !Array.isArray(key)) { result = _mixin(result, key); }
stores.forEach(function(store) {
if (!store) { return; }
function addKey(key) {
if (key in store) {
if(typeof store[key] === 'object' && store[key] !== null) {
if(typeof result[key] === 'undefined') {
result[key] = store[key];
} else {
result[key] = _mixin(result[key], store[key]);
}
} else {
result[key] = store[key];
}
}
}
if (!key) {
Object.keys(store).forEach(addKey);
} else if (typeof key === "string") {
addKey(key);
} else if (Array.isArray(key)) {
key.forEach(addKey);
} else {
Object.keys(key).forEach(addKey);
}
});
return result;
});
},
/**
* @name put
* @description Sets multiple preferences in the node specified by <code>namespace</code>.
* @function
* @param {String} namespace A slash-delimited path to the preference node to update
* @param data An object with key-value pairs to update. Any other key/value pairs in the node will not be affected unless the
* options <code>clear</code> is set.
* @param options the options object
* @returns A promise that will resolve when the preferences has been updated.
*/
put: function(namespace, data, options) {
var that = this;
options = options || {};
var provider = this._getProviders(options.scope)[0];
return provider.get(namespace).then(function(store) {
var newStore = data;
if (!options.clear) {
newStore = _mixin({}, store, newStore);
}
if (JSON.stringify(store) === JSON.stringify(newStore)) return;
that._valueChanged(namespace, data, store);
return provider.put(namespace, newStore);
});
},
/**
* @name remove
* @description Removes one or more preferences in the node specified by <code>namespace</code>.
* @function
* @param {String} namespace A slash-delimited path to the preference node to update
* @param {String|Array|Object} [key=undefined] the key, or array of keys, or an object specifying the
* the keys to remove. An empty array or object will remove nothing. Pass in <code>null</code> or <code>undefined</code>
* to remove all the preferences of the specified node.
* @param options the options object
* @returns A promise that will resolve when the preferences has been updated.
*/
remove: function(namespace, key, options) {
options = options || {};
var provider = this._getProviders(options.scope)[0];
return provider.get(namespace).then(function(store) {
function deleteKey(key) {
if (key in store) {
delete store[key];
}
}
if (!key) {
Object.keys(store).forEach(deleteKey);
} else if (typeof key === "string") {
deleteKey(key);
} else if (Array.isArray(key)) {
key.forEach(deleteKey);
} else {
Object.keys(key).forEach(deleteKey);
}
return provider.put(namespace, store);
});
},
/**
* @private
*/
_getProviders: function(optScope) {
if (!optScope || typeof(optScope) !== "number" || optScope > 7 || optScope < 1) { //$NON-NLS-0$
optScope = PreferencesService.DEFAULT_SCOPE;
optScope |= this._userProvider.available() ? PreferencesService.USER_SCOPE : PreferencesService.LOCAL_SCOPE;
}
var providers = [];
if ((PreferencesService.USER_SCOPE & optScope) && this._userProvider.available()) {
providers.push(this._userProvider);
}
if (PreferencesService.LOCAL_SCOPE & optScope) {
providers.push(this._localProvider);
}
if (PreferencesService.DEFAULT_SCOPE & optScope) {
providers.push(this._defaultsProvider);
}
return providers;
},
/**
* @private
*/
_valueChanged: function(namespace, data, store) {
if (!this._changeListeners.length) return;
var that = this;
function callChangeListener(key, value) {
that._changeListeners.forEach(function(current) {
current(key, value);
});
}
for (var key in data) {
var changeKey = namespace + "/" + key; //$NON-NLS-0$
var value = data[key];
if (typeof(value) === "string") { //$NON-NLS-0$
callChangeListener(changeKey, value);
} else {
for (var current in value) {
if (current !== "pid" && (!store || !store[key] || store[key][current] !== value[current])) { //$NON-NLS-0$
var stringValue = String(value[current]);
callChangeListener(changeKey + "/" + current, stringValue); //$NON-NLS-0$
}
}
}
}
},
/**
* @private this is intended to be used by the metrics services
* @name removeChangeListener
* @description description
* @function
* @param callback
* @returns returns
*/
removeChangeListener: function(callback) {
if (typeof(callback) === "function") { //$NON-NLS-0$
for (var i = 0; i < this._changeListeners.length; i++) {
if (this._changeListeners[i] === callback) {
this._changeListeners.splice(i, 1);
return;
}
}
}
}
};
return {
PreferencesService: PreferencesService,
Cache: Cache
};
});