diff options
-rw-r--r-- | fastdom.js | 45 | ||||
-rw-r--r-- | fastdom.min.js | 2 |
2 files changed, 42 insertions, 5 deletions
@@ -45,6 +45,7 @@ function FastDom() { } FastDom.prototype = { + version: 1, constructor: FastDom, /** @@ -231,12 +232,48 @@ function mixin(target, source) { } } -// There should never be more than -// one instance of `FastDom` in an app -var exports = win.fastdom = (win.fastdom || new FastDom()); // jshint ignore:line +/** + * Exports + * + * In order for fastdom to work in the most + * performant way it must be a global singleton + * within each app. If two fastdom's are operating + * at the same time, they can collide and force + * additional reflows. + * + * To circumvent this we: + * + * 1. Check for any pre-existing `fastdom` + * 2. Test if the major versions are the same + * 3. If compatible, we export the existing instance + * 4. If not compatible we console.warn and create a new instance + * + * This assumes that instances of the same semver + * major version have the same public API. Any + * minor or patch versions are disregarded. + * The first fastdom loaded wins. + */ -// Expose to CJS & AMD +var exports = (function() { + var existing = win['fastdom'] || win['__fastdom__']; + + if (existing) { + var version = existing.version || (existing.defer ? 0 : 1); + var compatible = version === FastDom.prototype.version; + var exports = compatible ? existing : new FastDom(); + if (compatible) return existing; + console.warn('[fastdom] Multiple incompatible versions detected (this could impact performance)'); + } + + return new FastDom(); +})(); + +// Expose to CJS, AMD or window if ((typeof define)[0] == 'f') define(function() { return exports; }); else if ((typeof module)[0] == 'o') module.exports = exports; +else win['fastdom'] = exports; + +// Expose 'hidden' global +win['__fastdom__'] = exports; })( window || this); diff --git a/fastdom.min.js b/fastdom.min.js index a1ae649..240211e 100644 --- a/fastdom.min.js +++ b/fastdom.min.js @@ -1 +1 @@ -!function(t){"use strict";function e(){var e=this;e.reads=[],e.writes=[],e.raf=a.bind(t)}function n(t){t.scheduled||(t.scheduled=!0,t.raf(i.bind(null,t)))}function i(t){var e,i=t.writes,o=t.reads;try{r(o),r(i)}catch(s){e=s}if(t.scheduled=!1,(o.length||i.length)&&n(t),e){if(!t["catch"])throw e;t["catch"](e)}}function r(t){for(var e;e=t.shift();)e()}function o(t,e){var n=t.indexOf(e);return!!~n&&!!t.splice(n,1)}function s(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])}var a=t.requestAnimationFrame||t.webkitRequestAnimationFrame||t.mozRequestAnimationFrame||t.msRequestAnimationFrame||function(t){return setTimeout(t,16)};e.prototype={constructor:e,measure:function(t,e){var i=e?t.bind(e):t;return this.reads.push(i),n(this),i},mutate:function(t,e){var i=e?t.bind(e):t;return this.writes.push(i),n(this),i},clear:function(t){return o(this.reads,t)||o(this.writes,t)},extend:function(t){if("object"!=typeof t)throw new Error("expected object");var e=Object.create(this);return s(e,t),e.fastdom=this,e.initialize&&e.initialize(),e},"catch":null};var exports=t.fastdom=t.fastdom||new e;"f"==(typeof define)[0]?define(function(){return exports}):"o"==(typeof module)[0]&&(module.exports=exports)}(window||this); +!function(t){"use strict";function e(){var e=this;e.reads=[],e.writes=[],e.raf=a.bind(t)}function n(t){t.scheduled||(t.scheduled=!0,t.raf(r.bind(null,t)))}function r(t){var e,r=t.writes,o=t.reads;try{i(o),i(r)}catch(s){e=s}if(t.scheduled=!1,(o.length||r.length)&&n(t),e){if(!t["catch"])throw e;t["catch"](e)}}function i(t){for(var e;e=t.shift();)e()}function o(t,e){var n=t.indexOf(e);return!!~n&&!!t.splice(n,1)}function s(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])}var a=t.requestAnimationFrame||t.webkitRequestAnimationFrame||t.mozRequestAnimationFrame||t.msRequestAnimationFrame||function(t){return setTimeout(t,16)};e.prototype={version:1,constructor:e,measure:function(t,e){var r=e?t.bind(e):t;return this.reads.push(r),n(this),r},mutate:function(t,e){var r=e?t.bind(e):t;return this.writes.push(r),n(this),r},clear:function(t){return o(this.reads,t)||o(this.writes,t)},extend:function(t){if("object"!=typeof t)throw new Error("expected object");var e=Object.create(this);return s(e,t),e.fastdom=this,e.initialize&&e.initialize(),e},"catch":null};var exports=function(){var n=t.fastdom||t.__fastdom__;if(n){var r=n.version||(n.defer?0:1),i=r===e.prototype.version;i?n:new e;if(i)return n}return new e}();"f"==(typeof define)[0]?define(function(){return exports}):"o"==(typeof module)[0]?module.exports=exports:t.fastdom=exports,t.__fastdom__=exports}(window||this); |