@novasamatech/host-api 0.7.2-2 → 0.7.3
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/dist/protocol/v1/notification.d.ts +1 -1
- package/dist/transport.js +37 -6
- package/dist/types.d.ts +6 -0
- package/dist/types.js +1 -1
- package/package.json +2 -2
|
@@ -6,6 +6,6 @@ export declare const PushNotificationV1_request: import("scale-ts").Codec<{
|
|
|
6
6
|
text: string;
|
|
7
7
|
deeplink: string | undefined;
|
|
8
8
|
}>;
|
|
9
|
-
export declare const PushNotificationV1_response: import("scale-ts").Codec<import("scale-ts").ResultPayload<undefined, import("
|
|
9
|
+
export declare const PushNotificationV1_response: import("scale-ts").Codec<import("scale-ts").ResultPayload<undefined, import("@novasamatech/scale").CodecError<{
|
|
10
10
|
reason: string;
|
|
11
11
|
}, "GenericError">>>;
|
package/dist/transport.js
CHANGED
|
@@ -10,6 +10,38 @@ function isConnected(status) {
|
|
|
10
10
|
function getSubscriptionKey(method, payload) {
|
|
11
11
|
return `${method}_${toHex(MessagePayload.enc(payload))}`;
|
|
12
12
|
}
|
|
13
|
+
function createMessageProvider(provider) {
|
|
14
|
+
const subscribers = new Set();
|
|
15
|
+
let unsubscribeProvider = null;
|
|
16
|
+
return {
|
|
17
|
+
postMessage(message) {
|
|
18
|
+
provider.postMessage(Message.enc(message));
|
|
19
|
+
},
|
|
20
|
+
subscribe(fn) {
|
|
21
|
+
if (subscribers.size === 0) {
|
|
22
|
+
unsubscribeProvider = provider.subscribe(payload => {
|
|
23
|
+
try {
|
|
24
|
+
const message = Message.dec(payload);
|
|
25
|
+
for (const subscriber of subscribers) {
|
|
26
|
+
subscriber(message);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
provider.logger.error('Transport error', e);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
subscribers.add(fn);
|
|
35
|
+
return () => {
|
|
36
|
+
subscribers.delete(fn);
|
|
37
|
+
if (subscribers.size === 0 && unsubscribeProvider) {
|
|
38
|
+
unsubscribeProvider();
|
|
39
|
+
unsubscribeProvider = null;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
13
45
|
export function createTransport(provider) {
|
|
14
46
|
let codecVersion = JAM_CODEC_PROTOCOL_ID;
|
|
15
47
|
const handshakeAbortController = new AbortController();
|
|
@@ -44,6 +76,7 @@ export function createTransport(provider) {
|
|
|
44
76
|
throwIfIncorrectEnvironment();
|
|
45
77
|
throwIfInvalidCodecVersion();
|
|
46
78
|
}
|
|
79
|
+
const messageProvider = createMessageProvider(provider);
|
|
47
80
|
// subscriptions management (multiplexing)
|
|
48
81
|
const activeSubscriptions = new Map();
|
|
49
82
|
const transport = {
|
|
@@ -253,15 +286,13 @@ export function createTransport(provider) {
|
|
|
253
286
|
},
|
|
254
287
|
postMessage(requestId, payload) {
|
|
255
288
|
checks();
|
|
256
|
-
|
|
257
|
-
provider.postMessage(encoded);
|
|
289
|
+
messageProvider.postMessage({ requestId, payload });
|
|
258
290
|
},
|
|
259
291
|
listenMessages(action, callback, onError) {
|
|
260
|
-
return
|
|
292
|
+
return messageProvider.subscribe(message => {
|
|
261
293
|
try {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
callback(result.requestId, result.payload);
|
|
294
|
+
if (isEnumVariant(message.payload, action)) {
|
|
295
|
+
callback(message.requestId, message.payload);
|
|
265
296
|
}
|
|
266
297
|
}
|
|
267
298
|
catch (e) {
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import type { CodecType } from 'scale-ts';
|
|
1
2
|
import type { HostApiProtocol } from './protocol/impl.js';
|
|
2
3
|
import type { ComposeMessageAction, MessageAction, MessagePayloadSchema, PickMessagePayload, PickMessagePayloadValue } from './protocol/messageCodec.js';
|
|
4
|
+
import { Message } from './protocol/messageCodec.js';
|
|
3
5
|
import type { Provider } from './provider.js';
|
|
4
6
|
export type HostApiMethod = keyof HostApiProtocol;
|
|
5
7
|
export type Logger = Record<'info' | 'warn' | 'error' | 'log', (...args: unknown[]) => void> & {
|
|
@@ -13,6 +15,10 @@ export type Subscription<InterruptPayload = unknown> = {
|
|
|
13
15
|
onInterrupt(callback: (payload: InterruptPayload) => void): VoidFunction;
|
|
14
16
|
};
|
|
15
17
|
export type SubscriptionFor<Method extends HostApiMethod> = Subscription<PickMessagePayloadValue<ComposeMessageAction<Method, 'interrupt'>>>;
|
|
18
|
+
export type MessageProvider = {
|
|
19
|
+
postMessage(message: CodecType<typeof Message>): void;
|
|
20
|
+
subscribe(fn: (message: CodecType<typeof Message>) => void): VoidFunction;
|
|
21
|
+
};
|
|
16
22
|
export type Transport = {
|
|
17
23
|
readonly provider: Provider;
|
|
18
24
|
isCorrectEnvironment(): boolean;
|
package/dist/types.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
import { Message } from './protocol/messageCodec.js';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@novasamatech/host-api",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.7.
|
|
4
|
+
"version": "0.7.3",
|
|
5
5
|
"description": "Host API: transport implementation for host - product integration.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"README.md"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@novasamatech/scale": "0.7.
|
|
25
|
+
"@novasamatech/scale": "0.7.3",
|
|
26
26
|
"nanoevents": "9.1.0",
|
|
27
27
|
"nanoid": "5.1.9",
|
|
28
28
|
"neverthrow": "^8.2.0",
|