@bitsocial/bitsocial-react-hooks 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/README.md +2 -0
  3. package/dist/hooks/accounts/accounts.d.ts.map +1 -1
  4. package/dist/hooks/accounts/accounts.js +35 -10
  5. package/dist/hooks/accounts/accounts.js.map +1 -1
  6. package/dist/hooks/actions/actions.d.ts.map +1 -1
  7. package/dist/hooks/actions/actions.js +166 -93
  8. package/dist/hooks/actions/actions.js.map +1 -1
  9. package/dist/hooks/authors/author-avatars.d.ts +2 -2
  10. package/dist/hooks/authors/author-avatars.d.ts.map +1 -1
  11. package/dist/hooks/authors/author-avatars.js +48 -20
  12. package/dist/hooks/authors/author-avatars.js.map +1 -1
  13. package/dist/hooks/authors/authors.d.ts.map +1 -1
  14. package/dist/hooks/authors/authors.js +68 -22
  15. package/dist/hooks/authors/authors.js.map +1 -1
  16. package/dist/hooks/comments.d.ts.map +1 -1
  17. package/dist/hooks/comments.js +54 -3
  18. package/dist/hooks/comments.js.map +1 -1
  19. package/dist/hooks/communities.d.ts.map +1 -1
  20. package/dist/hooks/communities.js +41 -12
  21. package/dist/hooks/communities.js.map +1 -1
  22. package/dist/hooks/feeds/feeds.d.ts.map +1 -1
  23. package/dist/hooks/feeds/feeds.js +47 -11
  24. package/dist/hooks/feeds/feeds.js.map +1 -1
  25. package/dist/hooks/pkc-rpc.d.ts.map +1 -1
  26. package/dist/hooks/pkc-rpc.js +33 -20
  27. package/dist/hooks/pkc-rpc.js.map +1 -1
  28. package/dist/hooks/replies.d.ts.map +1 -1
  29. package/dist/hooks/replies.js +30 -4
  30. package/dist/hooks/replies.js.map +1 -1
  31. package/dist/index.d.ts +6 -4
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +4 -2
  34. package/dist/index.js.map +1 -1
  35. package/dist/lib/debug-utils.d.ts +4 -4
  36. package/dist/lib/debug-utils.d.ts.map +1 -1
  37. package/dist/lib/debug-utils.js +56 -11
  38. package/dist/lib/debug-utils.js.map +1 -1
  39. package/dist/lib/localforage-lru/localforage-lru.d.ts +1 -0
  40. package/dist/lib/localforage-lru/localforage-lru.d.ts.map +1 -1
  41. package/dist/lib/localforage-lru/localforage-lru.js +48 -10
  42. package/dist/lib/localforage-lru/localforage-lru.js.map +1 -1
  43. package/dist/lib/page-sorts.d.ts +6 -0
  44. package/dist/lib/page-sorts.d.ts.map +1 -1
  45. package/dist/lib/page-sorts.js +45 -9
  46. package/dist/lib/page-sorts.js.map +1 -1
  47. package/dist/lib/pkc-js/pkc-js-mock-content.d.ts.map +1 -1
  48. package/dist/lib/pkc-js/pkc-js-mock-content.js +5 -1
  49. package/dist/lib/pkc-js/pkc-js-mock-content.js.map +1 -1
  50. package/dist/lib/utils/utils.d.ts.map +1 -1
  51. package/dist/lib/utils/utils.js +24 -16
  52. package/dist/lib/utils/utils.js.map +1 -1
  53. package/dist/lib/validator.d.ts +1 -1
  54. package/dist/lib/validator.d.ts.map +1 -1
  55. package/dist/lib/validator.js +7 -1
  56. package/dist/lib/validator.js.map +1 -1
  57. package/dist/stores/accounts/account-generator.d.ts +1 -0
  58. package/dist/stores/accounts/account-generator.d.ts.map +1 -1
  59. package/dist/stores/accounts/account-generator.js +12 -4
  60. package/dist/stores/accounts/account-generator.js.map +1 -1
  61. package/dist/stores/accounts/accounts-actions-internal.d.ts.map +1 -1
  62. package/dist/stores/accounts/accounts-actions-internal.js +24 -6
  63. package/dist/stores/accounts/accounts-actions-internal.js.map +1 -1
  64. package/dist/stores/accounts/accounts-actions.d.ts.map +1 -1
  65. package/dist/stores/accounts/accounts-actions.js +339 -98
  66. package/dist/stores/accounts/accounts-actions.js.map +1 -1
  67. package/dist/stores/accounts/accounts-database.d.ts +1 -0
  68. package/dist/stores/accounts/accounts-database.d.ts.map +1 -1
  69. package/dist/stores/accounts/accounts-database.js +82 -15
  70. package/dist/stores/accounts/accounts-database.js.map +1 -1
  71. package/dist/stores/accounts/accounts-store.d.ts +1 -1
  72. package/dist/stores/accounts/accounts-store.d.ts.map +1 -1
  73. package/dist/stores/accounts/accounts-store.js +1 -1
  74. package/dist/stores/accounts/accounts-store.js.map +1 -1
  75. package/dist/stores/accounts/utils.d.ts +5 -1
  76. package/dist/stores/accounts/utils.d.ts.map +1 -1
  77. package/dist/stores/accounts/utils.js +58 -0
  78. package/dist/stores/accounts/utils.js.map +1 -1
  79. package/dist/stores/authors-comments/authors-comments-store.d.ts.map +1 -1
  80. package/dist/stores/authors-comments/authors-comments-store.js +39 -12
  81. package/dist/stores/authors-comments/authors-comments-store.js.map +1 -1
  82. package/dist/stores/comments/comments-store.d.ts +2 -0
  83. package/dist/stores/comments/comments-store.d.ts.map +1 -1
  84. package/dist/stores/comments/comments-store.js +170 -21
  85. package/dist/stores/comments/comments-store.js.map +1 -1
  86. package/dist/stores/communities/communities-store.d.ts.map +1 -1
  87. package/dist/stores/communities/communities-store.js +27 -7
  88. package/dist/stores/communities/communities-store.js.map +1 -1
  89. package/dist/stores/communities-pages/communities-pages-store.d.ts.map +1 -1
  90. package/dist/stores/communities-pages/communities-pages-store.js +60 -8
  91. package/dist/stores/communities-pages/communities-pages-store.js.map +1 -1
  92. package/dist/stores/feeds/feed-sorter.d.ts +5 -2
  93. package/dist/stores/feeds/feed-sorter.d.ts.map +1 -1
  94. package/dist/stores/feeds/feed-sorter.js +15 -4
  95. package/dist/stores/feeds/feed-sorter.js.map +1 -1
  96. package/dist/stores/feeds/feeds-store.d.ts +3 -0
  97. package/dist/stores/feeds/feeds-store.d.ts.map +1 -1
  98. package/dist/stores/feeds/feeds-store.js +36 -26
  99. package/dist/stores/feeds/feeds-store.js.map +1 -1
  100. package/dist/stores/feeds/utils.d.ts +16 -0
  101. package/dist/stores/feeds/utils.d.ts.map +1 -1
  102. package/dist/stores/feeds/utils.js +82 -26
  103. package/dist/stores/feeds/utils.js.map +1 -1
  104. package/dist/stores/replies/replies-store.d.ts.map +1 -1
  105. package/dist/stores/replies/replies-store.js +32 -6
  106. package/dist/stores/replies/replies-store.js.map +1 -1
  107. package/dist/stores/replies/utils.d.ts +3 -2
  108. package/dist/stores/replies/utils.d.ts.map +1 -1
  109. package/dist/stores/replies/utils.js +38 -5
  110. package/dist/stores/replies/utils.js.map +1 -1
  111. package/dist/stores/replies-pages/replies-pages-store.d.ts.map +1 -1
  112. package/dist/stores/replies-pages/replies-pages-store.js +68 -13
  113. package/dist/stores/replies-pages/replies-pages-store.js.map +1 -1
  114. package/dist/types.d.ts +11 -2
  115. package/dist/types.d.ts.map +1 -1
  116. package/dist/types.js.map +1 -1
  117. package/package.json +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"authors-comments-store.js","sourceRoot":"","sources":["../../../src/stores/authors-comments/authors-comments-store.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,MAAM,GAAG,GAAG,MAAM,CAAC,sCAAsC,CAAC,CAAC;AAC3D,OAAO,WAAW,MAAM,SAAS,CAAC;AAClC,OAAO,MAAM,MAAM,kBAAkB,CAAC;AAQtC,OAAO,aAAgC,MAAM,aAAa,CAAC;AAC3D,OAAO,QAAQ,MAAM,WAAW,CAAC;AACjC,OAAO,EAEL,mCAAmC,EACnC,kCAAkC,GACnC,MAAM,SAAS,CAAC;AACjB,OAAO,aAAa,MAAM,aAAa,CAAC;AAExC,wEAAwE;AACxE,MAAM,CAAC,MAAM,eAAe,GAAG,EAAE,CAAC;AAClC,kDAAkD;AAClD,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAmBpC,MAAM,oBAAoB,GAAG,WAAW,CACtC,CAAC,QAAkB,EAAE,QAAkB,EAAE,EAAE,CAAC,CAAC;IAC3C,OAAO,EAAE,EAAE;IACX,cAAc,EAAE,EAAE;IAClB,uBAAuB,EAAE,EAAE;IAC3B,mBAAmB,EAAE,EAAE;IACvB,eAAe,EAAE,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+F;IAC/F,IAAI,MAAA,OAAO,CAAC,MAAM,0CAAE,kBAAkB,EAAE,CAAC;QACvC,yBAAyB,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;IAC5D,CAAC;AACH,CAAC,CAAC;AAEJ,+BAA+B;AAC/B,MAAM,aAAa,GAAG,oBAAoB,CAAC,QAAQ,EAAE,CAAC;AACtD,qDAAqD;AACrD,MAAM,CAAC,MAAM,yBAAyB,GAAG,GAAS,EAAE;IAClD,gCAAgC,GAAG,EAAE,CAAC;IACtC,gBAAgB,GAAG,IAAI,QAAQ,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;IACpD,yBAAyB,GAAG,EAAE,CAAC;IAC/B,oBAAoB,GAAG,IAAI,QAAQ,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;IAExD,mDAAmD;IACnD,oBAAoB,CAAC,OAAO,EAAE,CAAC;IAC/B,yBAAyB;IACzB,oBAAoB,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;AAC/C,CAAC,CAAA,CAAC;AAEF,4CAA4C;AAC5C,MAAM,CAAC,MAAM,oCAAoC,GAAG,GAAS,EAAE;IAC7D,MAAM,yBAAyB,EAAE,CAAC;AACpC,CAAC,CAAA,CAAC;AAEF,eAAe,oBAAoB,CAAC","sourcesContent":["import Logger from \"@pkcprotocol/pkc-logger\";\nconst log = Logger(\"bitsocial-react-hooks:authors:stores\");\nimport createStore from \"zustand\";\nimport assert from \"../../lib/assert\";\nimport {\n CommentsFilter,\n AuthorCommentsOptions,\n AuthorsComments,\n Account,\n Comment,\n} from \"../../types\";\nimport commentsStore, { CommentsState } from \"../comments\";\nimport QuickLru from \"quick-lru\";\nimport {\n toSizes,\n getUpdatedLoadedAndBufferedComments,\n getNextCommentCidToFetchNotFetched,\n} from \"./utils\";\nimport accountsStore from \"../accounts\";\n\n// reddit loads approximately 25 posts per page while infinite scrolling\nexport const commentsPerPage = 25;\n// keep large buffer because fetching cids is slow\nexport const commentBufferSize = 50;\n\ntype AuthorsCommentsState = {\n // authorCommentsName is a string used a key to represent authorAddress + filter + accountId\n options: { [authorCommentsName: string]: AuthorCommentsOptions };\n loadedComments: { [authorCommentsName: string]: Comment[] };\n hasMoreBufferedComments: { [authorCommentsName: string]: boolean };\n bufferedCommentCids: { [authorAddress: string]: Set<string> };\n nextCommentCidsToFetch: { [authorAddress: string]: string | undefined };\n shouldFetchNextComment: { [authorAddress: string]: boolean };\n lastCommentCids: { [authorAddress: string]: string | undefined };\n addAuthorCommentsToStore: Function;\n setNextCommentCidsToFetch: Function;\n incrementPageNumber: Function;\n addBufferedCommentCid: Function;\n updateLoadedComments: Function;\n setLastCommentCid: Function;\n};\n\nconst authorsCommentsStore = createStore<AuthorsCommentsState>(\n (setState: Function, getState: Function) => ({\n options: {},\n loadedComments: {},\n hasMoreBufferedComments: {},\n bufferedCommentCids: {},\n lastCommentCids: {},\n nextCommentCidsToFetch: {},\n shouldFetchNextComment: {},\n\n addAuthorCommentsToStore: (\n authorCommentsName: string,\n authorAddress: string,\n commentCid: string,\n filter: CommentsFilter | undefined,\n account: Account,\n ) => {\n assert(\n authorCommentsName && typeof authorCommentsName === \"string\",\n `addAuthorCommentsToStore.incrementPageNumber invalid argument authorCommentsName '${authorCommentsName}'`,\n );\n assert(\n authorAddress && typeof authorAddress === \"string\",\n `authorsCommentsStore.addAuthorCommentsToStore invalid argument authorAddress '${authorAddress}'`,\n );\n assert(\n commentCid && typeof commentCid === \"string\",\n `authorsCommentsStore.addAuthorCommentsToStore invalid argument commentCid '${commentCid}'`,\n );\n assert(\n !filter || typeof filter?.filter === \"function\",\n `authorsCommentsStore.addAuthorCommentsToStore invalid argument filter.filter '${filter?.filter}'`,\n );\n assert(\n !filter || typeof filter?.key === \"string\",\n `authorsCommentsStore.addAuthorCommentsToStore invalid argument filter.key '${filter?.key}'`,\n );\n assert(\n typeof account?.pkc?.getComment === \"function\",\n `authorsCommentsStore.addAuthorCommentsToStore account '${account}' invalid`,\n );\n\n const { options, updateLoadedComments } = getState();\n // in store already, do nothing\n if (options[authorCommentsName]) {\n return;\n }\n const authorCommentsOptions = { authorAddress, pageNumber: 1, filter, accountId: account.id };\n\n // subscribe to nextCommentCidsToFetch and shouldFetchNextComment to fetch the comments\n authorsCommentsStore.subscribe(\n fetchCommentOnShouldFetchOrNextCidChange(authorCommentsOptions),\n );\n\n log(\"authorsCommentsActions.addAuthorCommentsToStore\", {\n authorCommentsName,\n authorCommentsOptions,\n commentCid,\n previousAuthorsCommentsOptions: options,\n });\n setState((state: AuthorsCommentsState) => ({\n options: { ...state.options, [authorCommentsName]: authorCommentsOptions },\n loadedComments: { ...state.loadedComments, [authorCommentsName]: [] },\n hasMoreBufferedComments: { ...state.hasMoreBufferedComments, [authorCommentsName]: true },\n bufferedCommentCids: {\n ...state.bufferedCommentCids,\n [authorAddress]: state.bufferedCommentCids[authorAddress] || new Set(),\n },\n lastCommentCids: {\n ...state.lastCommentCids,\n [authorAddress]: state.lastCommentCids[authorAddress] || undefined,\n },\n nextCommentCidsToFetch: {\n ...state.nextCommentCidsToFetch,\n [authorAddress]: state.nextCommentCidsToFetch[authorAddress] || commentCid,\n },\n shouldFetchNextComment: {\n ...state.shouldFetchNextComment,\n [authorAddress]: state.shouldFetchNextComment[authorAddress] || true,\n },\n }));\n\n // update loadedComments in case the author already has bufferedCommentCids\n updateLoadedComments();\n },\n\n setNextCommentCidsToFetch: (authorAddress: string, authorComment: Comment) => {\n assert(\n authorAddress && typeof authorAddress === \"string\",\n `authorsCommentsActions.setNextCommentCidsToFetch invalid argument authorAddress '${authorAddress}'`,\n );\n assert(\n typeof authorComment?.timestamp === \"number\",\n `authorsCommentsActions.setNextCommentCidsToFetch invalid argument authorComment '${authorComment}'`,\n );\n const { nextCommentCidsToFetch, shouldFetchNextComment, lastCommentCids } = getState();\n if (typeof shouldFetchNextComment[authorAddress] !== \"boolean\") {\n throw Error(\n `authorsCommentsActions.setNextCommentCidsToFetch can't set nextCommentCidToFetch '${authorAddress}' not in store`,\n );\n }\n const nextCommentCidToFetch = authorComment?.author?.previousCommentCid;\n if (nextCommentCidToFetch === nextCommentCidsToFetch[authorAddress]) {\n throw Error(\n `authorsCommentsActions.setNextCommentCidsToFetch can't set nextCommentCidToFetch '${authorAddress}' to '${nextCommentCidToFetch}' same value`,\n );\n }\n const nextCommentCidToFetchNotFetched =\n getNextCommentCidToFetchNotFetched(nextCommentCidToFetch);\n\n // log.trace('authorsCommentsActions.setNextCommentCidsToFetch', {\n // authorAddress,\n // authorComment,\n // previousNextCommentCidToFetch: nextCommentCidsToFetch[authorAddress],\n // nextCommentCidToFetch,\n // nextCommentCidToFetchNotFetched,\n // lastCommentCid: lastCommentCids[authorAddress],\n // shouldFetchNextComment: shouldFetchNextComment[authorAddress],\n // })\n setState((state: AuthorsCommentsState) => ({\n nextCommentCidsToFetch: {\n ...state.nextCommentCidsToFetch,\n [authorAddress]: nextCommentCidToFetchNotFetched,\n },\n }));\n },\n\n incrementPageNumber: (authorCommentsName: string) => {\n assert(\n authorCommentsName && typeof authorCommentsName === \"string\",\n `authorsCommentsActions.incrementPageNumber invalid argument authorCommentsName '${authorCommentsName}'`,\n );\n const { options, updateLoadedComments, loadedComments, nextCommentCidsToFetch } = getState();\n if (!options[authorCommentsName]) {\n throw Error(\n `authorsCommentsActions.incrementPageNumber can't increment page number of options '${authorCommentsName}' not in store`,\n );\n }\n assert(\n options[authorCommentsName].pageNumber * commentsPerPage <=\n loadedComments[authorCommentsName].length,\n `authorsCommentsActions.incrementPageNumber cannot increment page number before current page has loaded`,\n );\n\n log(\"authorsCommentsActions.incrementPageNumber\", {\n authorCommentsName,\n pageNumber: options[authorCommentsName].pageNumber + 1,\n nextCommentCidsToFetch: nextCommentCidsToFetch[options[authorCommentsName].authorAddress],\n });\n setState(({ options }: AuthorsCommentsState) => {\n const authorCommentOptions = { ...options[authorCommentsName] };\n authorCommentOptions.pageNumber++;\n return { options: { ...options, [authorCommentsName]: authorCommentOptions } };\n });\n\n // must update loadedComments to reflect the new added page\n updateLoadedComments();\n },\n\n addBufferedCommentCid: (authorAddress: string, commentCid: string) => {\n assert(\n authorAddress && typeof authorAddress === \"string\",\n `authorsCommentsActions.addBufferedCommentCid invalid argument authorAddress '${authorAddress}'`,\n );\n assert(\n commentCid && typeof commentCid === \"string\",\n `authorsCommentsActions.addBufferedCommentCid invalid argument commentCid '${commentCid}'`,\n );\n\n const { bufferedCommentCids } = getState();\n if (!bufferedCommentCids[authorAddress]) {\n throw Error(\n `authorsCommentsActions.addBufferedCommentCid can't add commentCid '${authorAddress}' not in store`,\n );\n }\n if (bufferedCommentCids[authorAddress].has(commentCid)) {\n throw Error(\n `authorsCommentsActions.addBufferedCommentCid can't add commentCid '${authorAddress}' '${commentCid}' already added`,\n );\n }\n\n // log.trace('authorsCommentsActions.addBufferedCommentCid', {authorAddress, commentCid, previousBufferedCommentCidsSize: bufferedCommentCids[authorAddress].size})\n setState((state: AuthorsCommentsState) => ({\n bufferedCommentCids: {\n ...state.bufferedCommentCids,\n [authorAddress]: new Set([...bufferedCommentCids[authorAddress], commentCid]),\n },\n }));\n },\n\n updateLoadedComments() {\n const { comments } = commentsStore.getState();\n let {\n loadedComments: previousAuthorsLoadedComments,\n bufferedCommentCids,\n options,\n nextCommentCidsToFetch,\n lastCommentCids,\n } = getState();\n\n const newAuthorsLoadedComments: AuthorsComments = {};\n const newShouldFetchNextComment: { [authorAddress: string]: boolean } = {};\n const newHasMoreBufferedComments: { [authorCommentsName: string]: boolean } = {};\n const authorCommentsNames = Object.keys(options);\n for (const name of authorCommentsNames) {\n const { authorAddress, pageNumber, filter } = options[name];\n const previousLoadedComments = previousAuthorsLoadedComments[name];\n const unfilteredBufferedComments: Comment[] = [...bufferedCommentCids[authorAddress]].map(\n (commentCid: string) => comments[commentCid],\n );\n\n const { loadedComments, bufferedComments: filteredBufferedComments } =\n getUpdatedLoadedAndBufferedComments(\n previousLoadedComments,\n unfilteredBufferedComments,\n pageNumber,\n filter,\n comments,\n );\n newAuthorsLoadedComments[name] = loadedComments;\n newHasMoreBufferedComments[name] = filteredBufferedComments.length > loadedComments.length;\n\n // if another authorCommentOptions should fetch, don't change it\n if (newShouldFetchNextComment[authorAddress] !== true) {\n // fetch if less comments than full page + buffer size\n newShouldFetchNextComment[authorAddress] =\n filteredBufferedComments.length < pageNumber * commentsPerPage + commentBufferSize;\n }\n }\n\n // log.trace('authorsCommentsActions.updateLoadedComments', {\n // bufferedCommentCids,\n // bufferedCommentCidsSizes: toSizes(bufferedCommentCids),\n // previousAuthorsLoadedComments,\n // newAuthorsLoadedComments,\n // previousAuthorsLoadedCommentsSizes: toSizes(previousAuthorsLoadedComments),\n // newAuthorsLoadedCommentsSizes: toSizes(newAuthorsLoadedComments),\n // newShouldFetchNextComment,\n // lastCommentCids\n // })\n setState(() => ({\n loadedComments: newAuthorsLoadedComments,\n shouldFetchNextComment: newShouldFetchNextComment,\n hasMoreBufferedComments: newHasMoreBufferedComments,\n }));\n },\n\n setLastCommentCid: (authorAddress: string, lastCommentCid: string) => {\n assert(\n authorAddress && typeof authorAddress === \"string\",\n `authorsCommentsActions.setLastCommentCid invalid argument authorAddress '${authorAddress}'`,\n );\n assert(\n lastCommentCid && typeof lastCommentCid === \"string\",\n `authorsCommentsActions.setLastCommentCid invalid argument lastCommentCid '${lastCommentCid}'`,\n );\n const { lastCommentCids, shouldFetchNextComment, nextCommentCidsToFetch } = getState();\n if (typeof shouldFetchNextComment[authorAddress] !== \"boolean\") {\n throw Error(\n `authorsCommentsActions.setLastCommentCid can't set lastCommentCid '${authorAddress}' not in store`,\n );\n }\n if (lastCommentCid === lastCommentCids[authorAddress]) {\n throw Error(\n `authorsCommentsActions.setLastCommentCid can't set setLastCommentCid '${authorAddress}' to '${lastCommentCid}' same value`,\n );\n }\n\n log(\"authorsCommentsActions.setLastCommentCid\", {\n authorAddress,\n lastCommentCid,\n previousLastCommentCid: lastCommentCids[authorAddress],\n shouldFetchNextComment: shouldFetchNextComment[authorAddress],\n nextCommentCidsToFetch: nextCommentCidsToFetch[authorAddress],\n });\n setState((state: AuthorsCommentsState) => ({\n lastCommentCids: { ...state.lastCommentCids, [authorAddress]: lastCommentCid },\n }));\n },\n }),\n);\n\n// if nextCommentCidsToFetch or shouldFetchNextComment changed, fetch the next comment\nconst fetchCommentOnShouldFetchOrNextCidChange =\n (options: AuthorCommentsOptions) => (state: AuthorsCommentsState) => {\n const nextCommentCidToFetch = state.nextCommentCidsToFetch[options.authorAddress];\n if (!nextCommentCidToFetch) {\n return;\n }\n\n // the buffered comments are already full, not need to fetch next comment\n const shouldFetchNextComment = state.shouldFetchNextComment[options.authorAddress];\n if (!shouldFetchNextComment) {\n return;\n }\n\n // when comment has fetched, update loadedComments, bufferedComments and shouldFetchNextComment\n if (!authorCommentCidsFetching[nextCommentCidToFetch]) {\n authorCommentCidsFetching[nextCommentCidToFetch] = true;\n commentsStore.subscribe(updateCommentsOnCommentsChange(options, nextCommentCidToFetch));\n }\n\n // start fetching comment\n const account = accountsStore.getState().accounts[options.accountId];\n const addCommentToStore = commentsStore.getState().addCommentToStore;\n addCommentToStore(nextCommentCidToFetch, account).catch((error: unknown) =>\n log.error(\n \"authorsCommentsStore fetchCommentOnShouldFetchOrNextCidChange addCommentToStore error\",\n { error, nextCommentCidToFetch, account },\n ),\n );\n };\n\n// if commentStore changed, update loadedComments, bufferedCommentCids, shouldFetchNextComment and nextCommentCidsToFetch\nlet previousComments = new QuickLru({ maxSize: 10000 });\nlet authorCommentCidsFetching: { [commentCid: string]: boolean } = {};\nlet communityLastCommentCidsFetching: { [commentCid: string]: boolean } = {};\nconst updateCommentsOnCommentsChange =\n (options: AuthorCommentsOptions, commentCid: string) => (state: CommentsState) => {\n // not a next cid, do nothing\n if (!authorCommentCidsFetching[commentCid]) {\n return;\n }\n const comment = state.comments[commentCid];\n // comment hasn't changed, do nothing\n if (!comment?.timestamp || comment === previousComments.get(commentCid)) {\n return;\n }\n // comment author address is incorrect, do nothing\n if (comment.author?.address !== options.authorAddress) {\n return;\n }\n previousComments.set(commentCid, comment);\n\n const {\n addBufferedCommentCid,\n bufferedCommentCids,\n updateLoadedComments,\n setNextCommentCidsToFetch,\n nextCommentCidsToFetch,\n } = authorsCommentsStore.getState();\n\n // the comment is a new comment, add it to buffered comment cids\n if (!bufferedCommentCids[options.authorAddress].has(commentCid)) {\n addBufferedCommentCid(options.authorAddress, commentCid);\n }\n\n // the comment was the last cid to fetch, set the next cid to fetch as the author previous cid\n const nextCidToFetch = nextCommentCidsToFetch[options.authorAddress];\n if (commentCid === nextCidToFetch) {\n setNextCommentCidsToFetch(options.authorAddress, comment);\n }\n\n // one of the comment changed, must update loaded comments\n updateLoadedComments();\n\n // the changed comment might have a new author.community.lastCommentCid, try to fetch it\n const communityLastCommentCid = comment.author?.community?.lastCommentCid;\n if (communityLastCommentCid) {\n // when last comment has fetched, update lastCommentCid\n if (!communityLastCommentCidsFetching[communityLastCommentCid]) {\n communityLastCommentCidsFetching[communityLastCommentCid] = true;\n commentsStore.subscribe(\n setLastCommentCidOnCommentsChange(options, communityLastCommentCid),\n );\n }\n\n // start fetching lastCommentCid\n const account = accountsStore.getState().accounts[options.accountId];\n state.addCommentToStore(communityLastCommentCid, account).catch((error: unknown) =>\n log.error(\"authorsCommentsStore updateCommentsOnCommentsChange addCommentToStore error\", {\n error,\n communityLastCommentCid,\n account,\n }),\n );\n }\n };\n\nlet previousLastComments = new QuickLru({ maxSize: 10000 });\nconst setLastCommentCidOnCommentsChange =\n (options: AuthorCommentsOptions, commentCid: string) => (state: CommentsState) => {\n // not a last cid candidate, do nothing\n if (!communityLastCommentCidsFetching[commentCid]) {\n return;\n }\n const { comments } = state;\n\n const comment = comments[commentCid];\n // comment hasn't changed, do nothing\n if (!comment?.timestamp || comment === previousLastComments.get(commentCid)) {\n return;\n }\n // comment author address is incorrect, do nothing\n if (comment.author?.address !== options.authorAddress) {\n return;\n }\n previousLastComments.set(commentCid, comment);\n\n const {\n addBufferedCommentCid,\n lastCommentCids,\n bufferedCommentCids,\n setLastCommentCid,\n setNextCommentCidsToFetch,\n updateLoadedComments,\n } = authorsCommentsStore.getState();\n\n // if the comment is a new comment, add it to buffered comment cids\n if (!bufferedCommentCids[options.authorAddress].has(commentCid)) {\n addBufferedCommentCid(options.authorAddress, commentCid);\n }\n\n // already last comment cid, no need to set it\n if (commentCid === lastCommentCids[options.authorAddress]) {\n return;\n }\n\n // if comment is newer than current lastCommentCid and all bufferedComments, is lastCommentCid\n const currentLastCommentCid = lastCommentCids[options.authorAddress];\n const currentLastComment = comments[currentLastCommentCid || \"\"];\n // comment is older or equal to current lastCommentCid, do nothing\n if (comment.timestamp <= (currentLastComment?.timestamp || 0)) {\n log.trace(\n `authorsCommentsStore setLastCommentCidOnCommentsChange don't set lastCommentCid older than current lastCommentCid`,\n { comment, currentLastComment },\n );\n return;\n }\n\n // make sure lastComment is newer than all comments already in bufferedComments\n const bufferedComments: Comment[] = [...bufferedCommentCids[options.authorAddress]].map(\n (commentCid: string) => comments[commentCid],\n );\n for (const bufferedComment of bufferedComments) {\n const bufferedTs =\n bufferedComment == null || bufferedComment.timestamp == null\n ? 0\n : bufferedComment.timestamp;\n if (bufferedTs > comment.timestamp) {\n log.trace(\n `authorsCommentsStore setLastCommentCidOnCommentsChange don't set lastCommentCid older than buffered comments`,\n {\n comment,\n currentLastComment,\n bufferedComments,\n },\n );\n return;\n }\n }\n\n // is last comment cid, set it\n log(`authorsCommentsStore setLastCommentCidOnCommentsChange`, {\n lastCommentCid: comment.cid,\n lastComment: comment,\n currentLastComment,\n bufferedComments,\n });\n setLastCommentCid(options.authorAddress, commentCid);\n // add the last comment to loadedComments\n updateLoadedComments();\n // start a new linked list of comments to fetch using the lastComment.author.previousCommentCid\n if (comment.author?.previousCommentCid) {\n setNextCommentCidsToFetch(options.authorAddress, comment);\n }\n };\n\n// reset store in between tests\nconst originalState = authorsCommentsStore.getState();\n// async function because some stores have async init\nexport const resetAuthorsCommentsStore = async () => {\n communityLastCommentCidsFetching = {};\n previousComments = new QuickLru({ maxSize: 10000 });\n authorCommentCidsFetching = {};\n previousLastComments = new QuickLru({ maxSize: 10000 });\n\n // destroy all component subscriptions to the store\n authorsCommentsStore.destroy();\n // restore original state\n authorsCommentsStore.setState(originalState);\n};\n\n// reset database and store in between tests\nexport const resetAuthorsCommentsDatabaseAndStore = async () => {\n await resetAuthorsCommentsStore();\n};\n\nexport default authorsCommentsStore;\n"]}
