@bitsocial/bitsocial-react-hooks 0.1.0

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 (269) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +1365 -0
  3. package/dist/hooks/accounts/accounts.d.ts +64 -0
  4. package/dist/hooks/accounts/accounts.d.ts.map +1 -0
  5. package/dist/hooks/accounts/accounts.js +706 -0
  6. package/dist/hooks/accounts/accounts.js.map +1 -0
  7. package/dist/hooks/accounts/index.d.ts +2 -0
  8. package/dist/hooks/accounts/index.d.ts.map +1 -0
  9. package/dist/hooks/accounts/index.js +2 -0
  10. package/dist/hooks/accounts/index.js.map +1 -0
  11. package/dist/hooks/accounts/utils.d.ts +6 -0
  12. package/dist/hooks/accounts/utils.d.ts.map +1 -0
  13. package/dist/hooks/accounts/utils.js +226 -0
  14. package/dist/hooks/accounts/utils.js.map +1 -0
  15. package/dist/hooks/actions/actions.d.ts +19 -0
  16. package/dist/hooks/actions/actions.d.ts.map +1 -0
  17. package/dist/hooks/actions/actions.js +552 -0
  18. package/dist/hooks/actions/actions.js.map +1 -0
  19. package/dist/hooks/actions/index.d.ts +2 -0
  20. package/dist/hooks/actions/index.d.ts.map +1 -0
  21. package/dist/hooks/actions/index.js +2 -0
  22. package/dist/hooks/actions/index.js.map +1 -0
  23. package/dist/hooks/authors/author-avatars.d.ts +28 -0
  24. package/dist/hooks/authors/author-avatars.d.ts.map +1 -0
  25. package/dist/hooks/authors/author-avatars.js +191 -0
  26. package/dist/hooks/authors/author-avatars.js.map +1 -0
  27. package/dist/hooks/authors/authors.d.ts +37 -0
  28. package/dist/hooks/authors/authors.d.ts.map +1 -0
  29. package/dist/hooks/authors/authors.js +509 -0
  30. package/dist/hooks/authors/authors.js.map +1 -0
  31. package/dist/hooks/authors/index.d.ts +2 -0
  32. package/dist/hooks/authors/index.d.ts.map +1 -0
  33. package/dist/hooks/authors/index.js +2 -0
  34. package/dist/hooks/authors/index.js.map +1 -0
  35. package/dist/hooks/authors/utils.d.ts +4 -0
  36. package/dist/hooks/authors/utils.d.ts.map +1 -0
  37. package/dist/hooks/authors/utils.js +21 -0
  38. package/dist/hooks/authors/utils.js.map +1 -0
  39. package/dist/hooks/comments.d.ts +17 -0
  40. package/dist/hooks/comments.d.ts.map +1 -0
  41. package/dist/hooks/comments.js +351 -0
  42. package/dist/hooks/comments.js.map +1 -0
  43. package/dist/hooks/communities.d.ts +31 -0
  44. package/dist/hooks/communities.d.ts.map +1 -0
  45. package/dist/hooks/communities.js +389 -0
  46. package/dist/hooks/communities.js.map +1 -0
  47. package/dist/hooks/feeds/feeds.d.ts +18 -0
  48. package/dist/hooks/feeds/feeds.d.ts.map +1 -0
  49. package/dist/hooks/feeds/feeds.js +315 -0
  50. package/dist/hooks/feeds/feeds.js.map +1 -0
  51. package/dist/hooks/feeds/index.d.ts +2 -0
  52. package/dist/hooks/feeds/index.d.ts.map +1 -0
  53. package/dist/hooks/feeds/index.js +2 -0
  54. package/dist/hooks/feeds/index.js.map +1 -0
  55. package/dist/hooks/pkc-rpc.d.ts +7 -0
  56. package/dist/hooks/pkc-rpc.d.ts.map +1 -0
  57. package/dist/hooks/pkc-rpc.js +88 -0
  58. package/dist/hooks/pkc-rpc.js.map +1 -0
  59. package/dist/hooks/replies.d.ts +5 -0
  60. package/dist/hooks/replies.d.ts.map +1 -0
  61. package/dist/hooks/replies.js +155 -0
  62. package/dist/hooks/replies.js.map +1 -0
  63. package/dist/hooks/states.d.ts +15 -0
  64. package/dist/hooks/states.d.ts.map +1 -0
  65. package/dist/hooks/states.js +213 -0
  66. package/dist/hooks/states.js.map +1 -0
  67. package/dist/hooks/utils/use-interval.d.ts +3 -0
  68. package/dist/hooks/utils/use-interval.d.ts.map +1 -0
  69. package/dist/hooks/utils/use-interval.js +36 -0
  70. package/dist/hooks/utils/use-interval.js.map +1 -0
  71. package/dist/hooks/utils/use-previous.d.ts +1 -0
  72. package/dist/hooks/utils/use-previous.js +10 -0
  73. package/dist/index.d.ts +82 -0
  74. package/dist/index.d.ts.map +1 -0
  75. package/dist/index.js +128 -0
  76. package/dist/index.js.map +1 -0
  77. package/dist/lib/chain/chain.d.ts +36 -0
  78. package/dist/lib/chain/chain.d.ts.map +1 -0
  79. package/dist/lib/chain/chain.js +195 -0
  80. package/dist/lib/chain/chain.js.map +1 -0
  81. package/dist/lib/chain/index.d.ts +4 -0
  82. package/dist/lib/chain/index.d.ts.map +1 -0
  83. package/dist/lib/chain/index.js +4 -0
  84. package/dist/lib/chain/index.js.map +1 -0
  85. package/dist/lib/community-address.d.ts +6 -0
  86. package/dist/lib/community-address.d.ts.map +1 -0
  87. package/dist/lib/community-address.js +26 -0
  88. package/dist/lib/community-address.js.map +1 -0
  89. package/dist/lib/community-ref.d.ts +23 -0
  90. package/dist/lib/community-ref.d.ts.map +1 -0
  91. package/dist/lib/community-ref.js +113 -0
  92. package/dist/lib/community-ref.js.map +1 -0
  93. package/dist/lib/debug-utils.d.ts +9 -0
  94. package/dist/lib/debug-utils.d.ts.map +1 -0
  95. package/dist/lib/debug-utils.js +21 -0
  96. package/dist/lib/debug-utils.js.map +1 -0
  97. package/dist/lib/feed-sort-type.d.ts +2 -0
  98. package/dist/lib/feed-sort-type.d.ts.map +1 -0
  99. package/dist/lib/feed-sort-type.js +22 -0
  100. package/dist/lib/feed-sort-type.js.map +1 -0
  101. package/dist/lib/localforage-lru/index.d.ts +3 -0
  102. package/dist/lib/localforage-lru/index.d.ts.map +1 -0
  103. package/dist/lib/localforage-lru/index.js +46 -0
  104. package/dist/lib/localforage-lru/index.js.map +1 -0
  105. package/dist/lib/localforage-lru/localforage-lru.d.ts +6 -0
  106. package/dist/lib/localforage-lru/localforage-lru.d.ts.map +1 -0
  107. package/dist/lib/localforage-lru/localforage-lru.js +182 -0
  108. package/dist/lib/localforage-lru/localforage-lru.js.map +1 -0
  109. package/dist/lib/pkc-compat.d.ts +25 -0
  110. package/dist/lib/pkc-compat.d.ts.map +1 -0
  111. package/dist/lib/pkc-compat.js +131 -0
  112. package/dist/lib/pkc-compat.js.map +1 -0
  113. package/dist/lib/pkc-js/fixtures/markdown-example.d.ts +3 -0
  114. package/dist/lib/pkc-js/fixtures/markdown-example.d.ts.map +1 -0
  115. package/dist/lib/pkc-js/fixtures/markdown-example.js +280 -0
  116. package/dist/lib/pkc-js/fixtures/markdown-example.js.map +1 -0
  117. package/dist/lib/pkc-js/index.d.ts +11 -0
  118. package/dist/lib/pkc-js/index.d.ts.map +1 -0
  119. package/dist/lib/pkc-js/index.js +85 -0
  120. package/dist/lib/pkc-js/index.js.map +1 -0
  121. package/dist/lib/pkc-js/pkc-js-mock-content.d.ts +3 -0
  122. package/dist/lib/pkc-js/pkc-js-mock-content.d.ts.map +1 -0
  123. package/dist/lib/pkc-js/pkc-js-mock-content.js +1235 -0
  124. package/dist/lib/pkc-js/pkc-js-mock-content.js.map +1 -0
  125. package/dist/lib/pkc-js/pkc-js-mock.d.ts +137 -0
  126. package/dist/lib/pkc-js/pkc-js-mock.d.ts.map +1 -0
  127. package/dist/lib/pkc-js/pkc-js-mock.js +644 -0
  128. package/dist/lib/pkc-js/pkc-js-mock.js.map +1 -0
  129. package/dist/lib/polyfill.d.ts +3 -0
  130. package/dist/lib/polyfill.d.ts.map +1 -0
  131. package/dist/lib/polyfill.js +14 -0
  132. package/dist/lib/polyfill.js.map +1 -0
  133. package/dist/lib/protocol-compat.d.ts +14 -0
  134. package/dist/lib/protocol-compat.d.ts.map +1 -0
  135. package/dist/lib/protocol-compat.js +67 -0
  136. package/dist/lib/protocol-compat.js.map +1 -0
  137. package/dist/lib/test-utils.d.ts +29 -0
  138. package/dist/lib/test-utils.d.ts.map +1 -0
  139. package/dist/lib/test-utils.js +184 -0
  140. package/dist/lib/test-utils.js.map +1 -0
  141. package/dist/lib/utils/comment-moderation.d.ts +4 -0
  142. package/dist/lib/utils/comment-moderation.d.ts.map +1 -0
  143. package/dist/lib/utils/comment-moderation.js +56 -0
  144. package/dist/lib/utils/comment-moderation.js.map +1 -0
  145. package/dist/lib/utils/index.d.ts +4 -0
  146. package/dist/lib/utils/index.d.ts.map +1 -0
  147. package/dist/lib/utils/index.js +4 -0
  148. package/dist/lib/utils/index.js.map +1 -0
  149. package/dist/lib/utils/utils.d.ts +23 -0
  150. package/dist/lib/utils/utils.d.ts.map +1 -0
  151. package/dist/lib/utils/utils.js +375 -0
  152. package/dist/lib/utils/utils.js.map +1 -0
  153. package/dist/lib/validator.d.ts +30 -0
  154. package/dist/lib/validator.d.ts.map +1 -0
  155. package/dist/lib/validator.js +307 -0
  156. package/dist/lib/validator.js.map +1 -0
  157. package/dist/stores/accounts/account-generator.d.ts +51 -0
  158. package/dist/stores/accounts/account-generator.d.ts.map +1 -0
  159. package/dist/stores/accounts/account-generator.js +160 -0
  160. package/dist/stores/accounts/account-generator.js.map +1 -0
  161. package/dist/stores/accounts/accounts-actions-internal.d.ts +8 -0
  162. package/dist/stores/accounts/accounts-actions-internal.d.ts.map +1 -0
  163. package/dist/stores/accounts/accounts-actions-internal.js +403 -0
  164. package/dist/stores/accounts/accounts-actions-internal.js.map +1 -0
  165. package/dist/stores/accounts/accounts-actions.d.ts +46 -0
  166. package/dist/stores/accounts/accounts-actions.d.ts.map +1 -0
  167. package/dist/stores/accounts/accounts-actions.js +1341 -0
  168. package/dist/stores/accounts/accounts-actions.js.map +1 -0
  169. package/dist/stores/accounts/accounts-database.d.ts +34 -0
  170. package/dist/stores/accounts/accounts-database.d.ts.map +1 -0
  171. package/dist/stores/accounts/accounts-database.js +685 -0
  172. package/dist/stores/accounts/accounts-database.js.map +1 -0
  173. package/dist/stores/accounts/accounts-store.d.ts +32 -0
  174. package/dist/stores/accounts/accounts-store.d.ts.map +1 -0
  175. package/dist/stores/accounts/accounts-store.js +169 -0
  176. package/dist/stores/accounts/accounts-store.js.map +1 -0
  177. package/dist/stores/accounts/index.d.ts +4 -0
  178. package/dist/stores/accounts/index.d.ts.map +1 -0
  179. package/dist/stores/accounts/index.js +4 -0
  180. package/dist/stores/accounts/index.js.map +1 -0
  181. package/dist/stores/accounts/utils.d.ts +49 -0
  182. package/dist/stores/accounts/utils.d.ts.map +1 -0
  183. package/dist/stores/accounts/utils.js +419 -0
  184. package/dist/stores/accounts/utils.js.map +1 -0
  185. package/dist/stores/authors-comments/authors-comments-store.d.ts +37 -0
  186. package/dist/stores/authors-comments/authors-comments-store.d.ts.map +1 -0
  187. package/dist/stores/authors-comments/authors-comments-store.js +338 -0
  188. package/dist/stores/authors-comments/authors-comments-store.js.map +1 -0
  189. package/dist/stores/authors-comments/index.d.ts +4 -0
  190. package/dist/stores/authors-comments/index.d.ts.map +1 -0
  191. package/dist/stores/authors-comments/index.js +4 -0
  192. package/dist/stores/authors-comments/index.js.map +1 -0
  193. package/dist/stores/authors-comments/utils.d.ts +14 -0
  194. package/dist/stores/authors-comments/utils.d.ts.map +1 -0
  195. package/dist/stores/authors-comments/utils.js +81 -0
  196. package/dist/stores/authors-comments/utils.js.map +1 -0
  197. package/dist/stores/comments/comments-store.d.ts +19 -0
  198. package/dist/stores/comments/comments-store.d.ts.map +1 -0
  199. package/dist/stores/comments/comments-store.js +385 -0
  200. package/dist/stores/comments/comments-store.js.map +1 -0
  201. package/dist/stores/comments/index.d.ts +4 -0
  202. package/dist/stores/comments/index.d.ts.map +1 -0
  203. package/dist/stores/comments/index.js +4 -0
  204. package/dist/stores/comments/index.js.map +1 -0
  205. package/dist/stores/communities/communities-store.d.ts +17 -0
  206. package/dist/stores/communities/communities-store.d.ts.map +1 -0
  207. package/dist/stores/communities/communities-store.js +304 -0
  208. package/dist/stores/communities/communities-store.js.map +1 -0
  209. package/dist/stores/communities/index.d.ts +4 -0
  210. package/dist/stores/communities/index.d.ts.map +1 -0
  211. package/dist/stores/communities/index.js +4 -0
  212. package/dist/stores/communities/index.js.map +1 -0
  213. package/dist/stores/communities-pages/communities-pages-store.d.ts +23 -0
  214. package/dist/stores/communities-pages/communities-pages-store.d.ts.map +1 -0
  215. package/dist/stores/communities-pages/communities-pages-store.js +316 -0
  216. package/dist/stores/communities-pages/communities-pages-store.js.map +1 -0
  217. package/dist/stores/communities-pages/index.d.ts +4 -0
  218. package/dist/stores/communities-pages/index.d.ts.map +1 -0
  219. package/dist/stores/communities-pages/index.js +4 -0
  220. package/dist/stores/communities-pages/index.js.map +1 -0
  221. package/dist/stores/feeds/feed-sorter.d.ts +5 -0
  222. package/dist/stores/feeds/feed-sorter.d.ts.map +1 -0
  223. package/dist/stores/feeds/feed-sorter.js +135 -0
  224. package/dist/stores/feeds/feed-sorter.js.map +1 -0
  225. package/dist/stores/feeds/feeds-store.d.ts +25 -0
  226. package/dist/stores/feeds/feeds-store.d.ts.map +1 -0
  227. package/dist/stores/feeds/feeds-store.js +459 -0
  228. package/dist/stores/feeds/feeds-store.js.map +1 -0
  229. package/dist/stores/feeds/index.d.ts +4 -0
  230. package/dist/stores/feeds/index.d.ts.map +1 -0
  231. package/dist/stores/feeds/index.js +4 -0
  232. package/dist/stores/feeds/index.js.map +1 -0
  233. package/dist/stores/feeds/utils.d.ts +43 -0
  234. package/dist/stores/feeds/utils.d.ts.map +1 -0
  235. package/dist/stores/feeds/utils.js +736 -0
  236. package/dist/stores/feeds/utils.js.map +1 -0
  237. package/dist/stores/replies/index.d.ts +4 -0
  238. package/dist/stores/replies/index.d.ts.map +1 -0
  239. package/dist/stores/replies/index.js +4 -0
  240. package/dist/stores/replies/index.js.map +1 -0
  241. package/dist/stores/replies/replies-comments-store.d.ts +8 -0
  242. package/dist/stores/replies/replies-comments-store.d.ts.map +1 -0
  243. package/dist/stores/replies/replies-comments-store.js +23 -0
  244. package/dist/stores/replies/replies-comments-store.js.map +1 -0
  245. package/dist/stores/replies/replies-store.d.ts +29 -0
  246. package/dist/stores/replies/replies-store.d.ts.map +1 -0
  247. package/dist/stores/replies/replies-store.js +413 -0
  248. package/dist/stores/replies/replies-store.js.map +1 -0
  249. package/dist/stores/replies/utils.d.ts +25 -0
  250. package/dist/stores/replies/utils.d.ts.map +1 -0
  251. package/dist/stores/replies/utils.js +549 -0
  252. package/dist/stores/replies/utils.js.map +1 -0
  253. package/dist/stores/replies-pages/index.d.ts +4 -0
  254. package/dist/stores/replies-pages/index.d.ts.map +1 -0
  255. package/dist/stores/replies-pages/index.js +4 -0
  256. package/dist/stores/replies-pages/index.js.map +1 -0
  257. package/dist/stores/replies-pages/replies-pages-store.d.ts +20 -0
  258. package/dist/stores/replies-pages/replies-pages-store.d.ts.map +1 -0
  259. package/dist/stores/replies-pages/replies-pages-store.js +270 -0
  260. package/dist/stores/replies-pages/replies-pages-store.js.map +1 -0
  261. package/dist/stores/replies-pages/utils.d.ts +3 -0
  262. package/dist/stores/replies-pages/utils.d.ts.map +1 -0
  263. package/dist/stores/replies-pages/utils.js +43 -0
  264. package/dist/stores/replies-pages/utils.js.map +1 -0
  265. package/dist/types.d.ts +638 -0
  266. package/dist/types.d.ts.map +1 -0
  267. package/dist/types.js +3 -0
  268. package/dist/types.js.map +1 -0
  269. package/package.json +160 -0
