Skip to content

Commit

Permalink
created the 'add' method into the cargo flow
Browse files Browse the repository at this point in the history
  • Loading branch information
jorishermans committed Aug 4, 2014
1 parent 7a5998c commit f8a9975
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

This file contains highlights of what changes on each version of the force package.

#### Pub version 0.3.4 ####

- Add the method, 'add' to cargo, so you can add data to a key in a list. This can be handy if you want to collect data.
- Improve the working of localstorage, encode and decode objects.

#### Pub version 0.3.3 ####

- Add the implementation of IndexDb as an option
Expand Down
1 change: 1 addition & 0 deletions lib/cargo_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ library cargo_client;
import 'package:logging/logging.dart' show Logger, Level, LogRecord;
import 'dart:html';
import 'dart:async';
import 'dart:convert';
import 'dart:indexed_db';

import 'package:cargo/cargo_base.dart';
Expand Down
3 changes: 3 additions & 0 deletions lib/src/cargo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ abstract class CargoBase {

/// Set/update an item synchronously
void setItem(String key, data);

/// Add item synchronously
void add(String key, data);

/// Remove/delete an item synchronously
void removeItem(String key);
Expand Down
32 changes: 29 additions & 3 deletions lib/src/client/indexdb_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,41 @@ class IndexDbCargo extends Cargo {
}

Future getItem(String key) {
return _doCommand((ObjectStore store) => store.delete(key));
return _doCommand((ObjectStore store) => store.getObject(key),
'readonly');
}

void setItem(String key, data) {
_doCommand((ObjectStore store) {
return store.put(key, data);
dispatch(key, data);
return store.put(key, data);
});

dispatch(key, data);

}

void add(String key, data) {

_doCommand((ObjectStore store) {

store.getObject(key).then((obj) {
if (obj is List) {
List list = obj;
list.add(data);

dispatch(key, list);
return store.put(key, list);
}

}, onError: (e) {
List list = new List();
list.add(data);

dispatch(key, list);
return store.put(key, list);
});
});

}

void removeItem(String key) {
Expand Down
23 changes: 20 additions & 3 deletions lib/src/client/storage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,37 @@ class LocalstorageCargo extends Cargo {
}

dynamic getItemSync(String key) {
return values[key];
return JSON.decode(values[key]);
}

Future getItem(String key) {
Completer complete = new Completer();
complete.complete(values[key]);
complete.complete(JSON.decode(values[key]));
return complete.future;
}

void setItem(String key, data) {
values[key] = data;
values[key] = JSON.encode(data);

dispatch(key, data);
}

void add(String key, data) {
List list = new List();
if (values.containsKey(key)) {
Object obj = JSON.decode(values[key]);
if (obj is List) {
list = JSON.decode(values[key]);
}
}
_add(list, key, data);
}

void _add(List list, String key, data) {
list.add(data);

setItem(key, list);
}

void removeItem(String key) {
values.remove(key);
Expand Down
18 changes: 18 additions & 0 deletions lib/src/server/file_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ class FileBackend extends Cargo {
}
dispatch(key, data);
}

void add(String key, data) {
List list = new List();
if (keys.contains(key)) {
Object obj = getItem(key).then((obj) {
if (obj is List) {
list = obj;
_add(list, key, data);
}
});
}
}

void _add(List list, String key, data) {
list.add(data);

setItem(key, list);
}

void _writeFile (File file, key, data) {
file.writeAsStringSync(JSON.encode(data));
Expand Down
17 changes: 17 additions & 0 deletions lib/src/shared/memory_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ class MemoryImpl extends CargoBase with CargoDispatch {

dispatch(key, data);
}

void add(String key, data) {
List list = new List();
if (values.containsKey(key)) {
if (values[key] is List) {
list = values[key];
}
}
_add(list, key, data);
}

void _add(List list, String key, data) {
list.add(data);

dispatch(key, list);
values[key] = list;
}

void removeItem(String key) {
values.remove(key);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: cargo
version: 0.3.3
version: 0.3.4
author: Joris Hermans <hermansj@gmail.com>
description: A key value, storage library for dart
homepage: https://github.com/jorishermans/cargo
Expand Down

0 comments on commit f8a9975

Please sign in to comment.