@telperion/messenger 8.4.2 → 8.5.0
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/.build-manifest.json +1 -1
- package/README.md +30 -1
- package/broadcast/index.d.cts +1 -1
- package/broadcast/index.d.ts +1 -1
- package/chrome/index.d.cts +1 -1
- package/chrome/index.d.ts +1 -1
- package/fesm/messenger.mjs +21 -10
- package/fesm/messenger.mjs.map +1 -1
- package/iframe/index.d.cts +1 -1
- package/iframe/index.d.ts +1 -1
- package/index.d.cts +1 -1
- package/index.d.ts +1 -1
- package/package.json +1 -1
- package/request.decorator.cjs +20 -9
- package/request.decorator.cjs.map +1 -1
- package/request.decorator.d.cts +53 -1
- package/request.decorator.d.ts +53 -1
- package/request.decorator.js +22 -11
- package/request.decorator.js.map +1 -1
- package/rtc/index.d.cts +1 -1
- package/rtc/index.d.ts +1 -1
- package/worker/index.d.cts +1 -1
- package/worker/index.d.ts +1 -1
package/.build-manifest.json
CHANGED
package/README.md
CHANGED
|
@@ -47,6 +47,34 @@ The library uses two main abstractions:
|
|
|
47
47
|
- **`@Request(path)`**: Marks a method as a message sender
|
|
48
48
|
- **`@Listen(path)`**: Marks a method as a message listener
|
|
49
49
|
|
|
50
|
+
#### Requests are sent eagerly
|
|
51
|
+
|
|
52
|
+
A `@Request` method sends its message **when it is called**, not when the returned `Observable` is subscribed. Calling it without subscribing still sends the message, and subscribing twice does not send it twice. Subscribe right away: responses that arrive before the subscription are not replayed.
|
|
53
|
+
|
|
54
|
+
#### Single-response requests
|
|
55
|
+
|
|
56
|
+
A request that is answered once can return a `Promise` of the first response instead of an `Observable`, with `{ single: true }`. No `firstValueFrom` is needed at the call site:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { IframeMessageClient, Request } from '@telperion/messenger/iframe';
|
|
60
|
+
|
|
61
|
+
class SignInFrameClient extends IframeMessageClient {
|
|
62
|
+
@Request('sign-in/done', { single: true })
|
|
63
|
+
done(result: SignInResult): Promise<boolean> {
|
|
64
|
+
return null!; // Implementation handled by decorator
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const accepted = await client.done({ next: '/home' });
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The promise:
|
|
72
|
+
- resolves with the first response (a `@Listen` handler returning `void` or `Promise<void>` responds with `undefined`),
|
|
73
|
+
- rejects with the error the host throws or the transport reports (also when sending fails),
|
|
74
|
+
- rejects with rxjs' `EmptyError` when the host completes without responding (e.g. a handler returning `EMPTY`).
|
|
75
|
+
|
|
76
|
+
TypeScript only accepts `{ single: true }` on methods typed to return a `Promise`. Without the option, `@Request` keeps returning an `Observable` of all responses.
|
|
77
|
+
|
|
50
78
|
## Usage
|
|
51
79
|
|
|
52
80
|
### Iframe Communication
|
|
@@ -716,13 +744,14 @@ service.initialize(peerConnection);
|
|
|
716
744
|
|
|
717
745
|
### Decorators
|
|
718
746
|
|
|
719
|
-
- **`@Request(path: string)`** - Decorator for sending messages
|
|
747
|
+
- **`@Request(path: string, options?: RequestOptions)`** - Decorator for sending messages; `{ single: true }` returns a `Promise` of the first response
|
|
720
748
|
- **`@Listen(path: string)`** - Decorator for receiving messages
|
|
721
749
|
|
|
722
750
|
### Types
|
|
723
751
|
|
|
724
752
|
- **`Message`** - Message payload structure
|
|
725
753
|
- **`MessageResponse`** - Response payload structure
|
|
754
|
+
- **`RequestOptions`** - Options of `@Request` (`single`)
|
|
726
755
|
- **`UpcomingMessage<T>`** - Incoming message with sender info (iframe)
|
|
727
756
|
|
|
728
757
|
## Requirements
|
package/broadcast/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { BroadcastMessageClient } from './message-client.cjs';
|
|
2
2
|
export { BroadcastMessageHost } from './message-host.cjs';
|
|
3
3
|
export { BroadcastMessageService } from './message-service.cjs';
|
|
4
|
-
export { Request } from '../request.decorator.cjs';
|
|
4
|
+
export { Request, RequestOptions, SingleRequestDecorator } from '../request.decorator.cjs';
|
|
5
5
|
export { Listen } from '../listen.decorator.cjs';
|
|
6
6
|
import 'rxjs';
|
|
7
7
|
import '../message-client.cjs';
|
package/broadcast/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { BroadcastMessageClient } from './message-client.js';
|
|
2
2
|
export { BroadcastMessageHost } from './message-host.js';
|
|
3
3
|
export { BroadcastMessageService } from './message-service.js';
|
|
4
|
-
export { Request } from '../request.decorator.js';
|
|
4
|
+
export { Request, RequestOptions, SingleRequestDecorator } from '../request.decorator.js';
|
|
5
5
|
export { Listen } from '../listen.decorator.js';
|
|
6
6
|
import 'rxjs';
|
|
7
7
|
import '../message-client.js';
|
package/chrome/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Request } from '../request.decorator.cjs';
|
|
1
|
+
export { Request, RequestOptions, SingleRequestDecorator } from '../request.decorator.cjs';
|
|
2
2
|
export { Listen } from '../listen.decorator.cjs';
|
|
3
3
|
export { ChromeMessageClient } from './message-client.cjs';
|
|
4
4
|
export { ChromeMessageHost } from './message-host.cjs';
|
package/chrome/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Request } from '../request.decorator.js';
|
|
1
|
+
export { Request, RequestOptions, SingleRequestDecorator } from '../request.decorator.js';
|
|
2
2
|
export { Listen } from '../listen.decorator.js';
|
|
3
3
|
export { ChromeMessageClient } from './message-client.js';
|
|
4
4
|
export { ChromeMessageHost } from './message-host.js';
|
package/fesm/messenger.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ReplaySubject, merge, Subject, from, Observable, of } from 'rxjs';
|
|
1
|
+
import { ReplaySubject, merge, firstValueFrom, Subject, from, Observable, of } from 'rxjs';
|
|
2
2
|
import { filter, map, takeWhile, takeUntil } from 'rxjs/operators';
|
|
3
3
|
|
|
4
4
|
const MESSAGE_LISTENERS = Symbol('Message Listeners');
|
|
@@ -9,24 +9,35 @@ const LISTEN = Symbol('Listen');
|
|
|
9
9
|
const TERMINATE_MESSAGE$ = Symbol('Terminate Message');
|
|
10
10
|
const RESPONSE = Symbol('Response');
|
|
11
11
|
|
|
12
|
-
function Request(path) {
|
|
12
|
+
function Request(path, { single = false } = {}) {
|
|
13
13
|
return function (target, key, descriptor) {
|
|
14
14
|
descriptor.value = function (message) {
|
|
15
15
|
const messageId = this[GET_NEW_ID]();
|
|
16
16
|
// Replays a send failure so it reaches the caller even if they subscribe late.
|
|
17
17
|
const sendFailure$ = new ReplaySubject(1);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
const response$ = responsesOf(merge(this[RESPONSES$], sendFailure$), messageId);
|
|
19
|
+
// A promise subscribes before sending, so a response that arrives synchronously is not missed
|
|
20
|
+
const result = single ? firstValueFrom(response$) : response$;
|
|
21
|
+
const send = () => this[SEND]({ body: message, id: messageId, path });
|
|
22
|
+
// A promise-returning stub must not throw synchronously: a throwing send becomes a rejection
|
|
23
|
+
const sent = single ? new Promise(resolve => resolve(send())) : Promise.resolve(send());
|
|
24
|
+
sent.catch(error => sendFailure$.next({ id: messageId, completed: true, error }));
|
|
25
|
+
return result;
|
|
26
26
|
};
|
|
27
27
|
return descriptor;
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Bodies of the responses to one message, until the host completes or reports an error
|
|
32
|
+
*/
|
|
33
|
+
function responsesOf(responses$, messageId) {
|
|
34
|
+
return responses$.pipe(filter(({ id }) => id === messageId), map((data) => {
|
|
35
|
+
const { error } = data;
|
|
36
|
+
if (error)
|
|
37
|
+
throw error;
|
|
38
|
+
return data;
|
|
39
|
+
}), takeWhile((res) => !res.completed), map(({ body }) => body));
|
|
40
|
+
}
|
|
30
41
|
|
|
31
42
|
/**
|
|
32
43
|
* Decorate a generator method to listen messages of a domain and reply them by using `yield`
|
package/fesm/messenger.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messenger.mjs","sources":["../../../../libs/messenger/src/selectors.ts","../../../../libs/messenger/src/request.decorator.ts","../../../../libs/messenger/src/listen.decorator.ts","../../../../libs/messenger/src/message-client.ts","../../../../libs/messenger/src/message-host.ts"],"sourcesContent":[null,null,null,null,null],"names":[],"mappings":";;;MAAa,iBAAiB,GAAG,MAAM,CAAC,mBAAmB;MAC9C,UAAU,GAAG,MAAM,CAAC,WAAW;MAC/B,IAAI,GAAG,MAAM,CAAC,MAAM;MACpB,UAAU,GAAG,MAAM,CAAC,YAAY;MAChC,MAAM,GAAG,MAAM,CAAC,QAAQ;MACxB,kBAAkB,GAAG,MAAM,CAAC,mBAAmB;MAC/C,QAAQ,GAAG,MAAM,CAAC,UAAU;;
|
|
1
|
+
{"version":3,"file":"messenger.mjs","sources":["../../../../libs/messenger/src/selectors.ts","../../../../libs/messenger/src/request.decorator.ts","../../../../libs/messenger/src/listen.decorator.ts","../../../../libs/messenger/src/message-client.ts","../../../../libs/messenger/src/message-host.ts"],"sourcesContent":[null,null,null,null,null],"names":[],"mappings":";;;MAAa,iBAAiB,GAAG,MAAM,CAAC,mBAAmB;MAC9C,UAAU,GAAG,MAAM,CAAC,WAAW;MAC/B,IAAI,GAAG,MAAM,CAAC,MAAM;MACpB,UAAU,GAAG,MAAM,CAAC,YAAY;MAChC,MAAM,GAAG,MAAM,CAAC,QAAQ;MACxB,kBAAkB,GAAG,MAAM,CAAC,mBAAmB;MAC/C,QAAQ,GAAG,MAAM,CAAC,UAAU;;ACyDnC,SAAU,OAAO,CAAC,IAAY,EAAE,EAAE,MAAM,GAAG,KAAK,EAAA,GAAqB,EAAE,EAAA;AAC3E,IAAA,OAAO,UAAS,MAAM,EAAE,GAAG,EAAE,UAAwC,EAAA;AACnE,QAAA,UAAU,CAAC,KAAK,GAAG,UAA8B,OAAY,EAAA;AAC3D,YAAA,MAAM,SAAS,GAAG,IAAI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B,EAAE;oBAC5F,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,EAAE,GAAG,QAAQ,CAAC,CAAC;gBACtD;YACF;AAEA,YAAA,YAAY,GAAG,YAAY,CAAC,SAAS;QACvC;AAEA,QAAA,OAAO,GAAG;IACZ;AACD;;;;"}
|
package/iframe/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Request } from '../request.decorator.cjs';
|
|
1
|
+
export { Request, RequestOptions, SingleRequestDecorator } from '../request.decorator.cjs';
|
|
2
2
|
export { Listen } from '../listen.decorator.cjs';
|
|
3
3
|
export { IframeMessageClient } from './message-client.cjs';
|
|
4
4
|
export { IframeMessageHost } from './message-host.cjs';
|
package/iframe/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Request } from '../request.decorator.js';
|
|
1
|
+
export { Request, RequestOptions, SingleRequestDecorator } from '../request.decorator.js';
|
|
2
2
|
export { Listen } from '../listen.decorator.js';
|
|
3
3
|
export { IframeMessageClient } from './message-client.js';
|
|
4
4
|
export { IframeMessageHost } from './message-host.js';
|
package/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Request } from './request.decorator.cjs';
|
|
1
|
+
export { Request, RequestOptions, SingleRequestDecorator } from './request.decorator.cjs';
|
|
2
2
|
export { Listen } from './listen.decorator.cjs';
|
|
3
3
|
export { Message } from './message.interface.cjs';
|
|
4
4
|
export { MessageResponse } from './message-response.type.cjs';
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Request } from './request.decorator.js';
|
|
1
|
+
export { Request, RequestOptions, SingleRequestDecorator } from './request.decorator.js';
|
|
2
2
|
export { Listen } from './listen.decorator.js';
|
|
3
3
|
export { Message } from './message.interface.js';
|
|
4
4
|
export { MessageResponse } from './message-response.type.js';
|
package/package.json
CHANGED
package/request.decorator.cjs
CHANGED
|
@@ -4,24 +4,35 @@ var rxjs = require('rxjs');
|
|
|
4
4
|
var operators = require('rxjs/operators');
|
|
5
5
|
var selectors = require('./selectors.cjs');
|
|
6
6
|
|
|
7
|
-
function Request(path) {
|
|
7
|
+
function Request(path, { single = false } = {}) {
|
|
8
8
|
return function (target, key, descriptor) {
|
|
9
9
|
descriptor.value = function (message) {
|
|
10
10
|
const messageId = this[selectors.GET_NEW_ID]();
|
|
11
11
|
// Replays a send failure so it reaches the caller even if they subscribe late.
|
|
12
12
|
const sendFailure$ = new rxjs.ReplaySubject(1);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
const response$ = responsesOf(rxjs.merge(this[selectors.RESPONSES$], sendFailure$), messageId);
|
|
14
|
+
// A promise subscribes before sending, so a response that arrives synchronously is not missed
|
|
15
|
+
const result = single ? rxjs.firstValueFrom(response$) : response$;
|
|
16
|
+
const send = () => this[selectors.SEND]({ body: message, id: messageId, path });
|
|
17
|
+
// A promise-returning stub must not throw synchronously: a throwing send becomes a rejection
|
|
18
|
+
const sent = single ? new Promise(resolve => resolve(send())) : Promise.resolve(send());
|
|
19
|
+
sent.catch(error => sendFailure$.next({ id: messageId, completed: true, error }));
|
|
20
|
+
return result;
|
|
21
21
|
};
|
|
22
22
|
return descriptor;
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Bodies of the responses to one message, until the host completes or reports an error
|
|
27
|
+
*/
|
|
28
|
+
function responsesOf(responses$, messageId) {
|
|
29
|
+
return responses$.pipe(operators.filter(({ id }) => id === messageId), operators.map((data) => {
|
|
30
|
+
const { error } = data;
|
|
31
|
+
if (error)
|
|
32
|
+
throw error;
|
|
33
|
+
return data;
|
|
34
|
+
}), operators.takeWhile((res) => !res.completed), operators.map(({ body }) => body));
|
|
35
|
+
}
|
|
25
36
|
|
|
26
37
|
exports.Request = Request;
|
|
27
38
|
//# sourceMappingURL=request.decorator.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.decorator.cjs","sources":["../../../libs/messenger/src/request.decorator.ts"],"sourcesContent":[null],"names":["GET_NEW_ID","ReplaySubject","
|
|
1
|
+
{"version":3,"file":"request.decorator.cjs","sources":["../../../libs/messenger/src/request.decorator.ts"],"sourcesContent":[null],"names":["GET_NEW_ID","ReplaySubject","merge","RESPONSES$","firstValueFrom","SEND","filter","map","takeWhile"],"mappings":";;;;;;AA+DM,SAAU,OAAO,CAAC,IAAY,EAAE,EAAE,MAAM,GAAG,KAAK,EAAA,GAAqB,EAAE,EAAA;AAC3E,IAAA,OAAO,UAAS,MAAM,EAAE,GAAG,EAAE,UAAwC,EAAA;AACnE,QAAA,UAAU,CAAC,KAAK,GAAG,UAA8B,OAAY,EAAA;AAC3D,YAAA,MAAM,SAAS,GAAG,IAAI,CAACA,oBAAU,CAAC,EAAE;;AAEpC,YAAA,MAAM,YAAY,GAAG,IAAIC,kBAAa,CAAkB,CAAC,CAAC;AAC1D,YAAA,MAAM,SAAS,GAAG,WAAW,CAACC,UAAK,CAAC,IAAI,CAACC,oBAAU,CAAC,EAAE,YAAY,CAAC,EAAE,SAAS,CAAC;;AAE/E,YAAA,MAAM,MAAM,GAAG,MAAM,GAAGC,mBAAc,CAAC,SAAS,CAAC,GAAG,SAAS;YAC7D,MAAM,IAAI,GAAG,MAAM,IAAI,CAACC,cAAI,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;;AAErE,YAAA,MAAM,IAAI,GAAG,MAAM,GAAG,IAAI,OAAO,CAAO,OAAO,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;YAE7F,IAAI,CAAC,KAAK,CAAC,KAAK,IAAI,YAAY,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;AAEjF,YAAA,OAAO,MAAM;AACf,QAAA,CAAC;AAED,QAAA,OAAO,UAAU;AACnB,IAAA,CAAC;AACH;AAEA;;AAEG;AACH,SAAS,WAAW,CAAC,UAAuC,EAAE,SAAiB,EAAA;IAC7E,OAAO,UAAU,CAAC,IAAI,CACpBC,gBAAM,CAAC,CAAC,EAAC,EAAE,EAAC,KAAK,EAAE,KAAK,SAAS,CAAC,EAClCC,aAAG,CAAC,CAAC,IAAI,KAAI;AACX,QAAA,MAAM,EAAE,KAAK,EAAE,GAAG,IAA4B;AAE9C,QAAA,IAAI,KAAK;AAAE,YAAA,MAAM,KAAK;AAEtB,QAAA,OAAO,IAAiC;IAC1C,CAAC,CAAC,EACFC,mBAAS,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,EAClCD,aAAG,CAAC,CAAC,EAAE,IAAI,EAA8B,KAAK,IAAI,CAAC,CACpD;AACH;;;;"}
|
package/request.decorator.d.cts
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Options of the {@link Request} decorator
|
|
3
|
+
*/
|
|
4
|
+
interface RequestOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Return a `Promise` of the first response instead of an `Observable` of all responses
|
|
7
|
+
*
|
|
8
|
+
* The promise resolves with the first response, rejects with the error the host or the transport reports,
|
|
9
|
+
* and rejects with rxjs' `EmptyError` when the host completes without responding.
|
|
10
|
+
* Type the stub's return value as a `Promise`.
|
|
11
|
+
*
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
single?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Decorator of a {@link Request} stub that returns a `Promise` (`{ single: true }`)
|
|
18
|
+
*/
|
|
19
|
+
type SingleRequestDecorator = <T extends (...args: never[]) => Promise<unknown>>(target: object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
|
|
20
|
+
/**
|
|
21
|
+
* Turns a method stub of a {@link MessageClient} into a request to the hosts listening on `path`
|
|
22
|
+
*
|
|
23
|
+
* The request is sent **eagerly**, when the method is called, not when the returned `Observable` is subscribed:
|
|
24
|
+
* calling the method without subscribing still sends the message (and subscribing twice does not send it twice).
|
|
25
|
+
* Subscribe right away, because responses that arrive before the subscription are not replayed.
|
|
26
|
+
*
|
|
27
|
+
* By default the stub returns an `Observable` that emits every response and completes when the host completes.
|
|
28
|
+
* With `{ single: true }` it returns a `Promise` of the first response instead; see {@link RequestOptions.single}.
|
|
29
|
+
*
|
|
30
|
+
* ```typescript
|
|
31
|
+
* class Client extends IframeMessageClient {
|
|
32
|
+
* @Request('progress')
|
|
33
|
+
* progress(jobId: string): Observable<number> {
|
|
34
|
+
* return null!;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* @Request('done', { single: true })
|
|
38
|
+
* done(result: Result): Promise<boolean> {
|
|
39
|
+
* return null!;
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* const accepted = await client.done(result);
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @param path Acts like an api endpoint path of an XHR for extensions messaging
|
|
47
|
+
* @param options Whether to return a `Promise` of the first response
|
|
48
|
+
*/
|
|
49
|
+
declare function Request(path: string, options: RequestOptions & {
|
|
50
|
+
single: true;
|
|
51
|
+
}): SingleRequestDecorator;
|
|
52
|
+
declare function Request(path: string, options?: RequestOptions): MethodDecorator;
|
|
2
53
|
|
|
3
54
|
export { Request };
|
|
55
|
+
export type { RequestOptions, SingleRequestDecorator };
|
package/request.decorator.d.ts
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Options of the {@link Request} decorator
|
|
3
|
+
*/
|
|
4
|
+
interface RequestOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Return a `Promise` of the first response instead of an `Observable` of all responses
|
|
7
|
+
*
|
|
8
|
+
* The promise resolves with the first response, rejects with the error the host or the transport reports,
|
|
9
|
+
* and rejects with rxjs' `EmptyError` when the host completes without responding.
|
|
10
|
+
* Type the stub's return value as a `Promise`.
|
|
11
|
+
*
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
single?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Decorator of a {@link Request} stub that returns a `Promise` (`{ single: true }`)
|
|
18
|
+
*/
|
|
19
|
+
type SingleRequestDecorator = <T extends (...args: never[]) => Promise<unknown>>(target: object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
|
|
20
|
+
/**
|
|
21
|
+
* Turns a method stub of a {@link MessageClient} into a request to the hosts listening on `path`
|
|
22
|
+
*
|
|
23
|
+
* The request is sent **eagerly**, when the method is called, not when the returned `Observable` is subscribed:
|
|
24
|
+
* calling the method without subscribing still sends the message (and subscribing twice does not send it twice).
|
|
25
|
+
* Subscribe right away, because responses that arrive before the subscription are not replayed.
|
|
26
|
+
*
|
|
27
|
+
* By default the stub returns an `Observable` that emits every response and completes when the host completes.
|
|
28
|
+
* With `{ single: true }` it returns a `Promise` of the first response instead; see {@link RequestOptions.single}.
|
|
29
|
+
*
|
|
30
|
+
* ```typescript
|
|
31
|
+
* class Client extends IframeMessageClient {
|
|
32
|
+
* @Request('progress')
|
|
33
|
+
* progress(jobId: string): Observable<number> {
|
|
34
|
+
* return null!;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* @Request('done', { single: true })
|
|
38
|
+
* done(result: Result): Promise<boolean> {
|
|
39
|
+
* return null!;
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* const accepted = await client.done(result);
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @param path Acts like an api endpoint path of an XHR for extensions messaging
|
|
47
|
+
* @param options Whether to return a `Promise` of the first response
|
|
48
|
+
*/
|
|
49
|
+
declare function Request(path: string, options: RequestOptions & {
|
|
50
|
+
single: true;
|
|
51
|
+
}): SingleRequestDecorator;
|
|
52
|
+
declare function Request(path: string, options?: RequestOptions): MethodDecorator;
|
|
2
53
|
|
|
3
54
|
export { Request };
|
|
55
|
+
export type { RequestOptions, SingleRequestDecorator };
|
package/request.decorator.js
CHANGED
|
@@ -1,25 +1,36 @@
|
|
|
1
|
-
import { ReplaySubject, merge } from 'rxjs';
|
|
1
|
+
import { ReplaySubject, merge, firstValueFrom } from 'rxjs';
|
|
2
2
|
import { filter, map, takeWhile } from 'rxjs/operators';
|
|
3
|
-
import { GET_NEW_ID, SEND
|
|
3
|
+
import { GET_NEW_ID, RESPONSES$, SEND } from './selectors.js';
|
|
4
4
|
|
|
5
|
-
function Request(path) {
|
|
5
|
+
function Request(path, { single = false } = {}) {
|
|
6
6
|
return function (target, key, descriptor) {
|
|
7
7
|
descriptor.value = function (message) {
|
|
8
8
|
const messageId = this[GET_NEW_ID]();
|
|
9
9
|
// Replays a send failure so it reaches the caller even if they subscribe late.
|
|
10
10
|
const sendFailure$ = new ReplaySubject(1);
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
const response$ = responsesOf(merge(this[RESPONSES$], sendFailure$), messageId);
|
|
12
|
+
// A promise subscribes before sending, so a response that arrives synchronously is not missed
|
|
13
|
+
const result = single ? firstValueFrom(response$) : response$;
|
|
14
|
+
const send = () => this[SEND]({ body: message, id: messageId, path });
|
|
15
|
+
// A promise-returning stub must not throw synchronously: a throwing send becomes a rejection
|
|
16
|
+
const sent = single ? new Promise(resolve => resolve(send())) : Promise.resolve(send());
|
|
17
|
+
sent.catch(error => sendFailure$.next({ id: messageId, completed: true, error }));
|
|
18
|
+
return result;
|
|
19
19
|
};
|
|
20
20
|
return descriptor;
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Bodies of the responses to one message, until the host completes or reports an error
|
|
25
|
+
*/
|
|
26
|
+
function responsesOf(responses$, messageId) {
|
|
27
|
+
return responses$.pipe(filter(({ id }) => id === messageId), map((data) => {
|
|
28
|
+
const { error } = data;
|
|
29
|
+
if (error)
|
|
30
|
+
throw error;
|
|
31
|
+
return data;
|
|
32
|
+
}), takeWhile((res) => !res.completed), map(({ body }) => body));
|
|
33
|
+
}
|
|
23
34
|
|
|
24
35
|
export { Request };
|
|
25
36
|
//# sourceMappingURL=request.decorator.js.map
|
package/request.decorator.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.decorator.js","sources":["../../../libs/messenger/src/request.decorator.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"request.decorator.js","sources":["../../../libs/messenger/src/request.decorator.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AA+DM,SAAU,OAAO,CAAC,IAAY,EAAE,EAAE,MAAM,GAAG,KAAK,EAAA,GAAqB,EAAE,EAAA;AAC3E,IAAA,OAAO,UAAS,MAAM,EAAE,GAAG,EAAE,UAAwC,EAAA;AACnE,QAAA,UAAU,CAAC,KAAK,GAAG,UAA8B,OAAY,EAAA;AAC3D,YAAA,MAAM,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC,EAAE;;AAEpC,YAAA,MAAM,YAAY,GAAG,IAAI,aAAa,CAAkB,CAAC,CAAC;AAC1D,YAAA,MAAM,SAAS,GAAG,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,YAAY,CAAC,EAAE,SAAS,CAAC;;AAE/E,YAAA,MAAM,MAAM,GAAG,MAAM,GAAG,cAAc,CAAC,SAAS,CAAC,GAAG,SAAS;YAC7D,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;;AAErE,YAAA,MAAM,IAAI,GAAG,MAAM,GAAG,IAAI,OAAO,CAAO,OAAO,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;YAE7F,IAAI,CAAC,KAAK,CAAC,KAAK,IAAI,YAAY,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;AAEjF,YAAA,OAAO,MAAM;AACf,QAAA,CAAC;AAED,QAAA,OAAO,UAAU;AACnB,IAAA,CAAC;AACH;AAEA;;AAEG;AACH,SAAS,WAAW,CAAC,UAAuC,EAAE,SAAiB,EAAA;IAC7E,OAAO,UAAU,CAAC,IAAI,CACpB,MAAM,CAAC,CAAC,EAAC,EAAE,EAAC,KAAK,EAAE,KAAK,SAAS,CAAC,EAClC,GAAG,CAAC,CAAC,IAAI,KAAI;AACX,QAAA,MAAM,EAAE,KAAK,EAAE,GAAG,IAA4B;AAE9C,QAAA,IAAI,KAAK;AAAE,YAAA,MAAM,KAAK;AAEtB,QAAA,OAAO,IAAiC;IAC1C,CAAC,CAAC,EACF,SAAS,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,EAClC,GAAG,CAAC,CAAC,EAAE,IAAI,EAA8B,KAAK,IAAI,CAAC,CACpD;AACH;;;;"}
|
package/rtc/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { RTCMessageService } from './message-service.cjs';
|
|
2
2
|
export { RTCMessageClient } from './message-client.cjs';
|
|
3
3
|
export { RTCMessageHost } from './message-host.cjs';
|
|
4
|
-
export { Request } from '../request.decorator.cjs';
|
|
4
|
+
export { Request, RequestOptions, SingleRequestDecorator } from '../request.decorator.cjs';
|
|
5
5
|
export { Listen } from '../listen.decorator.cjs';
|
|
6
6
|
import './rtc.type.cjs';
|
|
7
7
|
import 'rxjs';
|
package/rtc/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { RTCMessageService } from './message-service.js';
|
|
2
2
|
export { RTCMessageClient } from './message-client.js';
|
|
3
3
|
export { RTCMessageHost } from './message-host.js';
|
|
4
|
-
export { Request } from '../request.decorator.js';
|
|
4
|
+
export { Request, RequestOptions, SingleRequestDecorator } from '../request.decorator.js';
|
|
5
5
|
export { Listen } from '../listen.decorator.js';
|
|
6
6
|
import './rtc.type.js';
|
|
7
7
|
import 'rxjs';
|
package/worker/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { WorkerMessageService } from './message-service.cjs';
|
|
2
2
|
export { WorkerMessageClient } from './message-client.cjs';
|
|
3
3
|
export { WorkerMessageHost } from './message-host.cjs';
|
|
4
|
-
export { Request } from '../request.decorator.cjs';
|
|
4
|
+
export { Request, RequestOptions, SingleRequestDecorator } from '../request.decorator.cjs';
|
|
5
5
|
export { Listen } from '../listen.decorator.cjs';
|
|
6
6
|
import './initializer.cjs';
|
|
7
7
|
import 'rxjs';
|
package/worker/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { WorkerMessageService } from './message-service.js';
|
|
2
2
|
export { WorkerMessageClient } from './message-client.js';
|
|
3
3
|
export { WorkerMessageHost } from './message-host.js';
|
|
4
|
-
export { Request } from '../request.decorator.js';
|
|
4
|
+
export { Request, RequestOptions, SingleRequestDecorator } from '../request.decorator.js';
|
|
5
5
|
export { Listen } from '../listen.decorator.js';
|
|
6
6
|
import './initializer.js';
|
|
7
7
|
import 'rxjs';
|