@@ -0,0 +1,338 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import Logger from "@pkcprotocol/pkc-logger";
11
+ const log = Logger("bitsocial-react-hooks:authors:stores");
12
+ import createStore from "zustand";
13
+ import assert from "assert";
14
+ import commentsStore from "../comments/index.js";
15
+ import QuickLru from "quick-lru";
16
+ import { getUpdatedLoadedAndBufferedComments, getNextCommentCidToFetchNotFetched, } from "./utils.js";
17
+ import accountsStore from "../accounts/index.js";
18
+ // reddit loads approximately 25 posts per page while infinite scrolling
19
+ export const commentsPerPage = 25;
20
+ // keep large buffer because fetching cids is slow
21
+ export const commentBufferSize = 50;
22
+ const authorsCommentsStore = createStore((setState, getState) => ({
23
+ options: {},
24
+ loadedComments: {},
25
+ hasMoreBufferedComments: {},
26
+ bufferedCommentCids: {},
27
+ lastCommentCids: {},
28
+ nextCommentCidsToFetch: {},
29
+ shouldFetchNextComment: {},
30
+ addAuthorCommentsToStore: (authorCommentsName, authorAddress, commentCid, filter, account) => {
31
+ var _a;
32
+ assert(authorCommentsName && typeof authorCommentsName === "string", `addAuthorCommentsToStore.incrementPageNumber invalid argument authorCommentsName '${authorCommentsName}'`);
33
+ assert(authorAddress && typeof authorAddress === "string", `authorsCommentsStore.addAuthorCommentsToStore invalid argument authorAddress '${authorAddress}'`);
34
+ assert(commentCid && typeof commentCid === "string", `authorsCommentsStore.addAuthorCommentsToStore invalid argument commentCid '${commentCid}'`);
35
+ assert(!filter || typeof (filter === null || filter === void 0 ? void 0 : filter.filter) === "function", `authorsCommentsStore.addAuthorCommentsToStore invalid argument filter.filter '${filter === null || filter === void 0 ? void 0 : filter.filter}'`);
36
+ assert(!filter || typeof (filter === null || filter === void 0 ? void 0 : filter.key) === "string", `authorsCommentsStore.addAuthorCommentsToStore invalid argument filter.key '${filter === null || filter === void 0 ? void 0 : filter.key}'`);
37
+ assert(typeof ((_a = account === null || account === void 0 ? void 0 : account.pkc) === null || _a === void 0 ? void 0 : _a.getComment) === "function", `authorsCommentsStore.addAuthorCommentsToStore account '${account}' invalid`);
38
+ const { options, updateLoadedComments } = getState();
39
+ // in store already, do nothing
40
+ if (options[authorCommentsName]) {
41
+ return;
42
+ }
43
+ const authorCommentsOptions = { authorAddress, pageNumber: 1, filter, accountId: account.id };
44
+ // subscribe to nextCommentCidsToFetch and shouldFetchNextComment to fetch the comments
45
+ authorsCommentsStore.subscribe(fetchCommentOnShouldFetchOrNextCidChange(authorCommentsOptions));
46
+ log("authorsCommentsActions.addAuthorCommentsToStore", {
47
+ authorCommentsName,
48
+ authorCommentsOptions,
49
+ commentCid,
50
+ previousAuthorsCommentsOptions: options,
51
+ });
52
+ setState((state) => ({
53
+ options: Object.assign(Object.assign({}, state.options), { [authorCommentsName]: authorCommentsOptions }),
54
+ loadedComments: Object.assign(Object.assign({}, state.loadedComments), { [authorCommentsName]: [] }),
55
+ hasMoreBufferedComments: Object.assign(Object.assign({}, state.hasMoreBufferedComments), { [authorCommentsName]: true }),
56
+ bufferedCommentCids: Object.assign(Object.assign({}, state.bufferedCommentCids), { [authorAddress]: state.bufferedCommentCids[authorAddress] || new Set() }),
57
+ lastCommentCids: Object.assign(Object.assign({}, state.lastCommentCids), { [authorAddress]: state.lastCommentCids[authorAddress] || undefined }),
58
+ nextCommentCidsToFetch: Object.assign(Object.assign({}, state.nextCommentCidsToFetch), { [authorAddress]: state.nextCommentCidsToFetch[authorAddress] || commentCid }),
59
+ shouldFetchNextComment: Object.assign(Object.assign({}, state.shouldFetchNextComment), { [authorAddress]: state.shouldFetchNextComment[authorAddress] || true }),
60
+ }));
61
+ // update loadedComments in case the author already has bufferedCommentCids
62
+ updateLoadedComments();
63
+ },
64
+ setNextCommentCidsToFetch: (authorAddress, authorComment) => {
65
+ var _a;
66
+ assert(authorAddress && typeof authorAddress === "string", `authorsCommentsActions.setNextCommentCidsToFetch invalid argument authorAddress '${authorAddress}'`);
67
+ assert(typeof (authorComment === null || authorComment === void 0 ? void 0 : authorComment.timestamp) === "number", `authorsCommentsActions.setNextCommentCidsToFetch invalid argument authorComment '${authorComment}'`);
68
+ const { nextCommentCidsToFetch, shouldFetchNextComment, lastCommentCids } = getState();
69
+ if (typeof shouldFetchNextComment[authorAddress] !== "boolean") {
70
+ throw Error(`authorsCommentsActions.setNextCommentCidsToFetch can't set nextCommentCidToFetch '${authorAddress}' not in store`);
71
+ }
72
+ const nextCommentCidToFetch = (_a = authorComment === null || authorComment === void 0 ? void 0 : authorComment.author) === null || _a === void 0 ? void 0 : _a.previousCommentCid;
73
+ if (nextCommentCidToFetch === nextCommentCidsToFetch[authorAddress]) {
74
+ throw Error(`authorsCommentsActions.setNextCommentCidsToFetch can't set nextCommentCidToFetch '${authorAddress}' to '${nextCommentCidToFetch}' same value`);
75
+ }
76
+ const nextCommentCidToFetchNotFetched = getNextCommentCidToFetchNotFetched(nextCommentCidToFetch);
77
+ // log.trace('authorsCommentsActions.setNextCommentCidsToFetch', {
78
+ // authorAddress,
79
+ // authorComment,
80
+ // previousNextCommentCidToFetch: nextCommentCidsToFetch[authorAddress],
81
+ // nextCommentCidToFetch,
82
+ // nextCommentCidToFetchNotFetched,
83
+ // lastCommentCid: lastCommentCids[authorAddress],
84
+ // shouldFetchNextComment: shouldFetchNextComment[authorAddress],
85
+ // })
86
+ setState((state) => ({
87
+ nextCommentCidsToFetch: Object.assign(Object.assign({}, state.nextCommentCidsToFetch), { [authorAddress]: nextCommentCidToFetchNotFetched }),
88
+ }));
89
+ },
90
+ incrementPageNumber: (authorCommentsName) => {
91
+ assert(authorCommentsName && typeof authorCommentsName === "string", `authorsCommentsActions.incrementPageNumber invalid argument authorCommentsName '${authorCommentsName}'`);
92
+ const { options, updateLoadedComments, loadedComments, nextCommentCidsToFetch } = getState();
93
+ if (!options[authorCommentsName]) {
94
+ throw Error(`authorsCommentsActions.incrementPageNumber can't increment page number of options '${authorCommentsName}' not in store`);
95
+ }
96
+ assert(options[authorCommentsName].pageNumber * commentsPerPage <=
97
+ loadedComments[authorCommentsName].length, `authorsCommentsActions.incrementPageNumber cannot increment page number before current page has loaded`);
98
+ log("authorsCommentsActions.incrementPageNumber", {
99
+ authorCommentsName,
100
+ pageNumber: options[authorCommentsName].pageNumber + 1,
101
+ nextCommentCidsToFetch: nextCommentCidsToFetch[options[authorCommentsName].authorAddress],
102
+ });
103
+ setState(({ options }) => {
104
+ const authorCommentOptions = Object.assign({}, options[authorCommentsName]);
105
+ authorCommentOptions.pageNumber++;
106
+ return { options: Object.assign(Object.assign({}, options), { [authorCommentsName]: authorCommentOptions }) };
107
+ });
108
+ // must update loadedComments to reflect the new added page
109
+ updateLoadedComments();
110
+ },
111
+ addBufferedCommentCid: (authorAddress, commentCid) => {
112
+ assert(authorAddress && typeof authorAddress === "string", `authorsCommentsActions.addBufferedCommentCid invalid argument authorAddress '${authorAddress}'`);
113
+ assert(commentCid && typeof commentCid === "string", `authorsCommentsActions.addBufferedCommentCid invalid argument commentCid '${commentCid}'`);
114
+ const { bufferedCommentCids } = getState();
115
+ if (!bufferedCommentCids[authorAddress]) {
116
+ throw Error(`authorsCommentsActions.addBufferedCommentCid can't add commentCid '${authorAddress}' not in store`);
117
+ }
118
+ if (bufferedCommentCids[authorAddress].has(commentCid)) {
119
+ throw Error(`authorsCommentsActions.addBufferedCommentCid can't add commentCid '${authorAddress}' '${commentCid}' already added`);
120
+ }
121
+ // log.trace('authorsCommentsActions.addBufferedCommentCid', {authorAddress, commentCid, previousBufferedCommentCidsSize: bufferedCommentCids[authorAddress].size})
122
+ setState((state) => ({
123
+ bufferedCommentCids: Object.assign(Object.assign({}, state.bufferedCommentCids), { [authorAddress]: new Set([...bufferedCommentCids[authorAddress], commentCid]) }),
124
+ }));
125
+ },
126
+ updateLoadedComments() {
127
+ const { comments } = commentsStore.getState();
128
+ let { loadedComments: previousAuthorsLoadedComments, bufferedCommentCids, options, nextCommentCidsToFetch, lastCommentCids, } = getState();
129
+ const newAuthorsLoadedComments = {};
130
+ const newShouldFetchNextComment = {};
131
+ const newHasMoreBufferedComments = {};
132
+ const authorCommentsNames = Object.keys(options);
133
+ for (const name of authorCommentsNames) {
134
+ const { authorAddress, pageNumber, filter } = options[name];
135
+ const previousLoadedComments = previousAuthorsLoadedComments[name];
136
+ const unfilteredBufferedComments = [...bufferedCommentCids[authorAddress]].map((commentCid) => comments[commentCid]);
137
+ const { loadedComments, bufferedComments: filteredBufferedComments } = getUpdatedLoadedAndBufferedComments(previousLoadedComments, unfilteredBufferedComments, pageNumber, filter, comments);
138
+ newAuthorsLoadedComments[name] = loadedComments;
139
+ newHasMoreBufferedComments[name] = filteredBufferedComments.length > loadedComments.length;
140
+ // if another authorCommentOptions should fetch, don't change it
141
+ if (newShouldFetchNextComment[authorAddress] !== true) {
142
+ // fetch if less comments than full page + buffer size
143
+ newShouldFetchNextComment[authorAddress] =
144
+ filteredBufferedComments.length < pageNumber * commentsPerPage + commentBufferSize;
145
+ }
146
+ }
147
+ // log.trace('authorsCommentsActions.updateLoadedComments', {
148
+ // bufferedCommentCids,
149
+ // bufferedCommentCidsSizes: toSizes(bufferedCommentCids),
150
+ // previousAuthorsLoadedComments,
151
+ // newAuthorsLoadedComments,
152
+ // previousAuthorsLoadedCommentsSizes: toSizes(previousAuthorsLoadedComments),
153
+ // newAuthorsLoadedCommentsSizes: toSizes(newAuthorsLoadedComments),
154
+ // newShouldFetchNextComment,
155
+ // lastCommentCids
156
+ // })
157
+ setState(() => ({
158
+ loadedComments: newAuthorsLoadedComments,
159
+ shouldFetchNextComment: newShouldFetchNextComment,
160
+ hasMoreBufferedComments: newHasMoreBufferedComments,
161
+ }));
162
+ },
163
+ setLastCommentCid: (authorAddress, lastCommentCid) => {
164
+ assert(authorAddress && typeof authorAddress === "string", `authorsCommentsActions.setLastCommentCid invalid argument authorAddress '${authorAddress}'`);
165
+ assert(lastCommentCid && typeof lastCommentCid === "string", `authorsCommentsActions.setLastCommentCid invalid argument lastCommentCid '${lastCommentCid}'`);
166
+ const { lastCommentCids, shouldFetchNextComment, nextCommentCidsToFetch } = getState();
167
+ if (typeof shouldFetchNextComment[authorAddress] !== "boolean") {
168
+ throw Error(`authorsCommentsActions.setLastCommentCid can't set lastCommentCid '${authorAddress}' not in store`);
169
+ }
170
+ if (lastCommentCid === lastCommentCids[authorAddress]) {
171
+ throw Error(`authorsCommentsActions.setLastCommentCid can't set setLastCommentCid '${authorAddress}' to '${lastCommentCid}' same value`);
172
+ }
173
+ log("authorsCommentsActions.setLastCommentCid", {
174
+ authorAddress,
175
+ lastCommentCid,
176
+ previousLastCommentCid: lastCommentCids[authorAddress],
177
+ shouldFetchNextComment: shouldFetchNextComment[authorAddress],
178
+ nextCommentCidsToFetch: nextCommentCidsToFetch[authorAddress],
179
+ });
180
+ setState((state) => ({
181
+ lastCommentCids: Object.assign(Object.assign({}, state.lastCommentCids), { [authorAddress]: lastCommentCid }),
182
+ }));
183
+ },
184
+ }));
185
+ // if nextCommentCidsToFetch or shouldFetchNextComment changed, fetch the next comment
186
+ const fetchCommentOnShouldFetchOrNextCidChange = (options) => (state) => {
187
+ const nextCommentCidToFetch = state.nextCommentCidsToFetch[options.authorAddress];
188
+ if (!nextCommentCidToFetch) {
189
+ return;
190
+ }
191
+ // the buffered comments are already full, not need to fetch next comment
192
+ const shouldFetchNextComment = state.shouldFetchNextComment[options.authorAddress];
193
+ if (!shouldFetchNextComment) {
194
+ return;
195
+ }
196
+ // when comment has fetched, update loadedComments, bufferedComments and shouldFetchNextComment
197
+ if (!authorCommentCidsFetching[nextCommentCidToFetch]) {
198
+ authorCommentCidsFetching[nextCommentCidToFetch] = true;
199
+ commentsStore.subscribe(updateCommentsOnCommentsChange(options, nextCommentCidToFetch));
200
+ }
201
+ // start fetching comment
202
+ const account = accountsStore.getState().accounts[options.accountId];
203
+ const addCommentToStore = commentsStore.getState().addCommentToStore;
204
+ addCommentToStore(nextCommentCidToFetch, account).catch((error) => log.error("authorsCommentsStore fetchCommentOnShouldFetchOrNextCidChange addCommentToStore error", { error, nextCommentCidToFetch, account }));
205
+ };
206
+ // if commentStore changed, update loadedComments, bufferedCommentCids, shouldFetchNextComment and nextCommentCidsToFetch
207
+ let previousComments = new QuickLru({ maxSize: 10000 });
208
+ let authorCommentCidsFetching = {};
209
+ let communityLastCommentCidsFetching = {};
210
+ const updateCommentsOnCommentsChange = (options, commentCid) => (state) => {
211
+ var _a, _b, _c;
212
+ // not a next cid, do nothing
213
+ if (!authorCommentCidsFetching[commentCid]) {
214
+ return;
215
+ }
216
+ const comment = state.comments[commentCid];
217
+ // comment hasn't changed, do nothing
218
+ if (!(comment === null || comment === void 0 ? void 0 : comment.timestamp) || comment === previousComments.get(commentCid)) {
219
+ return;
220
+ }
221
+ // comment author address is incorrect, do nothing
222
+ if (((_a = comment.author) === null || _a === void 0 ? void 0 : _a.address) !== options.authorAddress) {
223
+ return;
224
+ }
225
+ previousComments.set(commentCid, comment);
226
+ const { addBufferedCommentCid, bufferedCommentCids, updateLoadedComments, setNextCommentCidsToFetch, nextCommentCidsToFetch, } = authorsCommentsStore.getState();
227
+ // the comment is a new comment, add it to buffered comment cids
228
+ if (!bufferedCommentCids[options.authorAddress].has(commentCid)) {
229
+ addBufferedCommentCid(options.authorAddress, commentCid);
230
+ }
231
+ // the comment was the last cid to fetch, set the next cid to fetch as the author previous cid
232
+ const nextCidToFetch = nextCommentCidsToFetch[options.authorAddress];
233
+ if (commentCid === nextCidToFetch) {
234
+ setNextCommentCidsToFetch(options.authorAddress, comment);
235
+ }
236
+ // one of the comment changed, must update loaded comments
237
+ updateLoadedComments();
238
+ // the changed comment might have a new author.community.lastCommentCid, try to fetch it
239
+ const communityLastCommentCid = (_c = (_b = comment.author) === null || _b === void 0 ? void 0 : _b.community) === null || _c === void 0 ? void 0 : _c.lastCommentCid;
240
+ if (communityLastCommentCid) {
241
+ // when last comment has fetched, update lastCommentCid
242
+ if (!communityLastCommentCidsFetching[communityLastCommentCid]) {
243
+ communityLastCommentCidsFetching[communityLastCommentCid] = true;
244
+ commentsStore.subscribe(setLastCommentCidOnCommentsChange(options, communityLastCommentCid));
245
+ }
246
+ // start fetching lastCommentCid
247
+ const account = accountsStore.getState().accounts[options.accountId];
248
+ state.addCommentToStore(communityLastCommentCid, account).catch((error) => log.error("authorsCommentsStore updateCommentsOnCommentsChange addCommentToStore error", {
249
+ error,
250
+ communityLastCommentCid,
251
+ account,
252
+ }));
253
+ }
254
+ };
255
+ let previousLastComments = new QuickLru({ maxSize: 10000 });
256
+ const setLastCommentCidOnCommentsChange = (options, commentCid) => (state) => {
257
+ var _a, _b;
258
+ // not a last cid candidate, do nothing
259
+ if (!communityLastCommentCidsFetching[commentCid]) {
260
+ return;
261
+ }
262
+ const { comments } = state;
263
+ const comment = comments[commentCid];
264
+ // comment hasn't changed, do nothing
265
+ if (!(comment === null || comment === void 0 ? void 0 : comment.timestamp) || comment === previousLastComments.get(commentCid)) {
266
+ return;
267
+ }
268
+ // comment author address is incorrect, do nothing
269
+ if (((_a = comment.author) === null || _a === void 0 ? void 0 : _a.address) !== options.authorAddress) {
270
+ return;
271
+ }
272
+ previousLastComments.set(commentCid, comment);
273
+ const { addBufferedCommentCid, lastCommentCids, bufferedCommentCids, setLastCommentCid, setNextCommentCidsToFetch, updateLoadedComments, } = authorsCommentsStore.getState();
274
+ // if the comment is a new comment, add it to buffered comment cids
275
+ if (!bufferedCommentCids[options.authorAddress].has(commentCid)) {
276
+ addBufferedCommentCid(options.authorAddress, commentCid);
277
+ }
278
+ // already last comment cid, no need to set it
279
+ if (commentCid === lastCommentCids[options.authorAddress]) {
280
+ return;
281
+ }
282
+ // if comment is newer than current lastCommentCid and all bufferedComments, is lastCommentCid
283
+ const currentLastCommentCid = lastCommentCids[options.authorAddress];
284
+ const currentLastComment = comments[currentLastCommentCid || ""];
285
+ // comment is older or equal to current lastCommentCid, do nothing
286
+ if (comment.timestamp <= ((currentLastComment === null || currentLastComment === void 0 ? void 0 : currentLastComment.timestamp) || 0)) {
287
+ log.trace(`authorsCommentsStore setLastCommentCidOnCommentsChange don't set lastCommentCid older than current lastCommentCid`, { comment, currentLastComment });
288
+ return;
289
+ }
290
+ // make sure lastComment is newer than all comments already in bufferedComments
291
+ const bufferedComments = [...bufferedCommentCids[options.authorAddress]].map((commentCid) => comments[commentCid]);
292
+ for (const bufferedComment of bufferedComments) {
293
+ const bufferedTs = bufferedComment == null || bufferedComment.timestamp == null
294
+ ? 0
295
+ : bufferedComment.timestamp;
296
+ if (bufferedTs > comment.timestamp) {
297
+ log.trace(`authorsCommentsStore setLastCommentCidOnCommentsChange don't set lastCommentCid older than buffered comments`, {
298
+ comment,
299
+ currentLastComment,
300
+ bufferedComments,
301
+ });
302
+ return;
303
+ }
304
+ }
305
+ // is last comment cid, set it
306
+ log(`authorsCommentsStore setLastCommentCidOnCommentsChange`, {
307
+ lastCommentCid: comment.cid,
308
+ lastComment: comment,
309
+ currentLastComment,
310
+ bufferedComments,
311
+ });
312
+ setLastCommentCid(options.authorAddress, commentCid);
313
+ // add the last comment to loadedComments
314
+ updateLoadedComments();
315
+ // start a new linked list of comments to fetch using the lastComment.author.previousCommentCid
316
+ if ((_b = comment.author) === null || _b === void 0 ? void 0 : _b.previousCommentCid) {
317
+ setNextCommentCidsToFetch(options.authorAddress, comment);
318
+ }
319
+ };
320
+ // reset store in between tests
321
+ const originalState = authorsCommentsStore.getState();
322
+ // async function because some stores have async init
323
+ export const resetAuthorsCommentsStore = () => __awaiter(void 0, void 0, void 0, function* () {
324
+ communityLastCommentCidsFetching = {};
325
+ previousComments = new QuickLru({ maxSize: 10000 });
326
+ authorCommentCidsFetching = {};
327
+ previousLastComments = new QuickLru({ maxSize: 10000 });
328
+ // destroy all component subscriptions to the store
329
+ authorsCommentsStore.destroy();
330
+ // restore original state
331
+ authorsCommentsStore.setState(originalState);
332
+ });
333
+ // reset database and store in between tests
334
+ export const resetAuthorsCommentsDatabaseAndStore = () => __awaiter(void 0, void 0, void 0, function* () {
335
+ yield resetAuthorsCommentsStore();
336
+ });
337
+ export default authorsCommentsStore;
338
+ //# sourceMappingURL=authors-comments-store.js.map
@@ -0,0 +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,QAAQ,CAAC;AAQ5B,OAA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AAG,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 \"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"]}
@@ -0,0 +1,4 @@
1
+ import authorsCommentsStore from "./authors-comments-store.js";
2
+ export * from "./authors-comments-store.js";
3
+ export default authorsCommentsStore;
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/stores/authors-comments/index.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,MAAM,0BAA0B,CAAC;AAC5D,cAAc,0BAA0B,CAAC;AACzC,eAAe,oBAAoB,CAAC"}
@@ -0,0 +1,4 @@
1
+ import authorsCommentsStore from "./authors-comments-store.js";
2
+ export * from "./authors-comments-store.js";
3
+ export default authorsCommentsStore;
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/stores/authors-comments/index.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,MAAM,0BAA0B,CAAC;AAC5D,cAAc,0BAA0B,CAAC;AACzC,eAAe,oBAAoB,CAAC","sourcesContent":["import authorsCommentsStore from \"./authors-comments-store\";\nexport * from \"./authors-comments-store\";\nexport default authorsCommentsStore;\n"]}
@@ -0,0 +1,14 @@
1
+ import { Comment, Comments, CommentsFilter } from "../../types.js";
2
+ export declare const getUpdatedLoadedAndBufferedComments: (loadedComments: Comment[], bufferedComments: Comment[], pageNumber: number, filter: CommentsFilter | undefined, comments: Comments) => {
3
+ loadedComments: Comment[];
4
+ bufferedComments: Comment[];
5
+ };
6
+ export declare const getUpdatedBufferedComments: (loadedComments: Comment[], bufferedComments: Comment[], filter: CommentsFilter | undefined, comments: Comments) => Comment[];
7
+ export declare const commentsHaveChanged: (comments1: Comment[], comments2: Comment[]) => boolean;
8
+ export declare const getNextCommentCidToFetchNotFetched: (nextCommentCidToFetch: string | undefined, maxAttemptForTest?: number) => string | undefined;
9
+ export declare const toSizes: (obj: {
10
+ [key: string]: any;
11
+ }) => {
12
+ [key: string]: number;
13
+ };
14
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/stores/authors-comments/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAKhE,eAAO,MAAM,mCAAmC,GAC9C,gBAAgB,OAAO,EAAE,EACzB,kBAAkB,OAAO,EAAE,EAC3B,YAAY,MAAM,EAClB,QAAQ,cAAc,GAAG,SAAS,EAClC,UAAU,QAAQ;;;CAmBnB,CAAC;AAEF,eAAO,MAAM,0BAA0B,GACrC,gBAAgB,OAAO,EAAE,EACzB,kBAAkB,OAAO,EAAE,EAC3B,QAAQ,cAAc,GAAG,SAAS,EAClC,UAAU,QAAQ,cA+BnB,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAI,WAAW,OAAO,EAAE,EAAE,WAAW,OAAO,EAAE,YAa7E,CAAC;AAIF,eAAO,MAAM,kCAAkC,GAC7C,uBAAuB,MAAM,GAAG,SAAS,EACzC,oBAAoB,MAAM,uBAkB3B,CAAC;AAGF,eAAO,MAAM,OAAO,GAAI,KAAK;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE;;CAMlD,CAAC"}
@@ -0,0 +1,81 @@
1
+ import { commentsPerPage } from "./authors-comments-store.js";
2
+ import commentsStore from "../comments/index.js";
3
+ export const getUpdatedLoadedAndBufferedComments = (loadedComments, bufferedComments, pageNumber, filter, comments) => {
4
+ const newBufferedComments = getUpdatedBufferedComments(loadedComments, bufferedComments, filter, comments);
5
+ // create new loaded comments using the page number and buffered comments
6
+ let newLoadedComments = newBufferedComments.slice(0, pageNumber * commentsPerPage);
7
+ // check if loadedComments have changed
8
+ // don't return a new object if there's no change, to avoid rerender
9
+ if (!commentsHaveChanged(loadedComments, newLoadedComments)) {
10
+ newLoadedComments = loadedComments;
11
+ }
12
+ return { loadedComments: newLoadedComments, bufferedComments: newBufferedComments };
13
+ };
14
+ export const getUpdatedBufferedComments = (loadedComments, bufferedComments, filter, comments) => {
15
+ // get previous loaded comment cids
16
+ const previousLoadedCommentCids = {};
17
+ for (const comment of loadedComments) {
18
+ previousLoadedCommentCids[comment.cid] = true;
19
+ }
20
+ // get buffered comments without loaded cids
21
+ let newBufferedComments = bufferedComments.filter((comment) => !previousLoadedCommentCids[comment.cid]);
22
+ // filter buffered comments
23
+ if (filter) {
24
+ newBufferedComments = newBufferedComments.filter(filter.filter);
25
+ }
26
+ // sort buffered comments by timestamp (newest first)
27
+ newBufferedComments.sort((a, b) => b.timestamp - a.timestamp);
28
+ // append the (new updated) loaded comments to buffered comments
29
+ for (const comment of [...loadedComments].reverse()) {
30
+ const updatedComment = comments[comment.cid];
31
+ newBufferedComments.unshift(updatedComment);
32
+ }
33
+ // no need to check if comments have changed because getUpdatedBufferedComments
34
+ // is not used anywhere outside of tests
35
+ return newBufferedComments;
36
+ };
37
+ export const commentsHaveChanged = (comments1, comments2) => {
38
+ if (comments1 === comments2) {
39
+ return false;
40
+ }
41
+ if (comments1.length !== comments2.length) {
42
+ return true;
43
+ }
44
+ for (const i in comments1) {
45
+ if (comments1[i] !== comments2[i]) {
46
+ return true;
47
+ }
48
+ }
49
+ return false;
50
+ };
51
+ // if comment already exist, find the actual nextCidToFetch
52
+ // can happen if a more recent lastCommentCid becomes nextCommentCidToFetch
53
+ export const getNextCommentCidToFetchNotFetched = (nextCommentCidToFetch, maxAttemptForTest) => {
54
+ var _a;
55
+ const { comments } = commentsStore.getState();
56
+ let nextCommentCidToFetchNotFetched = nextCommentCidToFetch;
57
+ // scroll through comments until the comment doesn't exist, which means hasn't been fetched yet
58
+ let maxAttempt = maxAttemptForTest !== null && maxAttemptForTest !== void 0 ? maxAttemptForTest : 99999999;
59
+ while (true) {
60
+ // can't happen in production because of hashing, but can happen in tests
61
+ if (!maxAttempt--) {
62
+ throw Error(`getNextCommentCidToFetchNotFetched '${nextCommentCidToFetch}' infinite loop`);
63
+ }
64
+ const comment = comments[nextCommentCidToFetchNotFetched || ""];
65
+ if (!(comment === null || comment === void 0 ? void 0 : comment.timestamp)) {
66
+ break;
67
+ }
68
+ nextCommentCidToFetchNotFetched = (_a = comment.author) === null || _a === void 0 ? void 0 : _a.previousCommentCid;
69
+ }
70
+ return nextCommentCidToFetchNotFetched;
71
+ };
72
+ // util for debugging sizes of object of arrays/sets
73
+ export const toSizes = (obj) => {
74
+ var _a;
75
+ const newObj = {};
76
+ for (const i in obj) {
77
+ newObj[i] = (_a = obj[i].length) !== null && _a !== void 0 ? _a : obj[i].size;
78
+ }
79
+ return newObj;
80
+ };
81
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot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,iBAAiB,cAAjB,iBAAiB,GAAI,QAAQ,CAAC;IAC/C,OAAO,IAAI,EAAE,CAAC;QACZ,yEAAyE;QACzE,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;YAClB,MAAM,KAAK,CAAC,uCAAuC,qBAAqB,iBAAiB,CAAC,CAAC;QAC7F,CAAC;QACD,MAAM,OAAO,GAAG,QAAQ,CAAC,+BAA+B,IAAI,EAAE,CAAC,CAAC;QAChE,IAAI,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS,CAAA,EAAE,CAAC;YACxB,MAAM;QACR,CAAC;QACD,+BAA+B,GAAG,MAAA,OAAO,CAAC,MAAM,0CAAE,kBAAkB,CAAC;IACvE,CAAC;IACD,OAAO,+BAA+B,CAAC;AACzC,CAAC,CAAC;AAEF,oDAAoD;AACpD,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,GAA2B,EAAE,EAAE;;IACrD,MAAM,MAAM,GAA8B,EAAE,CAAC;IAC7C,KAAK,MAAM,CAAC,IAAI,GAAG,EAAE,CAAC;QACpB,MAAM,CAAC,CAAC,CAAC,GAAG,MAAA,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,mCAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAC3C,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC","sourcesContent":["import { Comment, Comments, CommentsFilter } from \"../../types\";\nimport { commentsPerPage } from \"./authors-comments-store\";\nimport assert from \"assert\";\nimport commentsStore from \"../comments\";\n\nexport const getUpdatedLoadedAndBufferedComments = (\n loadedComments: Comment[],\n bufferedComments: Comment[],\n pageNumber: number,\n filter: CommentsFilter | undefined,\n comments: Comments,\n) => {\n const newBufferedComments = getUpdatedBufferedComments(\n loadedComments,\n bufferedComments,\n filter,\n comments,\n );\n\n // create new loaded comments using the page number and buffered comments\n let newLoadedComments = newBufferedComments.slice(0, pageNumber * commentsPerPage);\n\n // check if loadedComments have changed\n // don't return a new object if there's no change, to avoid rerender\n if (!commentsHaveChanged(loadedComments, newLoadedComments)) {\n newLoadedComments = loadedComments;\n }\n\n return { loadedComments: newLoadedComments, bufferedComments: newBufferedComments };\n};\n\nexport const getUpdatedBufferedComments = (\n loadedComments: Comment[],\n bufferedComments: Comment[],\n filter: CommentsFilter | undefined,\n comments: Comments,\n) => {\n // get previous loaded comment cids\n const previousLoadedCommentCids: { [commentCid: string]: boolean } = {};\n for (const comment of loadedComments) {\n previousLoadedCommentCids[comment.cid] = true;\n }\n\n // get buffered comments without loaded cids\n let newBufferedComments = bufferedComments.filter(\n (comment) => !previousLoadedCommentCids[comment.cid],\n );\n\n // filter buffered comments\n if (filter) {\n newBufferedComments = newBufferedComments.filter(filter.filter);\n }\n\n // sort buffered comments by timestamp (newest first)\n newBufferedComments.sort((a, b) => b.timestamp - a.timestamp);\n\n // append the (new updated) loaded comments to buffered comments\n for (const comment of [...loadedComments].reverse()) {\n const updatedComment = comments[comment.cid];\n newBufferedComments.unshift(updatedComment);\n }\n\n // no need to check if comments have changed because getUpdatedBufferedComments\n // is not used anywhere outside of tests\n\n return newBufferedComments;\n};\n\nexport const commentsHaveChanged = (comments1: Comment[], comments2: Comment[]) => {\n if (comments1 === comments2) {\n return false;\n }\n if (comments1.length !== comments2.length) {\n return true;\n }\n for (const i in comments1) {\n if (comments1[i] !== comments2[i]) {\n return true;\n }\n }\n return false;\n};\n\n// if comment already exist, find the actual nextCidToFetch\n// can happen if a more recent lastCommentCid becomes nextCommentCidToFetch\nexport const getNextCommentCidToFetchNotFetched = (\n nextCommentCidToFetch: string | undefined,\n maxAttemptForTest?: number,\n) => {\n const { comments } = commentsStore.getState();\n let nextCommentCidToFetchNotFetched = nextCommentCidToFetch;\n // scroll through comments until the comment doesn't exist, which means hasn't been fetched yet\n let maxAttempt = maxAttemptForTest ?? 99999999;\n while (true) {\n // can't happen in production because of hashing, but can happen in tests\n if (!maxAttempt--) {\n throw Error(`getNextCommentCidToFetchNotFetched '${nextCommentCidToFetch}' infinite loop`);\n }\n const comment = comments[nextCommentCidToFetchNotFetched || \"\"];\n if (!comment?.timestamp) {\n break;\n }\n nextCommentCidToFetchNotFetched = comment.author?.previousCommentCid;\n }\n return nextCommentCidToFetchNotFetched;\n};\n\n// util for debugging sizes of object of arrays/sets\nexport const toSizes = (obj: { [key: string]: any }) => {\n const newObj: { [key: string]: number } = {};\n for (const i in obj) {\n newObj[i] = obj[i].length ?? obj[i].size;\n }\n return newObj;\n};\n"]}
@@ -0,0 +1,19 @@
1
+ import Logger from "@pkcprotocol/pkc-logger";
2
+ export declare const log: Logger;
3
+ import { Comments } from "../../types.js";
4
+ export declare const listeners: any;
5
+ export type CommentsState = {
6
+ comments: Comments;
7
+ errors: {
8
+ [commentCid: string]: Error[];
9
+ };
10
+ addCommentToStore: Function;
11
+ startCommentAutoUpdate: Function;
12
+ stopCommentAutoUpdate: Function;
13
+ refreshComment: Function;
14
+ };
15
+ declare const commentsStore: import("zustand").UseBoundStore<import("zustand").StoreApi<CommentsState>>;
16
+ export declare const resetCommentsStore: () => Promise<void>;
17
+ export declare const resetCommentsDatabaseAndStore: () => Promise<void>;
18
+ export default commentsStore;
19
+ //# sourceMappingURL=comments-store.d.ts.map
@@ -0,0 +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;AAkBzD,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;IAC1C,iBAAiB,EAAE,QAAQ,CAAC;IAC5B,sBAAsB,EAAE,QAAQ,CAAC;IACjC,qBAAqB,EAAE,QAAQ,CAAC;IAChC,cAAc,EAAE,QAAQ,CAAC;CAC1B,CAAC;AAyCF,QAAA,MAAM,aAAa,4EAkUjB,CAAC;AAsBH,eAAO,MAAM,kBAAkB,qBAgC9B,CAAC;AAGF,eAAO,MAAM,6BAA6B,qBAGzC,CAAC;AAEF,eAAe,aAAa,CAAC"}