Note that there is no `sayHello` method. The FrontClass will generate a method on the Front object that matches the method declaration in the Actor class.
The generated methods will return a Promise. That promise will resolve to the RetVal of the actor method.
So if we have a reference to a HelloFront object, we can issue a `sayHello` request:
Magically - Once you have an initial reference to a protocol.js object, it can return other protocol.js objects and fronts will automatically be created.
The library tries hard to make using fronts feel like natural javascript (or as natural as you believe promises are, I guess). When building the response it will put the return value of the function where RetVal() is specified in the response template, and on the client side it will use the value in that position when resolving the promise.
That's probably unnecessary nesting (if you're sure you won't be returning an object with 'from' as a key!), so you can just replace `response` with:
response: RetVal("json")
and now your packet will look like:
{ from: <actorID>, a: <number>, b: <number> }
Types and Marshalling
---------------------
Things have been pretty simple up to this point - all the arguments we've passed in have been javascript primitives. But for some types (most importantly Actor types, which I'll get to eventually), we can't just copy them into a JSON packet and expect it to work, we need to marshal things ourselves.
Again, the protocol lib tries hard to provide a natural API to actors and clients, and sometime that natural API might involve object APIs. I'm going to use a wickedly contrived example, bear with me. Let's say I have a small object that contains a number and has a few methods associated with it:
I want that response to look like `{ from: <actorID>, value: <number> }`, but the client side needs to know to return an Incrementor, not a primitive number. So let's tell the protocol lib about Incrementors:
protocol.types.addType("incrementor", {
// When writing to a protocol packet, just send the value
write: (v) => v.value,
// When reading from a protocol packet, wrap with an Incrementor
Or maybe you want to return a dictionary where one item is a incrementor. To do this you need to tell the type system which members of the dictionary need custom marshallers:
If an argument, return value, or dict property can be null/undefined, you can prepend `nullable:` to the type name:
"nullable:incrementor", // Can be null/undefined or an incrementor
"array:nullable:incrementor", // An array of incrementors that can have holes.
"nullable:array:incrementor" // Either null/undefined or an array of incrementors without holes.
Actors
------
Probably the most common objects that need custom martialing are actors themselves. These are more interesting than the Incrementor object, but by default they're somewhat easy to work with. Let's add a ChildActor implementation that will be returned by the HelloActor (which is rapidly becoming the OverwhelminglyComplexActor):
{ from: <childActorID>, greeting: "hello from child1" }
But the ID is the only interesting part of this made-up example. You're never going to want a reference to a ChildActor without checking its ID. Making an extra request just to get that id is wasteful. You really want the first response to look like `{ from: <actorID>, child: { actor: <childActorID>, greeting: "hello from child1" } }`
You can customize the marshalling of an actor by providing a `form` method in the `ChildActor` class:
You may come across a situation where you want to customize the output of a `form` method depending on the operation being performed. For example, imagine that ChildActor is a bit more complex, with a, b, c, and d members:
You might want to update your front's state when an event is fired, before emitting it against the front. You can use `preEvent` in the front definition for that:
You can have events wait until an asynchronous action completes before firing by returning a promise. If you have multiple preEvents defined for a specific event, and atleast one fires asynchronously, then all preEvents most resolve before all events are fired.
On a somewhat related note, not every method needs to be request/response. Just like an actor can emit a one-way event, a method can be marked as a one-way request. Maybe we don't care about giveGoodNews returning anything:
No, let's talk about custom front methods instead.
Custom Front Methods
--------------------
You might have some bookkeeping to do before issuing a request. Let's say you're calling `echo`, but you want to count the number of times you issue that request. Just use the `custom` tag in your front implementation:
This puts the generated implementation in `_echo` instead of `echo`, letting you implement `echo` as needed. If you leave out the `impl`, it just won't generate the implementation at all. You're on your own.
Lifetimes
---------
OK, I can't think of any more ways to put this off. The remote debugging protocol has the concept of a *parent* for each actor. This is to make distributed memory management a bit easier. Basically, any descendents of an actor will be destroyed if the actor is destroyed.
Other than that, the basic protocol makes no guarantees about lifetime. Each interface defined in the protocol will need to discuss and document its approach to lifetime management (although there are a few common patterns).
The protocol library will maintain the child/parent relationships for you, but it needs some help deciding what the child/parent relationships are.
The default parent of an object is the first object that returns it after it is created. So to revisit our earlier HelloActor `getChild` implementation:
This creates a new child actor owned by the current child actor. But in this example we want all actors created by the child to be owned by the HelloActor. So we can define a `defaultParent` property that makes use of the `parent` proeprty provided by the Actor class:
get marshallPool() { return this.parent }
The front needs to provide a matching `defaultParent` property that returns an owning front, to make sure the client and server lifetimes stay synced.
For more complex situations, you can define your own lifetime properties. Take this new pair of HelloActor methods:
// When the "temp" lifetime is specified, look for the _temporaryParent attribute as the owner.