fluttervn / isolate_woker Goto Github PK
View Code? Open in Web Editor NEWLibrary help run flutter tasks in other isolate
License: MIT License
Library help run flutter tasks in other isolate
License: MIT License
I followed your example , but cancel is not working, cancelToken is always null in handleCancel()
class BackupTask implements FileTask<Future<bool>> {
final BackupRoutineModel rotinaBackup;
LibsshWrapper libssh;
LibsshCancelToken cancelToken;
BackupTask(this.rotinaBackup, {this.taskId}) {}
@override
Future<bool> execute() {
return _doExecute();
}
Future<bool> _doExecute() async {
var completer = Completer<bool>();
cancelToken = LibsshCancelToken();
try {
var server = rotinaBackup.servers.first;
libssh = LibsshWrapper(
server.host,
username: server.user,
password: server.password,
port: server.port,
verbosity: false,
);
libssh.connect();
List<DirectoryItem> fileObjects = server.fileObjects;
int totalSize = 0;
int totalLoaded = 0;
totalSize = fileObjects
.map((d) => libssh.getSizeOfFileSystemItem(d))
.toList()
.reduce((value, element) => value + element);
final start = DateTime.now();
for (var item in fileObjects) {
if (item.type == DirectoryItemType.directory) {
await libssh.scpDownloadDirectory(
item.path,
rotinaBackup.destinationDirectory,
printLog: (v) {
tasklogCallback(v);
},
callbackStats: (int total, int loaded, int currentFileSize,
int countDirectory, int countFiles) {
print('i');
totalLoaded += currentFileSize;
taskProgressCallback(totalSize, totalLoaded, 'a');
},
cancelToken: cancelToken,
);
} else if (item.type == DirectoryItemType.file) {
var currentFileSize = 0;
await libssh.scpDownloadFileTo(
item.path,
'${rotinaBackup.destinationDirectory}/${item.name}',
callbackStats: (int total, int loaded) {
currentFileSize = loaded;
},
recursive: false,
);
totalLoaded += currentFileSize;
taskProgressCallback(totalSize, totalLoaded, 'a');
}
if (cancelToken.isCancelled) {
break;
}
}
tasklogCallback('${DateTime.now().difference(start)}');
completer.complete(true);
} catch (e, s) {
tasklogCallback('BackupTask ${rotinaBackup.name} error: $e $s');
completer.completeError(e);
} finally {
libssh.dispose();
}
return completer.future;
}
@override
String taskId;
@override
ActionType actionType = ActionType.download;
///Function(int total, int loaded, String status);
@override
ProgressCallback taskProgressCallback;
@override
void handleCancel(String taskId) {
//cancelToken is null
print('BackupTask@handleCancel $cancelToken');
cancelToken?.cancel('Cancel backup $taskId');
}
@override
LogCallback tasklogCallback;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.