Picolab / pico-engine

An implementation of the pico-engine hosted on node.js
http://picolabs.io/
MIT License
43 stars 8 forks source link

Extend `event:send` to better support events sent through subscriptions #622

Closed b1conrad closed 1 year ago

b1conrad commented 1 year ago

event:send the action expects a Map and an optional host. Together, these allow it to reach the pico that is to receive the event. In particular, it needs to know the ECI to be used to attract the attention of that pico, and this is currently given explicitly by a key "eci" in the Map.

While a ruleset might have hold of an ECI for some other pico, it is more often the case that it holds a subscription to that pico. A subscription is itself represented by a Map, and the ECI to use for the other pico is held in that Map with the key "Tx". So, if we have a subscription, bound to the name my_subs then we can do an event:send like this:

event:send( {"eci":my_subs{"Tx"}, ... })

This issue suggests a couple of shorthand, convenience, alternatives:

Alternative the first:

event:send( {"sub":my_subs, ...})

This would be useful when the programmer has already obtained, or is storing, the subscription.

Alternative the second:

event:send( {"sub": my_subs_id, ...})

This would be useful when the programmer is hanging on the a subscription's identifier, as in

my_subs_id = my_subs{"Id"}

These alternatives will need to be implemented by changes to pico-engine/packages/pico-engine-core/src/modules/event.ts

In the spirit of Test-driven development, failing tests are provided at https://b1conrad.github.io/KRL-experiments/UnitTest/event_send/

b1conrad commented 1 year ago

Let's settle on the first alternative. For one, it requires less code from the KRL programmer. For another, it is a lot simpler to implement.

b1conrad commented 1 year ago

A note on host. The first alternative is also better here, because if the subscription has a "host" key then that value would be the domain:port of the other pico and so is readily available for the event:send to use.

In the base (current) functionality, the KRL programmer has the double burden of

  1. knowing what ECI to use for the other pico
  2. knowing what domain:port hosts the other pico

Both of those are resolved in a subscription Map, as the keys

  1. "Tx"
  2. "Tx_host"
b1conrad commented 1 year ago

Notice that in the test rulesets, the first alternative is tested by test.event_send.sub_map while the second would have been tested by test.event_send.sub and they are listed in the opposite order in UnitTest/event:send. Sorry about that.