From 7974a89bd3203ff77fb427196087bd6298121a3f Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 29 Jul 2020 12:21:46 -0400 Subject: [PATCH 1/4] init writes in parallel in case one of them fails --- services/gunDB/contact-api/actions.js | 90 +++++++++++++++------------ 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 8bd27958..2208b86f 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1543,53 +1543,63 @@ const unfollow = publicKey => const initWall = async () => { const user = require('../Mediator').getUser() - await new Promise((res, rej) => { - user - .get(Key.WALL) - .get(Key.NUM_OF_PAGES) - .put(0, ack => { - if (ack.err) { - rej(new Error(ack.err)) - } else { - res() - } - }) - }) + const promises = [] - await new Promise((res, rej) => { - user - .get(Key.WALL) - .get(Key.PAGES) - .get('0') - .get(Key.POSTS) - .put( - { - unused: null - }, - ack => { + promises.push( + new Promise((res, rej) => { + user + .get(Key.WALL) + .get(Key.NUM_OF_PAGES) + .put(0, ack => { if (ack.err) { rej(new Error(ack.err)) } else { res() } - } - ) - }) + }) + }) + ) - await new Promise((res, rej) => { - user - .get(Key.WALL) - .get(Key.PAGES) - .get('0') - .get(Key.COUNT) - .put(0, ack => { - if (ack.err) { - rej(new Error(ack.err)) - } else { - res() - } - }) - }) + promises.push( + new Promise((res, rej) => { + user + .get(Key.WALL) + .get(Key.PAGES) + .get('0') + .get(Key.POSTS) + .put( + { + unused: null + }, + ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + } + ) + }) + ) + + promises.push( + new Promise((res, rej) => { + user + .get(Key.WALL) + .get(Key.PAGES) + .get('0') + .get(Key.COUNT) + .put(0, ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + }) + }) + ) + + await Promise.all(promises) } module.exports = { From 2573a2aaf935300a4717d1efdaad481e3821a934 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 29 Jul 2020 13:20:03 -0400 Subject: [PATCH 2/4] correct import name --- services/gunDB/contact-api/getters/wall.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/gunDB/contact-api/getters/wall.js b/services/gunDB/contact-api/getters/wall.js index 9ac3c6fb..5e8a66d9 100644 --- a/services/gunDB/contact-api/getters/wall.js +++ b/services/gunDB/contact-api/getters/wall.js @@ -6,7 +6,7 @@ const Common = require('shock-common') const Utils = require('../utils') const Key = require('../key') -const Wall = require('./user') +const User = require('./user') /** * @param {string=} publicKey @@ -160,9 +160,9 @@ const getWallPage = async (page, publicKey) => { } else { post.author = publicKey ? // eslint-disable-next-line no-await-in-loop - await Wall.getAnUser(publicKey) + await User.getAnUser(publicKey) : // eslint-disable-next-line no-await-in-loop - await Wall.getMyUser() + await User.getMyUser() post.id = key } } From 7df73ca49ef59cfec80cddbe52e66601a18febf9 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 30 Jul 2020 10:11:38 -0400 Subject: [PATCH 3/4] force a prefecth --- services/gunDB/contact-api/getters/wall.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/getters/wall.js b/services/gunDB/contact-api/getters/wall.js index 5e8a66d9..515c6306 100644 --- a/services/gunDB/contact-api/getters/wall.js +++ b/services/gunDB/contact-api/getters/wall.js @@ -110,12 +110,22 @@ const getWallPage = async (page, publicKey) => { } return new Promise(res => { + // forces data fetch user .get(Key.WALL) .get(Key.PAGES) .get(actualPageIdx.toString()) // @ts-ignore - .load(res) + .load(() => {}) + + process.nextTick(() => { + user + .get(Key.WALL) + .get(Key.PAGES) + .get(actualPageIdx.toString()) + // @ts-ignore + .load(res) + }) }) }, maybePage => { From a48362c45b475e6bf86e37e7f136af2e12b91f4b Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 30 Jul 2020 10:16:49 -0400 Subject: [PATCH 4/4] simpler retry logic --- services/gunDB/contact-api/getters/wall.js | 46 ++++++++++++++-------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/services/gunDB/contact-api/getters/wall.js b/services/gunDB/contact-api/getters/wall.js index 515c6306..879ec97b 100644 --- a/services/gunDB/contact-api/getters/wall.js +++ b/services/gunDB/contact-api/getters/wall.js @@ -2,6 +2,9 @@ * @format */ 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 Key = require('../key') @@ -95,6 +98,11 @@ const getWallPage = async (page, publicKey) => { return empty } + /** + * We just use it so Common.Schema.isWallPage passes. + */ + const mockUser = await User.getMyUser() + /** * @type {Common.SchemaTypes.WallPage} */ @@ -129,32 +137,38 @@ const getWallPage = async (page, publicKey) => { }) }, 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 } - const clean = { - ...maybePage + const page = /** @type {Common.Schema.WallPage} */ (maybePage) + + if (typeof page.count !== 'number') { + return true } - // @ts-ignore - for (const [key, post] of Object.entries(clean.posts)) { - // delete unsuccessful writes - if (post === null) { - // @ts-ignore - delete clean.posts[key] - } else { - post.id = key - } - } + // removes 'unused' initializer and aborted writes + page.posts = pickBy(page.posts, v => v !== null) // .load() sometimes doesn't load all data on first call - // @ts-ignore - if (Object.keys(clean.posts).length === 0) { + if (size(page.posts) === 0) { 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) } )