Skip to content

Commit

Permalink
feat(core): new end option in scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Mar 16, 2021
1 parent da0b027 commit 8ae894c
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 30 deletions.
3 changes: 0 additions & 3 deletions packages/core/lib/actions/fs/base/stat.js

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

2 changes: 1 addition & 1 deletion packages/core/lib/actions/fs/copy.js

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

29 changes: 26 additions & 3 deletions packages/core/lib/actions/fs/glob.js

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

36 changes: 29 additions & 7 deletions packages/core/lib/schedulers/native.js

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

21 changes: 14 additions & 7 deletions packages/core/lib/session.js

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

17 changes: 12 additions & 5 deletions packages/core/src/schedulers/native.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ module.exports = (tasks, options = {}) ->
# handler pushed or unshifted are not.
# It is not possible to register managed handler once the scheduler has
# resolved.
options.managed ?= false
options.managed ?= !!tasks
options.parallel ?= 1
options.parallel = 1 if options.parallel is false
options.parallel = -1 if options.parallel is true
options.end ?= true
state =
stack: []
pause: if options.pause? then !!options.pause else false
Expand All @@ -39,20 +40,26 @@ module.exports = (tasks, options = {}) ->
promise = new Promise (resolve, reject) ->
scheduler =
state: state
end: (end) ->
options.end = end
scheduler.pump()
pump: ->
return if state.pause
return if state.running is options.parallel
unless state.managed.resolved
if not state.managed.resolved
if state.managed.error
state.managed.resolved = true
# Any pending managed task is stripped out after an error
clear_managed_tasks()
scheduler.pump()
return reject state.managed.error
else if count_pending_tasks() + state.managed.running is 0
else if options.managed and options.end and count_pending_tasks() + state.managed.running is 0
state.managed.resolved = true
scheduler.pump()
return resolve state.output
else if not options.managed and options.end and state.stack.length is 0
state.managed.resolved = true
return resolve()
return unless state.stack.length
task = state.stack.shift()
if options.strict is true and not task.managed and state.error
Expand Down Expand Up @@ -94,8 +101,8 @@ module.exports = (tasks, options = {}) ->
new Promise (resolve, reject) ->
unless isArray
state.pending++
tasks.managed ?= options.managed
state.stack.unshift {
...options
...tasks
resolve: resolve
reject: reject
Expand All @@ -119,8 +126,8 @@ module.exports = (tasks, options = {}) ->
prom = new Promise (resolve, reject) ->
unless isArray
state.pending++
tasks.managed ?= options.managed
state.stack.push {
...options
...tasks
resolve: resolve
reject: reject
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/session.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ session = (args, options={}) ->
args: name: name, action: act
# Local scheduler to execute children and be notified on finish
schedulers =
in: schedule null, action.scheduler
in: schedule null, {...action.scheduler, end: false}
out: schedule null, {...action.scheduler, pause: true}
# Start with a paused scheduler to register actions out of the handler
action.scheduler = schedulers.out
Expand Down Expand Up @@ -108,8 +108,9 @@ session = (args, options={}) ->
pump = ->
# Now that the handler has been executed,
# import all the actions registered outside of it
action.scheduler.state.stack.push child while child = schedulers.out.state.stack.shift()
action.scheduler.pump()
while task = schedulers.out.state.stack.shift()
action.scheduler.state.stack.push task
action.scheduler.end(true)
output.then pump, pump
# Make sure the promise is resolved after the scheduler and its children
Promise.all [output, action.scheduler]
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/scheduler/creation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe 'scheduler.creation', ->
]
scheduler.push -> new Promise (accept) -> accept 3
scheduler
.should.be.resolvedWith []
.should.be.resolvedWith undefined

describe 'error', ->

Expand Down

0 comments on commit 8ae894c

Please sign in to comment.