Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing in context in props.FromProducer #1471

Merged
merged 3 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Proto.Actor/Context/ActorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ private void SendUserMessage(PID target, object message)
private IActor IncarnateActor()
{
_state = ContextState.Alive;
return _props.Producer(System);
return _props.Producer(System, this);
}

private async ValueTask HandleRestartAsync()
Expand Down
2 changes: 2 additions & 0 deletions src/Proto.Actor/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Proto
public delegate IActor Producer();

public delegate IActor ProducerWithSystem(ActorSystem system);

public delegate IActor ProducerWithSystemAndContext(ActorSystem system, IContext context);

public delegate IMailbox MailboxProducer();
}
9 changes: 6 additions & 3 deletions src/Proto.Actor/Props.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ namespace Proto
[PublicAPI]
public sealed record Props
{
private static IActor NullProducer(ActorSystem _) => null!;
private static IActor NullProducer(ActorSystem _, IContext __) => null!;
public static readonly Props Empty = new();

public ProducerWithSystem Producer { get; init; } = NullProducer;
public ProducerWithSystemAndContext Producer { get; init; } = NullProducer;
public MailboxProducer MailboxProducer { get; init; } = () => UnboundedMailbox.Create();
public ISupervisorStrategy? GuardianStrategy { get; init; }
public ISupervisorStrategy SupervisorStrategy { get; init; } = Supervision.DefaultStrategy;
Expand Down Expand Up @@ -79,9 +79,12 @@ private static void Initialize(Props props, ActorContext ctx)
}

public Props WithProducer(Producer producer) =>
this with {Producer = _ => producer()};
this with {Producer = (_,_) => producer()};

public Props WithProducer(ProducerWithSystem producer) =>
this with {Producer = (system, _) => producer(system)};

public Props WithProducer(ProducerWithSystemAndContext producer) =>
this with {Producer = producer};

public Props WithDispatcher(IDispatcher dispatcher) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void CanResolveDependencies()
system.Extensions.Register(plugin);

var props = system.DI().PropsFor<DiActor>();
var actor = (DiActor) props.Producer(system);
var actor = (DiActor) props.Producer(system, null!);

Assert.NotNull(props);
Assert.NotNull(actor);
Expand Down
9 changes: 7 additions & 2 deletions tests/Proto.Actor.Tests/PropsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ namespace Proto.Tests
{
public class PropsTests
{
public class DummyActor : IActor
{
public Task ReceiveAsync(IContext context) => throw new NotImplementedException();
}

[Fact]
public async Task Can_pass_ActorSystem_via_Props()
{
await using var system = new ActorSystem();
var props = Props.FromProducer(s => new ActorWithSystem(s));
var actor = (ActorWithSystem) props.Producer(system);
var actor = (ActorWithSystem) props.Producer(system, null!);
Assert.Same(system, actor.System);
}

Expand Down Expand Up @@ -84,7 +89,7 @@ public void Given_Props_When_WithMiddleware_Then_mutate_Middleware()
[Fact]
public void Given_Props_When_WithProducer_Then_mutate_Producer()
{
static IActor Producer(ActorSystem s) => null!;
static IActor Producer(ActorSystem _, IContext __) => new DummyActor();

var props = new Props();
var props2 = props.WithProducer(Producer);
Expand Down