-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathindex.ts
More file actions
321 lines (280 loc) · 9.13 KB
/
index.ts
File metadata and controls
321 lines (280 loc) · 9.13 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
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
import { isChrome, isFirefox } from '@ember/-internals/browser-environment';
import type { AnyFn } from '@ember/-internals/utility-types';
import type { DeprecateFunc, DeprecationOptions } from './lib/deprecate';
import defaultDeprecate from './lib/deprecate';
import { isTesting } from './lib/testing';
import type { WarnFunc } from './lib/warn';
import _warn from './lib/warn';
import { assert, setAssert } from './lib/assert';
export { registerHandler as registerWarnHandler } from './lib/warn';
export {
registerHandler as registerDeprecationHandler,
type DeprecationOptions,
} from './lib/deprecate';
export { default as inspect } from './lib/inspect';
export { isTesting, setTesting } from './lib/testing';
export { default as captureRenderTree } from './lib/capture-render-tree';
export type DebugFunctionType =
| 'assert'
| 'info'
| 'warn'
| 'debug'
| 'deprecate'
| 'debugSeal'
| 'debugFreeze'
| 'runInDebug'
| 'deprecateFunc';
export type DebugFunc = (message: string) => void;
export type DebugSealFunc = (obj: object) => void;
export type DebugFreezeFunc = (obj: object) => void;
export type InfoFunc = (message: string, options?: object) => void;
export type RunInDebugFunc = (func: () => void) => void;
export type DeprecateFuncFunc = (
message: string,
options: DeprecationOptions,
func: Function
) => Function;
export type GetDebugFunction = {
(type: 'assert'): typeof assert;
(type: 'info'): InfoFunc;
(type: 'warn'): WarnFunc;
(type: 'debug'): DebugFunc;
(type: 'debugSeal'): DebugSealFunc;
(type: 'debugFreeze'): DebugFreezeFunc;
(type: 'deprecateFunc'): DeprecateFuncFunc;
(type: 'deprecate'): DeprecateFunc;
(type: 'runInDebug'): RunInDebugFunc;
};
export type SetDebugFunction = {
(type: 'assert', func: typeof assert): typeof assert;
(type: 'info', func: InfoFunc): InfoFunc;
(type: 'warn', func: WarnFunc): WarnFunc;
(type: 'debug', func: DebugFunc): DebugFunc;
(type: 'debugSeal', func: DebugSealFunc): DebugSealFunc;
(type: 'debugFreeze', func: DebugFreezeFunc): DebugFreezeFunc;
(type: 'deprecateFunc', func: DeprecateFuncFunc): DeprecateFuncFunc;
(type: 'deprecate', func: DeprecateFunc): DeprecateFunc;
(type: 'runInDebug', func: RunInDebugFunc): RunInDebugFunc;
};
// These are the default production build versions:
const noop = () => {};
// SAFETY: these casts are just straight-up lies, but the point is that they do
// not do anything in production builds.
let info: InfoFunc = noop;
let warn: WarnFunc = noop;
let debug: DebugFunc = noop;
let currentDeprecate: DeprecateFunc | undefined;
let debugSeal: DebugSealFunc = noop;
let debugFreeze: DebugFreezeFunc = noop;
let runInDebug: RunInDebugFunc = noop;
let setDebugFunction: SetDebugFunction = noop as unknown as SetDebugFunction;
let getDebugFunction: GetDebugFunction = noop as unknown as GetDebugFunction;
let deprecateFunc: DeprecateFuncFunc = function () {
return arguments[arguments.length - 1];
};
export function deprecate(...args: Parameters<DeprecateFunc>): ReturnType<DeprecateFunc> {
return (currentDeprecate ?? defaultDeprecate)(...args);
}
if (import.meta.env?.DEV) {
setDebugFunction = function (type: DebugFunctionType, callback: Function) {
switch (type) {
case 'assert':
return setAssert(callback as typeof assert);
case 'info':
return (info = callback as InfoFunc);
case 'warn':
return (warn = callback as WarnFunc);
case 'debug':
return (debug = callback as DebugFunc);
case 'deprecate':
if (callback === deprecate) {
currentDeprecate = undefined;
return deprecate;
} else {
return (currentDeprecate = callback as DeprecateFunc);
}
case 'debugSeal':
return (debugSeal = callback as DebugSealFunc);
case 'debugFreeze':
return (debugFreeze = callback as DebugFreezeFunc);
case 'runInDebug':
return (runInDebug = callback as RunInDebugFunc);
case 'deprecateFunc':
return (deprecateFunc = callback as DeprecateFuncFunc);
}
} as any;
getDebugFunction = function (type: DebugFunctionType) {
switch (type) {
case 'assert':
return assert;
case 'info':
return info;
case 'warn':
return warn;
case 'debug':
return debug;
case 'deprecate':
return deprecate;
case 'debugSeal':
return debugSeal;
case 'debugFreeze':
return debugFreeze;
case 'runInDebug':
return runInDebug;
case 'deprecateFunc':
return deprecateFunc;
}
} as any;
}
/**
@module @ember/debug
*/
if (import.meta.env?.DEV) {
/**
Display a debug notice.
Calls to this function are not invoked in production builds.
```javascript
import { debug } from '@ember/debug';
debug('I\'m a debug notice!');
```
@method debug
@for @ember/debug
@static
@param {String} message A debug message to display.
@public
*/
setDebugFunction('debug', function debug(message) {
console.debug(`Ember: ${message}`); /* eslint-disable-line no-console */
});
/**
Display an info notice.
Calls to this function are removed from production builds, so they can be
freely added for documentation and debugging purposes without worries of
incuring any performance penalty.
@method info
@private
*/
setDebugFunction('info', function info() {
console.info(...arguments); /* eslint-disable-line no-console */
});
/**
@module @ember/debug
@public
*/
/**
Alias an old, deprecated method with its new counterpart.
Display a deprecation warning with the provided message and a stack trace
(Chrome and Firefox only) when the assigned method is called.
Calls to this function are removed from production builds, so they can be
freely added for documentation and debugging purposes without worries of
incuring any performance penalty.
```javascript
import { deprecateFunc } from '@ember/debug';
oldMethod = deprecateFunc('Please use the new, updated method', options, newMethod);
```
@method deprecateFunc
@static
@for @ember/debug
@param {String} message A description of the deprecation.
@param {Object} [options] The options object for `deprecate`.
@param {Function} func The new function called to replace its deprecated counterpart.
@return {Function} A new function that wraps the original function with a deprecation warning
@private
*/
setDebugFunction('deprecateFunc', function deprecateFunc(...args: any[]) {
if (args.length === 3) {
let [message, options, func] = args as [string, DeprecationOptions, AnyFn];
return function (this: any, ...args: any[]) {
deprecate(message, false, options);
return func.apply(this, args);
};
} else {
let [message, func] = args;
return function (this: any) {
deprecate(message);
return func.apply(this, arguments);
};
}
});
/**
@module @ember/debug
@public
*/
/**
Run a function meant for debugging.
Calls to this function are removed from production builds, so they can be
freely added for documentation and debugging purposes without worries of
incuring any performance penalty.
```javascript
import Component from '@ember/component';
import { runInDebug } from '@ember/debug';
runInDebug(() => {
Component.reopen({
didInsertElement() {
console.log("I'm happy");
}
});
});
```
@method runInDebug
@for @ember/debug
@static
@param {Function} func The function to be executed.
@since 1.5.0
@public
*/
setDebugFunction('runInDebug', function runInDebug(func) {
func();
});
setDebugFunction('debugSeal', function debugSeal(obj) {
Object.seal(obj);
});
setDebugFunction('debugFreeze', function debugFreeze(obj) {
// re-freezing an already frozen object introduces a significant
// performance penalty on Chrome (tested through 59).
//
// See: https://bugs.chromium.org/p/v8/issues/detail?id=6450
if (!Object.isFrozen(obj)) {
Object.freeze(obj);
}
});
setDebugFunction('warn', _warn);
}
let _warnIfUsingStrippedFeatureFlags;
if (import.meta.env?.DEV && !isTesting()) {
if (typeof window !== 'undefined' && (isFirefox || isChrome) && window.addEventListener) {
window.addEventListener(
'load',
() => {
if (
document.documentElement &&
document.documentElement.dataset &&
!document.documentElement.dataset['emberExtension']
) {
let downloadURL;
if (isChrome) {
downloadURL =
'https://chrome.google.com/webstore/detail/ember-inspector/bmdblncegkenkacieihfhpjfppoconhi';
} else if (isFirefox) {
downloadURL = 'https://addons.mozilla.org/en-US/firefox/addon/ember-inspector/';
}
debug(`For more advanced debugging, install the Ember Inspector from ${downloadURL}`);
}
},
false
);
}
}
export {
assert,
info,
warn,
debug,
debugSeal,
debugFreeze,
runInDebug,
deprecateFunc,
setDebugFunction,
getDebugFunction,
_warnIfUsingStrippedFeatureFlags,
};