Skip to content

Commit

Permalink
Restructure Ember.libraries to be more idiomatic. Fixes emberjs#9291
Browse files Browse the repository at this point in the history
…and fixes emberjs#9339
  • Loading branch information
jayphelps committed Nov 21, 2014
1 parent 23ce2b3 commit 4d37df5
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 69 deletions.
19 changes: 11 additions & 8 deletions packages/ember-application/lib/system/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,23 @@ var Application = Namespace.extend(DeferredMixin, {
Ember.libraries.registerCoreLibrary('Handlebars' + (EmberHandlebars.compile ? '' : '-runtime'), EmberHandlebars.VERSION);
Ember.libraries.registerCoreLibrary('jQuery', jQuery().jquery);

if ( Ember.LOG_VERSION ) {
Ember.LOG_VERSION = false; // we only need to see this once per Application#init
if (Ember.LOG_VERSION) {
// we only need to see this once per Application#init
Ember.LOG_VERSION = false;
var libs = Ember.libraries._registry;

var nameLengths = EnumerableUtils.map(Ember.libraries, function(item) {
return get(item, "name.length");
var nameLengths = EnumerableUtils.map(libs, function(item) {
return get(item, 'name.length');
});

var maxNameLength = Math.max.apply(this, nameLengths);

Ember.debug('-------------------------------');
Ember.libraries.each(function(name, version) {
var spaces = new Array(maxNameLength - name.length + 1).join(" ");
Ember.debug([name, spaces, ' : ', version].join(""));
});
for (var i = 0, l = libs.length; i < l; i++) {
var lib = libs[i];
var spaces = new Array(maxNameLength - lib.name.length + 1).join(' ');
Ember.debug([lib.name, spaces, ' : ', lib.version].join(''));
}
Ember.debug('-------------------------------');
}
},
Expand Down
83 changes: 54 additions & 29 deletions packages/ember-metal/lib/libraries.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,70 @@
// Provides a way to register library versions with ember.
import Ember from "ember-metal/core";
import {
forEach,
indexOf
} from "ember-metal/enumerable_utils";

var libraries = function() {
var _libraries = [];
var coreLibIndex = 0;
/**
Helper class that allows you to register your library with Ember.
var getLibrary = function(name) {
for (var i = 0; i < _libraries.length; i++) {
if (_libraries[i].name === name) {
return _libraries[i];
Singleton created at `Ember.libraries`.
@class Libraries
@constructor
@private
*/
function Libraries() {
this._registry = [];
this._coreLibIndex = 0;
}

Libraries.prototype = {
constructor: Libraries,

_getLibraryByName: function(name) {
var libs = this._registry;
var count = libs.length;

for (var i = 0; i < count; i++) {
if (libs[i].name === name) {
return libs[i];
}
}
};
},

_libraries.register = function(name, version) {
if (!getLibrary(name)) {
_libraries.push({name: name, version: version});
}
};
register: function(name, version, isCoreLibrary) {
var index = this._registry.length;

_libraries.registerCoreLibrary = function(name, version) {
if (!getLibrary(name)) {
_libraries.splice(coreLibIndex++, 0, {name: name, version: version});
if (!this._getLibraryByName(name)) {
if (isCoreLibrary) {
index = this._coreLibIndex++;
}
this._registry.splice(index, 0, { name: name, version: version });
} else {
Ember.warn('Library "' + name + '" is already registered with Ember.');
}
};
},

registerCoreLibrary: function(name, version) {
this.register(name, version, true);
},

_libraries.deRegister = function(name) {
var lib = getLibrary(name);
if (lib) _libraries.splice(indexOf(_libraries, lib), 1);
};
deRegister: function(name) {
var lib = this._getLibraryByName(name);
var index;

_libraries.each = function (callback) {
forEach(_libraries, function(lib) {
if (lib) {
index = indexOf(this._registry, lib);
this._registry.splice(index, 1);
}
},

each: function(callback) {
Ember.deprecate('Using Ember.libraries.each() is deprecated. Access to a list of registered libraries is currently a private API. If you are not knowingly accessing this method, your out-of-date Ember Inspector may be doing so.');
forEach(this._registry, function(lib) {
callback(lib.name, lib.version);
});
};

return _libraries;
}();
}
};

export default libraries;
export default Libraries;
4 changes: 2 additions & 2 deletions packages/ember-metal/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ import {
oneWay
} from "ember-metal/binding";
import run from "ember-metal/run_loop";
import libraries from "ember-metal/libraries";
import Libraries from "ember-metal/libraries";
import isNone from 'ember-metal/is_none';
import isEmpty from 'ember-metal/is_empty';
import isBlank from 'ember-metal/is_blank';
Expand Down Expand Up @@ -311,7 +311,7 @@ Ember.isGlobalPath = isGlobalPath;

Ember.run = run;

Ember.libraries = libraries;
Ember.libraries = new Libraries();
Ember.libraries.registerCoreLibrary('Ember', Ember.VERSION);

Ember.isNone = isNone;
Expand Down
103 changes: 73 additions & 30 deletions packages/ember-metal/tests/libraries_test.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,93 @@
import libraries from "ember-metal/libraries";
/* globals EmberDev */
import Libraries from "ember-metal/libraries";

var libs = libraries;
var libs, registry;

test('Ember registers itself', function() {
equal(libs[0].name, "Ember");
QUnit.module('Libraries registry', {
setup: function() {
libs = new Libraries();
registry = libs._registry;
},

teardown: function() {
libs = null;
registry = null;
}
});

test('core libraries come before other libraries', function() {
var l = libs.length;

libs.register("my-lib", "2.0.0a");
libs.registerCoreLibrary("DS", "1.0.0-beta.2");
expect(2);

equal(libs[l].name, "DS");
equal(libs[l+1].name, "my-lib");
libs.register('my-lib', '2.0.0a');
libs.registerCoreLibrary('DS', '1.0.0-beta.2');

libs.deRegister("my-lib");
libs.deRegister("DS");
equal(registry[0].name, 'DS');
equal(registry[1].name, 'my-lib');
});

test('only the first registration of a library is stored', function() {
var l = libs.length;
expect(3);

libs.register('magic', 1.23);
libs.register('magic', 2.23);

equal(registry[0].name, 'magic');
equal(registry[0].version, 1.23);
equal(registry.length, 1);
});

test('attempting to register a library that is already registered warns you', function() {
if (EmberDev && EmberDev.runningProdBuild){
ok(true, 'Logging does not occur in production builds');
return;
}

expect(1);

libs.register("magic", 1.23);
libs.register("magic", 2.23);
libs.register("magic", 3.23);
var oldWarn = Ember.warn;
libs.register('magic', 1.23);

equal(libs[l].name, "magic");
equal(libs[l].version, 1.23);
equal(libs.length, l+1);
Ember.warn = function(msg, test) {
if (!test) {
equal(msg, 'Library "magic" is already registered with Ember.');
}
};

libs.deRegister("magic");
// Should warn us
libs.register('magic', 2.23);

Ember.warn = oldWarn;
});

test('libraries can be de-registered', function() {
var l = libs.length;

libs.register("lib1", "1.0.0b");
libs.register("lib2", "1.0.0b");
libs.register("lib3", "1.0.0b");
expect(2);

libs.deRegister("lib1");
libs.deRegister("lib3");
libs.register('lib1', '1.0.0b');
libs.register('lib2', '1.0.0b');
libs.register('lib3', '1.0.0b');

equal(libs[l].name, "lib2");
equal(libs.length, l+1);
libs.deRegister('lib1');
libs.deRegister('lib3');

libs.deRegister("lib2");
equal(registry[0].name, 'lib2');
equal(registry.length, 1);
});


test('Libraries#each allows us to loop through each registered library (but is deprecated)', function() {
expect(5);

var items = [{ name: 'lib1', version: '1.0.0' }, { name: 'lib2', version: '2.0.0' }];

for (var i = 0, l = items.length; i < l; i++) {
libs.register(items[i].name, items[i].version);
}

expectDeprecation(function() {
libs.each(function (name, version) {
var expectedLib = items.shift();
equal(expectedLib.name, name);
equal(expectedLib.version, version);
});
}, 'Using Ember.libraries.each() is deprecated. Access to a list of registered libraries is currently a private API. If you are not knowingly accessing this method, your out-of-date Ember Inspector may be doing so.');
});
10 changes: 10 additions & 0 deletions packages/ember-metal/tests/main_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Ember from "ember-metal/core";

QUnit.module('ember-metal/core/main');

test('Ember registers itself', function() {
var lib = Ember.libraries._registry[0];

equal(lib.name, 'Ember');
equal(lib.version, Ember.VERSION);
});

0 comments on commit 4d37df5

Please sign in to comment.