mirror of https://github.com/grpc/grpc-node.git
grpc-js-core: merge all eventemitter overloads
This commit is contained in:
parent
78f6458888
commit
cc231f1f67
|
@ -3,6 +3,7 @@ import {Duplex} from 'stream';
|
||||||
|
|
||||||
import {CallCredentials} from './call-credentials';
|
import {CallCredentials} from './call-credentials';
|
||||||
import {Status} from './constants';
|
import {Status} from './constants';
|
||||||
|
import {EmitterAugmentation1} from './events';
|
||||||
import {Filter} from './filter';
|
import {Filter} from './filter';
|
||||||
import {FilterStackFactory} from './filter-stack';
|
import {FilterStackFactory} from './filter-stack';
|
||||||
import {Metadata} from './metadata';
|
import {Metadata} from './metadata';
|
||||||
|
@ -34,7 +35,7 @@ export interface WriteObject {
|
||||||
/**
|
/**
|
||||||
* This interface represents a duplex stream associated with a single gRPC call.
|
* This interface represents a duplex stream associated with a single gRPC call.
|
||||||
*/
|
*/
|
||||||
export interface CallStream extends ObjectDuplex<WriteObject, Buffer> {
|
export type CallStream = {
|
||||||
cancelWithStatus(status: Status, details: string): void;
|
cancelWithStatus(status: Status, details: string): void;
|
||||||
getPeer(): string;
|
getPeer(): string;
|
||||||
|
|
||||||
|
@ -43,37 +44,9 @@ export interface CallStream extends ObjectDuplex<WriteObject, Buffer> {
|
||||||
/* If the return value is null, the call has not ended yet. Otherwise, it has
|
/* If the return value is null, the call has not ended yet. Otherwise, it has
|
||||||
* ended with the specified status */
|
* ended with the specified status */
|
||||||
getStatus(): StatusObject|null;
|
getStatus(): StatusObject|null;
|
||||||
|
} & EmitterAugmentation1<'metadata', Metadata>
|
||||||
addListener(event: string, listener: Function): this;
|
& EmitterAugmentation1<'status', StatusObject>
|
||||||
emit(event: string|symbol, ...args: any[]): boolean;
|
& ObjectDuplex<WriteObject, Buffer>;
|
||||||
on(event: string, listener: Function): this;
|
|
||||||
once(event: string, listener: Function): this;
|
|
||||||
prependListener(event: string, listener: Function): this;
|
|
||||||
prependOnceListener(event: string, listener: Function): this;
|
|
||||||
removeListener(event: string, listener: Function): this;
|
|
||||||
|
|
||||||
addListener(event: 'metadata', listener: (metadata: Metadata) => void): this;
|
|
||||||
emit(event: 'metadata', metadata: Metadata): boolean;
|
|
||||||
on(event: 'metadata', listener: (metadata: Metadata) => void): this;
|
|
||||||
once(event: 'metadata', listener: (metadata: Metadata) => void): this;
|
|
||||||
prependListener(event: 'metadata', listener: (metadata: Metadata) => void):
|
|
||||||
this;
|
|
||||||
prependOnceListener(
|
|
||||||
event: 'metadata', listener: (metadata: Metadata) => void): this;
|
|
||||||
removeListener(event: 'metadata', listener: (metadata: Metadata) => void):
|
|
||||||
this;
|
|
||||||
|
|
||||||
addListener(event: 'status', listener: (status: StatusObject) => void): this;
|
|
||||||
emit(event: 'status', status: StatusObject): boolean;
|
|
||||||
on(event: 'status', listener: (status: StatusObject) => void): this;
|
|
||||||
once(event: 'status', listener: (status: StatusObject) => void): this;
|
|
||||||
prependListener(event: 'status', listener: (status: StatusObject) => void):
|
|
||||||
this;
|
|
||||||
prependOnceListener(
|
|
||||||
event: 'status', listener: (status: StatusObject) => void): this;
|
|
||||||
removeListener(event: 'status', listener: (status: StatusObject) => void):
|
|
||||||
this;
|
|
||||||
}
|
|
||||||
|
|
||||||
enum ReadState {
|
enum ReadState {
|
||||||
NO_DATA,
|
NO_DATA,
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import {EventEmitter} from 'events';
|
import {EventEmitter} from 'events';
|
||||||
|
import {EmitterAugmentation1} from './events';
|
||||||
import {Duplex, Readable, Writable} from 'stream';
|
import {Duplex, Readable, Writable} from 'stream';
|
||||||
|
|
||||||
import {CallStream, StatusObject, WriteObject} from './call-stream';
|
import {CallStream, StatusObject, WriteObject} from './call-stream';
|
||||||
|
@ -16,44 +17,27 @@ export class ServiceErrorImpl extends Error implements ServiceError {
|
||||||
metadata?: Metadata;
|
metadata?: Metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Call extends EventEmitter {
|
export type Call = {
|
||||||
cancel(): void;
|
cancel(): void;
|
||||||
getPeer(): string;
|
getPeer(): string;
|
||||||
|
} & EmitterAugmentation1<'metadata', Metadata>
|
||||||
|
& EmitterAugmentation1<'status', StatusObject>
|
||||||
|
& EventEmitter;
|
||||||
|
|
||||||
addListener(event: string, listener: Function): this;
|
export type ClientUnaryCall = Call;
|
||||||
emit(event: string|symbol, ...args: any[]): boolean;
|
|
||||||
on(event: string, listener: Function): this;
|
|
||||||
once(event: string, listener: Function): this;
|
|
||||||
prependListener(event: string, listener: Function): this;
|
|
||||||
prependOnceListener(event: string, listener: Function): this;
|
|
||||||
removeListener(event: string, listener: Function): this;
|
|
||||||
|
|
||||||
addListener(event: 'metadata', listener: (metadata: Metadata) => void): this;
|
export type ClientReadableStream<ResponseType> = {
|
||||||
emit(event: 'metadata', metadata: Metadata): boolean;
|
deserialize: (chunk: Buffer) => ResponseType;
|
||||||
on(event: 'metadata', listener: (metadata: Metadata) => void): this;
|
} & Call & ObjectReadable<ResponseType>;
|
||||||
once(event: 'metadata', listener: (metadata: Metadata) => void): this;
|
|
||||||
prependListener(event: 'metadata', listener: (metadata: Metadata) => void):
|
|
||||||
this;
|
|
||||||
prependOnceListener(
|
|
||||||
event: 'metadata', listener: (metadata: Metadata) => void): this;
|
|
||||||
removeListener(event: 'metadata', listener: (metadata: Metadata) => void):
|
|
||||||
this;
|
|
||||||
|
|
||||||
addListener(event: 'status', listener: (status: StatusObject) => void): this;
|
export type ClientWritableStream<RequestType> = {
|
||||||
emit(event: 'status', status: StatusObject): boolean;
|
serialize: (value: RequestType) => Buffer;
|
||||||
on(event: 'status', listener: (status: StatusObject) => void): this;
|
} & Call & ObjectWritable<RequestType>;
|
||||||
once(event: 'status', listener: (status: StatusObject) => void): this;
|
|
||||||
prependListener(event: 'status', listener: (status: StatusObject) => void):
|
|
||||||
this;
|
|
||||||
prependOnceListener(
|
|
||||||
event: 'status', listener: (status: StatusObject) => void): this;
|
|
||||||
removeListener(event: 'status', listener: (status: StatusObject) => void):
|
|
||||||
this;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ClientUnaryCall extends Call {}
|
export type ClientDuplexStream<RequestType, ResponseType> =
|
||||||
|
ClientWritableStream<RequestType> & ClientReadableStream<ResponseType>;
|
||||||
|
|
||||||
export class ClientUnaryCallImpl extends EventEmitter implements Call {
|
export class ClientUnaryCallImpl extends EventEmitter implements ClientUnaryCall {
|
||||||
constructor(private readonly call: CallStream) {
|
constructor(private readonly call: CallStream) {
|
||||||
super();
|
super();
|
||||||
call.on('metadata', (metadata: Metadata) => {
|
call.on('metadata', (metadata: Metadata) => {
|
||||||
|
@ -73,43 +57,6 @@ export class ClientUnaryCallImpl extends EventEmitter implements Call {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ClientReadableStream<ResponseType> extends
|
|
||||||
Call, ObjectReadable<ResponseType> {
|
|
||||||
deserialize: (chunk: Buffer) => ResponseType;
|
|
||||||
|
|
||||||
addListener(event: string, listener: Function): this;
|
|
||||||
emit(event: string|symbol, ...args: any[]): boolean;
|
|
||||||
on(event: string, listener: Function): this;
|
|
||||||
once(event: string, listener: Function): this;
|
|
||||||
prependListener(event: string, listener: Function): this;
|
|
||||||
prependOnceListener(event: string, listener: Function): this;
|
|
||||||
removeListener(event: string, listener: Function): this;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ClientWritableStream<RequestType> extends
|
|
||||||
Call, ObjectWritable<RequestType> {
|
|
||||||
serialize: (value: RequestType) => Buffer;
|
|
||||||
|
|
||||||
addListener(event: string, listener: Function): this;
|
|
||||||
emit(event: string|symbol, ...args: any[]): boolean;
|
|
||||||
on(event: string, listener: Function): this;
|
|
||||||
once(event: string, listener: Function): this;
|
|
||||||
prependListener(event: string, listener: Function): this;
|
|
||||||
prependOnceListener(event: string, listener: Function): this;
|
|
||||||
removeListener(event: string, listener: Function): this;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ClientDuplexStream<RequestType, ResponseType> extends
|
|
||||||
ClientWritableStream<RequestType>, ClientReadableStream<ResponseType> {
|
|
||||||
addListener(event: string, listener: Function): this;
|
|
||||||
emit(event: string|symbol, ...args: any[]): boolean;
|
|
||||||
on(event: string, listener: Function): this;
|
|
||||||
once(event: string, listener: Function): this;
|
|
||||||
prependListener(event: string, listener: Function): this;
|
|
||||||
prependOnceListener(event: string, listener: Function): this;
|
|
||||||
removeListener(event: string, listener: Function): this;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setUpReadableStream<ResponseType>(
|
function setUpReadableStream<ResponseType>(
|
||||||
stream: ClientReadableStream<ResponseType>, call: CallStream,
|
stream: ClientReadableStream<ResponseType>, call: CallStream,
|
||||||
deserialize: (chunk: Buffer) => ResponseType): void {
|
deserialize: (chunk: Buffer) => ResponseType): void {
|
||||||
|
|
|
@ -135,7 +135,6 @@ export class Client {
|
||||||
method: string, serialize: (value: RequestType) => Buffer,
|
method: string, serialize: (value: RequestType) => Buffer,
|
||||||
deserialize: (value: Buffer) => ResponseType, argument: RequestType,
|
deserialize: (value: Buffer) => ResponseType, argument: RequestType,
|
||||||
callback: UnaryCallback<ResponseType>): ClientUnaryCall;
|
callback: UnaryCallback<ResponseType>): ClientUnaryCall;
|
||||||
|
|
||||||
makeUnaryRequest<RequestType, ResponseType>(
|
makeUnaryRequest<RequestType, ResponseType>(
|
||||||
method: string, serialize: (value: RequestType) => Buffer,
|
method: string, serialize: (value: RequestType) => Buffer,
|
||||||
deserialize: (value: Buffer) => ResponseType, argument: RequestType,
|
deserialize: (value: Buffer) => ResponseType, argument: RequestType,
|
||||||
|
@ -147,13 +146,14 @@ export class Client {
|
||||||
metadata, options, callback));
|
metadata, options, callback));
|
||||||
const call: CallStream =
|
const call: CallStream =
|
||||||
this.channel.createStream(method, metadata, options);
|
this.channel.createStream(method, metadata, options);
|
||||||
const emitter: ClientUnaryCall = new ClientUnaryCallImpl(call);
|
|
||||||
const message: Buffer = serialize(argument);
|
const message: Buffer = serialize(argument);
|
||||||
const writeObj: WriteObject = {message: message};
|
const writeObj: WriteObject = {message: message};
|
||||||
writeObj.flags = options.flags;
|
writeObj.flags = options.flags;
|
||||||
call.write(writeObj);
|
call.write(writeObj);
|
||||||
call.end();
|
call.end();
|
||||||
this.handleUnaryResponse<ResponseType>(call, deserialize, callback);
|
this.handleUnaryResponse<ResponseType>(call, deserialize, callback);
|
||||||
|
|
||||||
|
const emitter: ClientUnaryCall = new ClientUnaryCallImpl(call);
|
||||||
return emitter;
|
return emitter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +174,6 @@ export class Client {
|
||||||
method: string, serialize: (value: RequestType) => Buffer,
|
method: string, serialize: (value: RequestType) => Buffer,
|
||||||
deserialize: (value: Buffer) => ResponseType,
|
deserialize: (value: Buffer) => ResponseType,
|
||||||
callback: UnaryCallback<ResponseType>): ClientWritableStream<RequestType>;
|
callback: UnaryCallback<ResponseType>): ClientWritableStream<RequestType>;
|
||||||
|
|
||||||
makeClientStreamRequest<RequestType, ResponseType>(
|
makeClientStreamRequest<RequestType, ResponseType>(
|
||||||
method: string, serialize: (value: RequestType) => Buffer,
|
method: string, serialize: (value: RequestType) => Buffer,
|
||||||
deserialize: (value: Buffer) => ResponseType,
|
deserialize: (value: Buffer) => ResponseType,
|
||||||
|
@ -187,9 +186,10 @@ export class Client {
|
||||||
metadata, options, callback));
|
metadata, options, callback));
|
||||||
const call: CallStream =
|
const call: CallStream =
|
||||||
this.channel.createStream(method, metadata, options);
|
this.channel.createStream(method, metadata, options);
|
||||||
|
this.handleUnaryResponse<ResponseType>(call, deserialize, callback);
|
||||||
|
|
||||||
const stream: ClientWritableStream<RequestType> =
|
const stream: ClientWritableStream<RequestType> =
|
||||||
new ClientWritableStreamImpl<RequestType>(call, serialize);
|
new ClientWritableStreamImpl<RequestType>(call, serialize);
|
||||||
this.handleUnaryResponse<ResponseType>(call, deserialize, callback);
|
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,13 +233,14 @@ export class Client {
|
||||||
({metadata, options} = this.checkMetadataAndOptions(metadata, options));
|
({metadata, options} = this.checkMetadataAndOptions(metadata, options));
|
||||||
const call: CallStream =
|
const call: CallStream =
|
||||||
this.channel.createStream(method, metadata, options);
|
this.channel.createStream(method, metadata, options);
|
||||||
const stream: ClientReadableStream<ResponseType> =
|
|
||||||
new ClientReadableStreamImpl<ResponseType>(call, deserialize);
|
|
||||||
const message: Buffer = serialize(argument);
|
const message: Buffer = serialize(argument);
|
||||||
const writeObj: WriteObject = {message: message};
|
const writeObj: WriteObject = {message: message};
|
||||||
writeObj.flags = options.flags;
|
writeObj.flags = options.flags;
|
||||||
call.write(writeObj);
|
call.write(writeObj);
|
||||||
call.end();
|
call.end();
|
||||||
|
|
||||||
|
const stream: ClientReadableStream<ResponseType> =
|
||||||
|
new ClientReadableStreamImpl<ResponseType>(call, deserialize);
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,6 +260,7 @@ export class Client {
|
||||||
({metadata, options} = this.checkMetadataAndOptions(metadata, options));
|
({metadata, options} = this.checkMetadataAndOptions(metadata, options));
|
||||||
const call: CallStream =
|
const call: CallStream =
|
||||||
this.channel.createStream(method, metadata, options);
|
this.channel.createStream(method, metadata, options);
|
||||||
|
|
||||||
const stream: ClientDuplexStream<RequestType, ResponseType> =
|
const stream: ClientDuplexStream<RequestType, ResponseType> =
|
||||||
new ClientDuplexStreamImpl<RequestType, ResponseType>(
|
new ClientDuplexStreamImpl<RequestType, ResponseType>(
|
||||||
call, serialize, deserialize);
|
call, serialize, deserialize);
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
export interface EmitterAugmentation0<Name extends string|symbol> {
|
||||||
|
addListener(event: Name, listener: () => void): this;
|
||||||
|
emit(event: Name): boolean;
|
||||||
|
on(event: Name, listener: () => void): this;
|
||||||
|
once(event: Name, listener: () => void): this;
|
||||||
|
prependListener(event: Name, listener: () => void): this;
|
||||||
|
prependOnceListener(event: Name, listener: () => void): this;
|
||||||
|
removeListener(event: Name, listener: () => void): this;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface EmitterAugmentation1<Name extends string|symbol, Arg> {
|
||||||
|
addListener(event: Name, listener: (arg1: Arg) => void): this;
|
||||||
|
emit(event: Name, arg1: Arg): boolean;
|
||||||
|
on(event: Name, listener: (arg1: Arg) => void): this;
|
||||||
|
once(event: Name, listener: (arg1: Arg) => void): this;
|
||||||
|
prependListener(event: Name, listener: (arg1: Arg) => void): this;
|
||||||
|
prependOnceListener(event: Name, listener: (arg1: Arg) => void): this;
|
||||||
|
removeListener(event: Name, listener: (arg1: Arg) => void): this;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface EmitterAugmentation2<Name extends string|symbol, Arg1, Arg2> {
|
||||||
|
addListener(event: Name, listener: (arg1: Arg1, arg2: Arg2) => void): this;
|
||||||
|
emit(event: Name, arg1: Arg1, arg2: Arg2): boolean;
|
||||||
|
on(event: Name, listener: (arg1: Arg1, arg2: Arg2) => void): this;
|
||||||
|
once(event: Name, listener: (arg1: Arg1, arg2: Arg2) => void): this;
|
||||||
|
prependListener(event: Name, listener: (arg1: Arg1, arg2: Arg2) => void): this;
|
||||||
|
prependOnceListener(event: Name, listener: (arg1: Arg1, arg2: Arg2) => void): this;
|
||||||
|
removeListener(event: Name, listener: (arg1: Arg1, arg2: Arg2) => void): this;
|
||||||
|
}
|
|
@ -1,28 +1,14 @@
|
||||||
import {Duplex, Readable, Writable} from 'stream';
|
import {Duplex, Readable, Writable} from 'stream';
|
||||||
|
import {EmitterAugmentation1} from './events';
|
||||||
|
|
||||||
export interface IntermediateObjectReadable<T> extends Readable {
|
export interface IntermediateObjectReadable<T> extends Readable {
|
||||||
read(size?: number): any&T;
|
read(size?: number): any&T;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ObjectReadable<T> extends IntermediateObjectReadable<T> {
|
export type ObjectReadable<T> = {
|
||||||
read(size?: number): T;
|
read(size?: number): T;
|
||||||
|
} & EmitterAugmentation1<'data', T>
|
||||||
addListener(event: string, listener: Function): this;
|
& IntermediateObjectReadable<T>;
|
||||||
emit(event: string|symbol, ...args: any[]): boolean;
|
|
||||||
on(event: string, listener: Function): this;
|
|
||||||
once(event: string, listener: Function): this;
|
|
||||||
prependListener(event: string, listener: Function): this;
|
|
||||||
prependOnceListener(event: string, listener: Function): this;
|
|
||||||
removeListener(event: string, listener: Function): this;
|
|
||||||
|
|
||||||
addListener(event: 'data', listener: (chunk: T) => void): this;
|
|
||||||
emit(event: 'data', chunk: T): boolean;
|
|
||||||
on(event: 'data', listener: (chunk: T) => void): this;
|
|
||||||
once(event: 'data', listener: (chunk: T) => void): this;
|
|
||||||
prependListener(event: 'data', listener: (chunk: T) => void): this;
|
|
||||||
prependOnceListener(event: 'data', listener: (chunk: T) => void): this;
|
|
||||||
removeListener(event: 'data', listener: (chunk: T) => void): this;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IntermediateObjectWritable<T> extends Writable {
|
export interface IntermediateObjectWritable<T> extends Writable {
|
||||||
_write(chunk: any&T, encoding: string, callback: Function): void;
|
_write(chunk: any&T, encoding: string, callback: Function): void;
|
||||||
|
@ -44,8 +30,7 @@ export interface ObjectWritable<T> extends IntermediateObjectWritable<T> {
|
||||||
end(chunk: T, encoding?: any, cb?: Function): void;
|
end(chunk: T, encoding?: any, cb?: Function): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ObjectDuplex<T, U> extends Duplex, ObjectWritable<T>,
|
export type ObjectDuplex<T, U> = {
|
||||||
ObjectReadable<U> {
|
|
||||||
read(size?: number): U;
|
read(size?: number): U;
|
||||||
|
|
||||||
_write(chunk: T, encoding: string, callback: Function): void;
|
_write(chunk: T, encoding: string, callback: Function): void;
|
||||||
|
@ -54,13 +39,4 @@ export interface ObjectDuplex<T, U> extends Duplex, ObjectWritable<T>,
|
||||||
end(): void;
|
end(): void;
|
||||||
end(chunk: T, cb?: Function): void;
|
end(chunk: T, cb?: Function): void;
|
||||||
end(chunk: T, encoding?: any, cb?: Function): void;
|
end(chunk: T, encoding?: any, cb?: Function): void;
|
||||||
|
} & Duplex & ObjectWritable<T> & ObjectReadable<U>;
|
||||||
|
|
||||||
addListener(event: string, listener: Function): this;
|
|
||||||
emit(event: string|symbol, ...args: any[]): boolean;
|
|
||||||
on(event: string, listener: Function): this;
|
|
||||||
once(event: string, listener: Function): this;
|
|
||||||
prependListener(event: string, listener: Function): this;
|
|
||||||
prependOnceListener(event: string, listener: Function): this;
|
|
||||||
removeListener(event: string, listener: Function): this;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue