MithrilJS / mithril.js

A JavaScript Framework for Building Brilliant Applications
https://mithril.js.org
MIT License
13.98k stars 926 forks source link

concurrency issue: startComputation endComputation #240

Closed g00fy- closed 10 years ago

g00fy- commented 10 years ago

m.render() gets called when all pending computations are resolved,

lets take a case when on user action multiple (2) async computations are called (http call).

1st completes in 200 ms 2nd times out after 30 seconds

the dom would be updated after 30 seconds

The only thing i can think of that would help in this scenario is:

startComputation and endComputation accept config

startComputation(group | timeout);

startComputation returns endComputation callback

example 1 (batch updates)

var group = 'fetch-data-from-multiple-sources';

// somewhere in component  A
var end1 = m.startComputation(group);
// simulate a long http request;
setTimeout(end1,1000);

// somewhere in component B  - note: A & B are decoupled : they don't know about each other
var end2 = m.startComputation(group);
// simulate a timedout http request
setTimeout(end2,30000);

//  somewhere in component C - needs to refresh quickly;
var end3 = m.startComputation(0); // invokes render() immediately - doesn't wait for other computations
// simulate loading data from indexedDB (async)
setTimeout(end3,0);
lhorie commented 10 years ago

There's the background option in m.request to prevent it from affecting redrawing:

//go ahead and redraw without me
m.request({method: "GET", url: "/foo", background: true})
  .then(function() {
    //redraw again
    m.redraw()
  })
g00fy- commented 10 years ago

@lhorie the background:true is only m.request specific - what about other use cases ? 3rd party libs may use jquery for handling http or even IndexedDB which also is async. I think it would be worth implementing a generic solution.

Some components require the DOM to be already in place. Would it be possible (with the current architecture) to trigger synchronous redraw() (not with requestAnimationFrame but at the end of diff computing) ? I know about the config - but this still is asynchronous.

lhorie commented 10 years ago

@g00fy- third party libraries must call m.startComputation / m.endComputation if they want their asynchronous callbacks to be taken into consideration for the redrawing schedule. If not, they can simply call m.redraw when their completion callbacks run.

Re: synchronous redraws: Does m.redraw(true) do what you need?