diff options
author | bptato <nincsnevem662@gmail.com> | 2023-01-04 03:15:25 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-01-04 21:30:39 +0100 |
commit | 0edb5d3230730990d922328822e19833d807a040 (patch) | |
tree | b3971f17b7a60afa5f968f59161824a8164ea9e0 /src/io | |
parent | b35ec1b3e5c78c7939a85ebef17e2abde4a673ad (diff) | |
download | chawan-0edb5d3230730990d922328822e19833d807a040.tar.gz |
promise: set get, cb, next to nil after then is called
Actually we don't even need a state variable, just set cb to nil when resolving. (I'm sure this is a great idea that will absolutely not backfire in the future.)
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/promise.nim | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/src/io/promise.nim b/src/io/promise.nim index 46beec6f..46825244 100644 --- a/src/io/promise.nim +++ b/src/io/promise.nim @@ -1,14 +1,10 @@ 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 @@ -26,24 +22,25 @@ proc newPromiseMap*(opaque: pointer): PromiseMap = ) 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) + let promise = Promise[T](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) + let promise = EmptyPromise(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.cb = nil promise = promise.next if promise == nil: break + promise.next = nil proc resolve*[T](promise: Promise[T], res: T) = if promise.cb != nil: @@ -72,6 +69,7 @@ proc then*[T](promise: Promise[T], cb: (proc(x: T))): EmptyPromise {.discardable return promise.then(proc() = if promise.get != nil: promise.get(promise.opaque, promise.res) + promise.get = nil cb(promise.res)) proc then*[T](promise: EmptyPromise, cb: (proc(): Promise[T])): Promise[T] {.discardable.} = |