8000 Introduce a Plugin system, implement <py-terminal> as a plugin by antocuni · Pull Request #917 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Introduce a Plugin system, implement <py-terminal> as a plugin #917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add StdioMultiplexer, which allows to redirect streams to multiple li…
…steners
  • Loading branch information
antocuni committed Nov 3, 2022
commit 051f940b2168271b448e5276eb23005e77cdc339
24 changes: 24 additions & 0 deletions pyscriptjs/src/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,27 @@ export class CaptureStdio implements Stdio {
this.captured_stderr += msg + "\n";
}
}

/** Redirect stdio streams to multiple listeners
*/
export class StdioMultiplexer implements Stdio {
_listeners: Stdio[];

constructor() {
this._listeners = [];
}

addListener(obj: Stdio) {
this._listeners.push(obj);
}

stdout(msg: string) {
for(const obj of this._listeners)
obj.stdout(msg);
}

stderr(msg: string) {
for(const obj of this._listeners)
obj.stderr(msg);
}
}
41 changes: 40 additions & 1 deletion pyscriptjs/tests/unit/stdio.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Stdio, CaptureStdio } from '../../src/stdio';
import { type Stdio, CaptureStdio, StdioMultiplexer } from '../../src/stdio';

describe('CaptureStdio', () => {
it('captured streams are initialized to empty string', () => {
Expand Down Expand Up @@ -26,3 +26,42 @@ describe('CaptureStdio', () => {
});

});


describe('StdioMultiplexer', () => {
let a: CaptureStdio;
let b: CaptureStdio;
let multi: StdioMultiplexer;

beforeEach(() => {
a = new CaptureStdio();
b = new CaptureStdio();
multi = new StdioMultiplexer();
});

it('works without listeners', () => {
// no listeners, messages are ignored
multi.stdout('out 1');
multi.stderr('err 1');
expect(a.captured_stdout).toBe("");
expect(a.captured_stderr).toBe("");
expect(b.captured_stdout).toBe("");
expect(b.captured_stderr).toBe("");
});

it('redirects to multiple listeners', () => {
multi.addListener(a);
multi.stdout('out 1');
multi.stderr('err 1');

multi.addListener(b);
multi.stdout('out 2');
multi.stderr('err 2');

expect(a.captured_stdout).toBe("out 1\nout 2\n");
expect(a.captured_stderr).toBe("err 1\nerr 2\n");

expect(b.captured_stdout).toBe("out 2\n");
expect(b.captured_stderr).toBe("err 2\n");
});
});
0