Skip to content

Commit

Permalink
refactored configManager, added tests, moved metaProps out of parser …
Browse files Browse the repository at this point in the history
…and off AllNodes, simplified some stuff
  • Loading branch information
tconfrey committed Feb 21, 2024
1 parent e209e9a commit 13e2f12
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 148 deletions.
2 changes: 1 addition & 1 deletion app/BTAppNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ class BTAppNode extends BTNode {

static generateOrgFile() {
// iterate thru nodes to do the work
let orgText = metaPropertiesToString(AllNodes.metaProperties);
let orgText = configManager.metaPropertiesToString();

// find and order the top level nodes according to table position
const topNodes = AllNodes.filter(node => node && !node.parentId);
Expand Down
14 changes: 6 additions & 8 deletions app/bt.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ async function launchApp(msg) {
// BTId in local store and from org data should be the same. local store is primary
if (msg?.Config?.BTId) {
BTId = msg.Config.BTId;
if (getMetaProp('BTId') && (BTId != getMetaProp('BTId')))
alert(`Conflicting subscription id's found! This should not happen. I'm using the local value, if there are issues contact BrainTool support.\nLocal value:${BTId}\nOrg file value:${getMetaProp('BTId')}`);
setMetaProp('BTId', BTId);
if (configManager.getProp('BTId') && (BTId != configManager.getProp('BTId')))
alert(`Conflicting subscription id's found! This should not happen. I'm using the local value, if there are issues contact BrainTool support.\nLocal value:${BTId}\nOrg file value:${configManager.getProp('BTId')}`);
configManager.setProp('BTId', BTId);
} else {
// get from file if not in local storage and save locally (will allow for recovery if lost)
if (getMetaProp('BTId')) {
BTId = getMetaProp('BTId');
configManager.setProp('BTId', BTId);
if (configManager.getProp('BTId')) {
BTId = configManager.getProp('BTId');
//configManager.setProp('BTId', BTId);
}
}

Expand Down Expand Up @@ -1774,8 +1774,6 @@ function loadBookmarks(msg) {
});
gtag('event', 'BookmarkImport', {'event_category': 'Import'});

// remmember this import
configManager.setProp('BTLastBookmarkImport', dateString);
processImport(importNode.id); // see above
}

Expand Down
78 changes: 53 additions & 25 deletions app/configManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const configManager = (() => {
const Properties = {
'keys': ['CLIENT_ID', 'API_KEY', 'FB_KEY', 'STRIPE_KEY'],
'localStorageProps': ['BTId', 'BTTimestamp', 'BTFileID', 'BTGDriveConnected', 'BTStats', 'BTLastShownMessageIndex'],
'orgProps': ['BTCohort', 'BTVersion', 'BTGroupingMode', 'BTLastBookmarkImport', 'BTId', 'BTManagerHome', 'BTTheme', 'BTFavicons', 'BTNotes', 'BTDense', 'BTSize'],
'orgProps': ['BTCohort', 'BTVersion', 'BTGroupingMode', 'BTId', 'BTManagerHome', 'BTTheme', 'BTFavicons', 'BTNotes', 'BTDense', 'BTSize'],
'stats': ['BTNumTabOperations', 'BTNumSaves', 'BTNumLaunches', 'BTInstallDate', 'BTSessionStartTime', 'BTLastActivityTime', 'BTSessionStartSaves', 'BTSessionStartOps', 'BTDaysOfUse'],
};
let Config, Keys = {CLIENT_ID: '', API_KEY: '', FB_KEY: '', STRIPE_KEY: ''};
Expand All @@ -39,26 +39,69 @@ const configManager = (() => {
window.postMessage({'function': 'localStore', 'data': {'Config': Config}});
}
if (Properties.orgProps.includes(prop)) {
setMetaProp(prop, value); // see parser.js
saveBT();
Config[prop] = value;
//setMetaProp(prop, value); // see parser.js
//saveBT();
}
if (Properties.stats.includes(prop)) {
Config['BTStats'][prop] = value;
window.postMessage({'function': 'localStore', 'data': {'Config': Config}});
}
};

function getProp(prop) {
// getter for sync props
// getter for sync props, fetch from appropriate place based on Config array above

if (Properties.localStorageProps.includes(prop)) {
return Config[prop];
}
if (Properties.orgProps.includes(prop)) {
return getMetaProp(prop);
return Config[prop];
}
if (Properties.keys.includes(prop)) {
return Keys[prop];
}
if (Properties.stats.includes(prop)) {
return Config['BTStats'][prop];
}
return null;
};

function metaPropertiesToString(ary) {
// return the string to be used to output meta properties to .org file
let str = "";
if (!configManager.getProp('BTVersion'))
configManager.setProp('BTVersion', 1);

Properties['orgProps'].forEach(function(prop) {
if (getProp(prop))
str += `#+PROPERTY: ${prop} ${getProp(prop)}\n`;
});
return str;
}

/*
function getMetaProp(propName) {
// return the value of the meta property if it exists
let val = '';
if (!AllNodes.metaProperties || !AllNodes.metaProperties.length) return val;
AllNodes.metaProperties.forEach(prop => {
if (prop.name == propName)
val = prop.value;
});
return val;
}
function setMetaProp(propName, val) {
// set or change the value of the meta property
if (!AllNodes.metaProperties) AllNodes.metaProperties = [];
const index = AllNodes.metaProperties.findIndex(prop => prop.name == propName);
if (index > -1)
AllNodes.metaProperties[index] = {'name': propName, 'value': val};
else
AllNodes.metaProperties.push({'name': propName, 'value': val});
}
*/

function checkNewDayOfUse(prev, current) {
// last active timestamp same day as this timestamp?
const prevDate = new Date(prev).toLocaleDateString(); // eg 2/8/1966
Expand All @@ -84,24 +127,16 @@ const configManager = (() => {
};

function setStat(statName, statValue) {
// eg sessionStartTime
Config['BTStats'][statName] = statValue;
window.postMessage({'function': 'localStore', 'data': {'Config': Config}});
// just another prop eg sessionStartTime
setProp(statName, statValue);
};

function updatePrefs() {
// update preferences based on data read into AllNodes.metaProperties
// update preferences based on orgProperties read in from file

let groupMode = configManager.getProp('BTGroupingMode');
if (groupMode) {
const $radio = $('#tabGroupToggle :radio[name=grouping]');

// v099 move away from have a WINDOW default, new window now a choice on opening
if (groupMode == 'WINDOW') {
groupMode = 'TABGROUP';
configManager.setProp('BTGroupingMode', groupMode);
}

$radio.filter(`[value=${groupMode}]`).prop('checked', true);
GroupingMode = groupMode;
}
Expand Down Expand Up @@ -309,23 +344,16 @@ const configManager = (() => {
// best guess at install date cos wasn't previously set
// Use bookmark import data if set. Otherwise assume 2x saves/day up to 1 year
if (Config['BTStats']['BTInstallDate']) return;
if (getProp('BTLastBookmarkImport')) {
const datestr = getProp('BTLastBookmarkImport').replace(';', ':');
const date = Date.parse(datestr);
if (date) {
setStat('BTInstallDate', date);
return;
}
}
const saveDays = Math.min(Config['BTStats']['BTNumSaves'] / 2, 365);
const guessedInstallDate = Date.now() - (saveDays * 24 * 60 * 60000);
setStat('BTInstallDate', guessedInstallDate);
setProp('BTInstallDate', guessedInstallDate);
}

return {
setConfigAndKeys: setConfigAndKeys,
setProp: setProp,
getProp: getProp,
metaPropertiesToString: metaPropertiesToString,
setStat: setStat,
incrementStat: incrementStat,
updatePrefs: updatePrefs,
Expand Down
1 change: 1 addition & 0 deletions app/fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async function saveBT(localOnly = false) {
messageManager.removeWarning(); // remove stale warning if any
gtag('event', event, {'event_category': 'Save', 'event_label': 'NumNodes', 'value': AllNodes.length});
configManager.incrementStat('BTNumSaves');
configManager.setProp('BTVersion', parseInt(configManager.getProp('BTVersion')) + 1);
}

async function authorizeLocalFile() {
Expand Down
40 changes: 0 additions & 40 deletions app/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,46 +172,6 @@ function orgaNodeRawText(organode) {
return string;
}

function metaPropertiesToString(ary) {
// return the string to be used to output meta properties to .org file
// ary is as captured in original parse, array of {name: value:}
if (!ary || !ary.length) return "";
let str = "";
let metaprops = [];

if (!getMetaProp('BTVersion'))
ary.push({'name' : 'BTVersion', 'value' : 0});
ary.forEach(function(prop) {
if (prop.name == 'BTVersion') // increment version
prop.value++;
str += `#+PROPERTY: ${prop.name} ${prop.value}\n`;
metaprops.push(prop);
});
AllNodes.metaProperties = metaprops; // update AllNodes for next time around
return str;
}

function getMetaProp(propName) {
// return the value of the meta property if it exists
let val = '';
if (!AllNodes.metaProperties || !AllNodes.metaProperties.length) return val;
AllNodes.metaProperties.forEach(prop => {
if (prop.name == propName)
val = prop.value;
});
return val;
}

function setMetaProp(propName, val) {
// set or change the value of the meta property
if (!AllNodes.metaProperties) AllNodes.metaProperties = [];
const index = AllNodes.metaProperties.findIndex(prop => prop.name == propName);
if (index > -1)
AllNodes.metaProperties[index] = {'name': propName, 'value': val};
else
AllNodes.metaProperties.push({'name': propName, 'value': val});
}

function generateLinesAndColumns(filetext) {
// return an array of the original lines and columns for use in regnerating orga

Expand Down
2 changes: 1 addition & 1 deletion versions/Release-Candidate/app/BTAppNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ class BTAppNode extends BTNode {

static generateOrgFile() {
// iterate thru nodes to do the work
let orgText = metaPropertiesToString(AllNodes.metaProperties);
let orgText = configManager.metaPropertiesToString();

// find and order the top level nodes according to table position
const topNodes = AllNodes.filter(node => node && !node.parentId);
Expand Down
14 changes: 6 additions & 8 deletions versions/Release-Candidate/app/bt.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ async function launchApp(msg) {
// BTId in local store and from org data should be the same. local store is primary
if (msg?.Config?.BTId) {
BTId = msg.Config.BTId;
if (getMetaProp('BTId') && (BTId != getMetaProp('BTId')))
alert(`Conflicting subscription id's found! This should not happen. I'm using the local value, if there are issues contact BrainTool support.\nLocal value:${BTId}\nOrg file value:${getMetaProp('BTId')}`);
setMetaProp('BTId', BTId);
if (configManager.getProp('BTId') && (BTId != configManager.getProp('BTId')))
alert(`Conflicting subscription id's found! This should not happen. I'm using the local value, if there are issues contact BrainTool support.\nLocal value:${BTId}\nOrg file value:${configManager.getProp('BTId')}`);
configManager.setProp('BTId', BTId);
} else {
// get from file if not in local storage and save locally (will allow for recovery if lost)
if (getMetaProp('BTId')) {
BTId = getMetaProp('BTId');
configManager.setProp('BTId', BTId);
if (configManager.getProp('BTId')) {
BTId = configManager.getProp('BTId');
//configManager.setProp('BTId', BTId);
}
}

Expand Down Expand Up @@ -1774,8 +1774,6 @@ function loadBookmarks(msg) {
});
gtag('event', 'BookmarkImport', {'event_category': 'Import'});

// remmember this import
configManager.setProp('BTLastBookmarkImport', dateString);
processImport(importNode.id); // see above
}

Expand Down
Loading

0 comments on commit 13e2f12

Please sign in to comment.