Files
SubMusic/source/Store/ObjectStore.mc
Memen 1f3fba2289 fixed settings storage having 0 Bytes
fixed some rare crashes on receiving invalid response
added setting to enable debug logging
removed default debug output (speed improvement)
2023-02-20 15:42:22 +01:00

67 lines
1.3 KiB
MonkeyC

/*
* Store number of items in a Dictionary by id
* Item class should have a function id()
* Item class should have a function toStorage()
*/
class ObjectStore extends Store {
function initialize(key) {
Store.initialize(key, {}); // initialize with empty object
}
// returns a connected item
function get(id) {
// if ($.debug) {
// System.println("ObjectStore::get( id : " + id + " )");
// }
var items = Store.value();
if (id == null) {
return null;
}
return items.get(id);
}
function getIds() {
// if ($.debug) {
// System.println("ObjectStore::getIds()");
// }
return Store.value().keys();
}
function getValues() {
return Store.value().values();
}
function save(item) {
if ($.debug) {
System.println("ObjectStore::save( item : " + item.toStorage() + " )");
}
// return false if failed save
var id = item.id();
if (id == null) {
return false;
}
// save details of the item
Store.value().put(id, item.toStorage());
return Store.update();
}
// returns true if item id entry removed from storage or is not in storage
function remove(item) {
if ($.debug) {
System.println("ObjectStore::remove( " + item.toStorage() + " )");
}
var id = item.id();
if (id == null) {
return true;
}
Store.value().remove(id);
return Store.update();
}
}