Module: Jade::Task

Included in:
AndThen, Decoded, Dispatch, Literal, Map, MapError, OnError, Sequence
Defined in:
lib/jade/task.rb,
lib/jade/tasks.rb

Defined Under Namespace

Classes: AndThen, Decoded, Dispatch, Literal, Map, MapError, OnError, Sequence

Instance Method Summary collapse

Instance Method Details

#runObject

Every node is a value describing work, and run drives them from one loop with an explicit stack of continuations. Composing tasks therefore costs heap, not Ruby stack, so a chain built by recursion (a batch loop, a retry) is bounded by memory rather than by SystemStackError.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jade/task.rb', line 9

def run
  task = self
  pending = []

  loop do
    case task
    in AndThen[inner, fn]
      task = inner
      pending << [:ok, fn]

    in OnError[inner, fn]
      task = inner
      pending << [:err, fn]

    in Map[inner, fn]
      task = inner
      pending << [:map, fn]

    in MapError[inner, fn]
      task = inner
      pending << [:map_err, fn]

    in Decoded[inner, ok_decoder, err_decoder]
      task = inner
      pending << [:decode, [ok_decoder, err_decoder]]

    else
      result = task.step
      return result if pending.empty?

      task = resume(result, pending)
      return task if task.is_a?(Jade::Result::Ok) || task.is_a?(Jade::Result::Err)
    end
  end
end