diff options
author | bptato <nincsnevem662@gmail.com> | 2024-09-29 14:55:23 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-09-29 14:58:05 +0200 |
commit | 3b7ca109b9009d5875f8cc1bd8c8212fd9abc296 (patch) | |
tree | 52f707872c782c49fa442e7e7d989c7748d5821b /src/io | |
parent | 0035e58925ef005cca0ea0017ce3d54dfca1e72a (diff) | |
download | chawan-3b7ca109b9009d5875f8cc1bd8c8212fd9abc296.tar.gz |
promise: move PromiseMap to buffer
It's only used there, and there's no reason for every single promise to carry two pointers to support it.
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/promise.nim | 50 |
1 files changed, 5 insertions, 45 deletions
diff --git a/src/io/promise.nim b/src/io/promise.nim index 8fb55a9b..efb5a0a0 100644 --- a/src/io/promise.nim +++ b/src/io/promise.nim @@ -14,38 +14,14 @@ type 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 + res*: T proc newPromise*[T](): Promise[T] = return Promise[T]() -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](get: get, opaque: map.opaque) - map.tab[id] = promise - return promise - -proc addEmptyPromise*(map: var PromiseMap; id: int): EmptyPromise = - let promise = EmptyPromise(opaque: map.opaque) - map.tab[id] = promise - return promise - proc resolve*(promise: EmptyPromise) = var promise = promise while true: @@ -59,17 +35,9 @@ proc resolve*(promise: EmptyPromise) = promise.next = nil proc resolve*[T](promise: Promise[T]; res: T) = - 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() - proc newResolvedPromise*(): EmptyPromise = let res = EmptyPromise() res.resolve() @@ -80,9 +48,6 @@ proc newResolvedPromise*[T](x: T): Promise[T] = res.resolve(x) return res -func empty*(map: PromiseMap): bool = - map.tab.len == 0 - proc then*(promise: EmptyPromise; cb: (proc())): EmptyPromise {.discardable.} = promise.cb = cb promise.next = EmptyPromise() @@ -104,11 +69,7 @@ proc then*(promise: EmptyPromise; cb: (proc(): EmptyPromise)): EmptyPromise 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)) + return promise.then(proc() = cb(promise.res)) proc then*[T](promise: EmptyPromise; cb: (proc(): Promise[T])): Promise[T] {.discardable.} = @@ -189,8 +150,8 @@ proc all*(promises: seq[EmptyPromise]): EmptyPromise = res.resolve() return res -# * Promise is converted to a JS promise which will be resolved when the Nim -# promise is resolved. +# Promise is converted to a JS promise which will be resolved when the Nim +# promise is resolved. proc promiseThenCallback(ctx: JSContext; this_val: JSValue; argc: cint; argv: ptr UncheckedArray[JSValue]; magic: cint; @@ -279,8 +240,7 @@ proc toJS*[T, E](ctx: JSContext; promise: Promise[Result[T, E]]): JSValue = JS_UNDEFINED else: toJS(ctx, x.error) - let res = JS_Call(ctx, resolvingFuncs[1], JS_UNDEFINED, 1, - x.toJSValueArray()) + let res = JS_Call(ctx, resolvingFuncs[1], JS_UNDEFINED, 1, x.toJSValueArray()) JS_FreeValue(ctx, res) JS_FreeValue(ctx, x) JS_FreeValue(ctx, resolvingFuncs[0]) |