improved KVStore

This commit is contained in:
Djeeberjr 2023-05-29 12:49:42 +02:00
parent dd2e9a4993
commit a343db133e

View File

@ -10,7 +10,7 @@ import haxe.ds.StringMap;
**/ **/
class KVStore { class KVStore {
private var kvStore: StringMap<Dynamic> = new StringMap(); private var kvStore: StringMap<Dynamic> = new StringMap();
private final namespace:String; public final namespace:String;
public function new(namespace: String) { public function new(namespace: String) {
this.namespace = namespace; this.namespace = namespace;
@ -21,6 +21,11 @@ class KVStore {
FS.delete(nsFile); FS.delete(nsFile);
} }
public static function getStoreForClass(?pos:haxe.PosInfos) {
var className = pos.className;
return new KVStore(className);
}
private static function getNamespaceFile(namespace: String): String { private static function getNamespaceFile(namespace: String): String {
return '/var/ns/$namespace'; return '/var/ns/$namespace';
} }
@ -50,8 +55,8 @@ class KVStore {
this.kvStore.set(key,value); this.kvStore.set(key,value);
} }
public inline function get<T>(key: String): Null<T> { public inline function get<T>(key: String,?orElse:T = null): Null<T> {
return this.kvStore.get(key); return this.kvStore.get(key) ?? orElse;
} }
public inline function exists(key: String): Bool { public inline function exists(key: String): Bool {