Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for #113, Update to lscache #120

Merged
merged 2 commits into from May 30, 2012
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 26 additions & 25 deletions src/inject.coffee
Expand Up @@ -52,13 +52,13 @@ onErrorOffset = 0 # offset for onerror calls
funcCount = 0 # functions initialized to date
userConfig = {} # user configuration options (see reset)
undef = undef # undefined
schemaVersion = 1 # version of inject()'s localstorage schema
context = this # context is our local scope. Should be "window"
pauseRequired = false # can we run immediately? when using iframe transport, the answer is no
_db = {} # internal database of modules and transactions (see reset)
xDomainRpc = null # a cross domain RPC object (Porthole)
fileStorageToken = "FILEDB" # a storagetoken identifier we use (lscache)
fileStore = "Inject FileStorage" # file store to use
fileStorageToken = "INJECT" # a storagetoken identifier we use (lscache)
schemaVersion = 1 # the version of data storage schema for lscache
schemaVersionString = "!version" # the schema version string for validation of lscache schema
namespace = "Inject" # the namespace for inject() that is publicly reachable
userModules = {} # any mappings for module => handling defined by the user
fileSuffix = /.*?\.(js|txt)(\?.*)?$/# Regex for identifying things that end in *.js or *.txt
Expand All @@ -83,6 +83,18 @@ requireGreedyCapture = /require.*/
commentRegex = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg
relativePathRegex = /^(.\/|..\/).*/

###
lscache configuration
sets up lscache to operate within the local scope
###
lscache.setBucket(fileStorageToken)
lscacheSchemaVersion = lscache.get(schemaVersionString)

if lscacheSchemaVersion && lscacheSchemaVersion > 0 && lscacheSchemaVersion < schemaVersion
lscache.flush()
lscacheSchemaVersion = 0
if !lscacheSchemaVersion then lscache.set(schemaVersionString, schemaVersion)

###
CommonJS wrappers for a header and footer
these bookend the included code and insulate the scope so that it doesn't impact inject()
Expand Down Expand Up @@ -273,12 +285,11 @@ db = {
###
registry = _db.moduleRegistry
path = db.module.getPath(moduleId)
token = "#{fileStorageToken}#{schemaVersion}#{path}"
if registry[moduleId]?.file then return registry[moduleId].file

if userConfig.fileExpires is 0 then return false

file = lscache.get(token)
file = lscache.get(path)
if file and typeof(file) is "string" and file.length
db.module.setFile(moduleId, file)
return file
Expand All @@ -292,8 +303,7 @@ db = {
db.module.create(moduleId)
registry[moduleId].file = file
path = db.module.getPath(moduleId)
token = "#{fileStorageToken}#{schemaVersion}#{path}"
lscache.set(token, file, userConfig.fileExpires)
lscache.set(path, file, userConfig.fileExpires)
"clearAllFiles": () ->
###
## clearAllFiles() ##
Expand Down Expand Up @@ -614,26 +624,17 @@ reset = () ->
reset()


clearFileRegistry = (version = schemaVersion) ->
clearFileRegistry = () ->
###
## clearFileRegistry(version = schemaVersion) ##
_internal_ Clears the internal file registry at `version`
clearing all local storage keys that relate to the fileStorageToken and version
## clearFileRegistry() ##
_internal_ Clears the internal file registry
clearing all local storage keys that relate to the fileStorageToken
###

if ! ('localStorage' in context) then return

token = "#{fileStorageToken}#{version}"
`
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
if (key.indexOf(token) !== -1) {
localStorage.removeItem(key)
}
}
`

if version is schemaVersion then db.module.clearAllFiles()
db.module.clearAllFiles()
lscache.flush()

createIframe = () ->
###
Expand Down Expand Up @@ -1282,12 +1283,12 @@ require.setCrossDomain = (local, remote) ->
userConfig.xd.inject = local
userConfig.xd.xhr = remote

require.clearCache = (version) ->
require.clearCache = () ->
###
## require.clearCache(version) ##
## require.clearCache() ##
Remove the localStorage class at version. If no version is specified, the entire cache is cleared.
###
clearFileRegistry(version)
clearFileRegistry()

require.manifest = (manifest) ->
###
Expand Down