Skip to content

Commit 1ed9753

Browse files
committed
restore events to method that works with pong (events in a blocking wait are queued but not processed). But still investigating if it is possible to allow events inside a blocking wait.
1 parent 55739c2 commit 1ed9753

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

docs/gettingstarted/events.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Using events in C/C++ and WebAssembly with twr-wasm
3-
description: Certain twr-wasm APIs support events. This section describes how they function.
3+
description: Certain twr-wasm APIs support events. This section describes how events function.
44
---
55

66
# Overview of Events
@@ -55,7 +55,7 @@ You can use the same event/callback with multiple APIs if you wish. When the ev
5555
5656
# When using twrWasmModuleAsync
5757
58-
With a `twrWasmModuleAsync` module, various blocking APIs are available. For example: `twr_sleep`. When these functions are blocking (waiting), event callbacks are still processed. For example, in the `tests-timer` example, you can see the repeating timer callback happen while the function sleeps.
58+
With a `twrWasmModuleAsync` module, various blocking APIs are available. For example: `twr_sleep`. When these functions are blocking (waiting), event callbacks are queued and not processed until wait unblocks.
5959
6060
With a `twrWasmModuleAsync` module, events are sent from the JavaScript main thread to the worker thread that is running the C/C++ code.
6161

source/twr-ts/twreventqueue.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,8 @@ export class twrEventQueueReceive {
5050
if (!(eventID in twrEventQueueReceive.onEventCallbacks))
5151
throw new Error("internal error");
5252

53-
const onEventCallback=twrEventQueueReceive.onEventCallbacks[eventID];
54-
if (onEventCallback) {
55-
onEventCallback(eventID, ...args);
56-
}
57-
else { // events with not callback must be read, eg with waitEvent
58-
this.pendingEventIDs.push(eventID);
59-
this.pendingEventArgs.push(args);
60-
}
53+
this.pendingEventIDs.push(eventID);
54+
this.pendingEventArgs.push(args);
6155
}
6256

6357
private readMallocRemainder() {
@@ -89,17 +83,17 @@ export class twrEventQueueReceive {
8983
this.readCommandRemainder(firstValue);
9084
}
9185

92-
private findEvent(filterEvent:number) : [undefined | number, undefined | number[]] {
86+
private findEvent(filterEvent:number) : [undefined | number, undefined | number[], undefined | number] {
9387

9488
if (filterEvent===undefined) {
95-
return [this.pendingEventIDs.shift(), this.pendingEventArgs.shift()]
89+
return [this.pendingEventIDs.shift(), this.pendingEventArgs.shift(), 0]
9690
}
9791

9892
const index=this.pendingEventIDs.indexOf(filterEvent);
9993
if (index!=-1)
100-
return [this.pendingEventIDs.splice(index, 1)[0], this.pendingEventArgs.splice(index, 1)[0]]
94+
return [this.pendingEventIDs.splice(index, 1)[0], this.pendingEventArgs.splice(index, 1)[0], index];
10195

102-
return [undefined, undefined];
96+
return [undefined, undefined, undefined];
10397
}
10498

10599

@@ -110,19 +104,23 @@ export class twrEventQueueReceive {
110104
this.readCommand();
111105

112106
// is our event in the queue?
113-
const [eventID, args]=this.findEvent(filterEvent);
114-
if (eventID && args) return [eventID, args];
107+
const [eventID, args, index]=this.findEvent(filterEvent);
108+
// execute callbacks up to this filterEvent (so as to call them in order)
109+
// if filterEvent not found, index is undefined, which causes doCallbacks to execute all pendingEventIDs
110+
//this.doCallbacks(index); causes all kinds of problems. See notes in twrlibrary.ts
111+
if (eventID && args) {
112+
return [eventID, args];
113+
}
115114

116115
// wait for a new event
117116
this.readWaitCommand();
118117
}
119118
}
120119

121-
processIncomingCommands() {
122-
while (!this.circBuffer.isEmpty())
123-
this.readCommand();
124-
125-
for (let i=0; i<this.pendingEventIDs.length; i++) {
120+
private doCallbacks(upToIndex?:number) {
121+
const end=upToIndex?upToIndex:this.pendingEventIDs.length;
122+
console.log("end",end, upToIndex, this.pendingEventIDs.length);
123+
for (let i=0; i<end; i++) {
126124
const eventID=this.pendingEventIDs[i];
127125
const args=this.pendingEventArgs[i];
128126
const onEventCallback=twrEventQueueReceive.onEventCallbacks[eventID];
@@ -134,6 +132,13 @@ export class twrEventQueueReceive {
134132
}
135133
}
136134

135+
processIncomingCommands() {
136+
while (!this.circBuffer.isEmpty())
137+
this.readCommand();
138+
139+
this.doCallbacks();
140+
}
141+
137142
//see twrWasmModule.constructor - imports - twr_register_callback:this.registerCallback.bind(this),
138143
//TODO!! This static method works for twrWasmModuleAsync, but when/if I implement message loop for twrWasmModule, this may need to change?
139144
static registerCallback(funcName:string, onEventCallback:TOnEventCallback) {

source/twr-ts/twrlibrary.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ import {twrEventQueueReceive} from "./twreventqueue.js"
77
/////////////////////////////////////////////////////////////////////
88

99
// TODO List
10+
// Working on: Allow callbacks while blocking call is waiting.
11+
// see commented out doCallbacks in WaitEvents
12+
// what if printf in callback and char waits for ret val?
13+
// that is what drawseq does
14+
// is animateframe being called before drawseq retval?
15+
// ie, recursive waitEvent
16+
// update doc, can't block in a callback?
17+
// finish test_timer
1018

11-
// doc event behavior
1219
// fix example/lib/out and .gitignore so correct build artifacts are checked in
1320
// resolve fact that libraries with interfaces are passed in the "io" option. Eg Allow "libs" or other synonym.
1421
// current implementation has no libs: (akin to io:).

0 commit comments

Comments
 (0)