Update floDapps.js

Improving offline capabilities. Will not get hung if cloud supernodes are offline
This commit is contained in:
tripathyr 2025-08-25 07:52:11 +05:30 committed by GitHub
parent 032acc341d
commit 3750d98038
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -144,7 +144,7 @@
} }
}); });
var subAdmins, trustedIDs, settings; var subAdmins = [], trustedIDs = [], settings = {};
Object.defineProperties(floGlobals, { Object.defineProperties(floGlobals, {
subAdmins: { subAdmins: {
get: () => subAdmins get: () => subAdmins
@ -249,106 +249,154 @@
const startUpFunctions = []; const startUpFunctions = [];
startUpFunctions.push(function readSupernodeListFromAPI() { startUpFunctions.push(function readSupernodeListFromAPI() {
return new Promise((resolve, reject) => { return new Promise((resolve) => {
if (!startUpOptions.cloud) if (!startUpOptions.cloud)
return resolve("No cloud for this app"); return resolve("No cloud for this app");
const CLOUD_KEY = "floCloudAPI#" + floCloudAPI.SNStorageID; const CLOUD_KEY = "floCloudAPI#" + floCloudAPI.SNStorageID;
// Fallback: init from cached nodes (never reject)
const initFromCache = (tag) =>
compactIDB.readData("supernodes", CLOUD_KEY, DEFAULT.root)
.then(nodes => {
nodes = nodes || {};
return floCloudAPI.init(nodes)
.then(r => resolve(`${tag} (from cache)\n${r}`))
.catch(() => resolve(`${tag} (cache present, init skipped)`));
})
.catch(() => resolve(`${tag} (no cache)`));
compactIDB.readData("lastTx", CLOUD_KEY, DEFAULT.root).then(lastTx => { compactIDB.readData("lastTx", CLOUD_KEY, DEFAULT.root).then(lastTx => {
var query_options = { sentOnly: true, pattern: floCloudAPI.SNStorageName }; const query_options = { sentOnly: true, pattern: floCloudAPI.SNStorageName };
if (typeof lastTx == 'number') //lastTx is tx count (*backward support) if (typeof lastTx === 'number') // backward support (tx count)
query_options.ignoreOld = lastTx; query_options.ignoreOld = lastTx;
else if (typeof lastTx == 'string') //lastTx is txid of last tx else if (typeof lastTx === 'string') // last txid
query_options.after = lastTx; query_options.after = lastTx;
//fetch data from flosight
// Try online; if it fails, fall back to cache
floBlockchainAPI.readData(floCloudAPI.SNStorageID, query_options).then(result => { floBlockchainAPI.readData(floCloudAPI.SNStorageID, query_options).then(result => {
compactIDB.readData("supernodes", CLOUD_KEY, DEFAULT.root).then(nodes => { compactIDB.readData("supernodes", CLOUD_KEY, DEFAULT.root).then(nodes => {
nodes = nodes || {}; nodes = nodes || {};
for (var i = result.data.length - 1; i >= 0; i--) { for (let i = result.data.length - 1; i >= 0; i--) {
var content = JSON.parse(result.data[i])[floCloudAPI.SNStorageName]; const content = JSON.parse(result.data[i])[floCloudAPI.SNStorageName];
for (let sn in content.removeNodes) if (!content || typeof content !== 'object') continue;
delete nodes[sn]; if (content.removeNodes)
for (let sn in content.newNodes) for (let sn in content.removeNodes) delete nodes[sn];
nodes[sn] = content.newNodes[sn]; if (content.newNodes)
for (let sn in content.updateNodes) for (let sn in content.newNodes) nodes[sn] = content.newNodes[sn];
if (sn in nodes) //check if node is listed if (content.updateNodes)
nodes[sn].uri = content.updateNodes[sn]; for (let sn in content.updateNodes)
if (sn in nodes) nodes[sn].uri = content.updateNodes[sn];
} }
Promise.all([ Promise.all([
compactIDB.writeData("lastTx", result.lastItem, CLOUD_KEY, DEFAULT.root), compactIDB.writeData("lastTx", result.lastItem, CLOUD_KEY, DEFAULT.root),
compactIDB.writeData("supernodes", nodes, CLOUD_KEY, DEFAULT.root) compactIDB.writeData("supernodes", nodes, CLOUD_KEY, DEFAULT.root)
]).then(_ => { ]).then(() => {
floCloudAPI.init(nodes) floCloudAPI.init(nodes)
.then(result => resolve("Loaded Supernode list\n" + result)) .then(r => resolve("Loaded Supernode list\n" + r))
.catch(error => reject(error)) .catch(() => resolve("Loaded Supernode list (init deferred)"));
}).catch(error => reject(error)) }).catch(() => resolve("Supernode list updated (persist partial)"));
}).catch(error => reject(error)) }).catch(() => initFromCache("Supernode list read failed"));
}) }).catch(() => initFromCache("Supernode network fetch failed"));
}).catch(error => reject(error)) }).catch(() => initFromCache("Supernode lastTx read failed"));
}) });
}); });
startUpFunctions.push(function readAppConfigFromAPI() { startUpFunctions.push(function readAppConfigFromAPI() {
return new Promise((resolve, reject) => { return new Promise((resolve) => {
if (!startUpOptions.app_config) if (!startUpOptions.app_config)
return resolve("No configs for this app"); return resolve("No configs for this app");
compactIDB.readData("lastTx", `${DEFAULT.application}|${DEFAULT.adminID}`, DEFAULT.root).then(lastTx => {
var query_options = { sentOnly: true, pattern: DEFAULT.application }; // small helper: load cached directives into memory and resolve
if (typeof lastTx == 'number') //lastTx is tx count (*backward support) const loadFromIDB = (msg) => Promise.all([
query_options.ignoreOld = lastTx; compactIDB.readAllData("subAdmins"),
else if (typeof lastTx == 'string') //lastTx is txid of last tx compactIDB.readAllData("trustedIDs"),
query_options.after = lastTx; compactIDB.readAllData("settings")
//fetch data from flosight ]).then(([sub, trust, set]) => {
subAdmins = Object.keys(sub || {}); // arrays of IDs
trustedIDs = Object.keys(trust || {});
settings = set || {};
resolve(msg);
}).catch(() => {
// safe defaults if cache missing
subAdmins = []; trustedIDs = []; settings = {};
resolve(msg + " (no local cache)");
});
// If cloud is disabled, use cached config and move on
if (!startUpOptions.cloud)
return loadFromIDB("Read app configuration from local cache (offline)");
const lastKey = `${DEFAULT.application}|${DEFAULT.adminID}`;
// Try to read lastTx; on failure, just use cache (dont block startup)
compactIDB.readData("lastTx", lastKey, DEFAULT.root).then(lastTx => {
const query_options = { sentOnly: true, pattern: DEFAULT.application };
if (typeof lastTx === 'number') query_options.ignoreOld = lastTx;
else if (typeof lastTx === 'string') query_options.after = lastTx;
// Fetch deltas from chain; on failure, fall back to cache
floBlockchainAPI.readData(DEFAULT.adminID, query_options).then(result => { floBlockchainAPI.readData(DEFAULT.adminID, query_options).then(result => {
for (var i = result.data.length - 1; i >= 0; i--) { for (let i = result.data.length - 1; i >= 0; i--) {
var content = JSON.parse(result.data[i])[DEFAULT.application]; const content = JSON.parse(result.data[i])[DEFAULT.application];
if (!content || typeof content !== "object") if (!content || typeof content !== "object") continue;
continue;
if (Array.isArray(content.removeSubAdmin)) if (Array.isArray(content.removeSubAdmin))
for (var j = 0; j < content.removeSubAdmin.length; j++) for (let j = 0; j < content.removeSubAdmin.length; j++)
compactIDB.removeData("subAdmins", content.removeSubAdmin[j]); compactIDB.removeData("subAdmins", content.removeSubAdmin[j]);
if (Array.isArray(content.addSubAdmin)) if (Array.isArray(content.addSubAdmin))
for (var k = 0; k < content.addSubAdmin.length; k++) for (let k = 0; k < content.addSubAdmin.length; k++)
compactIDB.writeData("subAdmins", true, content.addSubAdmin[k]); compactIDB.writeData("subAdmins", true, content.addSubAdmin[k]);
if (Array.isArray(content.removeTrustedID)) if (Array.isArray(content.removeTrustedID))
for (var j = 0; j < content.removeTrustedID.length; j++) for (let j = 0; j < content.removeTrustedID.length; j++)
compactIDB.removeData("trustedIDs", content.removeTrustedID[j]); compactIDB.removeData("trustedIDs", content.removeTrustedID[j]);
if (Array.isArray(content.addTrustedID)) if (Array.isArray(content.addTrustedID))
for (var k = 0; k < content.addTrustedID.length; k++) for (let k = 0; k < content.addTrustedID.length; k++)
compactIDB.writeData("trustedIDs", true, content.addTrustedID[k]); compactIDB.writeData("trustedIDs", true, content.addTrustedID[k]);
if (content.settings) if (content.settings)
for (let l in content.settings) for (let l in content.settings)
compactIDB.writeData("settings", content.settings[l], l) compactIDB.writeData("settings", content.settings[l], l);
} }
compactIDB.writeData("lastTx", result.lastItem, `${DEFAULT.application}|${DEFAULT.adminID}`, DEFAULT.root);
compactIDB.readAllData("subAdmins").then(result => { // persist last item marker (best effort)
subAdmins = Object.keys(result); compactIDB.writeData("lastTx", result.lastItem, lastKey, DEFAULT.root).catch(() => {});
compactIDB.readAllData("trustedIDs").then(result => {
trustedIDs = Object.keys(result); // load fresh values from IDB into memory and finish
compactIDB.readAllData("settings").then(result => { loadFromIDB("Read app configuration from blockchain");
settings = result; }).catch(() => {
resolve("Read app configuration from blockchain"); // network failed → boot from cache
}) loadFromIDB("Read app configuration from local cache (network fail)");
}) });
}) }).catch(() => {
}) // couldn't read lastTx → still boot from cache
}).catch(error => reject(error)) loadFromIDB("Read app configuration from local cache (no lastTx)");
}) });
});
}); });
startUpFunctions.push(function loadDataFromAppIDB() { startUpFunctions.push(function loadDataFromAppIDB() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!startUpOptions.cloud) const loadData = ["appObjects", "generalData", "lastVC"];
return resolve("No cloud for this app");
var loadData = ["appObjects", "generalData", "lastVC"] // If cloud is disabled AND no IDB stores are expected, skip early
var promises = [] if (!startUpOptions.cloud && (!initIndexedDB.appObs || Object.keys(initIndexedDB.appObs).length === 0))
for (var i = 0; i < loadData.length; i++) return resolve("No cloud and no local data to load");
promises[i] = compactIDB.readAllData(loadData[i])
Promise.all(promises).then(results => { // Otherwise, read from IDB
for (var i = 0; i < loadData.length; i++) Promise.all(loadData.map(item => compactIDB.readAllData(item)))
floGlobals[loadData[i]] = results[i] .then(results => {
resolve("Loaded Data from app IDB") for (let i = 0; i < loadData.length; i++)
}).catch(error => reject(error)) floGlobals[loadData[i]] = results[i];
}) resolve("Loaded Data from app IDB");
})
.catch(error => reject(error));
});
}); });
var keyInput = type => new Promise((resolve, reject) => { var keyInput = type => new Promise((resolve, reject) => {
@ -840,4 +888,4 @@
.catch(error => reject(error)) .catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))
}); });
})('object' === typeof module ? module.exports : window.floDapps = {}); })('object' === typeof module ? module.exports : window.floDapps = {});