diff --git a/CHANGELOG.md b/CHANGELOG.md index d55022256..b46b2f8b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This better corresponds to JSON-RPC 2.0 server-client distinction and is expected to simplify writing new bindings because dispatching events can be done on higher level. +- JSON-RPC: TS: Client now has a mandatory argument whether you want to start listening for events. ### Fixes - JSON-RPC: do not print to stdout on failure to find an account. diff --git a/deltachat-jsonrpc/typescript/src/client.ts b/deltachat-jsonrpc/typescript/src/client.ts index 783ed7bad..97c4a8364 100644 --- a/deltachat-jsonrpc/typescript/src/client.ts +++ b/deltachat-jsonrpc/typescript/src/client.ts @@ -36,12 +36,15 @@ export class BaseDeltaChat< account?: T.Account; private contextEmitters: { [key: number]: TinyEmitter } = {}; + //@ts-ignore private eventTask: Promise; - constructor(public transport: Transport) { + constructor(public transport: Transport, startEventLoop: boolean) { super(); this.rpc = new RawClient(this.transport); - this.eventTask = this.eventLoop(); + if (startEventLoop) { + this.eventTask = this.eventLoop(); + } } async eventLoop(): Promise { @@ -77,10 +80,12 @@ export class BaseDeltaChat< export type Opts = { url: string; + startEventLoop: boolean; }; export const DEFAULT_OPTS: Opts = { url: "ws://localhost:20808/ws", + startEventLoop: true, }; export class DeltaChat extends BaseDeltaChat { opts: Opts; @@ -88,20 +93,24 @@ export class DeltaChat extends BaseDeltaChat { this.transport.close(); } constructor(opts?: Opts | string) { - if (typeof opts === "string") opts = { url: opts }; - if (opts) opts = { ...DEFAULT_OPTS, ...opts }; - else opts = { ...DEFAULT_OPTS }; + if (typeof opts === "string") { + opts = { ...DEFAULT_OPTS, url: opts }; + } else if (opts) { + opts = { ...DEFAULT_OPTS, ...opts }; + } else { + opts = { ...DEFAULT_OPTS }; + } const transport = new WebsocketTransport(opts.url); - super(transport); + super(transport, opts.startEventLoop); this.opts = opts; } } export class StdioDeltaChat extends BaseDeltaChat { close() {} - constructor(input: any, output: any) { + constructor(input: any, output: any, startEventLoop: boolean) { const transport = new StdioTransport(input, output); - super(transport); + super(transport, startEventLoop); } } diff --git a/deltachat-jsonrpc/typescript/test/basic.ts b/deltachat-jsonrpc/typescript/test/basic.ts index 1589a733d..1c430f3f5 100644 --- a/deltachat-jsonrpc/typescript/test/basic.ts +++ b/deltachat-jsonrpc/typescript/test/basic.ts @@ -12,7 +12,7 @@ describe("basic tests", () => { before(async () => { serverHandle = await startServer(); - dc = new DeltaChat(serverHandle.stdin, serverHandle.stdout); + dc = new DeltaChat(serverHandle.stdin, serverHandle.stdout, true); // dc.on("ALL", (event) => { //console.log("event", event); // }); diff --git a/deltachat-jsonrpc/typescript/test/online.ts b/deltachat-jsonrpc/typescript/test/online.ts index 418f14679..3ab6cb820 100644 --- a/deltachat-jsonrpc/typescript/test/online.ts +++ b/deltachat-jsonrpc/typescript/test/online.ts @@ -27,7 +27,7 @@ describe("online tests", function () { this.skip(); } serverHandle = await startServer(); - dc = new DeltaChat(serverHandle.stdin, serverHandle.stdout); + dc = new DeltaChat(serverHandle.stdin, serverHandle.stdout, true); dc.on("ALL", (contextId, { type }) => { if (type !== "Info") console.log(contextId, type);