about summary refs log tree commit diff stats
path: root/src/io/promise.nim
blob: 46beec6fab9f11a16107c4bff34c48f2abab8f51 (plain) (blame)
1
2
3
4
5
6
7
8
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import tables

type
  PromiseState = enum
    PROMISE_PENDING, PROMISE_FULFILLED, PROMISE_REJECTED

  EmptyPromise* = ref object of RootObj
    cb: (proc())
    next: EmptyPromise
    opaque: pointer
    state: PromiseState

  Promise*[T] = ref object of EmptyPromise
    res: T
    get: GetValueProc[T]

  GetValueProc[T] = (proc(opaque: pointer, res: var T))

  PromiseMap* = object
    tab: Table[int, EmptyPromise]
    opaque*: pointer

proc newPromiseMap*(opaque: pointer): PromiseMap =
  return PromiseMap(
    opaque: opaque
  )

proc addPromise*[T](map: var PromiseMap, id: int, get: GetValueProc[T]): Promise[T] =
  let promise = Promise[T](state: PROMISE_PENDING, get: get, opaque: map.opaque)
  map.tab[id] = promise
  return promise

proc addEmptyPromise*(map: var PromiseMap, id: int): EmptyPromise =
  let promise = EmptyPromise(state: PROMISE_PENDING, opaque: map.opaque)
  map.tab[id] = promise
  return promise

proc resolve*(promise: EmptyPromise) =
  var promise = promise
  while true:
    promise.state = PROMISE_FULFILLED
    if promise.cb != nil:
      promise.cb()
    promise = promise.next
    if promise == nil:
      break

proc resolve*[T](promise: Promise[T], res: T) =
  if promise.cb != nil:
    if promise.get != nil:
      promise.get(promise.opaque, promise.res)
      promise.get = nil
    promise.res = res
    promise.resolve()

proc resolve*(map: var PromiseMap, promiseid: int) =
  var promise: EmptyPromise
  if map.tab.pop(promiseid, promise):
    promise.resolve()

func empty*(map: PromiseMap): bool =
  map.tab.len == 0

proc then*(promise: EmptyPromise, cb: (proc())): EmptyPromise {.discardable.} =
  if promise == nil: return
  promise.cb = cb
  promise.next = EmptyPromise()
  return promise.next

proc then*[T](promise: Promise[T], cb: (proc(x: T))): EmptyPromise {.discardable.} =
  if promise == nil: return
  return promise.then(proc() =
    if promise.get != nil:
      promise.get(promise.opaque, promise.res)
    cb(promise.res))

proc then*[T](promise: EmptyPromise, cb: (proc(): Promise[T])): Promise[T] {.discardable.} =
  if promise == nil: return
  let next = Promise[T]()
  promise.then(proc() =
    let p2 = cb()
    if p2 != nil:
      p2.then(proc(x: T) =
        next.res = x
        next.resolve()))
  return next

proc then*[T](promise: Promise[T], cb: (proc(x: T): EmptyPromise)): EmptyPromise {.discardable.} =
  if promise == nil: return
  let next = EmptyPromise()
  promise.then(proc(x: T) =
    let p2 = cb(x)
    if p2 != nil:
      p2.then(proc() =
        next.resolve()))
  return next

proc then*[T, U](promise: Promise[T], cb: (proc(x: T): Promise[U])): Promise[U] {.discardable.} =
  if promise == nil: return
  let next = Promise[U]()
  promise.then(proc(x: T) =
    let p2 = cb(x)
    if p2 != nil:
      p2.then(proc(y: U) =
        next.res = y
        next.resolve()))
  return next