Skip to content

Commit

Permalink
fix simple storage, too many async write on same file at same time th…
Browse files Browse the repository at this point in the history
…rows error
  • Loading branch information
rinick committed Dec 6, 2015
1 parent 1e1ae9f commit e89af9a
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/src/storage/simple_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,37 @@ class SimpleValueStorage extends IValueStorage {
SimpleValueStorage(this.bucket, this.key) {
_file = new File("${bucket.dir.path}/${Uri.encodeComponent(key)}");
}

bool _pendingSet = false;
Object _pendingValue;
Object _setValue;

/// set the value, if previous setting is not finished, it will be set later
void setValue(Object value) {
// TODO: optimize this part so it doesn"t need to re-create path and File object everytime
_file.writeAsString(DsJson.encode(value));
_pendingValue = value;
if (_pendingSet) {
return;
}
_setValue = value;
_pendingSet = true;
_file.writeAsString(DsJson.encode(value)).then(onSetDone).catchError(onSetDone);
}
void onSetDone(Object obj) {
_pendingSet = false;
if (_setValue != _pendingValue) {
setValue(_pendingValue);
}
}

void destroy() {
_pendingValue = null;
_setValue = null;
_file.delete().catchError(_ignoreError);
}

getValueAsync() async {
if (_pendingValue != null) {
return _pendingValue;
}
try {
return DsJson.decode(await _file.readAsString());
} catch (err) {}
Expand Down

0 comments on commit e89af9a

Please sign in to comment.