From 1e06adaa45644900856e22b1553a5150e40fc18f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Tue, 3 Nov 2026 14:42:11 +0100 Subject: [PATCH] Surface `exit` event from Bare --- index.d.ts | 5 +++ index.js | 75 ++++++++++++++++++++++++++++++++++++++--- shared/BareKitModule.cc | 11 +++++- shared/BareKitModule.h | 3 +- specs/NativeBareKit.ts | 3 +- 5 files changed, 89 insertions(+), 8 deletions(-) diff --git a/index.d.ts b/index.d.ts index d17e64a..98603e4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -11,6 +11,7 @@ export interface WorkletEvents extends EventMap { wakeup: [deadline: number] idle: [] resume: [] + exit: [code: number] } export interface WorkletOptions { @@ -22,6 +23,9 @@ export class Worklet extends EventEmitter { constructor(id: string, options?: WorkletOptions) readonly IPC: IPC + readonly started: boolean + readonly terminated: boolean + readonly suspended: boolean start(filename: string, args?: string[]): void start(filename: string, source: Uint8Array, args?: string[]): void @@ -32,6 +36,7 @@ export class Worklet extends EventEmitter { wakeup(deadline?: number): void update(state?: AppStateStatus): void terminate(): void + wait(): Promise static suspend(linger?: number): void static resume(): void diff --git a/index.js b/index.js index c8e55bd..c306ffd 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,8 @@ class BareKitIPC extends Duplex { this._pendingOpen = null this._pendingRead = null this._pendingWrite = null + + this._ended = false } get worklet() { @@ -37,6 +39,8 @@ class BareKitIPC extends Duplex { } _update() { + if (this._worklet.terminated) return + NativeBareKit.update( this._worklet._handle, this._pendingRead !== null, @@ -54,7 +58,7 @@ class BareKitIPC extends Duplex { const data = NativeBareKit.read(this._worklet._handle) if (data) { - this.push(data.byteLength === 0 ? null : new Uint8Array(data)) + this._push(data) cb(null) } else { this._pendingRead = cb @@ -63,6 +67,8 @@ class BareKitIPC extends Duplex { } _write(data, cb) { + if (this._worklet.terminated) return cb(new Error('IPC has been closed')) + const written = NativeBareKit.write( this._worklet._handle, data.buffer, @@ -79,10 +85,46 @@ class BareKitIPC extends Duplex { } _final(cb) { - NativeBareKit.end(this._worklet._handle) + if (!this._worklet.terminated) NativeBareKit.end(this._worklet._handle) cb(null) } + _push(data) { + if (data.byteLength === 0) { + this._ended = true + this.push(null) + } else { + this.push(new Uint8Array(data)) + } + } + + // The worklet has exited and its IPC has reached end-of-stream, so whatever + // is left is read now, ignoring backpressure, before it is released. + _drain() { + while (!this._ended && !this.destroying) { + const data = NativeBareKit.read(this._worklet._handle) + if (data === null) break + this._push(data) + } + + if (!this._ended) { + this._ended = true + this.push(null) + } + + if (this._pendingRead !== null) { + const cb = this._pendingRead + this._pendingRead = null + cb(null) + } + + if (this._pendingWrite !== null) { + const [, cb] = this._pendingWrite + this._pendingWrite = null + cb(new Error('IPC has been closed')) + } + } + _continueOpen(err) { if (this._pendingOpen === null) { if (err) this.destroy(err) @@ -138,6 +180,7 @@ class BareKitWorklet extends EventEmitter { super() this._state = 0 + this._exitCode = null this._ipc = new BareKitIPC(this) const terminate = this.terminate.bind(this) @@ -150,7 +193,8 @@ class BareKitWorklet extends EventEmitter { this._onsuspend.bind(this), this._onwakeup.bind(this), this._onidle.bind(this), - this._onresume.bind(this) + this._onresume.bind(this), + this._onexit.bind(this) ) } @@ -305,10 +349,18 @@ class BareKitWorklet extends EventEmitter { } terminate() { - if (this.terminated) return - this._ipc.destroy() + if (!this.terminated) this._release() + } + + wait() { + if (this._exitCode !== null) return Promise.resolve(this._exitCode) + + return new Promise((resolve) => this.once('exit', resolve)) + } + + _release() { if (this.started) NativeBareKit.terminate(this._handle) this._state |= constants.TERMINATED @@ -340,6 +392,19 @@ class BareKitWorklet extends EventEmitter { _onresume() { this.emit('resume') } + + _onexit(code) { + if (!this.terminated) { + this._ipc._drain() + this._release() + } + + // Deferred so that the data just drained from the IPC is emitted first. + queueMicrotask(() => { + this._exitCode = code + this.emit('exit', code) + }) + } } exports.Worklet = BareKitWorklet diff --git a/shared/BareKitModule.cc b/shared/BareKitModule.cc index 8a112d2..4f41b53 100644 --- a/shared/BareKitModule.cc +++ b/shared/BareKitModule.cc @@ -124,6 +124,7 @@ struct BareKitEvents { AsyncCallback on_wakeup; AsyncCallback<> on_idle; AsyncCallback<> on_resume; + AsyncCallback on_exit; }; struct BareKitWorklet : HostObject { @@ -154,6 +155,7 @@ struct BareKitWorklet : HostObject { Function &&on_wakeup, Function &&on_idle, Function &&on_resume, + Function &&on_exit, std::shared_ptr jsInvoker ) : on_terminate(rt, std::move(on_terminate), jsInvoker), @@ -163,6 +165,7 @@ struct BareKitWorklet : HostObject { AsyncCallback(rt, std::move(on_wakeup), jsInvoker), AsyncCallback<>(rt, std::move(on_idle), jsInvoker), AsyncCallback<>(rt, std::move(on_resume), jsInvoker), + AsyncCallback(rt, std::move(on_exit), jsInvoker), })) { int err; @@ -432,6 +435,10 @@ struct BareKitWorklet : HostObject { static void on_exit_(int exit_code, void *data) { + events_from(data).on_exit.call([=](Runtime &rt, Function &function) { + function.call(rt, exit_code); + }); + delete static_cast *>(data); } }; @@ -454,7 +461,8 @@ BareKitModule::init( Function on_suspend, Function on_wakeup, Function on_idle, - Function on_resume + Function on_resume, + Function on_exit ) { auto worklet = std::make_shared( rt, @@ -466,6 +474,7 @@ BareKitModule::init( std::move(on_wakeup), std::move(on_idle), std::move(on_resume), + std::move(on_exit), jsInvoker_ ); diff --git a/shared/BareKitModule.h b/shared/BareKitModule.h index a48590c..18ba549 100644 --- a/shared/BareKitModule.h +++ b/shared/BareKitModule.h @@ -21,7 +21,8 @@ class BareKitModule : public NativeBareKitCxxSpec { jsi::Function on_suspend, jsi::Function on_wakeup, jsi::Function on_idle, - jsi::Function on_resume + jsi::Function on_resume, + jsi::Function on_exit ); void diff --git a/specs/NativeBareKit.ts b/specs/NativeBareKit.ts index b85778a..7e89abd 100644 --- a/specs/NativeBareKit.ts +++ b/specs/NativeBareKit.ts @@ -10,7 +10,8 @@ export interface Spec extends TurboModule { onsuspend: (linger: number) => void, onwakeup: (deadline: number) => void, onidle: () => void, - onresume: () => void + onresume: () => void, + onexit: (code: number) => void ): Object startFile(handle: Object, filename: string, args: Array): void