exports.Actor = class extends require('../actor').Actor

  @build: (freakset, scope, opts, fn) ->
    [freakset, scope, opts, fn] = @::ensureArgs(arguments...)

    if typeof opts == "function"
      fn = opts
      opts = undefined

    if freakset.runInParallel?
      freakset.parallelStepCount ||= 0
      freakset.parallelStepCount += 1

    freakset.modifyStack freakset, { type: "step", scope: scope, fn: fn }, opts

  @run: (freakset, actor) ->
    # Run the actor in the context of this
    # Note to myself: We cannot automatically #commit after running the fn because
    # if the step contains running more closures it may not be finished at this point.
    actor.fn(freakset)

    # Experimental: If the step shall be run in parallel, just #commit() to start the next step..
    if freakset.runInParallel?
      freakset.next()