1
+ {"version":3,"file":"authors-comments-store.js","sourceRoot":"","sources":["../../../src/stores/authors-comments/authors-comments-store.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,MAAM,GAAG,GAAG,MAAM,CAAC,sCAAsC,CAAC,CAAC;AAC3D,OAAO,WAAW,MAAM,SAAS,CAAC;AAClC,OAAO,MAAM,MAAM,kBAAkB,CAAC;AAQtC,OAAO,aAAgC,MAAM,aAAa,CAAC;AAC3D,OAAO,QAAQ,MAAM,WAAW,CAAC;AACjC,OAAO,EAEL,mCAAmC,EACnC,kCAAkC,GACnC,MAAM,SAAS,CAAC;AACjB,OAAO,aAAa,MAAM,aAAa,CAAC;AAExC,wEAAwE;AACxE,MAAM,CAAC,MAAM,eAAe,GAAG,EAAE,CAAC;AAClC,kDAAkD;AAClD,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAmBpC,MAAM,oBAAoB,GAAG,WAAW,CACtC,CAAC,QAAkB,EAAE,QAAkB,EAAE,EAAE,CAAC,CAAC;IAC3C,OAAO,EAAE,EAAE;IACX,cAAc,EAAE,EAAE;IAClB,uBAAuB,EAAE,EAAE;IAC3B,mBAAmB,EAAE,EAAE;IACvB,eAAe,EAAE,EAAE;IACnB,sBAAsB,EAAE,EAAE;IAC1B,sBAAsB,EAAE,EAAE;IAE1B,wBAAwB,EAAE,CACxB,kBAA0B,EAC1B,aAAqB,EACrB,UAAkB,EAClB,MAAkC,EAClC,OAAgB,EAChB,EAAE;;QAC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AAG,oBAAoB,CAAC,QAAQ,EAAE,CAAC;AACtD,qDAAqD;AACrD,MAAM,CAAC,MAAM,yBAAyB,GAAG,GAAS,EAAE;IAClD,gCAAgC,GAAG,EAAE,CAAC;IACtC,gBAAgB,GAAG,IAAI,QAAQ,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;IACpD,yBAAyB,GAAG,EAAE,CAAC;IAC/B,oBAAoB,GAAG,IAAI,QAAQ,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;IAExD,mDAAmD;IACnD,oBAAoB,CAAC,OAAO,EAAE,CAAC;IAC/B,yBAAyB;IACzB,oBAAoB,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;AAC/C,CAAC,CAAA,CAAC;AAEF,4CAA4C;AAC5C,MAAM,CAAC,MAAM,oCAAoC,GAAG,GAAS,EAAE;IAC7D,MAAM,yBAAyB,EAAE,CAAC;AACpC,CAAC,CAAA,CAAC;AAEF,eAAe,oBAAoB,CAAC","sourcesContent":["import Logger from \"@pkcprotocol/pkc-logger\";\nconst log = Logger(\"bitsocial-react-hooks:authors:stores\");\nimport createStore from \"zustand\";\nimport assert from \"../../lib/assert\";\nimport {\n CommentsFilter,\n AuthorCommentsOptions,\n AuthorsComments,\n Account,\n Comment,\n} from \"../../types\";\nimport commentsStore, { CommentsState } from \"../comments\";\nimport QuickLru from \"quick-lru\";\nimport {\n toSizes,\n getUpdatedLoadedAndBufferedComments,\n getNextCommentCidToFetchNotFetched,\n} from \"./utils\";\nimport accountsStore from \"../accounts\";\n\n// reddit loads approximately 25 posts per page while infinite scrolling\nexport const commentsPerPage = 25;\n// keep large buffer because fetching cids is slow\nexport const commentBufferSize = 50;\n\ntype AuthorsCommentsState = {\n // authorCommentsName is a string used a key to represent authorAddress + filter + accountId\n options: { [authorCommentsName: string]: AuthorCommentsOptions };\n loadedComments: { [authorCommentsName: string]: Comment[] };\n hasMoreBufferedComments: { [authorCommentsName: string]: boolean };\n bufferedCommentCids: { [authorAddress: string]: Set<string> };\n nextCommentCidsToFetch: { [authorAddress: string]: string | undefined };\n shouldFetchNextComment: { [authorAddress: string]: boolean };\n lastCommentCids: { [authorAddress: string]: string | undefined };\n addAuthorCommentsToStore: Function;\n setNextCommentCidsToFetch: Function;\n incrementPageNumber: Function;\n addBufferedCommentCid: Function;\n updateLoadedComments: Function;\n setLastCommentCid: Function;\n};\n\nconst authorsCommentsStore = createStore<AuthorsCommentsState>(\n (setState: Function, getState: Function) => ({\n options: {},\n loadedComments: {},\n hasMoreBufferedComments: {},\n bufferedCommentCids: {},\n lastCommentCids: {},\n nextCommentCidsToFetch: {},\n shouldFetchNextComment: {},\n\n addAuthorCommentsToStore: (\n authorCommentsName: string,\n authorAddress: string,\n commentCid: string,\n filter: CommentsFilter | undefined,\n account: Account,\n ) => {\n assert(\n authorCommentsName && typeof authorCommentsName === \"string\",\n `addAuthorCommentsToStore.incrementPageNumber invalid argument authorCommentsName '${authorCommentsName}'`,\n );\n assert(\n authorAddress && typeof authorAddress === \"string\",\n `authorsCommentsStore.addAuthorCommentsToStore invalid argument authorAddress '${authorAddress}'`,\n );\n assert(\n commentCid && typeof commentCid === \"string\",\n `authorsCommentsStore.addAuthorCommentsToStore invalid argument commentCid '${commentCid}'`,\n );\n assert(\n !filter || typeof filter?.filter === \"function\",\n `authorsCommentsStore.addAuthorCommentsToStore invalid argument filter.filter '${filter?.filter}'`,\n );\n assert(\n !filter || typeof filter?.key === \"string\",\n `authorsCommentsStore.addAuthorCommentsToStore invalid argument filter.key '${filter?.key}'`,\n );\n assert(\n typeof account?.pkc?.getComment === \"function\",\n `authorsCommentsStore.addAuthorCommentsToStore account '${account}' invalid`,\n );\n\n const { options, updateLoadedComments } = getState();\n // in store already, do nothing\n if (options[authorCommentsName]) {\n return;\n }\n const authorCommentsOptions = { authorAddress, pageNumber: 1, filter, accountId: account.id };\n\n // subscribe to nextCommentCidsToFetch and shouldFetchNextComment to fetch the comments\n authorsCommentsStore.subscribe(\n fetchCommentOnShouldFetchOrNextCidChange(authorCommentsOptions),\n );\n\n log(\"authorsCommentsActions.addAuthorCommentsToStore\", {\n authorCommentsName,\n authorCommentsOptions,\n commentCid,\n previousAuthorsCommentsOptions: options,\n });\n setState((state: AuthorsCommentsState) => ({\n options: { ...state.options, [authorCommentsName]: authorCommentsOptions },\n loadedComments: { ...state.loadedComments, [authorCommentsName]: [] },\n hasMoreBufferedComments: { ...state.hasMoreBufferedComments, [authorCommentsName]: true },\n bufferedCommentCids: {\n ...state.bufferedCommentCids,\n [authorAddress]: state.bufferedCommentCids[authorAddress] || new Set(),\n },\n lastCommentCids: {\n ...state.lastCommentCids,\n [authorAddress]: state.lastCommentCids[authorAddress] || undefined,\n },\n nextCommentCidsToFetch: {\n ...state.nextCommentCidsToFetch,\n [authorAddress]: state.nextCommentCidsToFetch[authorAddress] || commentCid,\n },\n shouldFetchNextComment: {\n ...state.shouldFetchNextComment,\n [authorAddress]: state.shouldFetchNextComment[authorAddress] || true,\n },\n }));\n\n // update loadedComments in case the author already has bufferedCommentCids\n updateLoadedComments();\n },\n\n setNextCommentCidsToFetch: (authorAddress: string, authorComment: Comment) => {\n assert(\n authorAddress && typeof authorAddress === \"string\",\n `authorsCommentsActions.setNextCommentCidsToFetch invalid argument authorAddress '${authorAddress}'`,\n );\n assert(\n typeof authorComment?.timestamp === \"number\",\n `authorsCommentsActions.setNextCommentCidsToFetch invalid argument authorComment '${authorComment}'`,\n );\n const { nextCommentCidsToFetch, shouldFetchNextComment, lastCommentCids } = getState();\n if (typeof shouldFetchNextComment[authorAddress] !== \"boolean\") {\n throw Error(\n `authorsCommentsActions.setNextCommentCidsToFetch can't set nextCommentCidToFetch '${authorAddress}' not in store`,\n );\n }\n const nextCommentCidToFetch = authorComment?.author?.previousCommentCid;\n if (nextCommentCidToFetch === nextCommentCidsToFetch[authorAddress]) {\n throw Error(\n `authorsCommentsActions.setNextCommentCidsToFetch can't set nextCommentCidToFetch '${authorAddress}' to '${nextCommentCidToFetch}' same value`,\n );\n }\n const nextCommentCidToFetchNotFetched =\n getNextCommentCidToFetchNotFetched(nextCommentCidToFetch);\n\n // log.trace('authorsCommentsActions.setNextCommentCidsToFetch', {\n // authorAddress,\n // authorComment,\n // previousNextCommentCidToFetch: nextCommentCidsToFetch[authorAddress],\n // nextCommentCidToFetch,\n // nextCommentCidToFetchNotFetched,\n // lastCommentCid: lastCommentCids[authorAddress],\n // shouldFetchNextComment: shouldFetchNextComment[authorAddress],\n // })\n setState((state: AuthorsCommentsState) => ({\n nextCommentCidsToFetch: {\n ...state.nextCommentCidsToFetch,\n [authorAddress]: nextCommentCidToFetchNotFetched,\n },\n }));\n },\n\n incrementPageNumber: (authorCommentsName: string) => {\n assert(\n authorCommentsName && typeof authorCommentsName === \"string\",\n `authorsCommentsActions.incrementPageNumber invalid argument authorCommentsName '${authorCommentsName}'`,\n );\n const { options, updateLoadedComments, loadedComments, nextCommentCidsToFetch } = getState();\n if (!options[authorCommentsName]) {\n throw Error(\n `authorsCommentsActions.incrementPageNumber can't increment page number of options '${authorCommentsName}' not in store`,\n );\n }\n assert(\n options[authorCommentsName].pageNumber * commentsPerPage <=\n loadedComments[authorCommentsName].length,\n `authorsCommentsActions.incrementPageNumber cannot increment page number before current page has loaded`,\n );\n\n log(\"authorsCommentsActions.incrementPageNumber\", {\n authorCommentsName,\n pageNumber: options[authorCommentsName].pageNumber + 1,\n nextCommentCidsToFetch: nextCommentCidsToFetch[options[authorCommentsName].authorAddress],\n });\n setState(({ options }: AuthorsCommentsState) => {\n const authorCommentOptions = { ...options[authorCommentsName] };\n authorCommentOptions.pageNumber++;\n return { options: { ...options, [authorCommentsName]: authorCommentOptions } };\n });\n\n // must update loadedComments to reflect the new added page\n updateLoadedComments();\n },\n\n addBufferedCommentCid: (authorAddress: string, commentCid: string) => {\n assert(\n authorAddress && typeof authorAddress === \"string\",\n `authorsCommentsActions.addBufferedCommentCid invalid argument authorAddress '${authorAddress}'`,\n );\n assert(\n commentCid && typeof commentCid === \"string\",\n `authorsCommentsActions.addBufferedCommentCid invalid argument commentCid '${commentCid}'`,\n );\n\n const { bufferedCommentCids } = getState();\n if (!bufferedCommentCids[authorAddress]) {\n throw Error(\n `authorsCommentsActions.addBufferedCommentCid can't add commentCid '${authorAddress}' not in store`,\n );\n }\n if (bufferedCommentCids[authorAddress].has(commentCid)) {\n throw Error(\n `authorsCommentsActions.addBufferedCommentCid can't add commentCid '${authorAddress}' '${commentCid}' already added`,\n );\n }\n\n // log.trace('authorsCommentsActions.addBufferedCommentCid', {authorAddress, commentCid, previousBufferedCommentCidsSize: bufferedCommentCids[authorAddress].size})\n setState((state: AuthorsCommentsState) => ({\n bufferedCommentCids: {\n ...state.bufferedCommentCids,\n [authorAddress]: new Set([...bufferedCommentCids[authorAddress], commentCid]),\n },\n }));\n },\n\n updateLoadedComments() {\n const { comments } = commentsStore.getState();\n let {\n loadedComments: previousAuthorsLoadedComments,\n bufferedCommentCids,\n options,\n nextCommentCidsToFetch,\n lastCommentCids,\n } = getState();\n\n const newAuthorsLoadedComments: AuthorsComments = {};\n const newShouldFetchNextComment: { [authorAddress: string]: boolean } = {};\n const newHasMoreBufferedComments: { [authorCommentsName: string]: boolean } = {};\n const authorCommentsNames = Object.keys(options);\n for (const name of authorCommentsNames) {\n const { authorAddress, pageNumber, filter } = options[name];\n const previousLoadedComments = previousAuthorsLoadedComments[name];\n const unfilteredBufferedComments: Comment[] = [...bufferedCommentCids[authorAddress]].map(\n (commentCid: string) => comments[commentCid],\n );\n\n const { loadedComments, bufferedComments: filteredBufferedComments } =\n getUpdatedLoadedAndBufferedComments(\n previousLoadedComments,\n unfilteredBufferedComments,\n pageNumber,\n filter,\n comments,\n );\n newAuthorsLoadedComments[name] = loadedComments;\n newHasMoreBufferedComments[name] = filteredBufferedComments.length > loadedComments.length;\n\n // if another authorCommentOptions should fetch, don't change it\n if (newShouldFetchNextComment[authorAddress] !== true) {\n // fetch if less comments than full page + buffer size\n newShouldFetchNextComment[authorAddress] =\n filteredBufferedComments.length < pageNumber * commentsPerPage + commentBufferSize;\n }\n }\n\n // log.trace('authorsCommentsActions.updateLoadedComments', {\n // bufferedCommentCids,\n // bufferedCommentCidsSizes: toSizes(bufferedCommentCids),\n // previousAuthorsLoadedComments,\n // newAuthorsLoadedComments,\n // previousAuthorsLoadedCommentsSizes: toSizes(previousAuthorsLoadedComments),\n // newAuthorsLoadedCommentsSizes: toSizes(newAuthorsLoadedComments),\n // newShouldFetchNextComment,\n // lastCommentCids\n // })\n setState(() => ({\n loadedComments: newAuthorsLoadedComments,\n shouldFetchNextComment: newShouldFetchNextComment,\n hasMoreBufferedComments: newHasMoreBufferedComments,\n }));\n },\n\n setLastCommentCid: (authorAddress: string, lastCommentCid: string) => {\n assert(\n authorAddress && typeof authorAddress === \"string\",\n `authorsCommentsActions.setLastCommentCid invalid argument authorAddress '${authorAddress}'`,\n );\n assert(\n lastCommentCid && typeof lastCommentCid === \"string\",\n `authorsCommentsActions.setLastCommentCid invalid argument lastCommentCid '${lastCommentCid}'`,\n );\n const { lastCommentCids, shouldFetchNextComment, nextCommentCidsToFetch } = getState();\n if (typeof shouldFetchNextComment[authorAddress] !== \"boolean\") {\n throw Error(\n `authorsCommentsActions.setLastCommentCid can't set lastCommentCid '${authorAddress}' not in store`,\n );\n }\n if (lastCommentCid === lastCommentCids[authorAddress]) {\n throw Error(\n `authorsCommentsActions.setLastCommentCid can't set setLastCommentCid '${authorAddress}' to '${lastCommentCid}' same value`,\n );\n }\n\n log(\"authorsCommentsActions.setLastCommentCid\", {\n authorAddress,\n lastCommentCid,\n previousLastCommentCid: lastCommentCids[authorAddress],\n shouldFetchNextComment: shouldFetchNextComment[authorAddress],\n nextCommentCidsToFetch: nextCommentCidsToFetch[authorAddress],\n });\n setState((state: AuthorsCommentsState) => ({\n lastCommentCids: { ...state.lastCommentCids, [authorAddress]: lastCommentCid },\n }));\n },\n }),\n);\n\n// This store watches the comments it fetches for the rest of the session, so it waits for the\n// update addCommentToStore requests on open, and a frozen useComment that leaves cannot end it.\nconst addCommentToCommentsStore = (commentCid: string, account: Account) => {\n const { addOpeningUpdateWaiter, addCommentToStore } = commentsStore.getState();\n addOpeningUpdateWaiter(commentCid, \"authorsCommentsStore\");\n return addCommentToStore(commentCid, account);\n};\n\n// if nextCommentCidsToFetch or shouldFetchNextComment changed, fetch the next comment\nconst fetchCommentOnShouldFetchOrNextCidChange =\n (options: AuthorCommentsOptions) => (state: AuthorsCommentsState) => {\n const nextCommentCidToFetch = state.nextCommentCidsToFetch[options.authorAddress];\n if (!nextCommentCidToFetch) {\n return;\n }\n\n // the buffered comments are already full, not need to fetch next comment\n const shouldFetchNextComment = state.shouldFetchNextComment[options.authorAddress];\n if (!shouldFetchNextComment) {\n return;\n }\n\n // when comment has fetched, update loadedComments, bufferedComments and shouldFetchNextComment\n const authorCommentKey = getAuthorCommentKey(options.authorAddress, nextCommentCidToFetch);\n if (!authorCommentCidsFetching[authorCommentKey]) {\n authorCommentCidsFetching[authorCommentKey] = true;\n commentsStore.subscribe(updateCommentsOnCommentsChange(options, nextCommentCidToFetch));\n }\n\n // start fetching comment\n const account = accountsStore.getState().accounts[options.accountId];\n addCommentToCommentsStore(nextCommentCidToFetch, account).catch((error: unknown) =>\n log.error(\n \"authorsCommentsStore fetchCommentOnShouldFetchOrNextCidChange addCommentToStore error\",\n { error, nextCommentCidToFetch, account },\n ),\n );\n };\n\n// the chains of 2 authors can reach the same cid, and each chain must handle it (e.g. end on a\n// comment by another author), so fetching listeners and previous comments are keyed per author\nconst getAuthorCommentKey = (authorAddress: string, commentCid: string) =>\n `${authorAddress} ${commentCid}`;\n\n// if commentStore changed, update loadedComments, bufferedCommentCids, shouldFetchNextComment and nextCommentCidsToFetch\nlet previousComments = new QuickLru({ maxSize: 10000 });\nlet authorCommentCidsFetching: { [authorCommentKey: string]: boolean } = {};\nlet communityLastCommentCidsFetching: { [commentCid: string]: boolean } = {};\nconst updateCommentsOnCommentsChange =\n (options: AuthorCommentsOptions, commentCid: string) => (state: CommentsState) => {\n const authorCommentKey = getAuthorCommentKey(options.authorAddress, commentCid);\n // not a next cid, do nothing\n if (!authorCommentCidsFetching[authorCommentKey]) {\n return;\n }\n const comment = state.comments[commentCid];\n // comment hasn't changed, do nothing\n if (!comment?.timestamp || comment === previousComments.get(authorCommentKey)) {\n return;\n }\n // comment author address is incorrect, do nothing\n if (comment.author?.address !== options.authorAddress) {\n // a next cid by another author can't continue the author's comments, so nothing is left to\n // fetch, otherwise the chain would wait on it forever\n if (\n commentCid === authorsCommentsStore.getState().nextCommentCidsToFetch[options.authorAddress]\n ) {\n log.error(\"authorsCommentsStore next comment cid has a different author address\", {\n commentCid,\n authorAddress: options.authorAddress,\n commentAuthorAddress: comment.author?.address,\n });\n authorsCommentsStore.setState((state: AuthorsCommentsState) => ({\n nextCommentCidsToFetch: {\n ...state.nextCommentCidsToFetch,\n [options.authorAddress]: undefined,\n },\n }));\n }\n return;\n }\n previousComments.set(authorCommentKey, comment);\n\n const {\n addBufferedCommentCid,\n bufferedCommentCids,\n updateLoadedComments,\n setNextCommentCidsToFetch,\n nextCommentCidsToFetch,\n } = authorsCommentsStore.getState();\n\n // the comment is a new comment, add it to buffered comment cids\n if (!bufferedCommentCids[options.authorAddress].has(commentCid)) {\n addBufferedCommentCid(options.authorAddress, commentCid);\n }\n\n // the comment was the last cid to fetch, set the next cid to fetch as the author previous cid\n const nextCidToFetch = nextCommentCidsToFetch[options.authorAddress];\n if (commentCid === nextCidToFetch) {\n setNextCommentCidsToFetch(options.authorAddress, comment);\n }\n\n // one of the comment changed, must update loaded comments\n updateLoadedComments();\n\n // the changed comment might have a new author.community.lastCommentCid, try to fetch it\n const communityLastCommentCid = comment.author?.community?.lastCommentCid;\n if (communityLastCommentCid) {\n // when last comment has fetched, update lastCommentCid\n if (!communityLastCommentCidsFetching[communityLastCommentCid]) {\n communityLastCommentCidsFetching[communityLastCommentCid] = true;\n commentsStore.subscribe(\n setLastCommentCidOnCommentsChange(options, communityLastCommentCid),\n );\n }\n\n // start fetching lastCommentCid\n const account = accountsStore.getState().accounts[options.accountId];\n addCommentToCommentsStore(communityLastCommentCid, account).catch((error: unknown) =>\n log.error(\"authorsCommentsStore updateCommentsOnCommentsChange addCommentToStore error\", {\n error,\n communityLastCommentCid,\n account,\n }),\n );\n }\n };\n\nlet previousLastComments = new QuickLru({ maxSize: 10000 });\nconst setLastCommentCidOnCommentsChange =\n (options: AuthorCommentsOptions, commentCid: string) => (state: CommentsState) => {\n // not a last cid candidate, do nothing\n if (!communityLastCommentCidsFetching[commentCid]) {\n return;\n }\n const { comments } = state;\n\n const comment = comments[commentCid];\n // comment hasn't changed, do nothing\n if (!comment?.timestamp || comment === previousLastComments.get(commentCid)) {\n return;\n }\n // comment author address is incorrect, do nothing\n if (comment.author?.address !== options.authorAddress) {\n return;\n }\n previousLastComments.set(commentCid, comment);\n\n const {\n addBufferedCommentCid,\n lastCommentCids,\n bufferedCommentCids,\n setLastCommentCid,\n setNextCommentCidsToFetch,\n updateLoadedComments,\n } = authorsCommentsStore.getState();\n\n // if the comment is a new comment, add it to buffered comment cids\n if (!bufferedCommentCids[options.authorAddress].has(commentCid)) {\n addBufferedCommentCid(options.authorAddress, commentCid);\n }\n\n // already last comment cid, no need to set it\n if (commentCid === lastCommentCids[options.authorAddress]) {\n return;\n }\n\n // if comment is newer than current lastCommentCid and all bufferedComments, is lastCommentCid\n const currentLastCommentCid = lastCommentCids[options.authorAddress];\n const currentLastComment = comments[currentLastCommentCid || \"\"];\n // comment is older or equal to current lastCommentCid, do nothing\n if (comment.timestamp <= (currentLastComment?.timestamp || 0)) {\n log.trace(\n `authorsCommentsStore setLastCommentCidOnCommentsChange don't set lastCommentCid older than current lastCommentCid`,\n { comment, currentLastComment },\n );\n return;\n }\n\n // make sure lastComment is newer than all comments already in bufferedComments\n const bufferedComments: Comment[] = [...bufferedCommentCids[options.authorAddress]].map(\n (commentCid: string) => comments[commentCid],\n );\n for (const bufferedComment of bufferedComments) {\n const bufferedTs =\n bufferedComment == null || bufferedComment.timestamp == null\n ? 0\n : bufferedComment.timestamp;\n if (bufferedTs > comment.timestamp) {\n log.trace(\n `authorsCommentsStore setLastCommentCidOnCommentsChange don't set lastCommentCid older than buffered comments`,\n {\n comment,\n currentLastComment,\n bufferedComments,\n },\n );\n return;\n }\n }\n\n // is last comment cid, set it\n log(`authorsCommentsStore setLastCommentCidOnCommentsChange`, {\n lastCommentCid: comment.cid,\n lastComment: comment,\n currentLastComment,\n bufferedComments,\n });\n setLastCommentCid(options.authorAddress, commentCid);\n // add the last comment to loadedComments\n updateLoadedComments();\n // start a new linked list of comments to fetch using the lastComment.author.previousCommentCid,\n // unless the list already continues from there, e.g. the lastComment is the one it started from\n const previousCommentCid = comment.author?.previousCommentCid;\n if (\n previousCommentCid &&\n previousCommentCid !==\n authorsCommentsStore.getState().nextCommentCidsToFetch[options.authorAddress]\n ) {\n setNextCommentCidsToFetch(options.authorAddress, comment);\n }\n };\n\n// reset store in between tests\nconst originalState = authorsCommentsStore.getState();\n// async function because some stores have async init\nexport const resetAuthorsCommentsStore = async () => {\n communityLastCommentCidsFetching = {};\n previousComments = new QuickLru({ maxSize: 10000 });\n authorCommentCidsFetching = {};\n previousLastComments = new QuickLru({ maxSize: 10000 });\n\n // destroy all component subscriptions to the store\n authorsCommentsStore.destroy();\n // restore original state\n authorsCommentsStore.setState(originalState);\n};\n\n// reset database and store in between tests\nexport const resetAuthorsCommentsDatabaseAndStore = async () => {\n await resetAuthorsCommentsStore();\n};\n\nexport default authorsCommentsStore;\n"]}
@@ -13,6 +13,8 @@ export type CommentsState = {
13
13
  addCommentToStore: Function;
14
14
  startCommentAutoUpdate: Function;
15
15
  stopCommentAutoUpdate: Function;
16
+ addOpeningUpdateWaiter: Function;
17
+ removeOpeningUpdateWaiter: Function;
16
18
  refreshComment: Function;
17
19
  };
18
20
  declare const commentsStore: import("zustand").UseBoundStore<import("zustand").StoreApi<CommentsState>>;
@@ -1 +1 @@
1
- {"version":3,"file":"comments-store.d.ts","sourceRoot":"","sources":["../../../src/stores/comments/comments-store.ts"],"names":[],"mappings":"AAKA,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,eAAO,MAAM,GAAG,QAAkD,CAAC;AACnE,OAAO,EAAW,QAAQ,EAAW,MAAM,aAAa,CAAC;AAqBzD,eAAO,MAAM,SAAS,EAAE,GAAQ,CAAC;AAEjC,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,QAAQ,CAAC;IACnB,MAAM,EAAE;QAAE,CAAC,UAAU,EAAE,MAAM,GAAG,KAAK,EAAE,CAAA;KAAE,CAAC;IAG1C,qBAAqB,EAAE;QAAE,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IACtD,iBAAiB,EAAE,QAAQ,CAAC;IAC5B,sBAAsB,EAAE,QAAQ,CAAC;IACjC,qBAAqB,EAAE,QAAQ,CAAC;IAChC,cAAc,EAAE,QAAQ,CAAC;CAC1B,CAAC;AAuDF,QAAA,MAAM,aAAa,4EAygBjB,CAAC;AA8BH,eAAO,MAAM,kBAAkB,qBAsC9B,CAAC;AAGF,eAAO,MAAM,6BAA6B,qBAGzC,CAAC;AAEF,eAAe,aAAa,CAAC"}
1
+ {"version":3,"file":"comments-store.d.ts","sourceRoot":"","sources":["../../../src/stores/comments/comments-store.ts"],"names":[],"mappings":"AAKA,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,eAAO,MAAM,GAAG,QAAkD,CAAC;AACnE,OAAO,EAAW,QAAQ,EAAW,MAAM,aAAa,CAAC;AAmCzD,eAAO,MAAM,SAAS,EAAE,GAAQ,CAAC;AAEjC,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,QAAQ,CAAC;IACnB,MAAM,EAAE;QAAE,CAAC,UAAU,EAAE,MAAM,GAAG,KAAK,EAAE,CAAA;KAAE,CAAC;IAG1C,qBAAqB,EAAE;QAAE,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IACtD,iBAAiB,EAAE,QAAQ,CAAC;IAC5B,sBAAsB,EAAE,QAAQ,CAAC;IACjC,qBAAqB,EAAE,QAAQ,CAAC;IAChC,sBAAsB,EAAE,QAAQ,CAAC;IACjC,yBAAyB,EAAE,QAAQ,CAAC;IACpC,cAAc,EAAE,QAAQ,CAAC;CAC1B,CAAC;AAsEF,QAAA,MAAM,aAAa,4EA+mBjB,CAAC;AAiEH,eAAO,MAAM,kBAAkB,qBAqD9B,CAAC;AAGF,eAAO,MAAM,6BAA6B,qBAGzC,CAAC;AAEF,eAAe,aAAa,CAAC"}
@@ -22,11 +22,23 @@ import { normalizeCommentCommunityAddress } from "../../lib/pkc-compat.js";
22
22
  let pkcGetCommentPending = {};
23
23
  const liveComments = {};
24
24
  const liveCommentPromises = {};
25
+ // settles when every comment.stop() in progress for the CID has returned
26
+ const liveCommentStops = {};
25
27
  const commentAutoUpdateSubscribers = {};
28
+ // hooks and stores waiting for the update requested on open: a frozen useComment until it
29
+ // settles, and the authors-comments store, which never leaves
30
+ const openingUpdateWaiters = {};
26
31
  const stopCommentAfterNextUpdate = {};
27
32
  const sparseCommentFollowupRequested = {};
28
33
  // set when the current update attempt emitted an error, until the next attempt starts
29
34
  const commentUpdateAttemptFailed = {};
35
+ // refreshComment calls waiting for their update cycle to end
36
+ const pendingRefreshes = {};
37
+ // CIDs whose pending refreshes outlived the last auto-update subscriber, e.g. the hook that called
38
+ // refresh() unmounted
39
+ const refreshesOutlivedSubscribers = {};
40
+ // CIDs whose last hook or refresh left while their one-shot update may still be retrying
41
+ const oneShotUpdateConsumersLeft = {};
30
42
  const initializedComments = new WeakSet();
31
43
  const trackedListeners = new WeakSet();
32
44
  // reset all event listeners in between tests
@@ -42,6 +54,16 @@ const removeCommentListener = (comment, event, listener) => {
42
54
  };
43
55
  const getCommentAutoUpdateSubscribersCount = (commentCid) => Object.keys(commentAutoUpdateSubscribers[commentCid] || {}).length;
44
56
  const hasCommentAutoUpdateSubscribers = (commentCid) => getCommentAutoUpdateSubscribersCount(commentCid) > 0;
57
+ const hasOpeningUpdateWaiters = (commentCid) => Object.keys(openingUpdateWaiters[commentCid] || {}).length > 0;
58
+ // hooks and refreshes that keep a failing one-shot update retrying
59
+ const hasOneShotUpdateConsumers = (commentCid) => hasCommentAutoUpdateSubscribers(commentCid) ||
60
+ hasOpeningUpdateWaiters(commentCid) ||
61
+ (Boolean(pendingRefreshes[commentCid]) && !refreshesOutlivedSubscribers[commentCid]);
62
+ const markOneShotUpdateConsumerLeft = (commentCid) => {
63
+ if (!hasOneShotUpdateConsumers(commentCid)) {
64
+ oneShotUpdateConsumersLeft[commentCid] = true;
65
+ }
66
+ };
45
67
  const mergeCommentData = (commentCid, ...commentDataList) => {
46
68
  const mergedCommentData = { cid: commentCid };
47
69
  for (const commentData of commentDataList) {
@@ -108,15 +130,28 @@ const commentsStore = createStore((setState, getState) => {
108
130
  return { openingUpdatesPending };
109
131
  });
110
132
  };
111
- // When the last auto-update subscriber leaves during the opening update, stopping would abort
112
- // it and leave a frozen useComment on a cached copy, so stop when that update ends instead.
113
- const deferStopUntilOpeningUpdateEnds = (commentCid) => {
114
- if (!getState().openingUpdatesPending[commentCid]) {
133
+ // When the last auto-update subscriber leaves during the opening update or a refresh, stopping
134
+ // would abort it and leave a frozen useComment on a cached copy or a refresh that never
135
+ // settles, so stop when that update ends instead.
136
+ const deferStopUntilRequestedUpdateEnds = (commentCid) => {
137
+ if (!getState().openingUpdatesPending[commentCid] && !pendingRefreshes[commentCid]) {
115
138
  return false;
116
139
  }
117
140
  stopCommentAfterNextUpdate[commentCid] = true;
118
141
  return true;
119
142
  };
143
+ // ensureLiveComment waits for these before it hands the comment out again
144
+ const trackLiveCommentStop = (commentCid, stopCall) => {
145
+ const stopped = Promise.resolve(stopCall).then(() => { }, () => { });
146
+ const previousStops = liveCommentStops[commentCid];
147
+ const stops = previousStops ? Promise.all([previousStops, stopped]).then(() => { }) : stopped;
148
+ liveCommentStops[commentCid] = stops;
149
+ void stops.then(() => {
150
+ if (liveCommentStops[commentCid] === stops) {
151
+ delete liveCommentStops[commentCid];
152
+ }
153
+ });
154
+ };
120
155
  const stopLiveComment = (commentCid, comment) => __awaiter(void 0, void 0, void 0, function* () {
121
156
  clearCommentUpdateFollowup(commentCid);
122
157
  // a stopped comment gets no further update from this cycle
@@ -126,7 +161,9 @@ const commentsStore = createStore((setState, getState) => {
126
161
  return;
127
162
  }
128
163
  try {
129
- yield liveComment.stop();
164
+ const stopCall = liveComment.stop();
165
+ trackLiveCommentStop(commentCid, stopCall);
166
+ yield stopCall;
130
167
  }
131
168
  catch (error) {
132
169
  log.trace("comment.stop error", { commentCid, comment: liveComment, error });
@@ -137,6 +174,7 @@ const commentsStore = createStore((setState, getState) => {
137
174
  return;
138
175
  }
139
176
  delete stopCommentAfterNextUpdate[commentCid];
177
+ delete oneShotUpdateConsumersLeft[commentCid];
140
178
  if (hasCommentAutoUpdateSubscribers(commentCid)) {
141
179
  return;
142
180
  }
@@ -187,6 +225,14 @@ const commentsStore = createStore((setState, getState) => {
187
225
  const isUnchangedUpdateCycleEnd = (updatingState, commentCid, comment) => updatingState === "waiting-retry" &&
188
226
  !commentUpdateAttemptFailed[commentCid] &&
189
227
  typeof (comment === null || comment === void 0 ? void 0 : comment.updatedAt) === "number";
228
+ // A one-shot update keeps retrying after a retriable error while anything waits for it, and for
229
+ // callers that never register. Once the hooks and refreshes that used it have all left, e.g. a
230
+ // frozen useComment of a comment whose community is unreachable unmounted, its next retry ends
231
+ // it instead of retrying for the rest of the session.
232
+ const isUnusedOneShotRetry = (updatingState, commentCid) => updatingState === "waiting-retry" &&
233
+ Boolean(stopCommentAfterNextUpdate[commentCid]) &&
234
+ Boolean(oneShotUpdateConsumersLeft[commentCid]) &&
235
+ !hasOneShotUpdateConsumers(commentCid);
190
236
  const initializeComment = (commentCid, comment, account) => {
191
237
  var _a, _b, _c, _d;
192
238
  if (initializedComments.has(comment)) {
@@ -228,7 +274,8 @@ const commentsStore = createStore((setState, getState) => {
228
274
  }
229
275
  if (updatingState === "succeeded" ||
230
276
  updatingState === "failed" ||
231
- isUnchangedUpdateCycleEnd(updatingState, commentCid, comment)) {
277
+ isUnchangedUpdateCycleEnd(updatingState, commentCid, comment) ||
278
+ isUnusedOneShotRetry(updatingState, commentCid)) {
232
279
  maybeStopCommentAfterOneShotUpdate(commentCid, comment);
233
280
  endOpeningUpdate(commentCid);
234
281
  }
@@ -278,7 +325,16 @@ const commentsStore = createStore((setState, getState) => {
278
325
  listeners.push(comment);
279
326
  }
280
327
  };
281
- const ensureLiveComment = (commentCid, account, commentData) => __awaiter(void 0, void 0, void 0, function* () {
328
+ // pkc-js stop() marks the comment stopped before it tears down the update loop, and update()
329
+ // in between joins that loop, after which the comment never updates again
330
+ const waitForLiveCommentStops = (commentCid) => __awaiter(void 0, void 0, void 0, function* () {
331
+ while (liveCommentStops[commentCid]) {
332
+ yield liveCommentStops[commentCid];
333
+ }
334
+ });
335
+ // addCommentToStore passes cacheChecked, because it has read the cache already
336
+ const ensureLiveComment = (commentCid, account, commentData, options) => __awaiter(void 0, void 0, void 0, function* () {
337
+ yield waitForLiveCommentStops(commentCid);
282
338
  if (liveComments[commentCid]) {
283
339
  return liveComments[commentCid];
284
340
  }
@@ -286,8 +342,12 @@ const commentsStore = createStore((setState, getState) => {
286
342
  return liveCommentPromises[commentCid];
287
343
  }
288
344
  const liveCommentPromise = (() => __awaiter(void 0, void 0, void 0, function* () {
289
- const initialComment = normalizeCommentCommunityAddress(mergeCommentData(commentCid, commentData));
290
- const liveComment = normalizeCommentCommunityAddress(yield account.pkc.createComment(initialComment));
345
+ // Whichever action creates the live comment, seed it from the cache: a comment created
346
+ // without its CommentIpfs emits that alone first, which would replace a cached CommentUpdate.
347
+ const cachedCommentData = (options === null || options === void 0 ? void 0 : options.cacheChecked)
348
+ ? undefined
349
+ : yield readCachedComment(commentCid);
350
+ const liveComment = normalizeCommentCommunityAddress(yield createCommentSeededFromCache(commentCid, account, cachedCommentData, commentData));
291
351
  initializeComment(commentCid, liveComment, account);
292
352
  return liveComment;
293
353
  }))();
@@ -341,6 +401,12 @@ const commentsStore = createStore((setState, getState) => {
341
401
  isUnchangedUpdateCycleEnd(updatingState, commentCid, comment);
342
402
  const onUpdatingStateChange = (updatingState) => {
343
403
  var _a;
404
+ // Nothing else waits for this cycle, which may never end, e.g. without a CommentUpdate, so
405
+ // settle at its next retry, where the store stops the comment if nothing else uses it.
406
+ if (updatingState === "waiting-retry" && refreshesOutlivedSubscribers[commentCid]) {
407
+ settle();
408
+ return;
409
+ }
344
410
  if (isSettledUpdatingState(updatingState)) {
345
411
  if (shouldWaitForSparseCommentFollowup(commentCid, comment)) {
346
412
  return;
@@ -389,11 +455,15 @@ const commentsStore = createStore((setState, getState) => {
389
455
  return;
390
456
  }
391
457
  pkcGetCommentPending[pendingKey] = true;
458
+ // only hooks that leave during this fetch end its failing update early
459
+ delete oneShotUpdateConsumersLeft[commentCid];
392
460
  try {
393
461
  // try to find comment in database
394
462
  comment = yield getCommentFromDatabase(commentCid, account, commentData);
395
463
  if (!comment) {
396
- comment = yield ensureLiveComment(commentCid, account, commentData);
464
+ comment = yield ensureLiveComment(commentCid, account, commentData, {
465
+ cacheChecked: true,
466
+ });
397
467
  comment = normalizeCommentCommunityAddress(comment);
398
468
  log("commentsStore.addCommentToStore", { commentCid, comment, account });
399
469
  setState((state) => ({
@@ -409,7 +479,7 @@ const commentsStore = createStore((setState, getState) => {
409
479
  }));
410
480
  // add comment replies pages to repliesPagesStore so they can be used in useComment
411
481
  repliesPagesStore.getState().addRepliesPageCommentsToStore(comment);
412
- comment = yield ensureLiveComment(commentCid, account, mergeCommentData(commentCid, comment, commentData));
482
+ comment = yield ensureLiveComment(commentCid, account, mergeCommentData(commentCid, comment, commentData), { cacheChecked: true });
413
483
  }
414
484
  if (comment) {
415
485
  const liveComment = comment;
@@ -435,6 +505,7 @@ const commentsStore = createStore((setState, getState) => {
435
505
  return __awaiter(this, void 0, void 0, function* () {
436
506
  const hadAutoUpdateSubscribers = hasCommentAutoUpdateSubscribers(commentCid);
437
507
  commentAutoUpdateSubscribers[commentCid] = Object.assign(Object.assign({}, (commentAutoUpdateSubscribers[commentCid] || {})), { [subscriberId]: true });
508
+ delete refreshesOutlivedSubscribers[commentCid];
438
509
  if (hadAutoUpdateSubscribers && liveComments[commentCid]) {
439
510
  return;
440
511
  }
@@ -446,7 +517,7 @@ const commentsStore = createStore((setState, getState) => {
446
517
  }));
447
518
  }
448
519
  if (!hasCommentAutoUpdateSubscribers(commentCid)) {
449
- if (deferStopUntilOpeningUpdateEnds(commentCid)) {
520
+ if (deferStopUntilRequestedUpdateEnds(commentCid)) {
450
521
  return;
451
522
  }
452
523
  yield stopLiveComment(commentCid, liveComment);
@@ -464,27 +535,66 @@ const commentsStore = createStore((setState, getState) => {
464
535
  delete commentAutoUpdateSubscribers[commentCid];
465
536
  }
466
537
  }
538
+ if (pendingRefreshes[commentCid] && !hasCommentAutoUpdateSubscribers(commentCid)) {
539
+ refreshesOutlivedSubscribers[commentCid] = true;
540
+ }
541
+ markOneShotUpdateConsumerLeft(commentCid);
467
542
  if (hasCommentAutoUpdateSubscribers(commentCid) ||
468
- deferStopUntilOpeningUpdateEnds(commentCid)) {
543
+ deferStopUntilRequestedUpdateEnds(commentCid)) {
469
544
  return;
470
545
  }
471
546
  delete stopCommentAfterNextUpdate[commentCid];
547
+ delete oneShotUpdateConsumersLeft[commentCid];
472
548
  const liveComment = liveComments[commentCid];
473
549
  yield stopLiveComment(commentCid, liveComment);
474
550
  maybeReleaseStoppedLiveComment(commentCid, liveComment);
475
551
  });
476
552
  },
553
+ // keeps a failing update requested on open retrying while a hook waits for it
554
+ addOpeningUpdateWaiter(commentCid, waiterId) {
555
+ openingUpdateWaiters[commentCid] = Object.assign(Object.assign({}, openingUpdateWaiters[commentCid]), { [waiterId]: true });
556
+ },
557
+ removeOpeningUpdateWaiter(commentCid, waiterId) {
558
+ if (!openingUpdateWaiters[commentCid]) {
559
+ return;
560
+ }
561
+ delete openingUpdateWaiters[commentCid][waiterId];
562
+ if (!hasOpeningUpdateWaiters(commentCid)) {
563
+ delete openingUpdateWaiters[commentCid];
564
+ }
565
+ markOneShotUpdateConsumerLeft(commentCid);
566
+ },
477
567
  refreshComment(commentCid, account, commentData) {
478
568
  return __awaiter(this, void 0, void 0, function* () {
479
- const storedComment = getState().comments[commentCid];
480
- const liveComment = yield ensureLiveComment(commentCid, account, mergeCommentData(commentCid, storedComment, commentData));
481
- if (!hasCommentAutoUpdateSubscribers(commentCid) &&
482
- (liveComment === null || liveComment === void 0 ? void 0 : liveComment.updatingState) !== "stopped") {
483
- yield stopLiveComment(commentCid, liveComment);
569
+ // counted from the start, so a subscriber that leaves while the refresh stops the comment
570
+ // defers its stop instead of stopping the comment again
571
+ pendingRefreshes[commentCid] = (pendingRefreshes[commentCid] || 0) + 1;
572
+ // a new refresh wants its whole cycle, so earlier refreshes wait for the cycle end again too
573
+ delete refreshesOutlivedSubscribers[commentCid];
574
+ try {
575
+ const storedComment = getState().comments[commentCid];
576
+ const liveComment = yield ensureLiveComment(commentCid, account, mergeCommentData(commentCid, storedComment, commentData));
577
+ if (!hasCommentAutoUpdateSubscribers(commentCid) &&
578
+ (liveComment === null || liveComment === void 0 ? void 0 : liveComment.updatingState) !== "stopped") {
579
+ yield stopLiveComment(commentCid, liveComment);
580
+ }
581
+ // another stop may have begun meanwhile, e.g. when the update requested on open ended
582
+ yield waitForLiveCommentStops(commentCid);
583
+ return yield waitForCommentUpdateCycle(commentCid, liveComment, () => requestCommentUpdate(commentCid, liveComment, {
584
+ stopAfterNextUpdate: !hasCommentAutoUpdateSubscribers(commentCid),
585
+ }));
586
+ }
587
+ finally {
588
+ const remainingRefreshes = (pendingRefreshes[commentCid] || 0) - 1;
589
+ if (remainingRefreshes > 0) {
590
+ pendingRefreshes[commentCid] = remainingRefreshes;
591
+ }
592
+ else {
593
+ delete pendingRefreshes[commentCid];
594
+ delete refreshesOutlivedSubscribers[commentCid];
595
+ }
596
+ markOneShotUpdateConsumerLeft(commentCid);
484
597
  }
485
- return waitForCommentUpdateCycle(commentCid, liveComment, () => requestCommentUpdate(commentCid, liveComment, {
486
- stopAfterNextUpdate: !hasCommentAutoUpdateSubscribers(commentCid),
487
- }));
488
598
  });
489
599
  },
490
600
  };
@@ -506,6 +616,30 @@ const getCommentFromDatabase = (commentCid, account, initialCommentData) => __aw
506
616
  });
507
617
  }
508
618
  });
619
+ const readCachedComment = (commentCid) => __awaiter(void 0, void 0, void 0, function* () {
620
+ try {
621
+ return (yield commentsDatabase.getItem(commentCid));
622
+ }
623
+ catch (error) {
624
+ log.error("commentsStore cached comment read error", { commentCid, error });
625
+ }
626
+ });
627
+ // Like getCommentFromDatabase, skips a cache entry pkc-js can no longer parse, e.g. after a schema
628
+ // change, and creates the comment from the caller's data alone.
629
+ const createCommentSeededFromCache = (commentCid, account, cachedCommentData, commentData) => __awaiter(void 0, void 0, void 0, function* () {
630
+ if (cachedCommentData) {
631
+ try {
632
+ return yield account.pkc.createComment(normalizeCommentCommunityAddress(mergeCommentData(commentCid, cachedCommentData, commentData)));
633
+ }
634
+ catch (error) {
635
+ console.error("failed pkc.createComment(cachedComment)", {
636
+ cachedComment: cachedCommentData,
637
+ error,
638
+ });
639
+ }
640
+ }
641
+ return account.pkc.createComment(normalizeCommentCommunityAddress(mergeCommentData(commentCid, commentData)));
642
+ });
509
643
  // reset store in between tests
510
644
  const originalState = commentsStore.getState();
511
645
  // async function because some stores have async init
@@ -514,6 +648,9 @@ export const resetCommentsStore = () => __awaiter(void 0, void 0, void 0, functi
514
648
  for (const commentCid in commentAutoUpdateSubscribers) {
515
649
  delete commentAutoUpdateSubscribers[commentCid];
516
650
  }
651
+ for (const commentCid in openingUpdateWaiters) {
652
+ delete openingUpdateWaiters[commentCid];
653
+ }
517
654
  for (const commentCid in stopCommentAfterNextUpdate) {
518
655
  delete stopCommentAfterNextUpdate[commentCid];
519
656
  }
@@ -523,9 +660,21 @@ export const resetCommentsStore = () => __awaiter(void 0, void 0, void 0, functi
523
660
  for (const commentCid in commentUpdateAttemptFailed) {
524
661
  delete commentUpdateAttemptFailed[commentCid];
525
662
  }
663
+ for (const commentCid in pendingRefreshes) {
664
+ delete pendingRefreshes[commentCid];
665
+ }
666
+ for (const commentCid in refreshesOutlivedSubscribers) {
667
+ delete refreshesOutlivedSubscribers[commentCid];
668
+ }
669
+ for (const commentCid in oneShotUpdateConsumersLeft) {
670
+ delete oneShotUpdateConsumersLeft[commentCid];
671
+ }
526
672
  for (const commentCid in liveCommentPromises) {
527
673
  delete liveCommentPromises[commentCid];
528
674
  }
675
+ for (const commentCid in liveCommentStops) {
676
+ delete liveCommentStops[commentCid];
677
+ }
529
678
  for (const commentCid in liveComments) {
530
679
  delete liveComments[commentCid];
531
680
  }