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

Actor initialization with parameters #5

Closed
vallettea opened this issue Jan 12, 2017 · 7 comments
Closed

Actor initialization with parameters #5

vallettea opened this issue Jan 12, 2017 · 7 comments
Assignees
Labels

Comments

@vallettea
Copy link

[I've been working a couple of days with your library and I wanted to thanks you because I really enjoy it.]

Coming back to my question, how can I initialize an actor with some parameters ?
When an actor is forked, it doesn't have access to the scope.

@weekens
Copy link
Contributor

weekens commented Jan 12, 2017

Custom actor parameters are not yet implemented in Comedy, but this feature should certainly be present.

There is, however, a resource injection mechanism in Comedy. It's not yet documented, but you can see examples in test/test-system.js ("should properly inject resources defined in contexts" and the like).

If shared resource is not what you need, you can try to get away with initialization message.

@vallettea
Copy link
Author

Yes but you have the same problem when you define your context:

class MyContext {
      initialize() {
        this.resource = myVariable;
      }

      getResource() {
        return this.resource;
      }
    }

myVariable is unknown

@weekens
Copy link
Contributor

weekens commented Jan 12, 2017

Yes, the context with all it's resources is supposed to be self-contained.

In your case, the initialization message would work. Custom actor parameters will be implemented shortly.

@weekens weekens self-assigned this Jan 12, 2017
@weekens
Copy link
Contributor

weekens commented Jan 13, 2017

Implemented in v0.1.0. Here is an example test:

it('should be able to pass custom parameters to child actor', P.coroutine(function*() {
  class MyActor {
    initialize(selfActor) {
      this.helloResponse = selfActor.getCustomParameters().helloResponse;
    }

    hello() {
      return this.helloResponse;
    }
  }

  // Create child actor with custom parameter.
  var childActor = yield rootActor.createChild(MyActor, {
    mode: 'forked',
    customParameters: { helloResponse: 'Hi there!' }
  });

  var response = yield childActor.sendAndReceive('hello');

  expect(response).to.be.equal('Hi there!');
}));

Enjoy!

@weekens weekens closed this as completed Jan 13, 2017
@vallettea
Copy link
Author

Works perfectly THANKS.

@vallettea
Copy link
Author

It seems that if I put a function in the parameters it works for non-forked actor but that functions are filtered out of the parameters when the actor is forked.
Any idea how to pass functions as parameters for forked actors ?

@weekens
Copy link
Contributor

weekens commented Jan 17, 2017

Well, yeah, for now I've just added support for plain objects in custom parameters. Class and function passing will be done in a next step (you can create an issue for that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants