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

Add a new preserveExecutionOrder option #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 15 additions & 8 deletions dist/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@
})
}

function $script(paths, idOrDone, optDone) {
function $script(paths, maybeId, maybeOptions, maybeDone) {
paths = paths[push] ? paths : [paths]
var idOrDoneIsDone = idOrDone && idOrDone.call
, done = idOrDoneIsDone ? idOrDone : optDone
, id = idOrDoneIsDone ? paths.join('') : idOrDone
var maybeIdIsDone = maybeId && maybeId.call
, maybeIdIsOptions = !maybeIdIsDone && typeof maybeId === 'object'
, maybeOptionsIsDone = !maybeIdIsDone && maybeOptions && maybeOptions.call
, done = maybeIdIsDone ? maybeId : (maybeOptionsIsDone ? maybeOptions : maybeDone)
, id = maybeIdIsDone || maybeIdIsOptions ? paths.join('') : maybeId
, options = maybeIdIsOptions ? maybeId : (maybeOptionsIsDone ? null : maybeOptions)
, queue = paths.length
function loopFn(item) {
return item.call ? item() : list[item]
Expand Down Expand Up @@ -67,13 +70,13 @@

scripts[path] = 1
if (id) ids[id] = 1
create(path, callback)
create(path, callback, options)
})
}, 0)
return $script
}

function create(path, fn) {
function create(path, fn, options) {
var el = doc.createElement('script'), loaded
el.onload = el.onerror = el[onreadystatechange] = function () {
if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return;
Expand All @@ -82,20 +85,24 @@
scripts[path] = 2
fn()
}
el.async = 1
el.async = !options || !options.preserveExecutionOrder
el.src = urlArgs ? path + (path.indexOf('?') === -1 ? '?' : '&') + urlArgs : path;
head.insertBefore(el, head.lastChild)
}

$script.get = create

$script.order = function (scripts, id, done) {
$script.order = $script.serial = function (scripts, id, done) {
(function callback(s) {
s = scripts.shift()
!scripts.length ? $script(s, id, done) : $script(s, callback)
}())
}

$script.parallel = function (scripts, id, done) {
return $script(scripts, id, { preserveExecutionOrder: true }, done)
}

$script.path = function (p) {
scriptpath = p
}
Expand Down
2 changes: 1 addition & 1 deletion dist/script.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
})
}

function $script(paths, idOrDone, optDone) {
function $script(paths, maybeId, maybeOptions, maybeDone) {
paths = paths[push] ? paths : [paths]
var idOrDoneIsDone = idOrDone && idOrDone.call
, done = idOrDoneIsDone ? idOrDone : optDone
, id = idOrDoneIsDone ? paths.join('') : idOrDone
var maybeIdIsDone = maybeId && maybeId.call
, maybeIdIsOptions = !maybeIdIsDone && typeof maybeId === 'object'
, maybeOptionsIsDone = !maybeIdIsDone && maybeOptions && maybeOptions.call
, done = maybeIdIsDone ? maybeId : (maybeOptionsIsDone ? maybeOptions : maybeDone)
, id = maybeIdIsDone || maybeIdIsOptions ? paths.join('') : maybeId
, options = maybeIdIsOptions ? maybeId : (maybeOptionsIsDone ? null : maybeOptions)
, queue = paths.length
function loopFn(item) {
return item.call ? item() : list[item]
Expand Down Expand Up @@ -61,13 +64,13 @@

scripts[path] = 1
if (id) ids[id] = 1
create(path, callback)
create(path, callback, options)
})
}, 0)
return $script
}

function create(path, fn) {
function create(path, fn, options) {
var el = doc.createElement('script'), loaded
el.onload = el.onerror = el[onreadystatechange] = function () {
if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return;
Expand All @@ -76,20 +79,24 @@
scripts[path] = 2
fn()
}
el.async = 1
el.async = !options || !options.preserveExecutionOrder
el.src = urlArgs ? path + (path.indexOf('?') === -1 ? '?' : '&') + urlArgs : path;
head.insertBefore(el, head.lastChild)
}

$script.get = create

$script.order = function (scripts, id, done) {
$script.order = $script.serial = function (scripts, id, done) {
(function callback(s) {
s = scripts.shift()
!scripts.length ? $script(s, id, done) : $script(s, callback)
}())
}

$script.parallel = function (scripts, id, done) {
return $script(scripts, id, { preserveExecutionOrder: true }, done)
}

$script.path = function (p) {
scriptpath = p
}
Expand Down