auto-bind send target functions in broker. ensures listeners exist when needed

This commit is contained in:
Stewart Allen 2021-12-29 00:36:04 -05:00
commit c9fdf9f7c0
2 changed files with 6 additions and 2 deletions

View file

@ -55,7 +55,7 @@ function init() {
space.useDefaultKeys(true); space.useDefaultKeys(true);
// trigger space event binding // trigger space event binding
broker.publish('space.init', { space, platform }); broker.send.space_init({ space, platform });
// start worker // start worker
moto.client.start(`/code/mesh_work?${gapp.version}`); moto.client.start(`/code/mesh_work?${gapp.version}`);
@ -79,7 +79,7 @@ broker.subscribe('space.init', data => {
platform.setColor(platcolor); platform.setColor(platcolor);
load.File.load(evt.dataTransfer.files[0]) load.File.load(evt.dataTransfer.files[0])
.then(data => { .then(data => {
broker.publish('space.load', data); broker.send.space_load(data);
}) })
.catch(error => { .catch(error => {
dbug.error(error); dbug.error(error);

View file

@ -10,6 +10,7 @@ class Broker {
constructor() { constructor() {
this.topics = {}; this.topics = {};
this.used = {}; this.used = {};
this.send = {};
} }
topics() { topics() {
@ -29,6 +30,9 @@ class Broker {
channel = topics[topic] = []; channel = topics[topic] = [];
} }
if (channel.indexOf(listener) < 0) { if (channel.indexOf(listener) < 0) {
let send = this.send;
let name = topic.replace(/\./g, '_');
send[name] = send[name] = this.bind(topic);
channel.push(listener); channel.push(listener);
this.publish(".topic.add", topic); this.publish(".topic.add", topic);
} }