Skip to content
Merged
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
40 changes: 11 additions & 29 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,6 @@ declare const Packages: {} | undefined;
// a "program" to apply paths; see computeMapProg
let pathMapPrograms: DojoLoader.PathMap[] = [];

// hash: (mid | url)-->(function | string)
//
// Gives a set of cache modules pending entry into cache. When cached modules are published to the loader, they are
// entered into pendingCacheInsert; modules are then pressed into cache upon (1) AMD define or (2) upon receiving
// another independent set of cached modules. (1) is the usual case, and this case allows normalizing mids given
// in the pending cache for the local configuration, possibly relocating modules.
let pendingCacheInsert: { [moduleId: string]: any; } = {};

let setGlobals: (require: DojoLoader.RootRequire, define: DojoLoader.Define) => void;

let uidGenerator = 0;
Expand Down Expand Up @@ -399,20 +391,6 @@ declare const Packages: {} | undefined;
return <T> target;
}

function consumePendingCacheInsert(referenceModule?: DojoLoader.Module): void {
let item: any;

for (let key in pendingCacheInsert) {
item = pendingCacheInsert[key];

cache[
typeof item === 'string' ? toUrl(key, referenceModule) : getModuleInformation(key, referenceModule).mid
] = item;
}

pendingCacheInsert = {};
}

function noop(): void {};

let loadNodeModule: (moduleId: string, parent?: DojoLoader.Module) => any = noop;
Expand Down Expand Up @@ -844,9 +822,6 @@ declare const Packages: {} | undefined;
else if (module && !module.injected) {
let cached: DojoLoader.Factory;
const onLoadCallback = function (node?: HTMLScriptElement): void {
// DojoLoader.moduleDefinitionArguments is an array of [dependencies, factory]
consumePendingCacheInsert(module);

let moduleDefArgs: string[] = [];
let moduleDefFactory: DojoLoader.Factory | undefined = undefined;

Expand All @@ -867,7 +842,7 @@ declare const Packages: {} | undefined;

++waitingCount;
module.injected = true;
if ((cached = cache[module.mid])) {
if (cached = cache[module.mid]) {
try {
cached();
onLoadCallback();
Expand Down Expand Up @@ -1122,9 +1097,16 @@ declare const Packages: {} | undefined;
toAbsMid: toAbsMid,
toUrl: toUrl,

cache: function (cache: DojoLoader.ObjectMap): void {
consumePendingCacheInsert();
pendingCacheInsert = cache;
cache: function (cacheModules: DojoLoader.ObjectMap): void {
let item: any;

for (let key in cacheModules) {
item = cacheModules[key];

cache[
getModuleInformation(key, undefined).mid
] = item;
}
}
});

Expand Down
50 changes: 48 additions & 2 deletions tests/unit/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,26 @@ registerSuite({
});
},

'cache injected module is properly undefined'(this: any) {
'circular dependencies are required'(this: any) {
let dfd = this.async(DEFAULT_TIMEOUT);

global.require.config({
packages: [
{
name: 'recursive',
location: './_build/tests/common/recursive'
}
]
});

global.require([
'recursive/a'
], dfd.callback(function (a: any) {
assert.equal(a, 'a');
}));
},

'require.cache creates modules with only the cache call'(this: any) {
let dfd = this.async(DEFAULT_TIMEOUT);

global.require.config({
Expand All @@ -761,7 +780,34 @@ registerSuite({
}
});

global.require.cache({}); /* TODO: Remove when #124 resolve */
assert.doesNotThrow(() => {
global.require([
'common/app'
], dfd.callback((app: any) => {
assert.strictEqual(app, 'mock', 'should return cache factory value');
}));
});
},

'cache injected module is properly undefined'(this: any) {
let dfd = this.async(DEFAULT_TIMEOUT);

global.require.config({
packages: [
{
name: 'common',
location: './_build/tests/common'
}
]
});

global.require.cache({
'common/app'() {
define([], () => {
return 'mock';
});
}
});

global.require([
'common/app'
Expand Down