-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable isolates since they exacerbate the memory growth problem. Add QueueExecutor which adds deduplication to scheduled jobs. issue: #24
- Loading branch information
1 parent
1a7e211
commit 8a2ece5
Showing
16 changed files
with
327 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:vector_map_tiles/src/executor/executor.dart'; | ||
|
||
class QueueExecutor extends Executor { | ||
bool _disposed = false; | ||
final _queue = <_Job>[]; | ||
var _scheduled = false; | ||
|
||
@override | ||
void dispose() { | ||
_disposed = true; | ||
} | ||
|
||
@override | ||
bool get disposed => _disposed; | ||
|
||
@override | ||
Future<R> submit<Q, R>(Job<Q, R> job) { | ||
final internalJob = _Job(job); | ||
_queue.add(internalJob); //LIFO | ||
_schedule(); | ||
return internalJob.completer.future; | ||
} | ||
|
||
@override | ||
List<Future<R>> submitAll<Q, R>(Job<Q, R> job) => [submit(job)]; | ||
|
||
void _schedule() { | ||
_completeCancelled(); | ||
if (!_scheduled && _queue.isNotEmpty) { | ||
_scheduled = true; | ||
scheduleMicrotask(_runOneAndReschedule); | ||
} | ||
} | ||
|
||
void _runOneAndReschedule() async { | ||
_scheduled = false; | ||
if (_queue.isNotEmpty) { | ||
final job = _queue.removeLast(); //LIFO | ||
try { | ||
if (_disposed) { | ||
throw 'disposed'; | ||
} | ||
if (job.request.isCancelled) { | ||
throw CancellationException(); | ||
} | ||
final result = await job.apply(); | ||
job.completer.complete(result); | ||
_completeCancelled(); | ||
_completeDuplicates(job, result); | ||
} catch (error, stack) { | ||
job.completer.completeError(error, stack); | ||
} | ||
} | ||
_schedule(); | ||
} | ||
|
||
void _completeDuplicates(_Job job, result) { | ||
final deduplicationKey = job.request.deduplicationKey; | ||
if (deduplicationKey != null) { | ||
_queue.removeWhere((queued) { | ||
if (queued.request.deduplicationKey == deduplicationKey) { | ||
queued.completer.complete(result); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
} | ||
|
||
void _completeCancelled() {} | ||
} | ||
|
||
class _Job<Q, R> { | ||
final Job<Q, R> request; | ||
final completer = Completer<R>(); | ||
|
||
_Job(this.request); | ||
|
||
Future<R> apply() async => await request.computeFunction(request.value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.