mirror of
https://github.com/chatmail/core.git
synced 2026-05-09 01:46:30 +03:00
Improvements to typescript package
This commit is contained in:
committed by
Simon Laux
parent
97e0e0137a
commit
3404996fdd
@@ -1,6 +1,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
|
<title>Deltachat JSON-RPC example</title>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
@@ -41,6 +42,7 @@
|
|||||||
<script type="module" src="dist/example.bundle.js"></script>
|
<script type="module" src="dist/example.bundle.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<h1>Deltachat JSON-RPC example</h1>
|
||||||
<div class="grid">
|
<div class="grid">
|
||||||
<div id="header"></div>
|
<div id="header"></div>
|
||||||
<div id="main"></div>
|
<div id="main"></div>
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
import { RawClient, RPC } from "./src/lib";
|
import { RawClient } from "../src/lib";
|
||||||
import { WebsocketTransport, Request } from "yerpc";
|
import { WebsocketTransport, Request } from "yerpc";
|
||||||
|
|
||||||
type DeltaEvent = { id: string; contextId: number; field1: any; field2: any };
|
type DeltaEvent = { id: string; contextId: number; field1: any; field2: any };
|
||||||
var selectedAccount = 0;
|
var SELECTED_ACCOUNT = 0;
|
||||||
|
|
||||||
window.addEventListener("DOMContentLoaded", (_event) => {
|
window.addEventListener("DOMContentLoaded", (_event) => {
|
||||||
(window as any).selectDeltaAccount = (id: string) => {
|
(window as any).selectDeltaAccount = (id: string) => {
|
||||||
selectedAccount = Number(id);
|
SELECTED_ACCOUNT = Number(id);
|
||||||
window.dispatchEvent(new Event("account-changed"));
|
window.dispatchEvent(new Event("account-changed"));
|
||||||
};
|
};
|
||||||
run().catch((err) => console.error("run failed", err));
|
run().catch((err) => console.error("run failed", err));
|
||||||
@@ -18,8 +19,11 @@ async function run() {
|
|||||||
|
|
||||||
const transport = new WebsocketTransport("ws://localhost:20808/ws");
|
const transport = new WebsocketTransport("ws://localhost:20808/ws");
|
||||||
const client = new RawClient(transport);
|
const client = new RawClient(transport);
|
||||||
|
transport.on("error", (err) => {
|
||||||
|
console.error("WebSocket transport error", err)
|
||||||
|
});
|
||||||
|
|
||||||
(window as any).client = client;
|
;(window as any).client = client;
|
||||||
|
|
||||||
transport.on("request", (request: Request) => {
|
transport.on("request", (request: Request) => {
|
||||||
const method = request.method;
|
const method = request.method;
|
||||||
@@ -30,7 +34,7 @@ async function run() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener("account-changed", async (_event: Event) => {
|
window.addEventListener("account-changed", async (_event: Event) => {
|
||||||
await client.selectAccount(selectedAccount);
|
await client.selectAccount(SELECTED_ACCOUNT);
|
||||||
listChatsForSelectedAccount();
|
listChatsForSelectedAccount();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -43,9 +47,16 @@ async function run() {
|
|||||||
write(
|
write(
|
||||||
$head,
|
$head,
|
||||||
`<a href="#" onclick="selectDeltaAccount(${account.id})">
|
`<a href="#" onclick="selectDeltaAccount(${account.id})">
|
||||||
${account.addr!}
|
${account.id}: ${account.addr!}
|
||||||
</a> `
|
</a> `
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
write(
|
||||||
|
$head,
|
||||||
|
`<a href="#">
|
||||||
|
${account.id}: (unconfigured)
|
||||||
|
</a> `
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
import { Deltachat } from "./dist/deltachat.js";
|
import { Deltachat } from "../dist/deltachat.js";
|
||||||
|
|
||||||
run().catch(console.error);
|
run().catch(console.error);
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
const delta = new Deltachat();
|
const delta = new Deltachat();
|
||||||
delta.addEventListener("event", (event) => {
|
delta.on("event", (event) => {
|
||||||
console.log("event", event.data);
|
console.log("event", event.data);
|
||||||
});
|
});
|
||||||
|
|
||||||
const accounts = await delta.rpc.getAllAccounts();
|
const accounts = await delta.rpc.getAllAccounts();
|
||||||
console.log("accounts", accounts);
|
console.log("accounts", accounts);
|
||||||
|
console.log("waiting for events...")
|
||||||
}
|
}
|
||||||
@@ -12,15 +12,16 @@
|
|||||||
"build": "npm run generate-bindings && tsc",
|
"build": "npm run generate-bindings && tsc",
|
||||||
"bundle": "npm run build && esbuild --bundle dist/deltachat.js --outfile=dist/deltachat.bundle.js",
|
"bundle": "npm run build && esbuild --bundle dist/deltachat.js --outfile=dist/deltachat.bundle.js",
|
||||||
"generate-bindings": "cargo test",
|
"generate-bindings": "cargo test",
|
||||||
"example:build": "tsc && esbuild --bundle dist/example.js --outfile=dist/example.bundle.js",
|
"example:build": "tsc && esbuild --bundle dist/example.js --outfile=dist/example.bundle.js && cp example/index.html dist",
|
||||||
"example:dev": "esbuild example.ts --bundle --outdir=dist --servedir=.",
|
"example:start": "http-server dist/",
|
||||||
|
"example:dev": "esbuild example/example.ts --bundle --outfile=dist/example.bundle.js --servedir=.",
|
||||||
"coverage": "tsc -b test && COVERAGE=1 NODE_OPTIONS=--enable-source-maps c8 --include \"dist/*\" -r text -r html -r json mocha test_dist && node report_api_coverage.mjs",
|
"coverage": "tsc -b test && COVERAGE=1 NODE_OPTIONS=--enable-source-maps c8 --include \"dist/*\" -r text -r html -r json mocha test_dist && node report_api_coverage.mjs",
|
||||||
"test": "rm -rf dist && npm run build && npm run coverage && npm run prettier:check"
|
"test": "rm -rf dist && npm run build && npm run coverage && npm run prettier:check"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"isomorphic-ws": "^4.0.1",
|
"isomorphic-ws": "^4.0.1",
|
||||||
"tiny-emitter": "git+https://github.com/Simon-Laux/tiny-emitter.git",
|
"tiny-emitter": "git+https://github.com/Simon-Laux/tiny-emitter.git",
|
||||||
"yerpc": "^0.3"
|
"yerpc": "^0.3.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"prettier": "^2.6.2",
|
"prettier": "^2.6.2",
|
||||||
|
|||||||
@@ -17,11 +17,12 @@ export type Events = Record<
|
|||||||
>;
|
>;
|
||||||
|
|
||||||
export class BaseDeltachat<
|
export class BaseDeltachat<
|
||||||
Transport extends BaseTransport
|
Transport extends BaseTransport<any>
|
||||||
> extends TinyEmitter<Events> {
|
> extends TinyEmitter<Events> {
|
||||||
rpc: RawClient;
|
rpc: RawClient;
|
||||||
account?: T.Account;
|
account?: T.Account;
|
||||||
constructor(protected transport: Transport) {
|
private contextEmitters: TinyEmitter<Events>[] = [];
|
||||||
|
constructor(public transport: Transport) {
|
||||||
super();
|
super();
|
||||||
this.rpc = new RawClient(this.transport);
|
this.rpc = new RawClient(this.transport);
|
||||||
this.transport.on("request", (request: Request) => {
|
this.transport.on("request", (request: Request) => {
|
||||||
@@ -43,8 +44,6 @@ export class BaseDeltachat<
|
|||||||
return await this.rpc.getAllAccounts();
|
return await this.rpc.getAllAccounts();
|
||||||
}
|
}
|
||||||
|
|
||||||
private contextEmitters: TinyEmitter<Events>[] = [];
|
|
||||||
|
|
||||||
getContextEvents(account_id: number) {
|
getContextEvents(account_id: number) {
|
||||||
if (this.contextEmitters[account_id]) {
|
if (this.contextEmitters[account_id]) {
|
||||||
return this.contextEmitters[account_id];
|
return this.contextEmitters[account_id];
|
||||||
@@ -65,13 +64,14 @@ export const DEFAULT_OPTS: Opts = {
|
|||||||
export class Deltachat extends BaseDeltachat<WebsocketTransport> {
|
export class Deltachat extends BaseDeltachat<WebsocketTransport> {
|
||||||
opts: Opts;
|
opts: Opts;
|
||||||
close() {
|
close() {
|
||||||
this.transport._socket.close();
|
this.transport.close();
|
||||||
}
|
}
|
||||||
constructor(opts: Opts | string | undefined) {
|
constructor(opts?: Opts | string) {
|
||||||
if (typeof opts === "string") opts = { url: opts };
|
if (typeof opts === "string") opts = { url: opts };
|
||||||
if (opts) opts = { ...DEFAULT_OPTS, ...opts };
|
if (opts) opts = { ...DEFAULT_OPTS, ...opts };
|
||||||
else opts = { ...DEFAULT_OPTS };
|
else opts = { ...DEFAULT_OPTS };
|
||||||
super(new WebsocketTransport(opts.url));
|
const transport = new WebsocketTransport(opts.url)
|
||||||
|
super(transport);
|
||||||
this.opts = opts;
|
this.opts = opts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,6 @@
|
|||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
"isolatedModules": true
|
"isolatedModules": true
|
||||||
},
|
},
|
||||||
"include": ["*.ts"],
|
"include": ["*.ts", "example/*.ts"],
|
||||||
"compileOnSave": false
|
"compileOnSave": false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user