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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-02-21 04:55:16 +00:00
|
|
|
sink(1, {t: MessageTypes.SET_STATE, s: {}});
|
2020-02-16 05:42:58 +00:00
|
|
|
};
|