Comparison

util/pubsub.lua @ 10518:9f50489c2033 0.11

util.pubsub: Factor out calling of broadcaster This will simplify doing things along with broadcasting.
author Kim Alvefur <zash@zash.se>
date Thu, 06 Jun 2019 17:37:15 +0200
parent 9840:ec353524b739
child 10519:641e3b7a6a39
comparison
equal deleted inserted replaced
10439:97c0f5fe5f41 10518:9f50489c2033
497 return ok, err; 497 return ok, err;
498 end 498 end
499 end 499 end
500 500
501 self.events.fire_event("node-deleted", { service = self, node = node, actor = actor }); 501 self.events.fire_event("node-deleted", { service = self, node = node, actor = actor });
502 self.config.broadcaster("delete", node, node_obj.subscribers, nil, actor, node_obj, self); 502 self:broadcast("delete", node, node_obj.subscribers, nil, actor, node_obj);
503 return true; 503 return true;
504 end 504 end
505 505
506 -- Used to check that the config of a node is as expected (i.e. 'publish-options') 506 -- Used to check that the config of a node is as expected (i.e. 'publish-options')
507 local function check_preconditions(node_config, required_config) 507 local function check_preconditions(node_config, required_config)
560 end 560 end
561 if type(ok) == "string" then id = ok; end 561 if type(ok) == "string" then id = ok; end
562 local event_data = { service = self, node = node, actor = actor, id = id, item = item }; 562 local event_data = { service = self, node = node, actor = actor, id = id, item = item };
563 self.events.fire_event("item-published/"..node, event_data); 563 self.events.fire_event("item-published/"..node, event_data);
564 self.events.fire_event("item-published", event_data); 564 self.events.fire_event("item-published", event_data);
565 self.config.broadcaster("items", node, node_obj.subscribers, item, actor, node_obj, self); 565 self:broadcast("items", node, node_obj.subscribers, item, actor, node_obj);
566 return true; 566 return true;
567 end
568
569 function service:broadcast(event, node, subscribers, item, actor, node_obj)
570 return self.config.broadcaster(event, node, subscribers, item, actor, node_obj, self);
567 end 571 end
568 572
569 function service:retract(node, actor, id, retract) --> ok, err 573 function service:retract(node, actor, id, retract) --> ok, err
570 -- Access checking 574 -- Access checking
571 if not self:may(node, actor, "retract") then 575 if not self:may(node, actor, "retract") then
580 if not ok then 584 if not ok then
581 return nil, "internal-server-error"; 585 return nil, "internal-server-error";
582 end 586 end
583 self.events.fire_event("item-retracted", { service = self, node = node, actor = actor, id = id }); 587 self.events.fire_event("item-retracted", { service = self, node = node, actor = actor, id = id });
584 if retract then 588 if retract then
585 self.config.broadcaster("retract", node, node_obj.subscribers, retract, actor, node_obj, self); 589 self:broadcast("retract", node, node_obj.subscribers, retract, actor, node_obj);
586 end 590 end
587 return true 591 return true
588 end 592 end
589 593
590 function service:purge(node, actor, notify) --> ok, err 594 function service:purge(node, actor, notify) --> ok, err
602 else 606 else
603 self.data[node] = self.config.itemstore(self.nodes[node].config, node); 607 self.data[node] = self.config.itemstore(self.nodes[node].config, node);
604 end 608 end
605 self.events.fire_event("node-purged", { service = self, node = node, actor = actor }); 609 self.events.fire_event("node-purged", { service = self, node = node, actor = actor });
606 if notify then 610 if notify then
607 self.config.broadcaster("purge", node, node_obj.subscribers, nil, actor, node_obj, self); 611 self:broadcast("purge", node, node_obj.subscribers, nil, actor, node_obj);
608 end 612 end
609 return true 613 return true
610 end 614 end
611 615
612 function service:get_items(node, actor, ids) --> (true, { id, [id] = node }) or (false, err) 616 function service:get_items(node, actor, ids) --> (true, { id, [id] = node }) or (false, err)