Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface WorkletEvents extends EventMap {
wakeup: [deadline: number]
idle: []
resume: []
exit: [code: number]
}

export interface WorkletOptions {
Expand All @@ -22,6 +23,9 @@ export class Worklet extends EventEmitter<WorkletEvents> {
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
Expand All @@ -32,6 +36,7 @@ export class Worklet extends EventEmitter<WorkletEvents> {
wakeup(deadline?: number): void
update(state?: AppStateStatus): void
terminate(): void
wait(): Promise<number>

static suspend(linger?: number): void
static resume(): void
Expand Down
75 changes: 70 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class BareKitIPC extends Duplex {
this._pendingOpen = null
this._pendingRead = null
this._pendingWrite = null

this._ended = false
}

get worklet() {
Expand All @@ -37,6 +39,8 @@ class BareKitIPC extends Duplex {
}

_update() {
if (this._worklet.terminated) return

NativeBareKit.update(
this._worklet._handle,
this._pendingRead !== null,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion shared/BareKitModule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ struct BareKitEvents {
AsyncCallback<int> on_wakeup;
AsyncCallback<> on_idle;
AsyncCallback<> on_resume;
AsyncCallback<int> on_exit;
};

struct BareKitWorklet : HostObject {
Expand Down Expand Up @@ -154,6 +155,7 @@ struct BareKitWorklet : HostObject {
Function &&on_wakeup,
Function &&on_idle,
Function &&on_resume,
Function &&on_exit,
std::shared_ptr<CallInvoker> jsInvoker
)
: on_terminate(rt, std::move(on_terminate), jsInvoker),
Expand All @@ -163,6 +165,7 @@ struct BareKitWorklet : HostObject {
AsyncCallback<int>(rt, std::move(on_wakeup), jsInvoker),
AsyncCallback<>(rt, std::move(on_idle), jsInvoker),
AsyncCallback<>(rt, std::move(on_resume), jsInvoker),
AsyncCallback<int>(rt, std::move(on_exit), jsInvoker),
})) {
int err;

Expand Down Expand Up @@ -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<std::shared_ptr<BareKitEvents> *>(data);
}
};
Expand All @@ -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<BareKitWorklet>(
rt,
Expand All @@ -466,6 +474,7 @@ BareKitModule::init(
std::move(on_wakeup),
std::move(on_idle),
std::move(on_resume),
std::move(on_exit),
jsInvoker_
);

Expand Down
3 changes: 2 additions & 1 deletion shared/BareKitModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class BareKitModule : public NativeBareKitCxxSpec<BareKitModule> {
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
Expand Down
3 changes: 2 additions & 1 deletion specs/NativeBareKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>): void
Expand Down
Loading