Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Commit

Permalink
move default resolution logic into normalize, document symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Aug 18, 2016
1 parent f5becd2 commit 4ea0fbe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
10 changes: 6 additions & 4 deletions README.md
Expand Up @@ -57,14 +57,16 @@ class MyCustomLoader extends RegisterLoader {
/*
* Default normalize hook
*/
normalize(key, parentKey, metadata) {
[RegisterLoader.normalize](key, parentKey, metadata) {
// parent normalize is sync, providing relative normalization only
var relativeResolved = super[RegisterLoader.normalize](key, parentKey, metadata) || key;
return key;
}

/*
* Default instantiate hook
*/
instantiate(key, metadata) {
[RegisterLoader.instantiate](key, metadata) {
return undefined;
}
}
Expand Down Expand Up @@ -92,7 +94,7 @@ When instantiate returns `undefined`, it is assumed that the module key has alre
For example:

```javascript
instantate(key, metadata) {
[RegisterLoader.instantate](key, metadata) {
this.register(key, deps, declare);
return undefined;
}
Expand All @@ -102,7 +104,7 @@ When using the anonymous form of System.register - `loader.register(deps, declar
the context in which it was called, it is necessary to call the `loader.processRegisterContext(contextKey)` method:

```javascript
instantiate(key, metadata) {
[RegisterLoader.instantiate](key, metadata) {
this.register(deps, declare);
this.processRegisterContext(key);
return undefined;
Expand Down
1 change: 1 addition & 0 deletions core/loader-polyfill.js
Expand Up @@ -51,6 +51,7 @@ function Loader(baseKey) {
this.key = baseKey || baseURI;
this.registry = new Registry();

// NB deprecate
// evaluation flag to allow for tracing loaders
this.execute = true;
}
Expand Down
27 changes: 18 additions & 9 deletions core/register-loader.js
Expand Up @@ -43,9 +43,11 @@ RegisterLoader.prototype.constructor = RegisterLoader;
// aren't exposed to end-users
RegisterLoader.normalize = 'normalize';
RegisterLoader.instantiate = 'instantiate';
RegisterLoader.createMetadata = 'createMetadata';

// default normalize is the WhatWG style normalizer
RegisterLoader.prototype.normalize = function(key, parentKey, metadata) {
return key;
return resolveUrlToParentIfNotPlain(key, parentKey);
};

RegisterLoader.prototype.instantiate = function(key, metadata) {};
Expand All @@ -63,18 +65,25 @@ var RESOLVE = Loader.resolve;
RegisterLoader.prototype[RESOLVE] = function(key, parentKey) {
var loader = this;

var resolved = resolveUrlToParentIfNotPlain(key, parentKey);

// normalization shortpath if already in the registry or loading
if (resolved && (loader.registry.has(resolved) || loader._registerRegistry[resolved]))
return Promise.resolve(resolved);
if (key && (loader.registry.has(key) || loader._registerRegistry[key]))
return Promise.resolve(key);

var metadata = this.createMetadata();
return Promise.resolve(loader.normalize(resolved || key, parentKey, metadata))
return Promise.resolve(loader.normalize(key, parentKey, metadata))
.then(function(resolvedKey) {
if (typeof resolvedKey !== 'string') {
if (resolvedKey === undefined)
throw new RangeError('No resolution normalizing "' + key + '" to ' + parentKey);

// allow a non-string resolve for use cases like conditionals
return resolvedKey;
}

// we create the in-progress load record already here to store the normalization metadata
if (!loader.registry.has(resolvedKey))
getOrCreateLoadRecord(loader, resolvedKey).metadata = metadata;
getOrCreateLoadRecord(loader, resolvedKey, metadata);

return resolvedKey;
});
};
Expand All @@ -84,10 +93,10 @@ RegisterLoader.prototype[RESOLVE] = function(key, parentKey) {
// this record represents that waiting period, and when set, we then populate
// the esLinkRecord record into this load record.
// instantiate is a promise for a module namespace or undefined
function getOrCreateLoadRecord(loader, key) {
function getOrCreateLoadRecord(loader, key, metadata) {
return loader._registerRegistry[key] || (loader._registerRegistry[key] = {
key: key,
metadata: undefined,
metadata: metadata,

instantiatePromise: undefined,

Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/system-register-loader.js
Expand Up @@ -30,9 +30,10 @@ SystemRegisterLoader.prototype = Object.create(RegisterLoader.prototype);
// normalize is never given a relative name like "./x", that part is already handled
// so we just need to do plain name detect to throw as in the WhatWG spec
SystemRegisterLoader.prototype.normalize = function(key, parent, metadata) {
if (key.indexOf(':') === -1)
var resolved = RegisterLoader.prototype.normalize.call(this, key, parent, metadata);
if (!resolved)
throw new RangeError('System.register loader does not resolve plain module names, resolving "' + key + '" to ' + parent);
return key;
return resolved;
};

var fs;
Expand Down

0 comments on commit 4ea0fbe

Please sign in to comment.