Permalink
Browse files

further clean the window polyfill by moving the domReady/initializati…

…on/boot related code under me.device
  • Loading branch information...
1 parent 33bca2c commit 5a68571e09391a10f5cd5adf5764785a0e6938a2 @obiot obiot committed Nov 30, 2017
Showing with 101 additions and 80 deletions.
  1. +2 −2 sourceFiles.json
  2. +22 −0 src/config.js
  3. +1 −78 src/lang/window.js
  4. +76 −0 src/system/device.js
View
@@ -13,10 +13,10 @@
"src/lang/performance.js",
"src/vendors/es6-collections.js",
"src/vendors/es6-promise.js",
- "src/config.js",
- "src/game.js",
"src/system/agent.js",
"src/system/device.js",
+ "src/game.js",
+ "src/config.js",
"src/system/timer.js",
"src/system/pooling.js",
"src/math/vector2.js",
View
@@ -186,6 +186,13 @@
}
});
+ /**
+ * Disable melonJS auto-initialization
+ * @type {Boolean}
+ * @default false
+ * @memberOf me
+ */
+ me.skipAutoInit = false;
/**
* initial boot function
@@ -231,4 +238,19 @@
me_initialized = true;
};
+ // call the library init function when ready
+ if (me.skipAutoInit === false) {
+ me.device.onReady(function () {
+ me.boot();
+ });
+ } else {
+ /**
+ * @ignore
+ */
+ me.init = function () {
+ me.boot();
+ me.device._domReady();
+ };
+ }
+
})();
View
@@ -14,40 +14,6 @@
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Window.window|window}
*/
- /*
- * DOM loading stuff
- */
- var readyBound = false, isReady = false, readyList = [];
-
- // Handle when the DOM is ready
- function domReady() {
- // Make sure that the DOM is not already loaded
- if (!isReady) {
- // be sure document.body is there
- if (!document.body) {
- return setTimeout(domReady, 13);
- }
-
- // clean up loading event
- if (document.removeEventListener) {
- document.removeEventListener(
- "DOMContentLoaded",
- domReady,
- false
- );
- }
- // remove the event on window.onload (always added in `onReady`)
- window.removeEventListener("load", domReady, false);
-
- // execute all callbacks
- while (readyList.length){
- readyList.shift().call(window, []);
- }
-
- // Remember that the DOM is ready
- isReady = true;
- }
- }
/**
* Specify a function to execute when the DOM is fully loaded
@@ -95,52 +61,9 @@
* });
*/
window.onReady = function (fn) {
- // If the DOM is already ready
- if (isReady) {
- // Execute the function immediately
- fn.call(window, []);
- }
- else {
- // Add the function to the wait list
- readyList.push(fn);
-
- // attach listeners if not yet done
- if (!readyBound) {
- // directly call domReady if document is already "ready"
- if (document.readyState === "complete") {
- // defer the fn call to ensure our script is fully loaded
- window.setTimeout(domReady, 0);
- }
- else {
- if (document.addEventListener) {
- // Use the handy event callback
- document.addEventListener("DOMContentLoaded", domReady, false);
- }
- // A fallback to window.onload, that will always work
- window.addEventListener("load", domReady, false);
- }
- readyBound = true;
- }
- }
+ me.device.onReady.call(this, fn);
};
- // call the library init function when ready
- // (this should not be here?)
- if (me.skipAutoInit !== true) {
- window.onReady(function () {
- me.boot();
- });
- }
- else {
- /**
- * @ignore
- */
- me.init = function () {
- me.boot();
- domReady();
- };
- }
-
if (!window.throttle) {
/**
* a simple throttle function
View
@@ -26,6 +26,81 @@
return false;
};
+ /*
+ * DOM loading stuff
+ */
+ var readyBound = false, isReady = false, readyList = [];
+
+ /**
+ * called to check if the device is ready
+ * @ignore
+ */
+ api._domReady = function (fn) {
+ // Make sure that the DOM is not already loaded
+ if (!isReady) {
+ // be sure document.body is there
+ if (!document.body) {
+ return setTimeout(me.device._domReady, 13);
+ }
+
+ // clean up loading event
+ if (document.removeEventListener) {
+ document.removeEventListener(
+ "DOMContentLoaded",
+ me.device._domReady,
+ false
+ );
+ }
+ // remove the event on window.onload (always added in `onReady`)
+ window.removeEventListener("load", me.device._domReady, false);
+
+ // execute all callbacks
+ while (readyList.length) {
+ readyList.shift().call(window, []);
+ }
+
+ // Remember that the DOM is ready
+ isReady = true;
+ }
+ };
+
+ /**
+ * Specify a function to execute when the Device is fully loaded and ready
+ * @memberOf external:window#
+ * @alias onReady
+ * @param {Function} fn A function to execute after the DOM is ready.
+ * @see
+ */
+ api.onReady = function (fn) {
+ // If the DOM is already ready
+ if (isReady) {
+ // Execute the function immediately
+ fn.call(window, []);
+ }
+ else {
+ // Add the function to the wait list
+ readyList.push(fn);
+
+ // attach listeners if not yet done
+ if (!readyBound) {
+ // directly call domReady if document is already "ready"
+ if (document.readyState === "complete") {
+ // defer the fn call to ensure our script is fully loaded
+ window.setTimeout(me.device._domReady, 0);
+ }
+ else {
+ if (document.addEventListener) {
+ // Use the handy event callback
+ document.addEventListener("DOMContentLoaded", me.device._domReady, false);
+ }
+ // A fallback to window.onload, that will always work
+ window.addEventListener("load", me.device._domReady, false);
+ }
+ readyBound = true;
+ }
+ }
+ };
+
/**
* check the device capapbilities
@@ -195,6 +270,7 @@
};
+
/*
* PUBLIC Properties & Functions
*/

1 comment on commit 5a68571

Owner

obiot commented on 5a68571 Jan 1, 2018

Please sign in to comment.