Files
js/
factories/
object-data-factory.js
servers.js
index.js
Source Code
js/servers.js
// # servers
let servUserData = ObjectDataFactory();
index.js
{
urls: [
"js/factories/object-data-factory.js",
"js/libs/data-server.js",
"js/utils/storage-util.js",
],
},
{
urls: [
"js/servers.js",
"js/components/app-data-component.js",
],
callback: async function() {
// setup db
{
let dbName = "app-db1";
let dbVersion = 1;
await storageUtil.Init(dbName, dbVersion);
}
// init app data profile
{
compoAppData.Configure({
'userData': servUserData,
});
await compoAppData.Init_();
}
},
},
js/factories/object-data-factory.js
function ObjectDataFactory(opt) {
let SELF = {
SetItem: (key, value) => data[key] = value,
GetAll: () => data,
GetItem: (key) => data[key],
ClearItem: (key) => delete data[key],
Reset: () => data = JSON.parse(defaultDataJSON),
Clear: () => storageUtil.clearStore_(),
ChangeWorkspace_,
Save_,
};
// # data
let data = opt?.defaultData ?? {};
let defaultDataJSON = JSON.stringify(data);
let local = {
storageName: '', // assigned on workspace change
};
// # functions
function setStorageKey(storeKey) {
if (!storeKey || storeKey?.trim().length == 0) return false;
local.storageName = storeKey;
return true;
}
async function ChangeWorkspace_(storeKey) {
let isChanged = setStorageKey(storeKey);
if (isChanged) {
await restoreData_();
}
}
// # save
async function Save_() {
await storageUtil.setItem_(local.storageName, data);
}
async function restoreData_() {
let storeData = await storageUtil.getItem_(local.storageName);
data = storeData ?? JSON.parse(defaultDataJSON);
}
return SELF;
}
https://vanillawebdev.blogspot.com/2024/12/object-data-factory.html
https://www.blogger.com/blog/post/edit/8166404610182826392/551981161806800437
https://www.blogger.com/blog/page/edit/8166404610182826392/551981161806800437
Object Data Factory
December 11, 2024
Files
js/
factories/
object-data-factory.js
servers.js
index.js
Source Code
js/servers.js
// # servers
let servUserData = ObjectDataFactory();
index.js
{
urls: [
"js/factories/object-data-factory.js",
"js/libs/data-server.js",
"js/utils/storage-util.js",
],
},
{
urls: [
"js/servers.js",
"js/components/app-data-component.js",
],
callback: async function() {
// setup db
{
let dbName = "app-db1";
let dbVersion = 1;
await storageUtil.Init(dbName, dbVersion);
}
// init app data profile
{
compoAppData.Configure({
'userData': servUserData,
});
await compoAppData.Init_();
}
},
},
js/factories/object-data-factory.js
function ObjectDataFactory(opt) {
let SELF = {
SetItem: (key, value) => data[key] = value,
GetAll: () => data,
GetItem: (key) => data[key],
ClearItem: (key) => delete data[key],
Reset: () => data = JSON.parse(defaultDataJSON),
Clear: () => storageUtil.clearStore_(),
ChangeWorkspace_,
Save_,
};
// # data
let data = opt?.defaultData ?? {};
let defaultDataJSON = JSON.stringify(data);
let local = {
storageName: '', // assigned on workspace change
};
// # functions
function setStorageKey(storeKey) {
if (!storeKey || storeKey?.trim().length == 0) return false;
local.storageName = storeKey;
return true;
}
async function ChangeWorkspace_(storeKey) {
let isChanged = setStorageKey(storeKey);
if (isChanged) {
await restoreData_();
}
}
// # save
async function Save_() {
await storageUtil.setItem_(local.storageName, data);
}
async function restoreData_() {
let storeData = await storageUtil.getItem_(local.storageName);
data = storeData ?? JSON.parse(defaultDataJSON);
}
return SELF;
}
Comments
Post a Comment