Skip to content

Commit

Permalink
Add default Dispatcher StreamDelegate; Add debug instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hershal committed Sep 7, 2017
1 parent 08be8f7 commit 63b3253
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class DispatcherStandardInputSync implements IDispatcher {
* Otherwise, the Dispatcher publishes the output to the delegate. */
export class DispatcherStandardInputStream implements IDispatcher, IStreamHandlerDelegate {
public outputStreamDelegate: IStreamDelegate;

private streamHandler: StreamHandler;
private debug = Debug('DispatcherStandardInputStream');

Expand All @@ -56,6 +57,8 @@ export class DispatcherStandardInputStream implements IDispatcher, IStreamHandle
this.streamHandler = new StreamHandler();
this.streamHandler.delegate = this;
this.inputStream = inputStream;

this.debug('Created with Output Stream Delegate: %o', outputStreamDelegate);
}

public dispatch(operation: IStreamingOperation, args: string[]): Promise<void> {
Expand Down Expand Up @@ -120,11 +123,19 @@ export default class Dispatch {
cat: {dispatcher: DispatcherStandardInputStream, operation: TextOperations.Cat},
/* join: {dispIInputStreamDelegaterStandardInputStream, operation: TextOperations.Join} */
};

public streamDelegate?: IStreamDelegate;

private debug = Debug('Dispatch');

constructor(delegate?: IStreamDelegate) {
this.streamDelegate = delegate;
if (!delegate) {
this.streamDelegate = new DispatchDelegateConsoleLog();
this.debug('Stream delegate not given, installing ConsoleLog StreamDelegate');
} else {
this.streamDelegate = delegate;
this.debug('Installing custom Stream Delegate');
}
this.debug('Created with Stream Delegate: %o', this.streamDelegate);
}

public dispatch(program: string, args: string[]): Promise<string[]> {
Expand Down Expand Up @@ -158,12 +169,16 @@ interface IOperationHashItem {


export class DispatchDelegateConsoleLog implements IStreamDelegate {
private debug = Debug('DispatchDelegateConsoleLog');

public streamDidReceiveChunk(line: string) {
this.debug('streamDidReceiveChunk: %o', line);
/* tslint:disable-next-line */
console.log(line);
}

public streamDidEnd() {
this.debug('streamDidEnd');
/* nothing to do here */
}
}
2 changes: 2 additions & 0 deletions src/TextOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ export class Cat implements IStreamingOperation {

public parse(args: string[]): void {
this.stringToConcatenate = args.join(' ');
this.debug('Parsed arguments: %o', this.stringToConcatenate);
}

public run(data: string): string {
this.debug('Received string');
return data + this.stringToConcatenate;
}
}

0 comments on commit 63b3253

Please sign in to comment.