Warning | ||
---|---|---|
| ||
This page has not been adjusted for version 1.0 of the pico-engine |
Having a pico that maintains a collection of other picos is a pattern that is often useful.
...
Code Block | ||
---|---|---|
| ||
rule collection_subscriptions { select when wrangler child_initialized pre { eci = event:attr("eci"); collection_eci = get_wellKnown_eci(eci); } if collection_eci then noop(); fired { raise grouper event "need_subscriptions" attributes event:attrattrs("rs_attrs").put("collection_eci",collection_eci); } } |
...
This KRL code will respond to the internal event, expecting the collection_eci
and the child_specs
given to wrangler earlier (wrangler renames it rs_attrs
for some reason).
Code Block | ||
---|---|---|
| ||
rule collection_subscription_requests { select when grouper need_subscriptions foreach by_prefix(event:attr("name_prefix")) setting(room_name,tag_id) pre { eci = building:eci(tag_id); wellKnown_Tx = get_wellKnown_eci(eci).klog("wellKnown_Tx"); } if wellKnown_Tx then every { event:send({"eci":eci,"domain":"wrangler","type":"subscription", "attrs": { "wellKnown_Tx": event:attr("collection_eci"), "Rx_role": "member", "Tx_role": "collection", "name": tag_id, "channel_type": "subscription" } }) } } |
...
and specify additional processing in some_function
(defined in lines 6-8) that it shares (in line 3) to endpoints, and/or specify rules like some_rule
(defined in lines 10-14) that take action on the collection's members when certain events occur.
When a member is added
Since members of the collection are determined by subscriptions, your ruleset can listen for wrangler:subscription_added
events to do something when a new member joins your collection. Since your pico might participate in other subscriptions, only those in which the roles are "collection"
and "member"
would be applicable.
Code Block | ||
---|---|---|
| ||
rule new_member {
select when wrangler subscription_added
pre {
pertinent = event:attr("Rx_role")=="collection"
&& event:attr("Tx_role")=="member";
}
if pertinent then noop();
fired {
raise collection event "new_member" attributes event:attrs
}
} |
The event attributes available are those surrounding a subscription in general. Some of these might be useful when a new member arrives into your collection
Tip | ||
---|---|---|
| ||
|
When a member is removed
Your ruleset might wish to listen to the event wrangler:subscription_removed
which will be raised when a subscription is removed from your pico. Such events which are applicable to membership in your collection could then be handled appropriately.
Code Block | ||
---|---|---|
| ||
rule new_member {
select when wrangler subscription_removed
pre {
pertinent = event:attr("Rx_role")=="collection"
&& event:attr("Tx_role")=="member";
}
if pertinent then noop();
fired {
raise collection event "member_removed" attributes event:attrs
}
} |