base2020/src/Net/LoopbackServer.ts

25 lines
858 B
TypeScript
Raw Normal View History

2020-02-16 05:42:58 +00:00
import { Callbag } from "callbag";
import { ClientMessage, MessageTypes, ServerMessage } from "./LockstepClient";
type Client<Input, State> = Callbag<ServerMessage<Input, State>, ClientMessage<Input, State>>;
export function Loopback<Input, State>(start: number, data?: Client<Input, State> | ClientMessage<Input, State>) {
if(start != 0) return;
const sink = data as Client<Input, State>;
sink(0, (type: number, data?: Client<Input, State> | ClientMessage<Input, State>) => {
if(type == 1) {
// message from client; just reflect for now
const message = data as ClientMessage<Input, State>;
switch(message.t) {
case MessageTypes.INPUT:
sink(1, message);
break;
}
}
});
sink(1, {t: MessageTypes.RESET, s: {}});
};