From 56122a37c360a75addd8a949b998d08e111afc0a Mon Sep 17 00:00:00 2001 From: shmyga Date: Fri, 29 Mar 2019 11:49:41 +0300 Subject: [PATCH] [storage] use haxe Serializer/Deserializer --- src/main/haxework/provider/Provider.hx | 2 +- src/main/haxework/storage/IStorage.hx | 14 +++--- src/main/haxework/storage/NoStorage.hx | 20 ++++----- .../haxework/storage/SharedObjectStorage.hx | 44 ++++++++++++------- src/main/haxework/utils/ObjectUtil.hx | 11 +++++ 5 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 src/main/haxework/utils/ObjectUtil.hx diff --git a/src/main/haxework/provider/Provider.hx b/src/main/haxework/provider/Provider.hx index a617dda..f74b538 100755 --- a/src/main/haxework/provider/Provider.hx +++ b/src/main/haxework/provider/Provider.hx @@ -53,4 +53,4 @@ class Provider { Reflect.setProperty(o, field, value); } } -} \ No newline at end of file +} diff --git a/src/main/haxework/storage/IStorage.hx b/src/main/haxework/storage/IStorage.hx index 2feeec1..0655c8c 100755 --- a/src/main/haxework/storage/IStorage.hx +++ b/src/main/haxework/storage/IStorage.hx @@ -1,9 +1,11 @@ package haxework.storage; -import flash.utils.ByteArray; - interface IStorage { - public function exists(key:String):Bool; - public function write(key:String, value:ByteArray):Void; - public function read(key:String):Null; -} \ No newline at end of file + public function exists(key:String):Bool; + + public function write(key:String, value:T):Void; + + public function read(key:String):Null; + + public function clear():Void; +} diff --git a/src/main/haxework/storage/NoStorage.hx b/src/main/haxework/storage/NoStorage.hx index 878f95f..3d1ccb5 100755 --- a/src/main/haxework/storage/NoStorage.hx +++ b/src/main/haxework/storage/NoStorage.hx @@ -1,16 +1,16 @@ package haxework.storage; -import flash.utils.ByteArray; - class NoStorage implements IStorage { - public function exists(key:String):Bool { - return false; - } + public function exists(key:String):Bool { + return false; + } - public function write(key:String, value:ByteArray):Void {} + public function write(key:String, value:T):Void {} - public function read(key:String):Null { - return null; - } -} \ No newline at end of file + public function read(key:String):Null { + return null; + } + + public function clear():Void {} +} diff --git a/src/main/haxework/storage/SharedObjectStorage.hx b/src/main/haxework/storage/SharedObjectStorage.hx index b297bf8..c626e52 100755 --- a/src/main/haxework/storage/SharedObjectStorage.hx +++ b/src/main/haxework/storage/SharedObjectStorage.hx @@ -1,24 +1,36 @@ package haxework.storage; -import haxe.crypto.Md5; import flash.net.SharedObject; -import flash.utils.ByteArray; +import haxe.Serializer; +import haxe.Unserializer; class SharedObjectStorage implements IStorage { - public function exists(key:String):Bool { - var so:SharedObject = SharedObject.getLocal("storage/" + Md5.encode(key)); - return so.size > 0; - } + private var so:SharedObject; - public function write(key:String, value:ByteArray):Void { - var so:SharedObject = SharedObject.getLocal("storage/" + Md5.encode(key)); - so.setProperty("value", value); - so.flush(); - } + public function new(name:String = "storage") { + so = SharedObject.getLocal(name); + } - public function read(key:String):Null { - var so:SharedObject = SharedObject.getLocal("storage/" + Md5.encode(key)); - return so.data.value; - } -} \ No newline at end of file + public function exists(key:String):Bool { + return Reflect.hasField(so.data, key); + } + + public function write(key:String, value:T):Void { + so.setProperty(key, Serializer.run(value)); + so.flush(); + } + + public function read(key:String):Null { + if (exists(key)) { + var data = Reflect.field(so.data, key); + return new Unserializer(data).unserialize(); + } else { + return null; + } + } + + public function clear():Void { + so.clear(); + } +} diff --git a/src/main/haxework/utils/ObjectUtil.hx b/src/main/haxework/utils/ObjectUtil.hx new file mode 100644 index 0000000..0cbcdf6 --- /dev/null +++ b/src/main/haxework/utils/ObjectUtil.hx @@ -0,0 +1,11 @@ +package haxework.utils; + +import haxe.Unserializer; +import haxe.Serializer; + +class ObjectUtil { + + public static function clone(object:T):T { + return new Unserializer(Serializer.run(object)).unserialize(); + } +}