From 3fad3fecef6e45a28c2e2342dfc4d09475898cdf Mon Sep 17 00:00:00 2001 From: ryanml Date: Sat, 14 Jul 2018 16:04:28 -0700 Subject: [PATCH] Ensuring publishers file is read from as little possible --- app/browser/api/ledger.js | 12 ++++++++---- test/unit/app/browser/api/ledgerTest.js | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index 7e1eca3379f..afd2fed00d6 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -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) => { @@ -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`) } @@ -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 diff --git a/test/unit/app/browser/api/ledgerTest.js b/test/unit/app/browser/api/ledgerTest.js index a3b6316fe62..0d6b6bb36e0 100644 --- a/test/unit/app/browser/api/ledgerTest.js +++ b/test/unit/app/browser/api/ledgerTest.js @@ -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 () {