Skip to content

Commit

Permalink
Reuse Account objects and debounce sync()
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelklehr committed May 21, 2018
1 parent 204a829 commit b67ae13
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/lib/Account.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ const BATCH_SIZE = 10

export default class Account {
static async get (id) {
if (!this.cache) {
this.cache = {}
}
if (this.cache[id]) {
this.cache[id].updateFromStorage()
return this.cache[id]
}
let storage = new AccountStorage(id)
let background = await browser.runtime.getBackgroundPage()
let data = await storage.getAccountData(background.controller.key)
if (typeof data.serverRoot !== 'string') {
data.serverRoot = ''
}
let tree = new Tree(storage, data.localRoot, data.serverRoot)
return new Account(id, storage, Adapter.factory(data), tree)
let account = new Account(id, storage, Adapter.factory(data), tree)
this.cache[id] = account
return account
}

static async create (data) {
Expand Down Expand Up @@ -52,6 +61,12 @@ export default class Account {
await this.storage.setAccountData(data, background.controller.key)
}

async updateFromStorage () {
let background = await browser.runtime.getBackgroundPage()
let data = await this.storage.getAccountData(background.controller.key)
this.server.setData(data)
}

async tracksBookmark (localId) {
if (!(await this.isInitialized())) return false
let mappings = await this.storage.getMappings()
Expand Down Expand Up @@ -110,6 +125,7 @@ export default class Account {

async sync () {
try {
if (this.getData().syncing) return
console.log('Starting sync process for account ' + this.getLabel())
await this.setData({...this.getData(), syncing: true})
if (!(await this.isInitialized())) {
Expand Down

0 comments on commit b67ae13

Please sign in to comment.