Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterating on project by fixing minor bugs #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ObsidianWrapper/ObsidianWrapper.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "https://dev.jspm.io/react";
import React from "https://jspm.dev/react";
import BrowserCache from "../src/Browser/CacheClassBrowser.js";
import { insertTypenames } from "../src/Browser/insertTypenames.js";

Expand All @@ -8,7 +8,7 @@ function ObsidianWrapper(props) {
const [cache, setCache] = React.useState(new BrowserCache());

// You have to put your Google Chrome Obsidian developer tool extension id to connect Obsidian Wrapper with dev tool
const chromeExtensionId = "dkbfipkapkljpdbhdihnlnbieffhjdmh";
const chromeExtensionId = "dllaimdpnljhoipmgmjdnhnofgfhgajd";
window.localStorage.setItem("cache", JSON.stringify(cache));

async function query(query, options = {}) {
Expand All @@ -20,7 +20,7 @@ function ObsidianWrapper(props) {
});
console.log(
"Here's the message content: ",
window.localStorage.getItem("cache")
[window.localStorage.getItem("cache")]
);
// set the options object default properties if not provided
const {
Expand Down Expand Up @@ -55,6 +55,7 @@ function ObsidianWrapper(props) {
if (resObj) {
// returning cached response as a promise
const cacheHitResponseTime = Date.now() - startTime;
console.log('cacheHitResponseTime',cacheHitResponseTime)
chrome.runtime.sendMessage(chromeExtensionId, {
cacheHitResponseTime: cacheHitResponseTime,
});
Expand All @@ -72,6 +73,7 @@ function ObsidianWrapper(props) {
async function hunt(query) {
if (!wholeQuery) query = insertTypenames(query);
try {
console.log('ObsidianWrapper hunt 76 query:', query)
// send fetch request with query
const resJSON = await fetch(endpoint, {
method: "POST",
Expand Down
19 changes: 19 additions & 0 deletions src/Browser/CacheClassBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class BrowserCache {

// Main functionality methods
async read(queryStr) {
console.log('CacheClassBrowser.js read 21: ',queryStr)
if (typeof queryStr !== "string") {
throw TypeError("input should be a string");
}
Expand Down Expand Up @@ -54,6 +55,7 @@ export default class BrowserCache {

async writeThrough(queryStr, respObj, deleteFlag, endpoint) {
try {
console.log('CacheClassBrowser.js writeThrough 58: ',queryStr)
const queryObj = destructureQueries(queryStr);
const mutationName = queryObj.mutations[0].name;
// check if it's a mutation
Expand Down Expand Up @@ -99,6 +101,7 @@ export default class BrowserCache {
}

async write(queryStr, respObj, deleteFlag) {
console.log('CacheClassBrowser.js write 104: ',queryStr)
const queryObj = destructureQueries(queryStr);
const resFromNormalize = normalizeResult(queryObj, respObj, deleteFlag);
// update the original cache with same reference
Expand All @@ -116,6 +119,7 @@ export default class BrowserCache {
}

constructResponseObject(queryObj, respObj, deleteFlag) {
console.log('CacheClassBrowser.js constructResponseObject 122: ',queryStr)
const mutationData = queryObj.mutations[0];
const mutationName = mutationData.name;
const __typename = this.storage.writeThroughInfo[mutationName].type;
Expand Down Expand Up @@ -147,6 +151,7 @@ export default class BrowserCache {
}

separateArguments(str, respObj, mutationName) {
console.log('CacheClassBrowser.js separateArguments 154: ',str)
const startIndex = str.indexOf("{");
const slicedStr = str.slice(startIndex + 1, str.length - 2);
const argumentPairs = slicedStr.split(",");
Expand All @@ -164,6 +169,7 @@ export default class BrowserCache {
}

addNonScalarFields(respObj, mutationData) {
console.log('CacheClassBrowser.js addNonScalarFields 172: ',respObj)
for (const field in mutationData.fields) {
if (
mutationData.fields[field] !== "scalar" &&
Expand All @@ -177,13 +183,15 @@ export default class BrowserCache {
gc() {
// garbageCollection; garbage collection: removes any inaccessible hashes from the cache
const badHashes = getBadHashes();
console.log('CacheClassBrowser.js gc 186: ',badHashes)
const goodHashes = rootQueryCleaner(badHashes);
const goodHashes2 = getGoodHashes(badHashes, goodHashes);
removeInaccessibleHashes(badHashes, goodHashes2);
}

// remove hashes that are flagged for deletion and store records of them in a set badHashes for removal inside root queries
getBadHashes() {
console.log('CacheClassBrowser.js getBadHashes 194: ',queryStr)
const badHashes = new Set();
for (let key in this.storage) {
if (key === "ROOT_QUERY" || key === "ROOT_MUTATION") continue;
Expand All @@ -197,6 +205,7 @@ export default class BrowserCache {

// go through root queries, remove all instances of bad hashes, add remaining hashes into goodHashes set
rootQueryCleaner(badHashes) {
console.log('CacheClassBrowser.js rootQueryCleaner 208: ',badHashes)
const goodHashes = new Set();
const rootQuery = this.storage["ROOT_QUERY"];
for (let key in rootQuery) {
Expand All @@ -215,6 +224,7 @@ export default class BrowserCache {

// Go through the cache, check good hashes for any nested hashes and add them to goodHashes set
getGoodHashes(badHashes, goodHashes) {
console.log('CacheClassBrowser.js getGoodHashes 227: ',goodHashes)
for (let key in this.storage) {
if (key === "ROOT_QUERY" || key === "ROOT_MUTATION") continue;
for (let i in this.storage[key]) {
Expand All @@ -239,6 +249,7 @@ export default class BrowserCache {

// Remove inaccessible hashes by checking if they are in goodhashes set or not
removeInaccessibleHashes(badHashes, goodHashes) {
console.log('CacheClassBrowser.js removeInaccessibleHashes 252: ',this.storage)
for (let key in this.storage) {
if (key === "ROOT_QUERY" || key === "ROOT_MUTATION") continue;
if (!goodHashes.has(key)) delete this.storage[key];
Expand All @@ -261,18 +272,22 @@ export default class BrowserCache {

// cache read/write helper methods
async cacheRead(hash) {
console.log('CacheClassBrowser.js cacheRead 275: ',hash)
return this.storage[hash];
}

async cacheWrite(hash, value) {
console.log('CacheClassBrowser.js cacheWrite 280: ',hash)
this.storage[hash] = value;
}

async cacheDelete(hash) {
console.log('CacheClassBrowser.js cacheDelete 285: ',hash)
delete this.storage[hash];
}

async cacheClear() {
console.log('CacheClassBrowser.js cacheClear 290: ',this.storage)
this.storage = {
ROOT_QUERY: {},
ROOT_MUTATION: {},
Expand All @@ -281,16 +296,19 @@ export default class BrowserCache {

// functionality to stop polling
stopPollInterval(interval) {
console.log('CacheClassBrowser.js stopPollInterval: ',interval)
clearInterval(interval);
}

writeWholeQuery(queryStr, respObj) {
console.log('CacheClassBrowser.js writeWholeQuery 304: ',queryStr)
const hash = queryStr.replace(/\s/g, "");
this.cacheWrite(ROOT_QUERY[hash], respObj);
return respObj;
}

readWholeQuery(queryStr) {
console.log('CacheClassBrowser.js readWholeQuery 311: ',queryStr)
const hash = queryStr.replace(/\s/g, "");
const root = this.cacheRead("ROOT_QUERY");
if (root[hash]) return { data: root[hash] };
Expand All @@ -299,6 +317,7 @@ export default class BrowserCache {

// specialized helper methods
async populateAllHashes(allHashesFromQuery, fields) {
console.log('CacheClassBrowser.js populateAllHashes 320: ',allHashesFromQuery)
// include the hashname for each hash
if (!allHashesFromQuery.length) return [];
const hyphenIdx = allHashesFromQuery[0].indexOf("~");
Expand Down