@tokamakdev/plugin 0.1.0-beta.54 → 0.1.0-beta.56

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +17 -32
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokamakdev/plugin",
3
- "version": "0.1.0-beta.54",
3
+ "version": "0.1.0-beta.56",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/mantty/tokamak.git",
package/src/index.ts CHANGED
@@ -49,27 +49,13 @@ export abstract class FrontendPlugin {
49
49
 
50
50
  protected call<T>(method: string, arguments_: unknown = null): Promise<T> {
51
51
  return new Promise<T>((resolve, reject) => {
52
- const transport = requireNativeTransport();
53
- const id = nextRequestId++;
54
- pending.set(id, {
52
+ send("call", this.pluginId, method, arguments_, {
55
53
  kind: "call",
56
54
  resolve: (value) => {
57
55
  resolve(value as T);
58
56
  },
59
57
  reject,
60
58
  });
61
- post(
62
- transport,
63
- {
64
- type: "call",
65
- session,
66
- id,
67
- plugin: this.pluginId,
68
- method,
69
- arguments: arguments_,
70
- },
71
- id,
72
- );
73
59
  });
74
60
  }
75
61
 
@@ -79,27 +65,13 @@ export abstract class FrontendPlugin {
79
65
  error: (error: DOMException) => void,
80
66
  arguments_: unknown = null,
81
67
  ): () => void {
82
- const transport = requireNativeTransport();
83
- const id = nextRequestId++;
84
- pending.set(id, {
68
+ const { transport, id } = send("subscribe", this.pluginId, method, arguments_, {
85
69
  kind: "subscription",
86
70
  next: (value) => {
87
71
  next(value);
88
72
  },
89
73
  error,
90
74
  });
91
- post(
92
- transport,
93
- {
94
- type: "subscribe",
95
- session,
96
- id,
97
- plugin: this.pluginId,
98
- method,
99
- arguments: arguments_,
100
- },
101
- id,
102
- );
103
75
  return () => {
104
76
  if (!pending.delete(id)) return;
105
77
  transport.postMessage(JSON.stringify({ type: "cancel", session, id }));
@@ -127,13 +99,26 @@ function requireNativeTransport(): NativeTransport {
127
99
  throw new DOMException("Native plugin transport is unavailable", "NotSupportedError");
128
100
  }
129
101
 
130
- function post(transport: NativeTransport, message: object, id: number): void {
102
+ /** Posts a call or subscription, keeping `request` pending unless posting throws. */
103
+ function send(
104
+ type: "call" | "subscribe",
105
+ plugin: string,
106
+ method: string,
107
+ arguments_: unknown,
108
+ request: Pending,
109
+ ): { transport: NativeTransport; id: number } {
110
+ const transport = requireNativeTransport();
111
+ const id = nextRequestId++;
112
+ pending.set(id, request);
131
113
  try {
132
- transport.postMessage(JSON.stringify(message));
114
+ transport.postMessage(
115
+ JSON.stringify({ type, session, id, plugin, method, arguments: arguments_ }),
116
+ );
133
117
  } catch (error) {
134
118
  pending.delete(id);
135
119
  throw error;
136
120
  }
121
+ return { transport, id };
137
122
  }
138
123
 
139
124
  function receive(response: NativeResponse): void {