This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathPreferencesImpl.js
More file actions
145 lines (120 loc) · 5.83 KB
/
PreferencesImpl.js
File metadata and controls
145 lines (120 loc) · 5.83 KB
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
/*
* Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*global define, $, localStorage, brackets, console, Promise */
/**
* Generates the fully configured preferences systems used throughout Brackets. This is intended
* to be essentially private implementation that can be overridden for tests.
*/
define(function (require, exports, module) {
"use strict";
var PreferencesBase = require("./PreferencesBase"),
Async = require("utils/Async"),
// The SETTINGS_FILENAME is used with a preceding "." within user projects
SETTINGS_FILENAME = "brackets.json",
STATE_FILENAME = "state.json",
// User-level preferences
userPrefFile = brackets.app.getApplicationSupportDirectory() + "/" + SETTINGS_FILENAME;
/**
* A deferred object which is used to indicate PreferenceManager readiness during the start-up.
* @private
* @type {Promise}
*/
var _prefManagerReadyResolve;
var _prefManagerReadyPromise = new Promise(function (resolve, reject) {
_prefManagerReadyResolve = resolve;
});
/**
* A boolean property indicating if the user scope configuration file is malformed.
*/
var userScopeCorrupt = false;
function isUserScopeCorrupt() {
return userScopeCorrupt;
}
/**
* Promises to add scopes. Used at init time only.
* @private
* @type {Array.<Promise>}
*/
var _addScopePromises = [];
var manager = new PreferencesBase.PreferencesSystem();
manager.pauseChangeEvents();
// Create a Project scope
var projectStorage = new PreferencesBase.FileStorage(undefined, true),
projectScope = new PreferencesBase.Scope(projectStorage),
projectPathLayer = new PreferencesBase.PathLayer();
projectScope.addLayer(projectPathLayer);
var userScopeLoading = manager.addScope("user", new PreferencesBase.FileStorage(userPrefFile, true));
_addScopePromises.push(userScopeLoading);
// Set up the .brackets.json file handling
var fnUserScopeAlways = function () {
_addScopePromises.push(manager.addScope("project", projectScope, {
before: "user"
}));
// Session Scope is for storing prefs in memory only but with the highest precedence.
_addScopePromises.push(manager.addScope("session", new PreferencesBase.MemoryStorage()));
Async.waitForAll(_addScopePromises)
.then(_prefManagerReadyResolve, _prefManagerReadyResolve);
};
userScopeLoading
.catch(function (err) {
_addScopePromises.push(manager.addScope("user", new PreferencesBase.MemoryStorage(), {
before: "default"
}));
if (err.name && err.name === "ParsingError") {
userScopeCorrupt = true;
}
});
userScopeLoading
.then(fnUserScopeAlways, fnUserScopeAlways);
// "State" is stored like preferences but it is not generally intended to be user-editable.
// It's for more internal, implicit things like window size, working set, etc.
var stateManager = new PreferencesBase.PreferencesSystem();
var userStateFile = brackets.app.getApplicationSupportDirectory() + "/" + STATE_FILENAME;
var smUserScope = new PreferencesBase.Scope(new PreferencesBase.FileStorage(userStateFile, true));
var stateProjectLayer = new PreferencesBase.ProjectLayer();
smUserScope.addLayer(stateProjectLayer);
var smUserScopeLoading = stateManager.addScope("user", smUserScope);
// Listen for times where we might be unwatching a root that contains one of the user-level prefs files,
// and force a re-read of the file in order to ensure we can write to it later (see #7300).
function _reloadUserPrefs(rootDir) {
var prefsDir = brackets.app.getApplicationSupportDirectory() + "/";
if (prefsDir.indexOf(rootDir.fullPath) === 0) {
manager.fileChanged(userPrefFile);
stateManager.fileChanged(userStateFile);
}
}
// Semi-Public API. Use this at your own risk. The public API is in PreferencesManager.
exports.manager = manager;
exports.projectStorage = projectStorage;
exports.projectPathLayer = projectPathLayer;
exports.userScopeLoading = userScopeLoading;
exports.stateManager = stateManager;
exports.stateProjectLayer = stateProjectLayer;
exports.smUserScopeLoading = smUserScopeLoading;
exports.userPrefFile = userPrefFile;
exports.isUserScopeCorrupt = isUserScopeCorrupt;
exports.managerReady = _prefManagerReadyPromise;
exports.reloadUserPrefs = _reloadUserPrefs;
exports.STATE_FILENAME = STATE_FILENAME;
exports.SETTINGS_FILENAME = SETTINGS_FILENAME;
});