simpler retry logic

This commit is contained in:
Daniel Lugo 2020-07-30 10:16:49 -04:00
parent 7df73ca49e
commit a48362c45b

View file

@ -2,6 +2,9 @@
* @format * @format
*/ */
const Common = require('shock-common') const Common = require('shock-common')
const pickBy = require('lodash/pickBy')
const size = require('lodash/size')
const mapValues = require('lodash/mapValues')
const Utils = require('../utils') const Utils = require('../utils')
const Key = require('../key') const Key = require('../key')
@ -95,6 +98,11 @@ const getWallPage = async (page, publicKey) => {
return empty return empty
} }
/**
* We just use it so Common.Schema.isWallPage passes.
*/
const mockUser = await User.getMyUser()
/** /**
* @type {Common.SchemaTypes.WallPage} * @type {Common.SchemaTypes.WallPage}
*/ */
@ -129,32 +137,38 @@ const getWallPage = async (page, publicKey) => {
}) })
}, },
maybePage => { maybePage => {
if (typeof maybePage !== 'object' || maybePage === null) { // sometimes load() returns an empty object on the first call
if (size(/** @type {any} */ (maybePage)) === 0) {
return true return true
} }
const clean = { const page = /** @type {Common.Schema.WallPage} */ (maybePage)
...maybePage
if (typeof page.count !== 'number') {
return true
} }
// @ts-ignore // removes 'unused' initializer and aborted writes
for (const [key, post] of Object.entries(clean.posts)) { page.posts = pickBy(page.posts, v => v !== null)
// delete unsuccessful writes
if (post === null) {
// @ts-ignore
delete clean.posts[key]
} else {
post.id = key
}
}
// .load() sometimes doesn't load all data on first call // .load() sometimes doesn't load all data on first call
// @ts-ignore if (size(page.posts) === 0) {
if (Object.keys(clean.posts).length === 0) {
return true return true
} }
return !Common.Schema.isWallPage(clean) // Give ids based on keys
page.posts = mapValues(page.posts, (v, k) => ({
...v,
id: k
}))
page.posts = mapValues(page.posts, v => ({
...v,
// isWallPage() would otherwise not pass
author: mockUser
}))
return !Common.Schema.isWallPage(page)
} }
) )