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

Commit

Permalink
refactor instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jul 10, 2017
1 parent 095fb9d commit ebf9dda
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions core/register-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ function createLoadRecord (state, key, registration) {
// will be the array of dependency load record or a module namespace
dependencyInstantiations: undefined,

// indicates if the load and all its dependencies are instantiated and linked
// but not yet executed
// mostly just a performance shortpath to avoid rechecking the promises above
linked: false

// NB optimization and way of ensuring module objects in setters
// indicates setters which should run pre-execution of that dependency
// setters is then just for completely executed module objects
Expand Down Expand Up @@ -133,12 +128,9 @@ RegisterLoader.prototype[Loader.resolveInstantiate] = function (key, parentKey)
throw instantiated.evalError;
}

if (instantiated.linkRecord.linked)
return ensureEvaluate(loader, instantiated, instantiated.linkRecord, registry, state, undefined);

return instantiateDeps(loader, instantiated, instantiated.linkRecord, registry, state, [instantiated])
return deepInstantiateDeps(loader, instantiated, link, registry, state)
.then(function () {
return ensureEvaluate(loader, instantiated, instantiated.linkRecord, registry, state, undefined);
return ensureEvaluate(loader, instantiated, link, registry, state, undefined);
});
});
};
Expand Down Expand Up @@ -244,13 +236,6 @@ function instantiate (loader, load, link, registry, state) {
registerDeclarative(loader, load, link, registration[1]);
}

// shortpath to instantiateDeps
if (!link.dependencies.length) {
link.linked = true;
if (loader.trace)
traceLoad(loader, load, link);
}

return load;
})
.catch(function (err) {
Expand Down Expand Up @@ -372,16 +357,16 @@ function registerDeclarative (loader, load, link, declare) {
}
}

function instantiateDeps (loader, load, link, registry, state, seen) {
return (link.depsInstantiatePromise || (link.depsInstantiatePromise = Promise.resolve()
.then(function () {
var depsInstantiatePromises = Array(link.dependencies.length);
function instantiateDeps (loader, load, link, registry, state) {
if (link.depsInstantiatePromise)
return link.depsInstantiatePromise;

for (var i = 0; i < link.dependencies.length; i++)
depsInstantiatePromises[i] = resolveInstantiateDep(loader, link.dependencies[i], load.key, registry, state, loader.trace && link.depMap || (link.depMap = {}));
var depsInstantiatePromises = Array(link.dependencies.length);

return Promise.all(depsInstantiatePromises);
})
for (var i = 0; i < link.dependencies.length; i++)
depsInstantiatePromises[i] = resolveInstantiateDep(loader, link.dependencies[i], load.key, registry, state, loader.trace && link.depMap || (link.depMap = {}));

var depsInstantiatePromise = Promise.all(depsInstantiatePromises)
.then(function (dependencyInstantiations) {
link.dependencyInstantiations = dependencyInstantiations;

Expand All @@ -406,43 +391,58 @@ function instantiateDeps (loader, load, link, registry, state, seen) {
}
}
}
})))
.then(function () {
// now deeply instantiateDeps on each dependencyInstantiation that is a load record
var deepDepsInstantiatePromises = [];

for (var i = 0; i < link.dependencies.length; i++) {
var depLoad = link.dependencyInstantiations[i];
var depLink = depLoad.linkRecord;

if (!depLink || depLink.linked)
continue;

if (seen.indexOf(depLoad) !== -1) {
deepDepsInstantiatePromises.push(depLink.depsInstantiatePromise);
continue;
}
seen.push(depLoad);

deepDepsInstantiatePromises.push(instantiateDeps(loader, depLoad, depLoad.linkRecord, registry, state, seen));
}
return load;
});

return Promise.all(deepDepsInstantiatePromises);
})
.then(function () {
// as soon as all dependencies instantiated, we are ready for evaluation so can add to the registry
// this can run multiple times, but so what
link.linked = true;
if (loader.trace)
if (loader.trace)
depsInstantiatePromise = depsInstantiatePromise.then(function () {
traceLoad(loader, load, link);
return load;
});

return load;
})
.catch(function (err) {
depsInstantiatePromise = depsInstantiatePromise.catch(function (err) {
// throw up the instantiateDeps stack
link.depsInstantiatePromise = undefined;
throw addToError(err, 'Loading ' + load.key);
});

depsInstantiatePromise.catch(function () {});

return link.depsInstantiatePromise = depsInstantiatePromise;
}

function deepInstantiateDeps (loader, load, link, registry, state) {
return new Promise(function (resolve, reject) {
var seen = [];
var loadCnt = 0;
function queueLoad (load) {
var link = load.linkRecord;
if (!link)
return;

if (seen.indexOf(load) !== -1)
return;
seen.push(load);

loadCnt++;
instantiateDeps(loader, load, link, registry, state)
.then(processLoad, reject);
}
function processLoad (load) {
loadCnt--;
var link = load.linkRecord;
if (link) {
for (var i = 0; i < link.dependencies.length; i++) {
var depLoad = link.dependencyInstantiations[i];
if (!(depLoad instanceof ModuleNamespace))
queueLoad(depLoad);
}
}
if (loadCnt === 0)
resolve();
}
queueLoad(load);
});
}

/*
Expand Down

0 comments on commit ebf9dda

Please sign in to comment.