24 lines
862 B
TypeScript
24 lines
862 B
TypeScript
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.SET_STATE, s: {}});
|
|
};
|