@tokamakdev/plugin 0.1.0-beta.65 → 0.1.0-beta.70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/index.ts +5 -14
- package/src/testing.ts +39 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokamakdev/plugin",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.70",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/mantty/tokamak.git",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"type": "module",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": "./src/index.ts",
|
|
12
|
-
"./worker": "./src/worker.ts"
|
|
12
|
+
"./worker": "./src/worker.ts",
|
|
13
|
+
"./testing": "./src/testing.ts"
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
15
16
|
"src"
|
package/src/index.ts
CHANGED
|
@@ -32,7 +32,6 @@ type Pending = PendingCall | PendingSubscription;
|
|
|
32
32
|
|
|
33
33
|
declare global {
|
|
34
34
|
var __tokamakNative: NativeTransport | undefined;
|
|
35
|
-
var __tokamakReceive: ((response: NativeResponse) => void) | undefined;
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
let nextRequestId = 1;
|
|
@@ -49,27 +48,20 @@ export abstract class FrontendPlugin {
|
|
|
49
48
|
|
|
50
49
|
protected call<T>(method: string, arguments_: unknown = null): Promise<T> {
|
|
51
50
|
return new Promise<T>((resolve, reject) => {
|
|
52
|
-
send("call", this.pluginId, method, arguments_, {
|
|
53
|
-
kind: "call",
|
|
54
|
-
resolve: (value) => {
|
|
55
|
-
resolve(value as T);
|
|
56
|
-
},
|
|
57
|
-
reject,
|
|
58
|
-
});
|
|
51
|
+
send("call", this.pluginId, method, arguments_, { kind: "call", resolve, reject });
|
|
59
52
|
});
|
|
60
53
|
}
|
|
61
54
|
|
|
62
|
-
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters -- T types the native values, as in call.
|
|
56
|
+
protected listen<T>(
|
|
63
57
|
method: string,
|
|
64
|
-
next: (value:
|
|
58
|
+
next: (value: T) => void,
|
|
65
59
|
error: (error: DOMException) => void,
|
|
66
60
|
arguments_: unknown = null,
|
|
67
61
|
): () => void {
|
|
68
62
|
const { transport, id } = send("subscribe", this.pluginId, method, arguments_, {
|
|
69
63
|
kind: "subscription",
|
|
70
|
-
next
|
|
71
|
-
next(value);
|
|
72
|
-
},
|
|
64
|
+
next,
|
|
73
65
|
error,
|
|
74
66
|
});
|
|
75
67
|
return () => {
|
|
@@ -87,7 +79,6 @@ function nativeTransport(): NativeTransport | undefined {
|
|
|
87
79
|
transport.onmessage = ({ data }) => {
|
|
88
80
|
receive(JSON.parse(data) as NativeResponse);
|
|
89
81
|
};
|
|
90
|
-
globalThis.__tokamakReceive = receive;
|
|
91
82
|
transport.postMessage(JSON.stringify({ type: "reset", session }));
|
|
92
83
|
}
|
|
93
84
|
return transport;
|
package/src/testing.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** A native transport that records the page's messages and replies as the native bridges do. */
|
|
2
|
+
export class FakeNativeTransport {
|
|
3
|
+
/** Every message the page posted, parsed. */
|
|
4
|
+
readonly sent: Record<string, unknown>[] = [];
|
|
5
|
+
onmessage: ((event: { data: string }) => void) | null = null;
|
|
6
|
+
|
|
7
|
+
postMessage(message: string): void {
|
|
8
|
+
this.sent.push(JSON.parse(message) as Record<string, unknown>);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The last message's type, plugin, method and arguments. */
|
|
12
|
+
lastRequest(): Record<string, unknown> {
|
|
13
|
+
const { type, plugin, method, arguments: arguments_ } = this.sent.at(-1) ?? {};
|
|
14
|
+
return { type, plugin, method, arguments: arguments_ };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Replies to the last message. */
|
|
18
|
+
respond(reply: { value?: unknown; error?: { name: string; message: string } }, done = true): void {
|
|
19
|
+
const { session, id } = this.sent.at(-1) ?? {};
|
|
20
|
+
this.deliver({ session, id, done, ...reply });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Delivers `response` to the page. */
|
|
24
|
+
deliver(response: Record<string, unknown>): void {
|
|
25
|
+
this.onmessage?.({ data: JSON.stringify(response) });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Installs a new fake as the page's native transport. */
|
|
30
|
+
export function connectNative(): FakeNativeTransport {
|
|
31
|
+
const transport = new FakeNativeTransport();
|
|
32
|
+
globalThis.__tokamakNative = transport;
|
|
33
|
+
return transport;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Removes the page's native transport. */
|
|
37
|
+
export function disconnectNative(): void {
|
|
38
|
+
Reflect.deleteProperty(globalThis, "__tokamakNative");
|
|
39
|
+
}
|