improve ping-pong test

This commit is contained in:
Brian Sakal
2023-05-14 15:44:46 -04:00
parent ad41407ac6
commit a6f6c3cd17
2 changed files with 14 additions and 27 deletions
+8 -3
View File
@@ -15,6 +15,9 @@
var Goto = function(targetStateName) {
return { type: "Goto", targetStateName };
};
var Context = function(fn) {
return { type: "ContextMutation", fn };
};
function interpret(machine, options) {
let { state, context } = options;
if (typeof state === "undefined") {
@@ -86,11 +89,11 @@
send(ctx.clientActor, ["received-response", self]);
};
var startTimer = async (ctx, e, self) => {
await wait(500);
await wait(1500);
send(self, ["timer-finished", null]);
};
var log = (ctx, e, self) => {
console.log(self.state);
console.log(self.state, ctx);
};
var client = Machine(
State(
@@ -113,6 +116,7 @@
"entry",
SideEffect(log),
SideEffect(makeRequest),
Context((ctx) => ({ ...ctx, requestsMade: ctx.requestsMade + 1 })),
Goto("awaiting-response")
)
),
@@ -125,6 +129,7 @@
On(
"received-response",
SideEffect(log),
Context((ctx) => ({ ...ctx, responsesReceived: ctx.responsesReceived + 1 })),
Goto("making-request")
)
)
@@ -158,7 +163,7 @@
)
)
);
var clientActor = interpret(client, { context: {} });
var clientActor = interpret(client, { context: { requestsMade: 0, responsesReceived: 0 } });
var serverActor = interpret(server, { context: {} });
send(clientActor, ["server-created", serverActor]);
})();