Skip to content

Commit

Permalink
renaming & indexcb implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jorishermans committed Jul 31, 2014
1 parent 9f81013 commit 7a5998c
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

#### Pub version 0.3.3 ####

- Add the implementation of IndexDb as an option

#### Pub version 0.3.2+1 ####

- Small improvement in reading out a directory.
Expand Down
6 changes: 4 additions & 2 deletions lib/cargo_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ library cargo_client;
import 'package:logging/logging.dart' show Logger, Level, LogRecord;
import 'dart:html';
import 'dart:async';
import 'dart:indexed_db';

import 'package:cargo/cargo_base.dart';

part 'src/client/cargo.dart';
part 'src/client/storage_backend.dart';
part 'src/client/memory_client.dart';
part 'src/client/storage_impl.dart';
part 'src/client/memory_impl.dart';
part 'src/client/cargo_mode.dart';
part 'src/client/indexdb_impl.dart';

4 changes: 2 additions & 2 deletions lib/cargo_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import 'package:cargo/cargo_base.dart';

part 'src/server/cargo.dart';
part 'src/server/cargo_mode.dart';
part 'src/server/file_backend.dart';
part 'src/server/memory_backend.dart';
part 'src/server/file_impl.dart';
part 'src/server/memory_impl.dart';

11 changes: 8 additions & 3 deletions lib/src/client/cargo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ abstract class Cargo extends CargoBase with CargoDispatch {

switch(MODE) {
case CargoMode.MEMORY:
return new MemoryClient();
return new MemoryCargo();
case CargoMode.LOCAL:
return new LocalstorageBackend(window.localStorage);
return new LocalstorageCargo(window.localStorage);
case CargoMode.SESSION:
return new LocalstorageBackend(window.sessionStorage);
return new LocalstorageCargo(window.sessionStorage);
case CargoMode.INDEXDB:
if (IndexDbCargo.supported) {
return new IndexDbCargo("dbName", "storeName");
}
return new LocalstorageCargo(window.localStorage);
default:
Logger.root.warning("Error: Unsupported storage backend \"${MODE}\", supported backends on server is: ${CargoMode.MEMORY} and ${CargoMode.LOCAL}");
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/client/cargo_mode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ class CargoMode {
static const MEMORY = const CargoModeHolder('Memory');
static const LOCAL = const CargoModeHolder('Local');
static const SESSION = const CargoModeHolder('Session');
static const INDEXDB = const CargoModeHolder('INDEXDB');
}
87 changes: 87 additions & 0 deletions lib/src/client/indexdb_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
part of cargo_client;

class IndexDbCargo extends Cargo {
String dbName;
String storeName;

bool _isOpen;

int count = 0;

/// Returns true if IndexedDB is supported on this platform.
static bool get supported => IdbFactory.supported;

static Map<String, Database> _databases = new Map<String, Database>();

IndexDbCargo(this.dbName, this.storeName) : super._();

dynamic getItemSync(String key) {
throw new UnsupportedError('IndexedDB is not supporting synchronous retrieval of data');
}

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

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

dispatch(key, data);
}

void removeItem(String key) {
_doCommand((ObjectStore store) => store.delete(key));
}

void clear() {
_doCommand((ObjectStore store) => store.clear());
}

int length() {
throw new UnsupportedError('IndexedDB is not supporting length retrieval synchronously');
}

Future _doCommand(Future requestCommand(ObjectStore store),
[String txnMode = 'readwrite']) {
var completer = new Completer();
var trans = _db.transaction(storeName, txnMode);
var store = trans.objectStore(storeName);
var future = requestCommand(store);
return trans.completed.then((_) => future);
}

Future start() {
if (!supported) {
return new Future.error(
new UnsupportedError('IndexedDB is not supported on this platform'));
}

return window.indexedDB.open(dbName)
.then((Database db) {
//print("Newly opened db $dbName has version ${db.version} and stores ${db.objectStoreNames}");
if (!db.objectStoreNames.contains(storeName)) {
db.close();
//print('Attempting upgrading $storeName from ${db.version}');
return window.indexedDB.open(dbName,
onUpgradeNeeded: (e) {
//print('Upgrading db $dbName to ${db.version + 1}');
Database d = e.target.result;
d.createObjectStore(storeName);
}
);
} else {
//print('The store $storeName exists in $dbName');
return db;
}
})
.then((db){
_databases[dbName] = db;
_isOpen = true;
return true;
});
}

Database get _db => _databases[dbName];
}
5 changes: 0 additions & 5 deletions lib/src/client/memory_client.dart

This file was deleted.

5 changes: 5 additions & 0 deletions lib/src/client/memory_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
part of cargo_client;

class MemoryCargo extends MemoryImpl implements Cargo {

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
part of cargo_client;

class LocalstorageBackend extends Cargo {
class LocalstorageCargo extends Cargo {
Completer _completer;
Storage values;

LocalstorageBackend(this.values) : super._() {
LocalstorageCargo(this.values) : super._() {
_completer = new Completer();
_completer.complete();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/server/cargo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ abstract class Cargo extends CargoBase with CargoDispatch {

switch(MODE) {
case CargoMode.MEMORY:
return new MemoryBackend();
return new MemoryCargo();
case CargoMode.FILE:
return new FileBackend(path);
default:
Expand Down
File renamed without changes.
6 changes: 0 additions & 6 deletions lib/src/server/memory_backend.dart

This file was deleted.

6 changes: 6 additions & 0 deletions lib/src/server/memory_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
part of cargo_server;

class MemoryCargo extends MemoryImpl implements Cargo {

}

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.2+1
version: 0.3.3
author: Joris Hermans <hermansj@gmail.com>
description: A key value, storage library for dart
homepage: https://github.com/jorishermans/cargo
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ You can also turn the event off!
Or remove all the listeners

cargo.offAll("userData");

These are the modes that you can use:

Serverside:

CargoMode.MEMORY
CargoMode.FILE

Clientside:

CargoMode.MEMORY
CargoMode.INDEXDB
CargoMode.LOCAL
CargoMode.SESSION

### Contributing ###

Expand Down

0 comments on commit 7a5998c

Please sign in to comment.