Skip to content

Commit

Permalink
Add communication channel to Module.webr object
Browse files Browse the repository at this point in the history
Allows access to the communication channel from the worker thread even
once the worker script has been minified.
  • Loading branch information
georgestagg committed Sep 27, 2024
1 parent 8aecab1 commit 5c4fb78
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/webr/src/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void canvasMode(int mode, pDevDesc RGD) {
const canvas = Module.webr.canvas[$0];
if (!canvas.capture) {
const image = canvas.offscreen.transferToImageBitmap();
chan.write({ type: 'canvas', data: {
Module.webr.channel.write({ type: 'canvas', data: {
event: 'canvasImage',
image,
id: $0,
Expand Down Expand Up @@ -347,7 +347,7 @@ void canvasNewPage(const pGEcontext gc, pDevDesc RGD)

EM_ASM({
if (!Module.webr.canvas[$0].capture) {
chan.write({ type: 'canvas', data: {
Module.webr.channel.write({ type: 'canvas', data: {
event: 'canvasNewPage',
id: $0,
} });
Expand Down
2 changes: 2 additions & 0 deletions src/webR/emscripten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RPtr, RTypeNumber } from './robj';
import type { RObject, RList } from './robj-worker';
import type { EvalROptions } from './webr-chan';
import type { UnwindProtectException } from './utils-r';
import type { ChannelWorker } from './chan/channel';

export interface Module extends EmscriptenModule {
/* Add mkdirTree to FS namespace, missing from @types/emscripten at the
Expand Down Expand Up @@ -127,6 +128,7 @@ export interface Module extends EmscriptenModule {
// TODO: Namespace all webR properties
webr: {
UnwindProtectException: typeof UnwindProtectException;
chan: ChannelWorker | undefined,
canvas: {
[key: number]: {
ctx: OffscreenCanvasRenderingContext2D;
Expand Down
1 change: 1 addition & 0 deletions src/webR/webr-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ function init(config: Required<WebROptions>) {
UnwindProtectException: UnwindProtectException,
evalR: evalR,
captureR: captureR,
channel: chan,
canvas: {},

resolveInit: () => {
Expand Down

0 comments on commit 5c4fb78

Please sign in to comment.