Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Messages sent on execution time from external code are not (always) received by the DOM (v0.4.1 and v0.4.3) #211

Open
lmeurs opened this issue Jun 17, 2013 · 2 comments

Comments

@lmeurs
Copy link

lmeurs commented Jun 17, 2013

The Communication example works great, but as it seems only when the Bonsaijs code is sent as a code option when calling bonsai.run() and not when the code is in an external file, ie.:

var movie = bonsai.run(
  document.getElementById('movie'),
  {
    url: 'path/to/external/file.js'
  }
);
// emitted before code gets executed
movie.on('load', function() {
  // receive event from the runner context
  movie.on('message:ready', function() {
    // send a categorized message to the runner context
    movie.sendMessage('externalData', {
      nodeData: document.getElementById('movie').innerHTML
    });
    // send just a message to the runner context
    movie.sendMessage({
      bonsai: 'tree'
    });
  });
});

External file:

// receive data from the other side
var text = new Text().addTo(stage);
stage.on('message:externalData', function(data) {
  text.attr('text', data.nodeData);
});
stage.on('message', function(data) {
  if (data.bonsai === 'tree') {
    text.attr('textFillColor', 'red');
  }
});
stage.sendMessage('ready', {});

NB1: I could not get this to work on jsfiddle, since the external file from our server did not get loaded, probably due to cross domain issues.
NB2: The movie.on('load', function() { ... }); event did get triggered.

When I call bonsai.run() on an external file (with or without the binding of load and message events):

var movie = bonsai.run(
  document.getElementById('movie'),
  {
    url: 'path/to/external/file.js'
  }
);

And the external file only contains:

var result = stage.sendMessage('ready', {});
console.log(result);

The output in the Chrome console is _Error logging argument: (Error: DataCloneError: DOM Exception 25)_, Firefox outputs something similar.

Is this a bug or am I missing something?

@lmeurs
Copy link
Author

lmeurs commented Jun 20, 2013

Ok, the problem got more clear. Sending messages from an external file is possible the way one expects, but not (always) at execution time. Perhaps a loading issue?

The Communication example code sends a ready message at the final line. When the example code remains in an external file, this message is not (always) received by the DOM, only when you delay the execution of the example code by ie. setTimeout(func, 10);.

NB: This always worked on my local setup in Chrome and 50% of the times in Firefox.

@iamdustan
Copy link
Contributor

@lmeurs just to provide a little bit more color, I had a similar issue where the different bonsai events I expected to receive when I could start drawing and sending messages back wasn’t accurate when using plugins.

I have the following in my main bonsai code, and on the document side I wait for this message to be received before sending any messages across.

// emit ready event
  stage.once('tick', function () {
    stage.sendMessage('ready', {});
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants