Skip to content

Commit

Permalink
Release v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nmasnadithya committed Jan 29, 2022
1 parent 198522d commit 5b7a454
Show file tree
Hide file tree
Showing 20 changed files with 896 additions and 533 deletions.
342 changes: 159 additions & 183 deletions background/background.ts

Large diffs are not rendered by default.

34 changes: 14 additions & 20 deletions background/db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { submitError } from '../common/error'
import { LOGGER } from '../common/logger'
import {submitError} from '../common/error'
import {LOGGER} from '../common/logger'

class Db {
private database: IDBDatabase
Expand All @@ -11,13 +11,10 @@ class Db {
let tabsRequest = indexedDB.open('Data', 2)
tabsRequest.onerror = (ev) => {
LOGGER.log('Problem opening DB')
submitError(
{
type: 'Problem opening DB',
name: ev.type,
},
ev
)
submitError({
type: 'Problem opening DB',
name: ev.type,
}, ev)
reject(ev)
}

Expand Down Expand Up @@ -54,13 +51,10 @@ class Db {
LOGGER.log('Opened DB connection')
this.database.onerror = (ev) => {
LOGGER.log('Error on DB', ev)
submitError(
{
type: 'Error on DB',
name: ev.type,
},
ev
)
submitError({
type: 'Error on DB',
name: ev.type,
}, ev)
}
resolve()
}
Expand Down Expand Up @@ -139,7 +133,7 @@ class Db {

export let DB = new Db()
DB.initDatabases()
.then((value) => {
LOGGER.log('DB initializing succeeded')
})
.catch((reason) => LOGGER.error('DB initializing failed', reason))
.then((value) => {
LOGGER.log('DB initializing succeeded')
})
.catch((reason) => LOGGER.error('DB initializing failed', reason))
55 changes: 35 additions & 20 deletions background/papers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Paper, PaperModel } from '../common/models'
import { DB } from './db'
import { loadPaper } from '../common/utils'
import {Paper, PaperModel} from '../common/models'
import {DB} from './db'
import {loadPaper} from '../common/utils'

let paperDetails: { [paperId: string]: Paper } = {}
let paperLinks: { [link: string]: string } = {}
Expand All @@ -9,8 +9,8 @@ export async function getPaperDetails(links: { [link: string]: string }) {
let details = {}
for (let link in links) {
if (
paperLinks[link] == null ||
paperDetails[paperLinks[link]] == null
paperLinks[link] == null ||
paperDetails[paperLinks[link]] == null
) {
let paper = new Paper()
let data
Expand All @@ -31,22 +31,37 @@ export async function getPaperDetails(links: { [link: string]: string }) {
return details
}

export async function loadPaperDetails(link: string, referer: string) {
if (
paperLinks[link] == null ||
paperDetails[paperLinks[link]] == null ||
paperDetails[paperLinks[link]].data == null
) {
let data = await loadPaper(link, referer)
let paper = new Paper()
if (data != null) {
paper.loadFrom(data)
function shouldProcessLink(link: string) {
if (paperLinks[link] == null) {
return true
}
if (paperDetails[paperLinks[link]] == null) {
return true
}
return paperDetails[paperLinks[link]].data == null

}

export async function loadPaperDetails(links: string[], referer: string) {
let linksToProcess = []
links.forEach(link => {
if (shouldProcessLink(link)) {
linksToProcess.push(link)
}
let id = paper.paperId == null ? link : paper.paperId
paperDetails[id] = paper
paperLinks[link] = id
if (paper.paperId != null) {
await DB.upsertRecord('papers', paper.toJSON())
})
if (linksToProcess.length > 0) {
let data = await loadPaper(links, referer)
for (let link of linksToProcess) {
let paper = new Paper()
if (data[link] != null) {
paper.loadFrom(data[link])
}
let id = paper.paperId == null ? link : paper.paperId
paperDetails[id] = paper
paperLinks[link] = id
if (paper.paperId != null) {
await DB.upsertRecord('papers', paper.toJSON())
}
}
}
}
51 changes: 20 additions & 31 deletions background/popup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Paper } from '../common/models'
import { submitError } from '../common/error'
import { LOGGER } from '../common/logger'
import {Paper} from '../common/models'
import {submitError} from '../common/error'
import {LOGGER} from '../common/logger'

let popupTabs: { [tabId: number]: BgPopup } = {}

Expand All @@ -11,10 +11,7 @@ export async function createPopup(port: chrome.runtime.Port): Promise<number> {
return p.tabId
}

export function updatePopupPapers(
tabId: number,
papers: { [link: string]: Paper }
) {
export function updatePopupPapers(tabId: number, papers?: { [link: string]: Paper }) {
if (popupTabs[tabId] == null) {
return
}
Expand All @@ -39,50 +36,42 @@ class BgPopup {
this.tabId = (await this.getCurrentTab()).id
}
} catch (e) {
submitError(
{
error: e.message,
tabId: this.tabId,
},
e,
e.stack
).then()
submitError({
error: e.message,
tabId: this.tabId,
}, e, e.stack).then()
}
popupTabs[this.tabId] = this
}

updatePapers(papers: { [link: string]: Paper }) {
updatePapers(papers?: { [link: string]: Paper }) {
if (papers == null) {
this.port.postMessage({})
return
}
let res = {}
for (let link in papers) {
if (
papers[link] != null &&
papers[link].paperId != null &&
res[papers[link].paperId] == null
) {
if (papers[link] != null && papers[link].paperId != null && res[papers[link].paperId] == null) {
res[papers[link].paperId] = papers[link].toJSON()
}
}
this.port.postMessage(res)
this.port.postMessage({papers: res})
}

private async getCurrentTab(): Promise<chrome.tabs.Tab> {
return new Promise((resolve, reject) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
try {
if (tabs.length !== 1) {
reject(tabs)
} else {
resolve(tabs[0])
}
} catch (err) {
submitError(
{
data: err.message,
tabs: tabs,
},
err,
err.stack
).then()
submitError({
data: err.message,
tabs: tabs,
}, err, err.stack).then()
}
})
})
Expand Down
Loading

0 comments on commit 5b7a454

Please sign in to comment.