Skip to content

Commit

Permalink
Adding to cache as soon as require.cache is called instead of pending…
Browse files Browse the repository at this point in the history
… it for later, issue #124
  • Loading branch information
rorticus committed Apr 28, 2017
1 parent 1cb0fcf commit dde74b9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 23 deletions.
32 changes: 11 additions & 21 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,20 +399,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 +830,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 +850,7 @@ declare const Packages: {} | undefined;

++waitingCount;
module.injected = true;
if ((cached = cache[module.mid])) {
if ((cached = cache[module.mid]) || (module.mid in pendingCacheInsert)) {
try {
cached();
onLoadCallback();
Expand Down Expand Up @@ -1118,9 +1101,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[
typeof item === 'string' ? toUrl(key, undefined) : 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 @@ -709,7 +709,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 @@ -729,7 +748,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

0 comments on commit dde74b9

Please sign in to comment.