forked from apache/daffodil-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fea2736
commit 5585137
Showing
10 changed files
with
455 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { DataEditorResponseEvents } from './messages' | ||
|
||
type DefaultEventResponses = { | ||
[R in keyof DataEditorResponseEvents]: ( | ||
request: DataEditorResponseEvents[R] | ||
) => void | ||
} | ||
export const DefaultInputListeners: DefaultEventResponses = { | ||
requestEditedData: function ( | ||
request: [response: { bytes: Uint8Array; str: string }] | ||
): void { | ||
throw new Error('Function not implemented.') | ||
}, | ||
} |
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,95 @@ | ||
import assert from 'assert' | ||
import { describe } from 'mocha' | ||
import { EventEmitter } from 'stream' | ||
|
||
interface RequestEmitter<RequestTypes> { | ||
on<R extends keyof RequestTypes>( | ||
event: R, | ||
listener: (...args: ) => any | ||
): any | ||
} | ||
interface ResponseEmitter<ResponseTypes> { | ||
on(event: string, listener: (...args: any[]) => any): any | ||
} | ||
|
||
/// The extension handles all data anyways so it should define both request and response types, or the requester could send any arbitrary request event | ||
class EventChannel<RequestTypes, ResponseTypes> { | ||
__req: RequestEmitter<RequestTypes> | undefined = undefined | ||
__res: ResponseEmitter<ResponseTypes> | undefined = undefined | ||
constructor() {} | ||
setRequester(req: RequestEmitter<RequestTypes>) { | ||
if (this.__req) throw 'A Requester already exists on this channel' | ||
this.__req = req | ||
} | ||
setResponder(res: ResponseEmitter<ResponseTypes>) { | ||
if (this.__res) throw 'A Responder already exists on this channel' | ||
this.__res = res | ||
} | ||
|
||
} | ||
|
||
const EventChannelMap: Map<string, EventChannel<any, any>> = new Map() | ||
class EventManager { | ||
static CreateChannel<Req, Res>(id: string): EventChannel<Req, Res> { | ||
const channel = new EventChannel<Req, Res>() | ||
EventChannelMap.set(id, channel) | ||
return channel | ||
} | ||
static GetChannel<ID extends string>(id: string): EventChannel<EventChannelMap[ID],Res> { | ||
if (!EventChannelMap.get(id)) throw `No Channel with ID = ${id}` | ||
return EventChannelMap.get(id)! | ||
} | ||
} | ||
|
||
interface UIEvents { | ||
e1: [] | ||
save: [data: { filename: string }] | ||
shutdown: [data: { filename: string; time: number }] | ||
} | ||
class UIEventEmitter implements RequestEmitter<UIEvents> { | ||
constructor(private __emitter: EventEmitter<UIEvents>) {} | ||
// __emitter = new EventEmitter<UIEvents>() | ||
on = this.__emitter.on | ||
} | ||
class UI { | ||
readonly __docID = 'session-id' | ||
__emitter = new EventEmitter<UIEvents>() | ||
requester: RequestEmitter<UIEvents> = new UIEventEmitter(this.__emitter) | ||
constructor() { | ||
EventManager.GetChannel(this.__docID).__req = this.requester | ||
} | ||
simulateResponseListenerAdd() {} | ||
simulateInputReceived() { | ||
this.__emitter.emit('shutdown', { filename: 'testfile/name.bin', time: 0 }) | ||
} | ||
} | ||
interface ExtResponses { | ||
'data-refresh': [data: { encoding: string; offset: number; data: Uint8Array }] | ||
'file-info': [data: { filename: string; filesize: number }] | ||
} | ||
class ExtResponder implements ResponseEmitter<ExtResponses> { | ||
__emitter = new EventEmitter<ExtResponses>() | ||
on = this.__emitter.on | ||
} | ||
class Ext { | ||
readonly __sessionId = 'session-id' | ||
responder: ResponseEmitter<ExtResponses> = new ExtResponder() | ||
constructor() { | ||
const channel = EventManager.CreateChannel<UIEvents, ExtResponses>(this.__sessionId) | ||
} | ||
} | ||
const ui = new UI() | ||
describe('Event Channel Behavior', () => { | ||
describe('Manager', () => { | ||
it('Should create a channel if one does not already exist', () => { | ||
const channel = EventManager.GetChannel(ui.__docID) | ||
assert.equal(EventChannelMap.size, 1) | ||
assert(EventChannelMap.get(ui.__docID)) | ||
assert(channel) | ||
EventChannelMap.clear() | ||
}) | ||
}) | ||
it('Should allow a requester to add listeners to events that the responder emits', () => {}) | ||
it('Should allow a responder to add listeners to events that the requester emits', () => {}) | ||
it('Should contain requester and responder abstractions', () => {}) | ||
}) |
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,83 @@ | ||
import assert from 'assert' | ||
import EventEmitter from 'events' | ||
import { describe } from 'mocha' | ||
|
||
interface RequestEvents { | ||
ping: [] | ||
scroll: [data: { offset: number }] | ||
multidata: [data: { str: string; bin: Uint8Array; num: number }] | ||
} | ||
interface ResponseEvents { | ||
pong: [] | ||
'file-info': [data: { filename: string; size: number }] | ||
} | ||
type EventMap = { [K: string]: [any] } | ||
type RequestListener<E> = { | ||
on<Key extends string & keyof E>( | ||
event: Key, | ||
listener: (content: E[Key]) => void | ||
): void | ||
} | ||
class RequestEmitter extends EventEmitter {} | ||
class Requester<EventTypes> { | ||
constructor( | ||
public __channelNotify: <R extends keyof EventTypes>( | ||
type: R, | ||
data: EventTypes[R] | ||
) => void | ||
) {} | ||
request<R extends keyof EventTypes>(type: R, data: EventTypes[R]) {} | ||
} | ||
class Responder<EventTypes> { | ||
constructor( | ||
public __channelNotify: <R extends keyof EventTypes>( | ||
type: R, | ||
data: EventTypes[R] | ||
) => void | ||
) {} | ||
respond<R extends keyof EventTypes>(type: R, data: EventTypes[R]) {} | ||
} | ||
class ChannelMember<OutEvents, InEvents> { | ||
constructor( | ||
readonly send: <R extends keyof OutEvents>( | ||
type: R, | ||
data: OutEvents[R] | ||
) => void, | ||
readonly on: <R extends keyof InEvents>( | ||
type: R, | ||
listener: (data: InEvents[R]) => void | ||
) => void | ||
) {} | ||
} | ||
class EventChannel<Req, Res> { | ||
__requester: ChannelMember<Req, Res> | undefined | ||
__requestMap: { [K in keyof Res]: (request: Res[K]) => void } | ||
__responder: ChannelMember<Res, Req> | undefined | ||
__responseMap: { [K in keyof Req]: (request: Req[K]) => void } | ||
constructor() {} | ||
createRequester() { | ||
this.__requester = new ChannelMember<Req, Res>( | ||
(type, data) => { | ||
this.__responseMap![type](data) | ||
}, | ||
(type, listener) => { | ||
this.__requestMap![type] = listener | ||
} | ||
) | ||
return this.__requester | ||
} | ||
createResponder() { | ||
this.__responder = new ChannelMember<Res, Req>((type, data) => { | ||
this.__requestMap![type](data) | ||
}) | ||
return this.__responder | ||
} | ||
} | ||
|
||
// class EventChannelManager { | ||
// static CreateChannel<Req, Res>(): EventChannel | ||
// } | ||
describe('', () => { | ||
const ec = new EventChannel<RequestEvents, ResponseEvents>() | ||
const req = ec.createRequester().send('ping', []) | ||
}) |
Oops, something went wrong.