-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathAppInit.js
More file actions
185 lines (164 loc) · 5.65 KB
/
AppInit.js
File metadata and controls
185 lines (164 loc) · 5.65 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
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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// @INCLUDE_IN_API_DOCS
/**
* Defines hooks to assist with module initialization.
*
* This module defines 3 methods for client modules to attach callbacks:
* - htmlReady - When the main application template is rendered
* - extensionsLoaded - When the extension manager has loaded all extensions
* - appReady - When Brackets completes loading all modules and extensions
*
* These are *not* jQuery events. Each method is similar to $(document).ready
* in that it will call the handler immediately if brackets is already done
* loading.
*/
define(function (require, exports, module) {
const Metrics = require("utils/Metrics");
/**
* Fires when the base htmlContent/main-view.html is loaded
*
* @type {string}
* @const
*/
var HTML_READY = "htmlReady";
/**
* Fires when all extensions are loaded
*
* @type {string}
* @const
*/
var APP_READY = "appReady";
/**
* Fires after extensions have been loaded
*
* @type {string}
* @const
*/
var EXTENSIONS_LOADED = "extensionsLoaded";
/**
* Map of each state's trigger
*
* @type {Object.<string, boolean>}
* @private
*/
var _status = { HTML_READY: false, APP_READY: false, EXTENSIONS_LOADED: false };
/**
* Map of callbacks to states
*
* @type {Object.<string, Array.<function()>>}
* @private
*/
var _callbacks = {};
_callbacks[HTML_READY] = [];
_callbacks[APP_READY] = [];
_callbacks[EXTENSIONS_LOADED] = [];
/**
* calls the specified handler inside a try/catch handler
*
* @param {function()} handler - the callback to call
* @private
*/
function _callHandler(handler) {
try {
// TODO (issue 1034): We *could* use a $.Deferred for this, except deferred objects enter a broken
// state if any resolution callback throws an exception. Since third parties (e.g. extensions) may
// add callbacks to this, we need to be robust to exceptions
handler();
} catch (e) {
console.error("Exception when calling a 'brackets done loading' handler: " + e);
console.log(e.stack);
let supportStatus = "Y";
if(!window.Phoenix.isSupportedBrowser){
supportStatus = "N";
}
Metrics.countEvent(Metrics.EVENT_TYPE.ERROR, "appInit", `${supportStatus}-handlerFail`);
}
}
/**
* dispatches the event by calling all handlers registered for that type
*
* @param {string} type - the event type to dispatch (APP_READY, EXTENSIONS_READY, HTML_READY)
* @private
*/
function _dispatchReady(type) {
var i,
myHandlers = _callbacks[type];
// mark this status complete
_status[type] = true;
for (i = 0; i < myHandlers.length; i++) {
_callHandler(myHandlers[i]);
}
// clear all callbacks after being called
_callbacks[type] = [];
}
/**
* adds a callback to the list of functions to call for the specified event type
*
* @param {string} type - the event type to dispatch (APP_READY, EXTENSIONS_READY, HTML_READY)
* @param {function} handler - callback function to call when the event is triggered
* @private
*/
function _addListener(type, handler) {
if (_status[type]) {
_callHandler(handler);
} else {
_callbacks[type].push(handler);
}
}
/**
* Adds a callback for the ready hook. Handlers are called after
* htmlReady is done, the initial project is loaded, and all extensions are
* loaded.
*
* @param {function} handler - callback function to call when the event is fired
*/
function appReady(handler) {
_addListener(APP_READY, handler);
}
/**
* Adds a callback for the htmlReady hook. Handlers are called after the
* main application html template is rendered.
*
* @param {function} handler - callback function to call when the event is fired
*/
function htmlReady(handler) {
_addListener(HTML_READY, handler);
}
/**
* Adds a callback for the extensionsLoaded hook. Handlers are called after the
* extensions have been loaded
*
* @param {function} handler - callback function to call when the event is fired
*/
function extensionsLoaded(handler) {
_addListener(EXTENSIONS_LOADED, handler);
}
// Public API
exports.appReady = appReady;
exports.htmlReady = htmlReady;
exports.extensionsLoaded = extensionsLoaded;
exports.HTML_READY = HTML_READY;
exports.APP_READY = APP_READY;
exports.EXTENSIONS_LOADED = EXTENSIONS_LOADED;
// Unit Test API
exports._dispatchReady = _dispatchReady;
});