Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: aduros/flambe
base: c342b9bdaa
...
head fork: aduros/flambe
compare: 95d1f16793
  • 3 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 16, 2012
@aduros Add Storage.supported. 352951a
@aduros Derp. c22c538
@aduros Allow any serializable object as Storage values.
Every time I have a persisted String -> String map in any language, it mainly
ends up being I only use one big value to contain all my app's persisted
config, encoded in JSON or similar. It's way more convienient than spreading
things out over multiple keys and handling individual serialization, etc.
95d1f16
View
14 src/flambe/platform/DummyStorage.hx
@@ -9,19 +9,25 @@ class DummyStorage
{
private static var log = Log.log; // http://code.google.com/p/haxe/issues/detail?id=365
+ public var supported (isSupported, null) :Bool;
+
public function new ()
{
- log.warn("Using non-persistent storage");
clear();
}
- public function set (key :String, value :String) :Bool
+ public function isSupported () :Bool
+ {
+ return false;
+ }
+
+ public function set (key :String, value :Dynamic) :Bool
{
_hash.set(key, value);
return true;
}
- public function get (key :String) :String
+ public function get (key :String) :Dynamic
{
return _hash.get(key);
}
@@ -36,5 +42,5 @@ class DummyStorage
_hash = new Hash();
}
- private var _hash :Hash<String>;
+ private var _hash :Hash<Dynamic>;
}
View
13 src/flambe/platform/Storage.hx
@@ -7,13 +7,20 @@ package flambe.platform;
interface Storage
{
/**
+ * True if the environment supports persisted storage. Otherwise, the storage is backed by a
+ * Hash and not actually persisted between sessions.
+ */
+ var supported (isSupported, null) :Bool;
+
+ /**
* Add a key to the storage, replacing any existing value.
- * @return True if the value was successfully persisted.
+ * @param value An object that can be serialized with haxe.Serializer.
+ * @return True if the value was successfully serialized and persisted.
*/
- function set (key :String, value :String) :Bool;
+ function set (key :String, value :Dynamic) :Bool;
/** Retrieve a value from storage for a given key. */
- function get (key :String) :String;
+ function get (key :String) :Dynamic;
/** Deletes a key/value pair from storage. */
function remove (key :String) :Void;
View
3  src/flambe/platform/flash/FlashAppDriver.hx
@@ -98,7 +98,8 @@ class FlashAppDriver
try {
_storage = new FlashStorage(SharedObject.getLocal("flambe"));
} catch (err :Dynamic) {
- // SharedObject.getLocal may throw an error, fall back to temporary storage
+ // SharedObject.getLocal may throw an error
+ log.warn("SharedObject is unavailable, falling back to unpersisted storage");
_storage = new DummyStorage();
}
}
View
39 src/flambe/platform/flash/FlashStorage.hx
@@ -6,25 +6,56 @@ package flambe.platform.flash;
import flash.net.SharedObject;
+import haxe.Serializer;
+import haxe.Unserializer;
+
import flambe.platform.Storage;
class FlashStorage
implements Storage
{
+ private static var log = Log.log; // http://code.google.com/p/haxe/issues/detail?id=365
+
+ public var supported (isSupported, null) :Bool;
+
public function new (so :SharedObject)
{
_so = so;
}
- public function set (key :String, value :String) :Bool
+ public function isSupported () :Bool
+ {
+ return true;
+ }
+
+ public function set (key :String, value :Dynamic) :Bool
{
- _so.data[untyped key] = value;
+ var encoded :String;
+ try {
+ var serializer = new Serializer();
+ serializer.useCache = true; // Allow circular references
+ serializer.serialize(value);
+ encoded = serializer.toString();
+ } catch (error :Dynamic) {
+ log.warn("Storage serialization failed", ["message", error]);
+ return false;
+ }
+
+ _so.data[untyped key] = encoded;
return true;
}
- public function get (key :String) :String
+ public function get (key :String) :Dynamic
{
- return _so.data[untyped key];
+ var encoded :String = _so.data[untyped key];
+ if (encoded != null) {
+ try {
+ return Unserializer.run(encoded);
+ } catch (error :Dynamic) {
+ log.warn("Storage unserialization failed", ["message", error]);
+ }
+ }
+ return null;
}
public function remove (key :String)
View
4 src/flambe/platform/html/HtmlAppDriver.hx
@@ -190,10 +190,10 @@ class HtmlAppDriver
// Browsers may throw an error on accessing localStorage:
// http://dev.w3.org/html5/webstorage/#dom-localstorage
}
- if (localStorage == null) {
+ if (localStorage != null) {
_storage = new HtmlStorage(localStorage);
} else {
- log.warn("localStorage is unavailable, falling back to unpersisted DummyStorage");
+ log.warn("localStorage is unavailable, falling back to unpersisted storage");
_storage = new DummyStorage();
}
}
View
39 src/flambe/platform/html/HtmlStorage.hx
@@ -4,6 +4,9 @@
package flambe.platform.html;
+import haxe.Serializer;
+import haxe.Unserializer;
+
import flambe.platform.Storage;
class HtmlStorage
@@ -11,15 +14,33 @@ class HtmlStorage
{
private static var log = Log.log; // http://code.google.com/p/haxe/issues/detail?id=365
+ public var supported (isSupported, null) :Bool;
+
public function new (storage :Dynamic)
{
_storage = storage;
}
- public function set (key :String, value :String) :Bool
+ public function isSupported () :Bool
{
+ return true;
+ }
+
+ public function set (key :String, value :Dynamic) :Bool
+ {
+ var encoded :String;
try {
- _storage.setItem(PREFIX + key, value);
+ var serializer = new Serializer();
+ serializer.useCache = true; // Allow circular references
+ serializer.serialize(value);
+ encoded = serializer.toString();
+ } catch (error :Dynamic) {
+ log.warn("Storage serialization failed", ["message", error]);
+ return false;
+ }
+
+ try {
+ _storage.setItem(PREFIX + key, encoded);
} catch (error :Dynamic) {
// setItem may throw a QuotaExceededError:
// http://dev.w3.org/html5/webstorage/#dom-localstorage
@@ -29,13 +50,23 @@ class HtmlStorage
return true;
}
- public function get (key :String) :String
+ public function get (key :String) :Dynamic
{
+ var encoded :String;
try {
- return _storage.getItem(PREFIX + key);
+ encoded = _storage.getItem(PREFIX + key);
} catch (error :Dynamic) {
// This should never happen, but it sometimes does in Firefox and IE
log.warn("localStorage.getItem failed", ["message", error.message]);
+ return null;
+ }
+
+ if (encoded != null) {
+ try {
+ return Unserializer.run(encoded);
+ } catch (error :Dynamic) {
+ log.warn("Storage unserialization failed", ["message", error]);
+ }
}
return null;
}

No commit comments for this range

Something went wrong with that request. Please try again.