dart - Launching multiple async futures in response to events -


i launch expensive operation in response user clicking on canvas element.

mousedown(mouseevent e) {   print("entering event handler");   var future = new future<int>(expensivefunction);   future.then((int value) => redrawcanvas(value);   print("done event handler"); }  expensivefunction() {   for(int = 0; < 1000000000; i++){     //do insane here   } }  redrawcanvas(int value) {   //do stuff here   print("redrawing canvas"); } 

my understanding of m4 dart, future constructor should launch "expensivefunction" asynchronously, aka on different thread main one. , appear way, "done event handler" printed output window in ide, , time later "redrawing canvas" printed. however, if click on element again nothing happens until "expensivefunction" done running previous click.

how use futures launch compute intensive function on new thread such can have multiple of them queued in response multiple clicks, if first future not complete yet?

thanks.

as mentioned in different answer, futures "placeholder value made available in future". don't imply concurrency.

dart has concept of isolates concurrency. can spawn isolate run code in parallel thread or process.

dart2js can compile isolates web workers. web worker can run in separate thread.

try this:

import 'dart:isolate';  expensiveoperation(sendport replyto) {   var result = doexpensivething(msg);   replyto.send(result); }  main() async {   var receive = new receiveport();   var isolate = await isolate.spawn(expensiveoperation, receive.sendport);   var result = await receive.first;   print(result); } 

(i haven't tested above, should work.)


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -