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

Issue using d.listen #77

Closed
motosota opened this issue May 4, 2015 · 3 comments
Closed

Issue using d.listen #77

motosota opened this issue May 4, 2015 · 3 comments

Comments

@motosota
Copy link

motosota commented May 4, 2015

It looks like there may be a possible race condition in d.listen - or that I am misunderstanding how to use it (quite possible).

I have one function A which transacts data to a db using data from a websocket connection:

function listener(args, kwargs) {
console.log('** Listener called', kwargs[':tx-data']);
d.transact(conn, kwargs[':tx-data']);
db1 = d.db(conn);
}

I have a separate reporter function B which generates an event for react js to trigger a re-render.

d.listen(conn, function(report) {
console.log('**Reporter heard and declaring showtime here is my report', report);
document.dispatchEvent(new CustomEvent('SHOWTIME'));
});

Re-rendering triggers a query from React and isn't always working - it turns out the problem is the data which the reporter thinks should be in the DB (from the the report tx_data) isn't always - so as if the listener trigger isn't when the transaction actually finishes.

Confusing is that moving the 'showtime' event dispatch to the end of A works.
This would make sense if d.transact was blocking, but if that was the true then I wouldn't expect d.listen to run until the transaction was finished.
If d.transact is non-blocking then I would expect to see the same issue with the event dispatch in either place - and I don't.

Any thoughts / suggestions appreciated.

@tonsky
Copy link
Owner

tonsky commented May 4, 2015

Listeners are all called inside d.transact fn. First it applies a transaction, then swaps conn value, then call all listeners. By the end of d.transact all listeners will already be called. Maybe that’s your issue? How is render fn gets value of actual DB? What’s db1 = d.db(conn); there for?

@motosota
Copy link
Author

motosota commented May 4, 2015

So I'm probably doing this wrong..
conn and db1 are both defined as variables at the start of the application page.

var d = datascript;
var db1;
var conn;
...

I'm just using db1 as the single database used by the application

conn is set after retrieving schema and initialising:

 conn = d.create_conn(schema);

db1 is set after each transaction:

db1 = d.db(conn);

Render gets value of actual db (db1) directly:

  var data = d.q('query our data here', db1);
  React.render(
    <MyApp data={data} />,
    document.getElementById('content')
  );
}, false);

So in summary I'm viewing conn as something that doesn't change after creating the db, db1 is forced to change after each transaction - and db1 is then used directly for React queries

@tonsky
Copy link
Owner

tonsky commented May 4, 2015

Yep, you don’t need db1. Just use d.db(conn) where you need latest DB.

@tonsky tonsky closed this as completed May 6, 2015
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