-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ipc - lay foundation for events support
- Loading branch information
Showing
8 changed files
with
153 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { Event } from 'vs/base/common/event'; | ||
import { IChannel, IServerChannel } from 'vs/base/parts/ipc/common/ipc'; | ||
import { revive } from 'vs/base/common/marshalling'; | ||
import { isUndefinedOrNull } from 'vs/base/common/types'; | ||
import { isUpperAsciiLetter } from 'vs/base/common/strings'; | ||
|
||
// | ||
// Use both `createChannelReceiver` and `createChannelSender` | ||
// for automated process <=> process communication over methods. | ||
// | ||
|
||
export interface IBaseChannelOptions { | ||
|
||
/** | ||
* Disables automatic marshalling of `URI`. | ||
* If marshalling is disabled, `UriComponents` | ||
* must be used instead. | ||
*/ | ||
disableMarshalling?: boolean; | ||
} | ||
|
||
export interface IChannelReceiverOptions extends IBaseChannelOptions { } | ||
|
||
export function createChannelReceiver(service: unknown, options?: IChannelReceiverOptions): IServerChannel { | ||
const handler = service as { [key: string]: unknown }; | ||
const disableMarshalling = options && options.disableMarshalling; | ||
|
||
// Buffer any event that should be supported by | ||
// iterating over all property keys and finding them | ||
const mapEventNameToEvent = new Map<string, Event<unknown>>(); | ||
for (const key in handler) { | ||
if (propertyIsEvent(key)) { | ||
mapEventNameToEvent.set(key, Event.buffer(handler[key] as Event<unknown>, true)); | ||
} | ||
} | ||
|
||
return new class implements IServerChannel { | ||
|
||
listen<T>(_: unknown, event: string): Event<T> { | ||
const eventImpl = mapEventNameToEvent.get(event); | ||
if (eventImpl) { | ||
return eventImpl as Event<T>; | ||
} | ||
|
||
throw new Error(`Event not found: ${event}`); | ||
} | ||
|
||
call(_: unknown, command: string, args?: any[]): Promise<any> { | ||
const target = handler[command]; | ||
if (typeof target === 'function') { | ||
|
||
// Revive unless marshalling disabled | ||
if (!disableMarshalling && Array.isArray(args)) { | ||
for (let i = 0; i < args.length; i++) { | ||
args[i] = revive(args[i]); | ||
} | ||
} | ||
|
||
return target.apply(handler, args); | ||
} | ||
|
||
throw new Error(`Method not found: ${command}`); | ||
} | ||
}; | ||
} | ||
|
||
export interface IChannelSenderOptions extends IBaseChannelOptions { | ||
|
||
/** | ||
* If provided, will add the value of `context` | ||
* to each method call to the target. | ||
*/ | ||
context?: unknown; | ||
} | ||
|
||
export function createChannelSender<T>(channel: IChannel, options?: IChannelSenderOptions): T { | ||
const disableMarshalling = options && options.disableMarshalling; | ||
|
||
return new Proxy({}, { | ||
get(_target, propKey, _receiver) { | ||
if (typeof propKey === 'string') { | ||
|
||
// Event | ||
if (propertyIsEvent(propKey)) { | ||
return channel.listen(propKey); | ||
} | ||
|
||
// Function | ||
return async function (...args: any[]) { | ||
|
||
// Add context if any | ||
let methodArgs: any[]; | ||
if (options && !isUndefinedOrNull(options.context)) { | ||
methodArgs = [options.context, ...args]; | ||
} else { | ||
methodArgs = args; | ||
} | ||
|
||
const result = await channel.call(propKey, methodArgs); | ||
|
||
// Revive unless marshalling disabled | ||
if (!disableMarshalling) { | ||
return revive(result); | ||
} | ||
|
||
return result; | ||
}; | ||
} | ||
|
||
throw new Error(`Property not found: ${String(propKey)}`); | ||
} | ||
}) as T; | ||
} | ||
|
||
function propertyIsEvent(name: string): boolean { | ||
// Assume a property is an event if it has a form of "onSomething" | ||
return name[0] === 'o' && name[1] === 'n' && isUpperAsciiLetter(name.charCodeAt(2)); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters