diff options
author | Dan Finlay <dan@danfinlay.com> | 2017-11-29 07:16:04 +0800 |
---|---|---|
committer | Dan Finlay <dan@danfinlay.com> | 2017-11-29 07:16:04 +0800 |
commit | f0f6bb28e0e721c0f802354b72f02ed83afbb3b5 (patch) | |
tree | 29c264fc9f74389a6e9f902cae626d718cca53ba /app | |
parent | ec4b7de962d0c4913f8f65a21a6cbef9f2ebc261 (diff) | |
download | tangerine-wallet-browser-f0f6bb28e0e721c0f802354b72f02ed83afbb3b5.tar tangerine-wallet-browser-f0f6bb28e0e721c0f802354b72f02ed83afbb3b5.tar.gz tangerine-wallet-browser-f0f6bb28e0e721c0f802354b72f02ed83afbb3b5.tar.bz2 tangerine-wallet-browser-f0f6bb28e0e721c0f802354b72f02ed83afbb3b5.tar.lz tangerine-wallet-browser-f0f6bb28e0e721c0f802354b72f02ed83afbb3b5.tar.xz tangerine-wallet-browser-f0f6bb28e0e721c0f802354b72f02ed83afbb3b5.tar.zst tangerine-wallet-browser-f0f6bb28e0e721c0f802354b72f02ed83afbb3b5.zip |
Get notice version filtering working nicely
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/metamask-controller.js | 1 | ||||
-rw-r--r-- | app/scripts/notice-controller.js | 51 |
2 files changed, 28 insertions, 24 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 0c759010b..c9eb27fbf 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -45,6 +45,7 @@ module.exports = class MetamaskController extends EventEmitter { this.opts = opts const initState = opts.initState || {} this.recordFirstTimeInfo(initState) + opts.initState.firstTimeInfo.version = '4.5.0' // platform-specific api this.platform = opts.platform diff --git a/app/scripts/notice-controller.js b/app/scripts/notice-controller.js index 457161ccb..efad3e516 100644 --- a/app/scripts/notice-controller.js +++ b/app/scripts/notice-controller.js @@ -3,6 +3,7 @@ const semver = require('semver') const extend = require('xtend') const ObservableStore = require('obs-store') const hardCodedNotices = require('../../notices/notices.json') +const uniqBy = require('lodash.uniqby') module.exports = class NoticeController extends EventEmitter { @@ -10,6 +11,7 @@ module.exports = class NoticeController extends EventEmitter { super() this.noticePoller = null this.firstVersion = opts.firstVersion + this.version = opts.version const initState = extend({ noticesList: [], }, opts.initState) @@ -32,9 +34,9 @@ module.exports = class NoticeController extends EventEmitter { return unreadNotices[unreadNotices.length - 1] } - setNoticesList (noticesList) { + async setNoticesList (noticesList) { this.store.updateState({ noticesList }) - return Promise.resolve(true) + return true } markNoticeRead (noticeToMark, cb) { @@ -52,21 +54,14 @@ module.exports = class NoticeController extends EventEmitter { } } - updateNoticesList () { - return this._retrieveNoticeData().then((hardNotices) => { - const newNotices = hardNotices.filter((newNotice) => { - if ('version' in newNotice) { - return semver.satisfies(this.version, newNotice.version) - } - if ('firstVersion' in newNotice) { - return semver.satisfies(this.firstVersion, newNotice.firstVersion) - } - return true - }) - var oldNotices = this.getNoticesList() - var combinedNotices = this._mergeNotices(oldNotices, newNotices) - return Promise.resolve(this.setNoticesList(combinedNotices)) - }) + async updateNoticesList () { + const newNotices = await this._retrieveNoticeData() + const oldNotices = this.getNoticesList() + const combinedNotices = this._mergeNotices(oldNotices, newNotices) + const filteredNotices = this._filterNotices(combinedNotices) + const result = this.setNoticesList(filteredNotices) + this._updateMemstore() + return result } startPolling () { @@ -79,22 +74,30 @@ module.exports = class NoticeController extends EventEmitter { } _mergeNotices (oldNotices, newNotices) { - var noticeMap = this._mapNoticeIds(oldNotices) - newNotices.forEach((notice) => { - if (noticeMap.indexOf(notice.id) === -1) { - oldNotices.push(notice) + return uniqBy(oldNotices.concat(newNotices), 'id') + } + + _filterNotices( notices ) { + return notices.filter((newNotice) => { + if ('version' in newNotice) { + const satisfied = semver.satisfies(this.version, newNotice.version) + return satisfied + } + if ('firstVersion' in newNotice) { + const satisfied = semver.satisfies(this.firstVersion, newNotice.firstVersion) + return satisfied } + return true }) - return oldNotices } _mapNoticeIds (notices) { return notices.map((notice) => notice.id) } - _retrieveNoticeData () { + async _retrieveNoticeData () { // Placeholder for the API. - return Promise.resolve(hardCodedNotices) + return hardCodedNotices } _updateMemstore () { |