Skip to content

Commit

Permalink
expose just eventSender
Browse files Browse the repository at this point in the history
  • Loading branch information
aozgaa committed Nov 16, 2017
1 parent d6c3a15 commit a7822c5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 70 deletions.
60 changes: 38 additions & 22 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,31 @@ namespace ts.server {
}
}

class SocketEventSender extends DefaultMessageSender {
export class DefaultEventSender implements EventSender {
constructor(protected host: ServerHost,
protected byteLength: (buf: string, encoding?: string) => number,
protected logger: Logger,
protected canUseEvents: boolean) { }

public event = <T>(body: T, eventName: string) => {
const ev: protocol.Event = {
seq: 0,
type: "event",
event: eventName,
body
};
defaultSend(this.host, this.byteLength, this.logger, this.canUseEvents, ev);
}
}

class SocketEventSender implements EventSender {
private eventSocket: NodeSocket | undefined;
private socketEventQueue: { body: any, eventName: string }[] | undefined;

constructor(host: ServerHost,
byteLength: (buf: string, encoding?: string) => number,
logger: Logger,
constructor(private host: ServerHost,
private byteLength: (buf: string, encoding?: string) => number,
private logger: Logger,
private eventPort: number) {
super(host, byteLength, logger, /*canUseEvents*/ true);

const s = net.connect({ port: this.eventPort }, () => {
this.eventSocket = s;
Expand All @@ -538,36 +554,36 @@ namespace ts.server {
this.socketEventQueue = undefined;
}
});
}

this.event = <T>(body: T, eventName: string) => {
if (!this.eventSocket) {
if (this.logger.hasLevel(LogLevel.verbose)) {
this.logger.info(`eventPort: event "${eventName}" queued, but socket not yet initialized`);
}
(this.socketEventQueue || (this.socketEventQueue = [])).push({ body, eventName });
return;
}
else {
Debug.assert(this.socketEventQueue === undefined);
this.writeToEventSocket(body, eventName);
public event = <T>(body: T, eventName: string) => {
if (!this.eventSocket) {
if (this.logger.hasLevel(LogLevel.verbose)) {
this.logger.info(`eventPort: event "${eventName}" queued, but socket not yet initialized`);
}
};
}
(this.socketEventQueue || (this.socketEventQueue = [])).push({ body, eventName });
return;
}
else {
Debug.assert(this.socketEventQueue === undefined);
this.writeToEventSocket(body, eventName);
}
};

private writeToEventSocket(body: any, eventName: string): void {
this.eventSocket.write(formatMessage({ seq: 0, type: "event", event: eventName, body }, this.logger, Buffer.byteLength, this.host.newLine), "utf8");
this.eventSocket.write(formatMessage({ seq: 0, type: "event", event: eventName, body }, this.logger, this.byteLength, this.host.newLine), "utf8");
}
}

class IOSession extends Session {
constructor(options: IoSessionOptions) {
const { host, eventPort, globalTypingsCacheLocation, typingSafeListLocation, typesMapLocation, npmLocation, canUseEvents } = options;

const messageSender = eventPort && canUseEvents ? new SocketEventSender(host, Buffer.byteLength, logger, eventPort) : new DefaultMessageSender(host, Buffer.byteLength, logger, canUseEvents);
const eventSender = eventPort && canUseEvents ? new SocketEventSender(host, Buffer.byteLength, logger, eventPort) : new DefaultEventSender(host, Buffer.byteLength, logger, canUseEvents);

const typingsInstaller = disableAutomaticTypingAcquisition
? undefined
: new NodeTypingsInstaller(telemetryEnabled, logger, host, globalTypingsCacheLocation, typingSafeListLocation, typesMapLocation, npmLocation, canUseEvents ? messageSender : undefined);
: new NodeTypingsInstaller(telemetryEnabled, logger, host, globalTypingsCacheLocation, typingSafeListLocation, typesMapLocation, npmLocation, canUseEvents ? eventSender : undefined);

super({
host,
Expand All @@ -579,7 +595,7 @@ namespace ts.server {
hrtime: process.hrtime,
logger,
canUseEvents,
messageSender,
eventSender,
globalPlugins: options.globalPlugins,
pluginProbeLocations: options.pluginProbeLocations,
allowLocalPluginLoads: options.allowLocalPluginLoads });
Expand Down
62 changes: 14 additions & 48 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,12 @@ namespace ts.server {
}
}

export type Event = <T>(body: T, eventName: string) => void;

export interface EventSender {
event: Event;
}

export type Send = (msg: protocol.Message) => void;

export interface MessageSender extends EventSender {
send: Send;
event: Event;
event: <T>(body: T, eventName: string) => void;
}

function defaultSend(
/** @internal */
export function defaultSend(
host: ServerHost,
byteLength: (buf: string, encoding?: string) => number,
logger: Logger,
Expand All @@ -266,37 +258,6 @@ namespace ts.server {
}
host.write(formatMessage(msg, logger, byteLength, host.newLine));
}

function defaultEvent<T>(
host: ServerHost,
byteLength: (buf: string, encoding?: string) => number,
logger: Logger,
canUseEvents: boolean,
body: T, eventName: string): void {
const ev: protocol.Event = {
seq: 0,
type: "event",
event: eventName,
body
};
defaultSend(host, byteLength, logger, canUseEvents, ev);
}

export class DefaultMessageSender implements MessageSender {
constructor(protected host: ServerHost,
protected byteLength: (buf: string, encoding?: string) => number,
protected logger: Logger,
protected canUseEvents: boolean) { }

public send = (msg: protocol.Message) => {
defaultSend(this.host, this.byteLength, this.logger, this.canUseEvents, msg);
}

public event = <T>(body: T, eventName: string) => {
defaultEvent(this.host, this.byteLength, this.logger, this.canUseEvents, body, eventName);
}
}

export interface SessionOptions {
host: ServerHost;
cancellationToken: ServerCancellationToken;
Expand All @@ -313,7 +274,7 @@ namespace ts.server {
/**
* An optional callback overriding the default behavior for sending messages.
*/
messageSender?: MessageSender;
eventSender?: EventSender;
eventHandler?: ProjectServiceEventHandler;
throttleWaitMilliseconds?: number;

Expand All @@ -322,7 +283,7 @@ namespace ts.server {
allowLocalPluginLoads?: boolean;
}

export class Session implements MessageSender {
export class Session implements EventSender {
private readonly gcTimer: GcTimer;
protected projectService: ProjectService;
private changeSeq = 0;
Expand Down Expand Up @@ -351,9 +312,8 @@ namespace ts.server {

const { throttleWaitMilliseconds } = opts;

if (opts.messageSender) {
this.send = opts.messageSender.send;
this.event = opts.messageSender.event;
if (opts.eventSender) {
this.event = opts.eventSender.event;
}

this.eventHandler = this.canUseEvents
Expand Down Expand Up @@ -455,7 +415,13 @@ namespace ts.server {
}

public event<T>(body: T, eventName: string): void {
defaultEvent(this.host, this.byteLength, this.logger, this.canUseEvents, body, eventName);
const ev: protocol.Event = {
seq: 0,
type: "event",
event: eventName,
body
};
this.send(ev);
}

// For backwards-compatibility only.
Expand Down

0 comments on commit a7822c5

Please sign in to comment.