Navigation Menu

Skip to content

Commit

Permalink
Fix a bug for multi load a same non-module file. fix #247
Browse files Browse the repository at this point in the history
  • Loading branch information
lifesinger committed Jul 3, 2012
1 parent cb39d43 commit 3ad2e9c
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 63 deletions.
10 changes: 10 additions & 0 deletions dist/plugin-reload.js
@@ -0,0 +1,10 @@
define('seajs/plugin-reload', ['socketio'], function(require) {
var io = require('socketio')

// decide the server port
var socket = io.connect('http://localhost:8964')
socket.on('reload', function(data) {
// TODO refresh the give path
location.reload()
})
});
49 changes: 30 additions & 19 deletions dist/sea-debug.js
Expand Up @@ -708,9 +708,10 @@ seajs._config = {

var STATUS = {
'FETCHING': 1, // The module file is fetching now.
'SAVED': 2, // The module file has been fetched and info has been saved.
'LOADED': 3, // All dependencies are loaded.
'COMPILED': 4 // The module.exports is available.
'FETCHED': 2, // The module file has been fetched.
'SAVED': 3, // The module info has been saved.
'READY': 4, // All dependencies and self are ready to compile.
'COMPILED': 5 // The module.exports is available.
}


Expand All @@ -732,8 +733,7 @@ seajs._config = {

this._load(uris, function() {
var args = util.map(uris, function(uri) {
var module = cachedModules[uri]
return module ? module._compile() : null
return cachedModules[uri]._compile()
})

if (callback) {
Expand All @@ -746,25 +746,25 @@ seajs._config = {
Module.prototype._load = function(uris, callback) {
var unLoadedUris = util.filter(uris, function(uri) {
return uri && (!cachedModules[uri] ||
cachedModules[uri].status < STATUS.LOADED)
cachedModules[uri].status < STATUS.READY)
})

if (unLoadedUris.length === 0) {
var length = unLoadedUris.length
if (length === 0) {
callback()
return
}

var length = unLoadedUris.length
var remain = length

for (var i = 0; i < length; i++) {
(function(uri) {
var module = cachedModules[uri] ||
(cachedModules[uri] = new Module(uri, STATUS.FETCHING))

module.status === STATUS.SAVED ? onSaved() : fetch(uri, onSaved)
module.status >= STATUS.FETCHED ? onFetched() : fetch(uri, onFetched)

function onSaved() {
function onFetched() {
// cachedModules[uri] is changed in un-correspondence case
module = cachedModules[uri]

Expand All @@ -780,7 +780,8 @@ seajs._config = {
cb(module)
}
}
// Maybe failed to fetch successfully, such as 404 error.
// Maybe failed to fetch successfully, such as 404 or non-module.
// In these cases, module.status stay at FETCHING or FETCHED.
else {
cb()
}
Expand All @@ -790,28 +791,32 @@ seajs._config = {
}

function cb(module) {
module && (module.status = STATUS.LOADED)
module && (module.status = STATUS.READY)
--remain === 0 && callback()
}
}


Module.prototype._compile = function() {
var module = this
if (module.exports) {
if (module.status === STATUS.COMPILED) {
return module.exports
}

// Just return null when:
// 1. the module file is 404.
// 2. the module file is not written with valid module format.
// 3. other error cases.
if (module.status < STATUS.READY) {
return null
}

function require(id) {
var uri = resolve(id, module.uri)
var child = cachedModules[uri]

// Just return null when:
// 1. the module file is 404.
// 2. the module file is not written with valid module format.
// 3. other error cases.
if (!child || child.status < STATUS.LOADED) {
// Just return null when uri is invalid.
if (!child) {
return null
}

Expand Down Expand Up @@ -1008,6 +1013,12 @@ seajs._config = {
function() {
fetchedList[requestUri] = true

// Updates module status
var module = cachedModules[uri]
if (module.status === STATUS.FETCHING) {
module.status = STATUS.FETCHED
}

// Saves anonymous module meta data
if (anonymousModuleMeta) {
save(uri, anonymousModuleMeta)
Expand All @@ -1017,7 +1028,7 @@ seajs._config = {
// Assigns the first module in package to cachedModules[uri]
// See: test/issues/un-correspondence
var firstModule = currentPackageModules[0]
if (firstModule && cachedModules[uri].status === STATUS.FETCHING) {
if (firstModule && module.status === STATUS.FETCHED) {
cachedModules[uri] = firstModule
}
currentPackageModules = []
Expand Down
45 changes: 23 additions & 22 deletions dist/sea.js

Large diffs are not rendered by default.

49 changes: 30 additions & 19 deletions src/module.js
Expand Up @@ -9,9 +9,10 @@

var STATUS = {
'FETCHING': 1, // The module file is fetching now.
'SAVED': 2, // The module file has been fetched and info has been saved.
'LOADED': 3, // All dependencies are loaded.
'COMPILED': 4 // The module.exports is available.
'FETCHED': 2, // The module file has been fetched.
'SAVED': 3, // The module info has been saved.
'READY': 4, // All dependencies and self are ready to compile.
'COMPILED': 5 // The module.exports is available.
}


Expand All @@ -33,8 +34,7 @@

this._load(uris, function() {
var args = util.map(uris, function(uri) {
var module = cachedModules[uri]
return module ? module._compile() : null
return cachedModules[uri]._compile()
})

if (callback) {
Expand All @@ -47,25 +47,25 @@
Module.prototype._load = function(uris, callback) {
var unLoadedUris = util.filter(uris, function(uri) {
return uri && (!cachedModules[uri] ||
cachedModules[uri].status < STATUS.LOADED)
cachedModules[uri].status < STATUS.READY)
})

if (unLoadedUris.length === 0) {
var length = unLoadedUris.length
if (length === 0) {
callback()
return
}

var length = unLoadedUris.length
var remain = length

for (var i = 0; i < length; i++) {
(function(uri) {
var module = cachedModules[uri] ||
(cachedModules[uri] = new Module(uri, STATUS.FETCHING))

module.status === STATUS.SAVED ? onSaved() : fetch(uri, onSaved)
module.status >= STATUS.FETCHED ? onFetched() : fetch(uri, onFetched)

function onSaved() {
function onFetched() {
// cachedModules[uri] is changed in un-correspondence case
module = cachedModules[uri]

Expand All @@ -81,7 +81,8 @@
cb(module)
}
}
// Maybe failed to fetch successfully, such as 404 error.
// Maybe failed to fetch successfully, such as 404 or non-module.
// In these cases, module.status stay at FETCHING or FETCHED.
else {
cb()
}
Expand All @@ -91,28 +92,32 @@
}

function cb(module) {
module && (module.status = STATUS.LOADED)
module && (module.status = STATUS.READY)
--remain === 0 && callback()
}
}


Module.prototype._compile = function() {
var module = this
if (module.exports) {
if (module.status === STATUS.COMPILED) {
return module.exports
}

// Just return null when:
// 1. the module file is 404.
// 2. the module file is not written with valid module format.
// 3. other error cases.
if (module.status < STATUS.READY) {
return null
}

function require(id) {
var uri = resolve(id, module.uri)
var child = cachedModules[uri]

// Just return null when:
// 1. the module file is 404.
// 2. the module file is not written with valid module format.
// 3. other error cases.
if (!child || child.status < STATUS.LOADED) {
// Just return null when uri is invalid.
if (!child) {
return null
}

Expand Down Expand Up @@ -309,6 +314,12 @@
function() {
fetchedList[requestUri] = true

// Updates module status
var module = cachedModules[uri]
if (module.status === STATUS.FETCHING) {
module.status = STATUS.FETCHED
}

// Saves anonymous module meta data
if (anonymousModuleMeta) {
save(uri, anonymousModuleMeta)
Expand All @@ -318,7 +329,7 @@
// Assigns the first module in package to cachedModules[uri]
// See: test/issues/un-correspondence
var firstModule = currentPackageModules[0]
if (firstModule && cachedModules[uri].status === STATUS.FETCHING) {
if (firstModule && module.status === STATUS.FETCHED) {
cachedModules[uri] = firstModule
}
currentPackageModules = []
Expand Down
5 changes: 3 additions & 2 deletions test/config.js
Expand Up @@ -20,15 +20,15 @@
,'modules/config-map'
,'modules/cyclic'
,'modules/define'
,'modules/exact-exports'
//,'modules/exact-exports'
,'modules/exports'
,'modules/extend'
,'modules/hasOwnProperty'
,'modules/load'
,'modules/math'
,'modules/method'
,'modules/missing'
,'modules/monkeys'
//,'modules/monkeys'
,'modules/nested'
,'modules/preload'
,'modules/relative'
Expand Down Expand Up @@ -64,6 +64,7 @@
,'issues/multi-map'
,'issues/multi-preload'
,'issues/multi-use'
,'issues/multi-use-same'
,'issues/multi-use-tpl'
,'issues/multi-versions'
,'issues/no-conflict'
Expand Down
1 change: 1 addition & 0 deletions test/issues/multi-use-same/a.css
@@ -0,0 +1 @@
html { background: #F4F4F4 }
1 change: 1 addition & 0 deletions test/issues/multi-use-same/a.js
@@ -0,0 +1 @@
define({})
22 changes: 22 additions & 0 deletions test/issues/multi-use-same/main.js
@@ -0,0 +1,22 @@
define(function(require) {

var test = require('../../test')

require.async('./a.css')
require.async('./a.css')
require.async('./a.css')
require.async('./a.css')
require.async('./a.css')
require.async('./a.css')

require.async('./a.js')
require.async('./a.js')
require.async('./a.js')
require.async('./a.js')
require.async('./a.js')
require.async('./a.js')

test.assert(true, 'Multi use of css files is ok')
test.done()

})
28 changes: 28 additions & 0 deletions test/issues/multi-use-same/test.html
@@ -0,0 +1,28 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
<title>Issue Test</title>
</head>

<body>
<p><a href="https://github.com/seajs/seajs/issues/100">#100</a></p>
<div id="out"></div>

<script src="../../../dist/sea-debug.js"></script>
<script>

seajs.use('./main')


function printResults(txt, style) {
var d = document.createElement('div');
d.innerHTML = txt;
d.className = style;
document.getElementById('out').appendChild(d);
}

</script>
</body>
</html>
2 changes: 1 addition & 1 deletion test/issues/public-api/a.js
Expand Up @@ -64,7 +64,7 @@ define(function(require, exports, module) {
test.assert(util.isObject(module.exports), 'module.exports')
test.assert(module.parent instanceof Module, 'module.parent')
test.assert(module.parent.parent === undefined, 'module.parent.parent')
test.assert(module.status === 3, 'module.status')
test.assert(module.status === Module.STATUS.READY, 'module.status')
test.assert(util.isObject(module.exports), 'module.exports')
test.assert(util.isFunction(module.require), 'module.require')
test.assert(getOwnPropertyCount(module) === 8, getOwnPropertyCount(module))
Expand Down

0 comments on commit 3ad2e9c

Please sign in to comment.