This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pend and confirm sync records to allow pause/resume
Depends on brave/sync#122 Fix brave/sync#112
- Loading branch information
Showing
12 changed files
with
226 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict' | ||
|
||
const appConstants = require('../../../js/constants/appConstants') | ||
const syncPendState = require('../../common/state/syncPendState') | ||
|
||
const syncReducer = (state, action) => { | ||
switch (action.actionType) { | ||
case appConstants.APP_SET_STATE: | ||
state = syncPendState.initPendingRecords(state) | ||
break | ||
case appConstants.APP_PEND_SYNC_RECORDS: | ||
state = syncPendState.pendRecords(state, action.records) | ||
break | ||
case appConstants.APP_CONFIRM_SYNC_RECORDS: | ||
state = syncPendState.confirmRecords(state, action.records) | ||
break | ||
} | ||
return state | ||
} | ||
|
||
module.exports = syncReducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict' | ||
|
||
const Immutable = require('immutable') | ||
const syncUtil = require('../../../js/state/syncUtil') | ||
|
||
/** | ||
* Turn app state pendingRecords into an OrderedMap. | ||
* @param {Immutable.Map} state app state | ||
* @returns {Immutable.OrderedMap} new app state | ||
*/ | ||
module.exports.initPendingRecords = (state) => { | ||
const pendingRecords = state.getIn(['sync', 'pendingRecords']) | ||
const orderedPendingRecords = new Immutable.OrderedMap(pendingRecords) | ||
return state.setIn(['sync', 'pendingRecords'], orderedPendingRecords) | ||
} | ||
|
||
/** | ||
* Given records sent via SEND_SYNC_RECORDS, insert them in the app state. | ||
* @param {Immutable.Map} state app state | ||
* @param {Array.<Object>} records | ||
* @returns {Immutable.Map} new app state | ||
*/ | ||
module.exports.pendRecords = (state, records) => { | ||
const enqueueTimestamp = new Date().getTime() | ||
records.forEach(record => { | ||
const pendingRecord = Immutable.fromJS({enqueueTimestamp, record}) | ||
const key = record.objectId.toString() | ||
console.log(`APP_PEND_SYNC_RECORDS: ${key} => ${JSON.stringify(pendingRecord.toJS())}`) | ||
state = state.setIn(['sync', 'pendingRecords', key], pendingRecord) | ||
}) | ||
return state | ||
} | ||
|
||
/** | ||
* Given app state, extract records to be sent via SEND_SYNC_RECORDS. | ||
* @param {Immutable.Map} state app state | ||
* @returns {Array.<Object>} records | ||
*/ | ||
module.exports.getPendingRecords = (state) => { | ||
const pendingRecords = state.getIn(['sync', 'pendingRecords']) | ||
return pendingRecords.map(pendingRecord => pendingRecord.get('record').toJS()).toArray() | ||
} | ||
|
||
/** | ||
* Confirm downloaded records and remove them from the app state sync | ||
* pending records. | ||
* In case of multiple updates to the same object, the newest update takes | ||
* precedence. Thus, if a downloaded record has a newer timestamp it | ||
* dequeues any pending record. | ||
* @param {Immutable.Map} state app state | ||
* @param {Array.<Object>} downloadedRecord | ||
* @returns {Immutable.Map} new app state | ||
*/ | ||
module.exports.confirmRecords = (state, downloadedRecords) => { | ||
downloadedRecords.forEach(record => { | ||
// browser-laptop stores byte arrays like objectId as Arrays. | ||
// downloaded records use Uint8Arrays which we should convert back. | ||
const fixedRecord = syncUtil.deepArrayify(record) | ||
const key = fixedRecord.objectId.toString() | ||
const enqueueTimestamp = state.getIn(['sync', 'pendingRecords', key, 'enqueueTimestamp']) | ||
if (!enqueueTimestamp || (enqueueTimestamp > fixedRecord.syncTimestamp)) { | ||
return | ||
} | ||
state = state.deleteIn(['sync', 'pendingRecords', key]) | ||
}) | ||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.