DeclareModule IDB
	Declare Init(DBName.s, Version.i, Stores.s, *Callback)
	Declare Put(Store.s, Key.s, Value.s, *Callback)
	Declare Get(Store.s, Key.s, *Callback)
	Declare Exists(Store.s, Key.s, *Callback)
	Declare Delete(Store.s, Key.s, *Callback)
	Declare GetAllKeys(Store.s, *Callback)
	Declare GetAll(Store.s, *Callback)
	Declare Clear(Store.s, *Callback)
	Declare Count(Store.s, *Callback)
EndDeclareModule

Module IDB
	EnableExplicit

	!window._kumos_idb = null;

	Procedure Init(DBName.s, Version.i, Stores.s, *Callback)
		!var _cb     = p_callback;
		!var _dbname = v_dbname;
		!var _ver    = v_version;
		!var _stores = v_stores;
		!
		!var req = indexedDB.open(_dbname, _ver);
		!
		!req.onupgradeneeded = function(ev) {
		!  var db = ev.target.result;
		!  _stores.split(',').forEach(function(name) {
		!    name = name.trim();
		!    if (name && !db.objectStoreNames.contains(name)) {
		!      db.createObjectStore(name);
		!    }
		!  });
		!};
		!req.onsuccess = function(ev) {
		!  window._kumos_idb = ev.target.result;
		!  if (_cb) _cb(1, '');
		!};
		!req.onerror = function(ev) {
		!  var msg = ev.target.error ? ev.target.error.message : 'open failed';
		!  if (_cb) _cb(0, msg);
		!};
		!req.onblocked = function() {
		!  if (_cb) _cb(0, 'IDB blocked - close other tabs using this app');
		!};
	EndProcedure

	Procedure Put(Store.s, Key.s, Value.s, *Callback)
		!var _cb    = p_callback;
		!var _store = v_store;
		!var _key   = v_key;
		!var _val   = v_value;
		!if (!window._kumos_idb) { if (_cb) _cb(0, 'IDB not open'); return; }
		!var tx  = window._kumos_idb.transaction([_store], 'readwrite');
		!var st  = tx.objectStore(_store);
		!var req = st.put(_val, _key);
		!req.onsuccess = function()  { if (_cb) _cb(1, ''); };
		!req.onerror   = function(e) { if (_cb) _cb(0, e.target.error ? e.target.error.message : 'put failed'); };
	EndProcedure

	Procedure Get(Store.s, Key.s, *Callback)
		!var _cb    = p_callback;
		!var _store = v_store;
		!var _key   = v_key;
		!if (!window._kumos_idb) { if (_cb) _cb(0, 'IDB not open'); return; }
		!var tx  = window._kumos_idb.transaction([_store], 'readonly');
		!var st  = tx.objectStore(_store);
		!var req = st.get(_key);
		!req.onsuccess = function(ev) {
		!  var v = ev.target.result;
		!  if (_cb) _cb(1, v !== undefined ? String(v) : '');
		!};
		!req.onerror = function(e) { if (_cb) _cb(0, e.target.error ? e.target.error.message : 'get failed'); };
	EndProcedure

	Procedure Exists(Store.s, Key.s, *Callback)
		!var _cb    = p_callback;
		!var _store = v_store;
		!var _key   = v_key;
		!if (!window._kumos_idb) { if (_cb) _cb(0, 'IDB not open'); return; }
		!var tx  = window._kumos_idb.transaction([_store], 'readonly');
		!var st  = tx.objectStore(_store);
		!var req = st.getKey(_key);
		!req.onsuccess = function(ev) {
		!  if (_cb) _cb(1, ev.target.result !== undefined ? '1' : '0');
		!};
		!req.onerror = function(e) { if (_cb) _cb(0, e.target.error ? e.target.error.message : 'exists failed'); };
	EndProcedure

	Procedure Delete(Store.s, Key.s, *Callback)
		!var _cb    = p_callback;
		!var _store = v_store;
		!var _key   = v_key;
		!if (!window._kumos_idb) { if (_cb) _cb(0, 'IDB not open'); return; }
		!var tx  = window._kumos_idb.transaction([_store], 'readwrite');
		!var st  = tx.objectStore(_store);
		!var req = st.delete(_key);
		!req.onsuccess = function()  { if (_cb) _cb(1, ''); };
		!req.onerror   = function(e) { if (_cb) _cb(0, e.target.error ? e.target.error.message : 'delete failed'); };
	EndProcedure

	Procedure GetAllKeys(Store.s, *Callback)
		!var _cb    = p_callback;
		!var _store = v_store;
		!if (!window._kumos_idb) { if (_cb) _cb(0, 'IDB not open'); return; }
		!var tx  = window._kumos_idb.transaction([_store], 'readonly');
		!var st  = tx.objectStore(_store);
		!var req = st.getAllKeys();
		!req.onsuccess = function(ev) { if (_cb) _cb(1, JSON.stringify(ev.target.result || [])); };
		!req.onerror   = function(e)  { if (_cb) _cb(0, e.target.error ? e.target.error.message : 'getAllKeys failed'); };
	EndProcedure

	Procedure GetAll(Store.s, *Callback)
		!var _cb    = p_callback;
		!var _store = v_store;
		!if (!window._kumos_idb) { if (_cb) _cb(0, 'IDB not open'); return; }
		!var tx = window._kumos_idb.transaction([_store], 'readonly');
		!var st = tx.objectStore(_store);
		!var keysReq = st.getAllKeys();
		!keysReq.onsuccess = function(ev) {
		!  var keys    = ev.target.result;
		!  var valsReq = st.getAll();
		!  valsReq.onsuccess = function(ev2) {
		!    var vals = ev2.target.result;
		!    var out  = {};
		!    for (var i = 0; i < keys.length; i++) out[String(keys[i])] = String(vals[i]);
		!    if (_cb) _cb(1, JSON.stringify(out));
		!  };
		!  valsReq.onerror = function(e) { if (_cb) _cb(0, e.target.error ? e.target.error.message : 'getAll/vals failed'); };
		!};
		!keysReq.onerror = function(e) { if (_cb) _cb(0, e.target.error ? e.target.error.message : 'getAll/keys failed'); };
	EndProcedure

	Procedure Clear(Store.s, *Callback)
		!var _cb    = p_callback;
		!var _store = v_store;
		!if (!window._kumos_idb) { if (_cb) _cb(0, 'IDB not open'); return; }
		!var tx  = window._kumos_idb.transaction([_store], 'readwrite');
		!var st  = tx.objectStore(_store);
		!var req = st.clear();
		!req.onsuccess = function()  { if (_cb) _cb(1, ''); };
		!req.onerror   = function(e) { if (_cb) _cb(0, e.target.error ? e.target.error.message : 'clear failed'); };
	EndProcedure

	Procedure Count(Store.s, *Callback)
		!var _cb    = p_callback;
		!var _store = v_store;
		!if (!window._kumos_idb) { if (_cb) _cb(0, 'IDB not open'); return; }
		!var tx  = window._kumos_idb.transaction([_store], 'readonly');
		!var st  = tx.objectStore(_store);
		!var req = st.count();
		!req.onsuccess = function(ev) { if (_cb) _cb(1, String(ev.target.result)); };
		!req.onerror   = function(e)  { if (_cb) _cb(0, e.target.error ? e.target.error.message : 'count failed'); };
	EndProcedure

EndModule
; IDE Options = SpiderBasic 3.10 (Windows - x86)
; CursorPosition = 15
; Folding = Dg
; iOSAppOrientation = 0
; AndroidAppCode = 0
; AndroidAppOrientation = 0
; EnableXP
; DPIAware
; CompileSourceDirectory