Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions fastdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function FastDom() {
}

FastDom.prototype = {
version: 1,
constructor: FastDom,

/**
Expand Down Expand Up @@ -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)');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a link to the issue in the warning?

}

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);
2 changes: 1 addition & 1 deletion fastdom.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.