-
Notifications
You must be signed in to change notification settings - Fork 1
/
RemoteSourceNode.ts
65 lines (57 loc) · 2.18 KB
/
RemoteSourceNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { RemoteNode, RemoteNodeOptions } from '../RemoteNode';
import { DataFrame } from '../../data/DataFrame';
import { Edge } from '../../graph/Edge';
import { ModelBuilder } from '../../ModelBuilder';
import { RemoteService } from '../../service/RemoteService';
import { SourceNode, SourceNodeOptions } from '../SourceNode';
import { PushCompletedEvent, PushError } from '../../graph/events';
import { Constructor } from '../../data/decorators';
/**
* Remote source node
*/
export class RemoteSourceNode<
Out extends DataFrame,
S extends RemoteService,
N extends RemoteNode<Out, Out, S> = RemoteNode<Out, Out, S>,
> extends SourceNode<Out> {
protected remoteNode: N;
constructor(options?: RemoteSourceNodeOptions<S>) {
super(options);
this.remoteNode = new (options.type ?? RemoteNode)(options, this) as N;
this.uid = `${this.uid}-source`;
this.once('build', this._onRemoteBuild.bind(this));
this.on('error', this._onDownstreamError.bind(this));
this.on('completed', this._onDownstreamCompleted.bind(this));
}
private _onRemoteBuild(graphBuilder: ModelBuilder<any, any>): Promise<boolean> {
// Add a remote node before this node
this.remoteNode.graph = this.graph;
graphBuilder.addNode(this.remoteNode);
graphBuilder.addEdge(new Edge(this.remoteNode, this));
return this.remoteNode.emitAsync('build', graphBuilder);
}
public onPull(): Promise<Out> {
return new Promise((resolve, reject) => {
this.remoteNode
.pull()
.then(() => {
resolve(undefined);
})
.catch(reject);
});
}
private _onDownstreamError(error: PushError): void {
this.remoteNode.emit('error', error);
}
private _onDownstreamCompleted(event: PushCompletedEvent): void {
this.remoteNode.emit('completed', event);
}
}
export interface RemoteSourceNodeOptions<S extends RemoteService> extends SourceNodeOptions, RemoteNodeOptions<S> {
/**
* Node type to use
*
* @default RemoteNode a normal remote node
*/
type?: Constructor<RemoteNode<any, any, S>>;
}