public async Task pop_behavior_should_restore_pushed_behavior() { var behavior = new Behavior(); behavior.Become(ctx => { if (ctx.Message is string) { behavior.BecomeStacked(ctx2 => { ctx2.Respond(42); behavior.UnbecomeStacked(); return(Actor.Done); }); ctx.Respond(ctx.Message); } return(Actor.Done); }); PID pid = SpawnActorFromFunc(behavior.ReceiveAsync); var reply = await Context.RequestAsync <string>(pid, "number"); var replyAfterPush = await Context.RequestAsync <int>(pid, null); var replyAfterPop = await Context.RequestAsync <string>(pid, "answertolifetheuniverseandeverything"); Assert.Equal("number42answertolifetheuniverseandeverything", $"{reply}{replyAfterPush}{replyAfterPop}"); }
async Task <object> DoReceive(object message) { // any "global" message handling here switch (message) { case Activate _: await LoadState(); return(Done); case HitWithHammer _ when behavior.Current.Name != nameof(Smashed): await behavior.BecomeStacked(Smashed); return("Smashed!"); case Deactivate _: Console.WriteLine("Deactivated"); await Task.Delay(2000); return(""); } // if not handled, use behavior specific return(await behavior.Receive(message)); }
public Task ReceiveAsync(IContext context) { switch (context.Message) { case Suspend _: _behavior.BecomeStacked(ResumeAsync); Console.WriteLine("Stacked"); return(Actor.Done); } return(_behavior.ReceiveAsync(context)); }
Task Running(IContext context) { switch (context.Message) { case (Outbound, _): { context.Forward(context.Parent); return(Done); } case (Inbound, RawFrame frame)when frame.Type == FrameType.Heartbeat: { context.Forward(_state.HeartbeatAgent); return(Done); } case ConnectionConfiguration connectionConfiguration: { _state.ConnectionConfiguration = connectionConfiguration; return(Done); } case (Inbound, ConnectionStart message): { _state.HandshakeAgent = context.SpawnNamed( name: "handshake", props: Props.FromProducer(() => new HandshakeAgent(_state.ConnectionConfiguration)) .WithContextDecorator(context => new LoggingContextDecorator(context)) ); context.Forward(_state.HandshakeAgent); _behaviour.BecomeStacked(AwaitHandshake); return(Done); } case (Inbound, ConnectionClose message): { context.Send(context.Parent, (Outbound, new ConnectionCloseOk())); context.Self.Stop(); return(Done); } } return(Done); }
async Task BecomeStacked(Receive other) => await Switch(() => behavior.BecomeStacked(other));
public LightBulb() { _behavior = new Behavior(); //把Off方法放入栈 _behavior.BecomeStacked(Off); }