Skip to content

Commit

Permalink
Add STATUS.LOADING and restore Module.save to private save function
Browse files Browse the repository at this point in the history
  • Loading branch information
lifesinger committed Jun 15, 2013
1 parent c744975 commit 0b340e1
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ var fetchedList = {}
var callbackList = {}

var STATUS = Module.STATUS = {
// 1 - The module file is being fetched now
// 1 - The `module.uri` is being fetched
FETCHING: 1,
// 2 - The module data has been saved to cachedMods
// 2 - The meta data has been saved to cachedMods
SAVED: 2,
// 3 - The module and all its dependencies are ready to execute
LOADED: 3,
// 3 - The `module.dependencies` are being loaded
LOADING: 3,
// 3 - The module are ready to execute
LOADED: 4,
// 4 - The module is being executed
EXECUTING: 4,
// 5 - The module is executed and `module.exports` is available
EXECUTED: 5
EXECUTING: 5,
// 5 - The `module.exports` is available
EXECUTED: 6
}


Expand All @@ -43,18 +45,6 @@ Module.get = function(uri, deps) {
return cachedMods[uri] || (cachedMods[uri] = new Module(uri, deps))
}

Module.save = function(uri, meta) {
var mod = Module.get(uri)

// Do NOT override already saved modules
if (mod.status < STATUS.SAVED) {
mod.id = meta.id || uri
mod.dependencies = meta.deps
mod.factory = meta.factory
mod.status = STATUS.SAVED
}
}

// Resolve module.dependencies
Module.prototype._resolve = function() {
var mod = this
Expand All @@ -77,9 +67,16 @@ Module.prototype._resolve = function() {
// Load module.dependencies and fire onload when all done
Module.prototype._load = function() {
var mod = this
var uris = mod._resolve()

// If the module is being loaded, just wait it onload call
if (mod.status >= STATUS.LOADING) {
return
}

mod.status = STATUS.LOADING

// Emit `load` event for plugins such as plugin-combo
var uris = mod._resolve()
emit("load", uris)

var len = mod._remain = uris.length
Expand Down Expand Up @@ -185,7 +182,7 @@ Module.prototype._fetch = function() {

// Save meta data of anonymous module
if (anonymousMeta) {
Module.save(uri, anonymousMeta)
save(uri, anonymousMeta)
anonymousMeta = undefined
}

Expand Down Expand Up @@ -286,7 +283,7 @@ function define(id, deps, factory) {
// Emit `define` event, used in plugin-nocache, seajs node version etc
emit("define", meta)

meta.uri ? Module.save(meta.uri, meta) :
meta.uri ? save(meta.uri, meta) :
// Save information for "saving" work in the script onload event
anonymousMeta = meta
}
Expand Down Expand Up @@ -325,6 +322,18 @@ function resolve(id, refUri) {
return emitData.uri || id2Uri(emitData.id, refUri)
}

function save(uri, meta) {
var mod = Module.get(uri)

// Do NOT override already saved modules
if (mod.status < STATUS.SAVED) {
mod.id = meta.id || uri
mod.dependencies = meta.deps
mod.factory = meta.factory
mod.status = STATUS.SAVED
}
}

function getExports(mod) {
var exports = mod._exec()

Expand Down Expand Up @@ -374,6 +383,6 @@ data.fetchedList = fetchedList

seajs.resolve = id2Uri
seajs.require = function(id) {
return Module.get(resolve(id)).exports
return (cachedMods[resolve(id)] || {}).exports
}

0 comments on commit 0b340e1

Please sign in to comment.