rxjs#TeardownLogic TypeScript Examples
The following examples show how to use
rxjs#TeardownLogic.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: context-service.ts From scion-microfrontend-platform with Eclipse Public License 2.0 | 6 votes |
/**
* Looks up the context tree for a value associated with the given name.
*
* @param name - The name of the value to return.
* @param options - Options to control context lookup.
* @return An Observable that emits the context value associated with the given key and then completes.
* When the requested value is not found in a context, the Observable emits `null` and then completes.
*/
private lookupContextValue$<T>(name: string, options?: ContextLookupOptions): Observable<T | T[] | null> {
return new Observable((observer: Observer<T | T[] | null>): TeardownLogic => {
const replyTo = UUID.randomUUID();
const unsubscribe$ = new Subject<void>();
const contextValueLookupRequest = Contexts.newContextValueLookupRequest(name, replyTo, options);
// Wait until the reply is received.
Beans.get(MessageClient).observe$<T | T[] | null | undefined>(replyTo)
.pipe(
take(1),
map(reply => reply.headers.get(MessageHeaders.Status) === ResponseStatusCodes.OK ? (reply.body ?? null) : null),
takeUntil(unsubscribe$),
)
.subscribe(observer);
// Send the request.
Promise.all([whenSubscribedToReplyTopic(replyTo), this._whenContextTreeChangeListenerInstalled])
.then(() => window.parent.postMessage(contextValueLookupRequest, '*'))
.catch(error => observer.error(error));
return (): void => unsubscribe$.next();
});
}
Example #2
Source File: context-service.ts From scion-microfrontend-platform with Eclipse Public License 2.0 | 6 votes |
/**
* Looks up the context names of all values registered in the current and parent contexts.
*
* @return An Observable that emits the names of all values registered in the current and parent contexts and then completes.
*/
private lookupContextNames$(): Observable<Set<string>> {
return new Observable((observer: Observer<Set<string>>): TeardownLogic => {
const replyTo = UUID.randomUUID();
const unsubscribe$ = new Subject<void>();
const contextNamesLookupRequest = Contexts.newContextTreeNamesLookupRequest(replyTo);
// Wait until the reply is received.
Beans.get(MessageClient).observe$<Set<string>>(replyTo)
.pipe(
take(1),
map(reply => reply.headers.get(MessageHeaders.Status) === ResponseStatusCodes.OK ? reply.body! : new Set<string>()),
takeUntil(unsubscribe$),
)
.subscribe(observer);
// Send the request.
Promise.all([whenSubscribedToReplyTopic(replyTo), this._whenContextTreeChangeListenerInstalled])
.then(() => window.parent.postMessage(contextNamesLookupRequest, '*'))
.catch(error => observer.error(error));
return (): void => unsubscribe$.next();
});
}
Example #3
Source File: broker-gateway.ts From scion-microfrontend-platform with Eclipse Public License 2.0 | 6 votes |
public requestReply$<T = any>(channel: MessagingChannel, message: IntentMessage | TopicMessage): Observable<TopicMessage<T>> {
return new Observable((observer: Observer<TopicMessage>): TeardownLogic => {
if (isPlatformStopped()) {
observer.error(GatewayErrors.PLATFORM_STOPPED_ERROR);
return noop;
}
const replyTo = UUID.randomUUID();
const unsubscribe$ = new Subject<void>();
const requestError$ = new Subject<never>();
// Add 'ReplyTo' topic to the message headers where to receive the response(s).
message.headers.set(MessageHeaders.ReplyTo, replyTo);
// Receive replies sent to the reply topic.
merge(this.subscribeToTopic$<T>(replyTo), requestError$)
.pipe(takeUntil(merge(this._platformStopping$, unsubscribe$)))
.subscribe({
next: reply => observer.next(reply),
error: error => observer.error(error),
complete: noop, // As per the API, the Observable never completes.
});
// Post the request to the broker.
this.postMessage(channel, message)
.catch(error => requestError$.error(error));
return (): void => unsubscribe$.next();
});
}
Example #4
Source File: broker-gateway.ts From scion-microfrontend-platform with Eclipse Public License 2.0 | 6 votes |
public subscribeToTopic$<T>(topic: string): Observable<TopicMessage<T>> {
return new Observable((observer: Observer<TopicMessage>): TeardownLogic => {
if (isPlatformStopped()) {
observer.error(GatewayErrors.PLATFORM_STOPPED_ERROR);
return noop;
}
const subscriberId = UUID.randomUUID();
const unsubscribe$ = new Subject<void>();
const subscribeError$ = new Subject<never>();
// Receive messages sent to the given topic.
merge(this.message$, subscribeError$)
.pipe(
filterByChannel<TopicMessage>(MessagingChannel.Topic),
filterByMessageHeader({key: MessageHeaders.ɵTopicSubscriberId, value: subscriberId}),
pluckMessage(),
takeUntil(merge(this._platformStopping$, unsubscribe$)),
finalize(() => this.unsubscribeFromTopic(topic, subscriberId)),
)
.subscribe({
next: reply => observer.next(reply),
error: error => observer.error(error),
complete: noop, // As per the API, the Observable never completes.
});
// Post the topic subscription to the broker.
const topicSubscribeMessage: TopicSubscribeCommand = {subscriberId, topic, headers: new Map()};
this.postMessage(MessagingChannel.TopicSubscribe, topicSubscribeMessage)
.catch(error => subscribeError$.error(error));
return (): void => unsubscribe$.next();
});
}