Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Ensuring publishers file is read from as little possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanml committed Jul 14, 2018
1 parent 7cb4060 commit 3fad3fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 8 additions & 4 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,9 @@ const runPublishersUpdate = (state) => {
const updatePublishers = (state, publisherKeys) => {
const fs = require('fs')

if (publisherInfoData && publisherInfoData.length > 0) {
if (publisherInfoData && publisherInfoData.size > 0) {
appActions.onPublishersInfoRead(publisherKeys, publisherInfoData)
return state
}

fs.readFile(pathName(publisherInfoPath), (err, data) => {
Expand All @@ -715,7 +716,8 @@ const updatePublishers = (state, publisherKeys) => {

try {
const result = JSON.parse(data)
appActions.onPublishersInfoRead(publisherKeys, result)
publisherInfoData = makeImmutable(result)
appActions.onPublishersInfoRead(publisherKeys, publisherInfoData)
} catch (err) {
console.error(`Error: Could not parse data from publishers file`)
}
Expand Down Expand Up @@ -3168,9 +3170,11 @@ const onPublishersInfo = (state, result) => {

const fs = require('fs')
const path = pathName(publisherInfoPath)
publisherInfoData = result
const writeData = JSON.stringify(result)

publisherInfoData = makeImmutable(result)

fs.writeFile(path, JSON.stringify(result), (err) => {
fs.writeFile(path, writeData, (err) => {
if (err) {
console.error(`Error: Could not write file at ${path}`)
return
Expand Down
16 changes: 15 additions & 1 deletion test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4138,7 +4138,21 @@ describe('ledger api unit tests', function () {

ledgerApi.updatePublishers(defaultAppState, ['brave.com'])
assert.deepEqual(['brave.com'], onPublishersInfoReadSpy.getCall(0).args[0])
assert.deepEqual(JSON.parse(readData), onPublishersInfoReadSpy.getCall(0).args[1])
assert.deepEqual(Immutable.fromJS(JSON.parse(readData)), onPublishersInfoReadSpy.getCall(0).args[1])
})

it('does not dispatch file read if publisherInfoData has value', function () {
const infoData = Immutable.fromJS([
['brave.com', true, false]
])
readFileStub = sinon.stub(fs, 'readFile', (path, callback) => {
callback(null, readData)
})

ledgerApi.setPublisherInfoData(infoData)
ledgerApi.updatePublishers(defaultAppState, ['brave.com'])
assert(onPublishersInfoReadSpy.withArgs(['brave.com'], infoData).calledOnce)
assert(readFileStub.notCalled)
})

it('does not dispatch onPublishersInfoRead if data from file cannot be parsed as JSON', function () {
Expand Down

0 comments on commit 3fad3fe

Please sign in to comment.