parent
ef405093dc
commit
4aa33e8dd2
@ -0,0 +1,53 @@
|
||||
import {StateEvent, WebSocketBus} from '../../support/bus'
|
||||
import {constructable, Constructable, Instantiable, TypedDependencyKey} from '../../di'
|
||||
import {Awaitable, Collection, JSONState} from '../../util'
|
||||
import {Request} from '../lifecycle/Request'
|
||||
|
||||
export type SocketEventHandler<TState extends JSONState> = {
|
||||
eventClass: Instantiable<StateEvent<TState>>,
|
||||
handler: Constructable<(state: TState) => Awaitable<void>>,
|
||||
}
|
||||
|
||||
export class SocketRouteBuilder {
|
||||
public static get(): SocketRouteBuilder {
|
||||
return new SocketRouteBuilder()
|
||||
}
|
||||
|
||||
protected handlers: Collection<SocketEventHandler<any>> = new Collection()
|
||||
|
||||
protected connectionCallback?: Constructable<(ws: WebSocketBus) => Awaitable<void>>
|
||||
|
||||
connected<TKey extends any>(
|
||||
key: TypedDependencyKey<TKey>,
|
||||
selector: (x: TKey) => (ws: WebSocketBus) => Awaitable<void>,
|
||||
): this {
|
||||
this.connectionCallback = constructable<TKey>(key)
|
||||
.tap(inst => Function.prototype.bind.call(selector(inst), inst as any) as ((ws: WebSocketBus) => Awaitable<void>))
|
||||
return this
|
||||
}
|
||||
|
||||
event<TState extends JSONState, TKey extends any>(
|
||||
eventClass: Instantiable<StateEvent<TState>>,
|
||||
key: TypedDependencyKey<TKey>,
|
||||
selector: (x: TKey) => (state: TState) => Awaitable<void>,
|
||||
): this {
|
||||
const handler = constructable<TKey>(key)
|
||||
.tap(inst => Function.prototype.bind.call(selector(inst), inst as any) as ((state: TState) => Awaitable<void>))
|
||||
|
||||
this.handlers.push({
|
||||
eventClass,
|
||||
handler,
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
async build(request: Request, ws: WebSocketBus): Promise<void> {
|
||||
await this.handlers.promiseMap(handler => {
|
||||
ws.subscribe(handler.eventClass, handler.handler.apply(request))
|
||||
})
|
||||
|
||||
if ( this.connectionCallback ) {
|
||||
await this.connectionCallback.apply(request)(ws)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue