diff options
author | andy finch <andyfinch7@gmail.com> | 2019-06-13 23:43:54 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-06-13 20:43:54 -0700 |
commit | dc60fe9f300043f191286ef804a365e16e455f87 (patch) | |
tree | c6b74e9faa6f26745b8770a18d0ae46ee34f3774 /js/dispatch_minimal.ts | |
parent | fdd2eb538327ee3f50fe2869320411191830c985 (diff) |
Refactor dispatch handling (#2452)
Promise id is now created in core and passed back to JS.
Diffstat (limited to 'js/dispatch_minimal.ts')
-rw-r--r-- | js/dispatch_minimal.ts | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/js/dispatch_minimal.ts b/js/dispatch_minimal.ts index 17d328110..bf9065f56 100644 --- a/js/dispatch_minimal.ts +++ b/js/dispatch_minimal.ts @@ -5,14 +5,8 @@ import { core } from "./core"; const DISPATCH_MINIMAL_TOKEN = 0xcafe; const promiseTableMin = new Map<number, util.Resolvable<number>>(); -let _nextPromiseId = 0; - -export function nextPromiseId(): number { - return _nextPromiseId++; -} export interface RecordMinimal { - promiseId: number; opId: number; arg: number; result: number; @@ -28,10 +22,9 @@ export function hasMinimalToken(i32: Int32Array): boolean { export function recordFromBufMinimal(buf32: Int32Array): null | RecordMinimal { if (hasMinimalToken(buf32)) { return { - promiseId: buf32[1], - opId: buf32[2], - arg: buf32[3], - result: buf32[4] + opId: buf32[1], + arg: buf32[2], + result: buf32[3] }; } return null; @@ -46,12 +39,13 @@ const scratchBytes = new Uint8Array( util.assert(scratchBytes.byteLength === scratch32.length * 4); export function handleAsyncMsgFromRustMinimal( + promiseId: number, ui8: Uint8Array, record: RecordMinimal ): void { // Fast and new util.log("minimal handleAsyncMsgFromRust ", ui8.length); - const { promiseId, result } = record; + const { result } = record; const promise = promiseTableMin.get(promiseId); promiseTableMin.delete(promiseId); promise!.resolve(result); @@ -62,16 +56,16 @@ export function sendAsyncMinimal( arg: number, zeroCopy: Uint8Array ): Promise<number> { - const promiseId = nextPromiseId(); // AKA cmdId - scratch32[0] = DISPATCH_MINIMAL_TOKEN; - scratch32[1] = promiseId; - scratch32[2] = opId; - scratch32[3] = arg; + scratch32[1] = opId; + scratch32[2] = arg; + + const promiseId = core.dispatch(scratchBytes, zeroCopy); + + util.assert(typeof promiseId == "number"); const promise = util.createResolvable<number>(); - promiseTableMin.set(promiseId, promise); + promiseTableMin.set(promiseId as number, promise); - core.dispatch(scratchBytes, zeroCopy); return promise; } |