UNPKG

@enact/core

Version:

Enact is an open source JavaScript framework containing everything you need to create a fast, scalable mobile or web application.

83 lines (78 loc) 2.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; exports.isWindowReady = isWindowReady; exports.onWindowReady = onWindowReady; exports.windowReady = windowReady; var _invariant = _interopRequireDefault(require("invariant")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } /** * Utilities to facilitate integration with v8 snapshot builds * * @module core/snapshot * @exports isWindowReady * @exports onWindowReady * @exports windowReady * @public */ var windowCallbacks = []; /** * Determines if the `window` is available * * @function * * @returns {Boolean} `true` when `window` is ready * @memberof core/snapshot * @public */ function isWindowReady() { return typeof window !== 'undefined'; } /** * Executes a callback, such as registering event handlers, when a valid `window` is available. * * During normal operation, the callback will be executed immediately. During a pre-rendering pass, * the callback is not be executed at all. When using snapshot, the callback is added to a queue * and is executed in order once the window is available. * * *Important Notes* * * The callback should not alter the initial HTML state. If it does, it will invalidate the * pre-render state and interfere with React rehydration. * * The callback should be limited to module-scoped actions and not component instance actions. If * the action is tied to a component, it should be invoked from within the component's lifecycle * methods. * * @function * @param {Function} callback Function to run when the window is ready * * @returns {undefined} * @memberof core/snapshot * @public */ function onWindowReady(callback) { if (isWindowReady()) { callback(); } else { windowCallbacks.push(callback); } } /** * Executes all queued window callbacks. * * Requires that the window be, in fact, available and will throw an `Error` if not. * * @function * * @returns {undefined} * @memberof core/snapshot * @public */ function windowReady() { !isWindowReady() ? process.env.NODE_ENV !== "production" ? (0, _invariant["default"])(false, 'windowReady cannot be run until the window is available') : (0, _invariant["default"])(false) : void 0; windowCallbacks.forEach(function (f) { return f(); }); } var _default = exports["default"] = onWindowReady;