@amadeus-it-group/microfrontends-angular
Version:
Amadeus Micro Frontend Toolkit for Angular
105 lines (101 loc) • 3.55 kB
JavaScript
import { MessagePeer } from '@amadeus-it-group/microfrontends';
import { from } from 'rxjs';
import * as i0 from '@angular/core';
import { InjectionToken, inject, Injectable } from '@angular/core';
/**
* Injection token for {@link MessagePeerConfig}, required for the {@link MessagePeerService} configuration.
*/
const MESSAGE_PEER_CONFIG = new InjectionToken('MESSAGE_PEER_CONFIG');
/**
* Injection token for {@link PeerConnectionOptions} used as default options to pass to {@link MessagePeerService#connect}.
*/
const MESSAGE_PEER_CONNECT_OPTIONS = new InjectionToken('MESSAGE_PEER_CONNECT_OPTIONS');
/**
* Injection token for {@link PeerConnectionOptions} used as default options to pass to {@link MessagePeerService#listen}.
*/
const MESSAGE_PEER_LISTEN_OPTIONS = new InjectionToken('MESSAGE_PEER_LISTEN_OPTIONS');
/**
* Angular service that wraps {@link MessagePeer} and provides an observable for incoming messages and errors
* It is essentially just a wrapper around {@link MessagePeer} that integrates with Angular's DI system.
*/
class MessagePeerService {
#peer;
#diConnectOptions = inject(MESSAGE_PEER_CONNECT_OPTIONS, { optional: true });
#diListenOptions = inject(MESSAGE_PEER_LISTEN_OPTIONS, { optional: true });
/**
* @inheritDoc
*/
messages$;
/**
* @inheritDoc
*/
serviceMessages$;
/**
* @inheritDoc
*/
errors$;
constructor() {
const config = inject(MESSAGE_PEER_CONFIG);
this.#peer = new MessagePeer({
id: config.id,
knownMessages: config.knownMessages,
messageCheckStrategy: config.messageCheckStrategy,
});
this.messages$ = from(this.#peer.messages);
this.serviceMessages$ = from(this.#peer.serviceMessages);
this.errors$ = from(this.#peer.errors);
}
/**
* @inheritDoc
*/
get id() {
return this.#peer.id;
}
/**
* @inheritDoc
*/
get knownPeers() {
return this.#peer.knownPeers;
}
/**
* @inheritDoc
*/
listen(peerId, options) {
return this.#peer.listen(peerId, this.#diListenOptions ? { ...this.#diListenOptions, ...options } : options);
}
/**
* @inheritDoc
*/
connect(peerId, options) {
return this.#peer.connect(peerId, this.#diConnectOptions ? { ...this.#diConnectOptions, ...options } : options);
}
/**
* @inheritDoc
*/
registerMessage(message) {
this.#peer.registerMessage(message);
}
/**
* @inheritDoc
*/
send(message, options) {
this.#peer.send(message, options);
}
/**
* @inheritDoc
*/
disconnect(peerId) {
this.#peer.disconnect(peerId);
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.6", ngImport: i0, type: MessagePeerService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.6", ngImport: i0, type: MessagePeerService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.6", ngImport: i0, type: MessagePeerService, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}], ctorParameters: () => [] });
/**
* Generated bundle index. Do not edit.
*/
export { MESSAGE_PEER_CONFIG, MESSAGE_PEER_CONNECT_OPTIONS, MESSAGE_PEER_LISTEN_OPTIONS, MessagePeerService };
//# sourceMappingURL=amadeus-it-group-microfrontends-angular.mjs.map