forked from emberjs/ember.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure Ember.libraries to be more idiomatic. Fixes emberjs#9291 …
…and fixes emberjs#9339
- Loading branch information
Showing
5 changed files
with
150 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |