-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more refactoring, unlimited storage, fetching of all facts for the cache
- Loading branch information
Alexandru Badiu
committed
Sep 28, 2016
1 parent
cc4457e
commit 3499582
Showing
5 changed files
with
128 additions
and
74 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import Rx from 'rx'; | ||
import config from './config'; | ||
import { encodeParams, getUrlCode, getShortUrl } from './util'; | ||
import { convertFact, convertFacts } from './fact'; | ||
|
||
let factsCache = []; | ||
|
||
export const setFactsCache = (facts) => { | ||
factsCache = facts; | ||
}; | ||
|
||
export const getFactsFromCache = (url) => { | ||
return _.filter(factsCache, { source: getShortUrl(url) }); | ||
}; | ||
|
||
export const getFacts = (url, uid, client, origin) => { | ||
return new Promise((resolve) => { | ||
const urlCode = getUrlCode(url); | ||
const params = { | ||
q: urlCode, | ||
u: uid, | ||
client, | ||
origin, | ||
}; | ||
|
||
$.ajax({ | ||
dataType: 'json', | ||
url: `http:\/\/${config.api}?${encodeParams(params)}`, | ||
}).then((response) => { | ||
const facts = []; | ||
if (response.error) { | ||
return resolve(facts); | ||
} | ||
|
||
if (response.data) { | ||
Object.keys(response.data).forEach(id => facts.push(convertFact(response.data[id]))); | ||
} | ||
|
||
resolve(facts); | ||
}); | ||
}); | ||
}; | ||
|
||
const getAllPage = (page, uid, client, origin) => { | ||
const params = { | ||
page, | ||
q: 'all', | ||
u: uid, | ||
client, | ||
origin, | ||
}; | ||
|
||
return Rx.Observable.fromPromise(new Promise((resolve) => { | ||
$.ajax({ | ||
dataType: 'json', | ||
url: `http:\/\/${config.api}?${encodeParams(params)}`, | ||
}).then((response) => { | ||
const result = { | ||
total_pages: 0, | ||
current_page: 0, | ||
data: [], | ||
}; | ||
|
||
if (response.error) { | ||
return resolve(result); | ||
} | ||
|
||
result.total_pages = response.total_pages; | ||
result.current_page = response.current_page; | ||
|
||
if (response.data) { | ||
result.data = convertFacts(response.data); | ||
} | ||
|
||
return resolve(result); | ||
}); | ||
})); | ||
}; | ||
|
||
const getPagedItems = (index, uid, client, origin) => { | ||
return getAllPage(index, uid, client, origin) | ||
.flatMap((response) => { | ||
const result = Rx.Observable.return(response.data); | ||
|
||
if (response.total_pages > response.current_page) { | ||
const nextPage = response.current_page + 1; | ||
return result.concat(getPagedItems(nextPage, uid, client, origin)); | ||
} | ||
|
||
return result; | ||
}); | ||
}; | ||
|
||
export const getAllFacts = (uid, client, origin) => { | ||
return new Promise((resolve) => { | ||
const facts$ = getPagedItems(1, uid, client, origin); | ||
let facts = []; | ||
|
||
facts$ | ||
.subscribe( | ||
(result) => { | ||
facts = facts.concat(result); | ||
}, | ||
(error) => { | ||
console.log('error'); | ||
console.log(error); | ||
}, | ||
() => { | ||
resolve(facts); | ||
} | ||
); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,9 @@ | |
* | ||
* @author Alexandru Badiu <[email protected]> | ||
*/ | ||
import config from './config'; | ||
import FactualBase from './factual_base'; | ||
import { getUserToken, encodeParams, getUrlCode, getShortUrl } from './util'; | ||
import { convertFact, convertFacts } from './fact'; | ||
import { getFacts, getAllFacts, getFactsFromCache, setFactsCache } from './api'; | ||
import { getUserToken } from './util'; | ||
|
||
require('../css/factual.scss'); | ||
|
||
|
@@ -25,7 +24,7 @@ class FactualBackground extends FactualBase { | |
uid: '', | ||
}; | ||
|
||
chrome.alarms.clear(this.alarmName); | ||
// chrome.alarms.clear(this.alarmName); | ||
|
||
chrome.storage.sync.get('settings', (result) => { | ||
if (result) { | ||
|
@@ -38,19 +37,17 @@ class FactualBackground extends FactualBase { | |
} | ||
|
||
this.setupEvents(); | ||
// this.setupAlarms(); | ||
this.setupAlarms(); | ||
}); | ||
|
||
chrome.storage.local.get('facts', (data) => { | ||
this.cachedFacts = data.facts; | ||
setFactsCache(data.facts); | ||
}); | ||
} | ||
|
||
updateCachedFacts(facts) { | ||
this.cachedFacts = facts; | ||
setFactsCache(facts); | ||
chrome.storage.local.set({ facts: this.cachedFacts }); | ||
console.log('updated cached facts'); | ||
console.log(this.cachedFacts); | ||
} | ||
|
||
toolbarClicked() { | ||
|
@@ -106,13 +103,13 @@ class FactualBackground extends FactualBase { | |
} | ||
|
||
if (request.action === 'facts-get') { | ||
const cfacts = this.getFactsFromCache(request.url); | ||
const cfacts = getFactsFromCache(request.url); | ||
if (cfacts.length) { | ||
sendResponse(cfacts); | ||
return false; | ||
} | ||
|
||
this.getFacts(request.url) | ||
getFacts(request.url, this.settings.uid, 'chrome_extension', 'site') | ||
.then((facts) => { | ||
sendResponse(facts); | ||
}); | ||
|
@@ -133,7 +130,7 @@ class FactualBackground extends FactualBase { | |
|
||
onAlarm(alarm) { | ||
if (alarm.name === this.alarmName) { | ||
this.getAllFacts() | ||
getAllFacts(this.settings.uid, 'chrome_extension', 'site') | ||
.then((facts) => { | ||
this.updateCachedFacts(facts); | ||
}); | ||
|
@@ -153,69 +150,12 @@ class FactualBackground extends FactualBase { | |
const hasAlarm = alarms.some(a => a.name === this.alarmName); | ||
if (!hasAlarm) { | ||
chrome.alarms.create(this.alarmName, { | ||
delayInMinutes: 0.1, | ||
periodInMinutes: 0.1, | ||
delayInMinutes: 1, | ||
periodInMinutes: 60 * 24, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
getFactsFromCache(url) { | ||
return _.filter(this.cachedFacts, { source: getShortUrl(url) }); | ||
} | ||
|
||
getFacts(url) { | ||
return new Promise((resolve) => { | ||
const urlCode = getUrlCode(url); | ||
const params = { | ||
q: urlCode, | ||
u: this.settings.uid, | ||
client: 'chrome_extension', | ||
origin: 'site', | ||
}; | ||
|
||
$.ajax({ | ||
dataType: 'json', | ||
url: `http:\/\/${config.api}?${encodeParams(params)}`, | ||
}).then((response) => { | ||
const facts = []; | ||
if (response.error) { | ||
return resolve(facts); | ||
} | ||
|
||
if (response.data) { | ||
Object.keys(response.data).forEach(id => facts.push(convertFact(response.data[id]))); | ||
} | ||
|
||
resolve(facts); | ||
}); | ||
}); | ||
} | ||
|
||
getAllFacts() { | ||
return new Promise((resolve) => { | ||
const params = { | ||
q: 'all', | ||
u: this.settings.uid, | ||
client: 'chrome_extension', | ||
origin: 'site', | ||
}; | ||
$.ajax({ | ||
dataType: 'json', | ||
url: `http:\/\/${config.api}?${encodeParams(params)}`, | ||
}).then((response) => { | ||
if (response.error) { | ||
return resolve([]); | ||
} | ||
|
||
if (response.data) { | ||
return resolve(convertFacts(response.data)); | ||
} | ||
|
||
return resolve([]); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default new FactualBackground(); |