UNPKG

1.16 kBPlain TextView Raw
1import {connected} from 'process';
2
3import {RuntimeException} from '../exception';
4import {OutputOptions} from './options';
5import {OutputProducer} from './producer';
6import {Snapshot} from '../snapshot';
7
8export class InterprocessOutput implements OutputProducer {
9 constructor(options: OutputOptions) {}
10
11 initialize() {
12 if (connected === false) {
13 throw new RuntimeException('This application is not connected to a parent application and therefore cannot use the IPC functionality');
14 }
15 }
16
17 async write<V>(snapshot: Snapshot<V>): Promise<void> {
18 this.send(snapshot);
19 }
20
21 async exception(exception: Error & {originalStack?: string, zoneAwareStack?: string}) {
22 this.send({
23 exception: {
24 name: exception.name,
25 message: exception.message,
26 stack: exception.stack,
27 originalStack: exception.originalStack,
28 zoneAwareStack: exception.zoneAwareStack
29 }});
30 }
31
32 private send<T>(message: T) {
33 if (process.send == null) {
34 throw new RuntimeException(`Process (${process.pid}) is not connected to a parent process`);
35 }
36 return Promise.resolve(process.send(message));
37 }
38}