1 | import $ from './jquery';
|
2 | import * as logger from './internal/log';
|
3 | import globalize from './internal/globalize';
|
4 |
|
5 | /**
|
6 | * Adds functions to the list of methods to be run asynchronously after DomContentLoaded.
|
7 | *
|
8 | * Wraps error handling around the provided function so its failure won't prevent
|
9 | * other init functions running.
|
10 | *
|
11 | * @param {Function} func Function to be called on initialisation.
|
12 | *
|
13 | * @return {Object}
|
14 | */
|
15 | function toInit (func) {
|
16 | $.when($.ready).then(function () {
|
17 | // Ensure that every function passed through toInit happens asynchronously after DomContentLoaded events,
|
18 | // and will be executed in its own stack, meaning that the UI can be updated in-between expensive handlers.
|
19 | setTimeout(function () {
|
20 | try {
|
21 | func.call(this, $);
|
22 | } catch (ex) {
|
23 | logger.error('Failed to run init function: ' + ex.message, func, ex, ex.stack);
|
24 | }
|
25 | }, 1);
|
26 | });
|
27 |
|
28 | return this;
|
29 | }
|
30 |
|
31 | globalize('toInit', toInit);
|
32 |
|
33 | export default toInit;
|