@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,413 @@
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 assert from "assert";
11
+ import Logger from "@pkcprotocol/pkc-logger";
12
+ const log = Logger("bitsocial-react-hooks:replies:stores");
13
+ import createStore from "zustand";
14
+ import localForageLru from "../../lib/localforage-lru/index.js";
15
+ import accountsStore from "../accounts/index.js";
16
+ import repliesCommentsStore from "./replies-comments-store.js";
17
+ import repliesPagesStore from "../replies-pages/index.js";
18
+ import { getFeedsCommentsFirstPageCids, getLoadedFeeds, getBufferedFeedsWithoutLoadedFeeds, getUpdatedFeeds, getFeedsReplyCounts, getFeedsHaveMore, feedsCommentsChanged, getFeedsComments, getFeedsCommentsLoadedCount, getFeedsCommentsRepliesPagesFirstUpdatedAts, getFilteredSortedFeeds, getSortTypeFromComment, addAccountsComments, } from "./utils.js";
19
+ // reddit loads approximately 25 posts per page
20
+ // while infinite scrolling
21
+ export const defaultRepliesPerPage = 25;
22
+ // keep large buffer because fetching cids is slow
23
+ const commentRepliesLeftBeforeNextPage = 50;
24
+ const defaultAccountComments = { newerThan: Infinity, append: false };
25
+ const addDefaultFeedOptions = (feedOptions) => {
26
+ feedOptions = Object.assign({}, feedOptions);
27
+ if (feedOptions.flat === undefined || feedOptions.flat === null) {
28
+ feedOptions.flat = false;
29
+ }
30
+ feedOptions.onlyIfCached = feedOptions.onlyIfCached === true;
31
+ if (feedOptions.accountComments === undefined || feedOptions.accountComments === null) {
32
+ feedOptions.accountComments = defaultAccountComments;
33
+ }
34
+ feedOptions.repliesPerPage = feedOptions.repliesPerPage || defaultRepliesPerPage;
35
+ return feedOptions;
36
+ };
37
+ export const feedOptionsToFeedName = (feedOptions) => {
38
+ var _a, _b, _c;
39
+ feedOptions = addDefaultFeedOptions(feedOptions);
40
+ return `${feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.accountId}-${feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.commentCid}-${feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.postCid}-${feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.sortType}-${feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.flat}-${feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.onlyIfCached}-${(_a = feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.accountComments) === null || _a === void 0 ? void 0 : _a.newerThan}-${(_b = feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.accountComments) === null || _b === void 0 ? void 0 : _b.append}-${feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.repliesPerPage}-${(_c = feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.filter) === null || _c === void 0 ? void 0 : _c.key}-${feedOptions === null || feedOptions === void 0 ? void 0 : feedOptions.streamPage}`;
41
+ };
42
+ // don't updateFeeds more than once per updateFeedsMinIntervalTime
43
+ let updateFeedsPending = false;
44
+ let updateFeedsAgain = false;
45
+ const updateFeedsMinIntervalTime = 100;
46
+ const repliesStore = createStore((setState, getState) => ({
47
+ feedsOptions: {},
48
+ bufferedFeeds: {},
49
+ loadedFeeds: {},
50
+ updatedFeeds: {},
51
+ bufferedFeedsReplyCounts: {},
52
+ feedsHaveMore: {},
53
+ addFeedsToStore(feedOptionsArray) {
54
+ if (!feedOptionsArray.length) {
55
+ return;
56
+ }
57
+ const { feedsOptions: previousFeedsOptions } = getState();
58
+ const newFeedsOptions = {};
59
+ // get all newFeedsOptions
60
+ for (let feedOptions of feedOptionsArray) {
61
+ const feedName = feedOptionsToFeedName(feedOptions);
62
+ // feed is in store already, do nothing
63
+ // if the feed already exist but is at page 0, reset it to page 1
64
+ if (previousFeedsOptions[feedName] && previousFeedsOptions[feedName].pageNumber !== 0) {
65
+ continue;
66
+ }
67
+ feedOptions = addDefaultFeedOptions(feedOptions);
68
+ // to add a buffered feed, add a feed with pageNumber 0
69
+ feedOptions.pageNumber = 1;
70
+ newFeedsOptions[feedName] = feedOptions;
71
+ }
72
+ // set new feedsOptions state (first loop already skips existing feeds)
73
+ let feedsChanged = false;
74
+ setState(({ feedsOptions }) => {
75
+ if (!Object.keys(newFeedsOptions).length)
76
+ return {};
77
+ feedsChanged = true;
78
+ return { feedsOptions: Object.assign(Object.assign({}, feedsOptions), newFeedsOptions) };
79
+ });
80
+ if (feedsChanged) {
81
+ log("repliesStore.addFeedsToStore", newFeedsOptions);
82
+ }
83
+ return feedsChanged;
84
+ },
85
+ addFeedToStoreOrUpdateComment(comment, feedOptions) {
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ var _a, _b, _c, _d, _e;
88
+ // init here because must be called after async accounts store finished initializing
89
+ initializeRepliesStore();
90
+ // validate options
91
+ assert(comment && comment.cid && typeof comment.cid === "string", `repliesStore.addFeedToStoreOrUpdateComment comment.cid '${comment === null || comment === void 0 ? void 0 : comment.cid}' invalid`);
92
+ assert(feedOptions.commentCid && typeof feedOptions.commentCid === "string", `repliesStore.addFeedToStoreOrUpdateComment feedOptions.commentCid '${feedOptions.commentCid}' invalid`);
93
+ assert(feedOptions.sortType && typeof feedOptions.sortType === "string", `repliesStore.addFeedToStoreOrUpdateComment feedOptions.sortType '${feedOptions.sortType}' invalid`);
94
+ const account = accountsStore.getState().accounts[feedOptions.accountId];
95
+ assert(typeof ((_a = account === null || account === void 0 ? void 0 : account.pkc) === null || _a === void 0 ? void 0 : _a.createComment) === "function", `repliesStore.addFeedToStoreOrUpdateComment feedOptions.accountId '${feedOptions.accountId}' invalid`);
96
+ assert(!feedOptions.repliesPerPage || typeof feedOptions.repliesPerPage === "number", `repliesStore.addFeedToStoreOrUpdateComment feedOptions.repliesPerPage '${feedOptions.repliesPerPage}' invalid`);
97
+ assert(!feedOptions.filter || typeof ((_b = feedOptions.filter) === null || _b === void 0 ? void 0 : _b.filter) === "function", `repliesStore.addFeedToStoreOrUpdateComment feedOptions.filter.filter '${(_c = feedOptions.filter) === null || _c === void 0 ? void 0 : _c.filter}' invalid`);
98
+ assert(!feedOptions.filter || typeof ((_d = feedOptions.filter) === null || _d === void 0 ? void 0 : _d.key) === "string", `repliesStore.addFeedToStoreOrUpdateComment feedOptions.filter.key '${(_e = feedOptions.filter) === null || _e === void 0 ? void 0 : _e.key}' invalid`);
99
+ // if replies feed aren't in store yet, add them recursively
100
+ // TODO: optimize performance by only adding feeds that are in page 1, and add more on each page increase
101
+ const commentsToAddToStoreOrUpdate = [];
102
+ const feedsToAddToStore = [];
103
+ // use the sort type availabe on the comment when missing
104
+ const sortType = getSortTypeFromComment(comment, feedOptions);
105
+ const addRepliesFeedsToStoreRecursively = (comment) => {
106
+ var _a, _b, _c;
107
+ // NOTE: even a comment with no replies needs a feed, to know it has 0 replies and not displace the UI when a new replies appears
108
+ commentsToAddToStoreOrUpdate.push(comment);
109
+ feedsToAddToStore.push(Object.assign(Object.assign({}, feedOptions), { commentCid: comment === null || comment === void 0 ? void 0 : comment.cid, commentDepth: comment === null || comment === void 0 ? void 0 : comment.depth }));
110
+ // flat doesn't need nested feeds
111
+ if (!feedOptions.flat) {
112
+ for (const reply of ((_c = (_b = (_a = comment.replies) === null || _a === void 0 ? void 0 : _a.pages) === null || _b === void 0 ? void 0 : _b[sortType]) === null || _c === void 0 ? void 0 : _c.comments) || []) {
113
+ addRepliesFeedsToStoreRecursively(reply);
114
+ }
115
+ }
116
+ };
117
+ addRepliesFeedsToStoreRecursively(comment);
118
+ // add feeds to store and update feeds
119
+ const { addFeedsToStore, updateFeeds } = getState();
120
+ const feedsChanged = addFeedsToStore(feedsToAddToStore);
121
+ // add comments to store (do it after addFeedsToStore because it can trigger updateFeeds)
122
+ repliesCommentsStore
123
+ .getState()
124
+ .addCommentsToStoreOrUpdateComments(commentsToAddToStoreOrUpdate);
125
+ if (feedsChanged) {
126
+ log("repliesStore.addFeedToStoreOrUpdateComment", {
127
+ comment,
128
+ feedOptions,
129
+ sortType,
130
+ feedsToAddToStore,
131
+ commentsToAddToStoreOrUpdate,
132
+ });
133
+ updateFeeds();
134
+ }
135
+ });
136
+ },
137
+ incrementFeedPageNumber(feedName) {
138
+ const { feedsOptions, loadedFeeds, bufferedFeeds, updateFeeds } = getState();
139
+ assert(feedsOptions[feedName], `repliesStore.incrementFeedPageNumber feed name '${feedName}' does not exist in feeds store`);
140
+ log("repliesStore.incrementFeedPageNumber", { feedName });
141
+ // TODO: fix design issue, pageNumber shouldnt be increased when loadMore is called and repliesPerPage not reached
142
+ // assert(
143
+ // feedsOptions[feedName].pageNumber * feedsOptions[feedName].repliesPerPage <= loadedFeeds[feedName].length,
144
+ // `repliesStore.incrementFeedPageNumber cannot increment feed page number before current page has loaded`
145
+ // )
146
+ if (feedsOptions[feedName].pageNumber * feedsOptions[feedName].repliesPerPage <=
147
+ loadedFeeds[feedName].length) {
148
+ assert(bufferedFeeds[feedName].length > 0, `repliesStore.incrementFeedPageNumber cannot increment feed page number before current page has loaded`);
149
+ }
150
+ setState(({ feedsOptions, loadedFeeds }) => {
151
+ // don't increment page number before the current page has loaded
152
+ // if (feedsOptions[feedName].pageNumber * feedsOptions[feedName].repliesPerPage > loadedFeeds[feedName].length) {
153
+ // return {}
154
+ // }
155
+ const feedOptions = Object.assign(Object.assign({}, feedsOptions[feedName]), { pageNumber: feedsOptions[feedName].pageNumber + 1 });
156
+ return { feedsOptions: Object.assign(Object.assign({}, feedsOptions), { [feedName]: feedOptions }) };
157
+ });
158
+ // do not update feed at the same time as increment a page number or it might cause
159
+ // a race condition, rather schedule a feed update
160
+ updateFeeds();
161
+ },
162
+ resetFeed(feedName) {
163
+ const { feedsOptions, updateFeeds } = getState();
164
+ assert(feedsOptions[feedName], `repliesStore.resetFeed feed name '${feedName}' does not exist in feeds store`);
165
+ assert(feedsOptions[feedName].pageNumber >= 1, `repliesStore.resetFeed cannot reset feed page number '${feedsOptions[feedName].pageNumber}' lower than 1`);
166
+ log("repliesStore.resetFeed", { feedName });
167
+ setState(({ feedsOptions, loadedFeeds, updatedFeeds }) => {
168
+ const feedOptions = Object.assign(Object.assign({}, feedsOptions[feedName]), { pageNumber: 1 });
169
+ return {
170
+ feedsOptions: Object.assign(Object.assign({}, feedsOptions), { [feedName]: feedOptions }),
171
+ loadedFeeds: Object.assign(Object.assign({}, loadedFeeds), { [feedName]: [] }),
172
+ updatedFeeds: Object.assign(Object.assign({}, updatedFeeds), { [feedName]: [] }),
173
+ };
174
+ });
175
+ updateFeeds();
176
+ },
177
+ // recalculate all feeds using new comments.replies.pages, repliesPagesStore and page numbers
178
+ updateFeeds() {
179
+ if (updateFeedsPending) {
180
+ updateFeedsAgain = true;
181
+ return;
182
+ }
183
+ updateFeedsPending = true;
184
+ // don't update feeds more than once per updateFeedsMinIntervalTime
185
+ const timeUntilNextUpdate = Date.now() % updateFeedsMinIntervalTime;
186
+ setTimeout(() => __awaiter(this, void 0, void 0, function* () {
187
+ // get state from all stores
188
+ const previousState = getState();
189
+ const { feedsOptions, updateFeeds } = previousState;
190
+ const { comments } = repliesCommentsStore.getState();
191
+ const { repliesPages } = repliesPagesStore.getState();
192
+ const { accounts } = accountsStore.getState();
193
+ // calculate new feeds
194
+ const filteredSortedFeeds = getFilteredSortedFeeds(feedsOptions, comments, repliesPages, accounts);
195
+ const bufferedFeedsWithoutPreviousLoadedFeeds = getBufferedFeedsWithoutLoadedFeeds(filteredSortedFeeds, previousState.loadedFeeds);
196
+ const loadedFeeds = yield getLoadedFeeds(feedsOptions, previousState.loadedFeeds, bufferedFeedsWithoutPreviousLoadedFeeds, accounts);
197
+ // after loaded feeds are caculated, remove new loaded feeds (again) from buffered feeds
198
+ const bufferedFeeds = getBufferedFeedsWithoutLoadedFeeds(bufferedFeedsWithoutPreviousLoadedFeeds, loadedFeeds);
199
+ const bufferedFeedsReplyCounts = getFeedsReplyCounts(feedsOptions, bufferedFeeds);
200
+ const feedsHaveMore = getFeedsHaveMore(feedsOptions, bufferedFeeds, comments, repliesPages, accounts);
201
+ const updatedFeeds = yield getUpdatedFeeds(feedsOptions, filteredSortedFeeds, previousState.updatedFeeds, loadedFeeds, accounts);
202
+ // set new feeds
203
+ setState((state) => ({
204
+ bufferedFeeds,
205
+ loadedFeeds,
206
+ bufferedFeedsReplyCounts,
207
+ updatedFeeds,
208
+ feedsHaveMore,
209
+ }));
210
+ log("repliesStore.updateFeeds", {
211
+ feedsOptions,
212
+ bufferedFeeds,
213
+ loadedFeeds,
214
+ bufferedFeedsReplyCounts,
215
+ updatedFeeds,
216
+ feedsHaveMore,
217
+ comments,
218
+ repliesPages,
219
+ });
220
+ updateFeedsPending = false;
221
+ // if updateFeeds was called while updateFeedsPending = true, call updateFeeds again
222
+ if (updateFeedsAgain) {
223
+ updateFeedsAgain = false;
224
+ updateFeeds();
225
+ }
226
+ }), timeUntilNextUpdate);
227
+ },
228
+ }));
229
+ let repliesStoreInitialized = false;
230
+ const initializeRepliesStore = () => __awaiter(void 0, void 0, void 0, function* () {
231
+ if (repliesStoreInitialized) {
232
+ return;
233
+ }
234
+ repliesStoreInitialized = true;
235
+ // TODO: optimize subscriptions e.g. updateFeedsOnFeedsCommentsChange(comment)
236
+ // subscribe to comments store changes
237
+ repliesCommentsStore.subscribe(updateFeedsOnFeedsCommentsChange);
238
+ // subscribe to bufferedFeedsReplyCounts change
239
+ repliesStore.subscribe(addRepliesPagesOnLowBufferedFeedsReplyCounts);
240
+ // subscribe to replies pages store changes
241
+ repliesPagesStore.subscribe(updateFeedsOnFeedsRepliesPagesChange);
242
+ // subscribe to accounts store changes
243
+ accountsStore.subscribe(updateFeedsOnAccountsCommentsChange);
244
+ });
245
+ let previousRepliesPages = {};
246
+ const updateFeedsOnFeedsRepliesPagesChange = (repliesPagesStoreState) => {
247
+ const { repliesPages } = repliesPagesStoreState;
248
+ // no changes, do nothing
249
+ if (repliesPages === previousRepliesPages) {
250
+ return;
251
+ }
252
+ previousRepliesPages = repliesPages;
253
+ // currently only the feeds use repliesPagesStore, so any change must
254
+ // trigger a feed update, if in the future another hook uses the repliesPagesStore
255
+ // we should check if the replies pages changed are actually used by the feeds before
256
+ // triggering an update
257
+ repliesStore.getState().updateFeeds();
258
+ };
259
+ let previousBufferedFeedsReplyCountsPageCids = [];
260
+ let previousBufferedFeedsComments = new Map();
261
+ let previousBufferedFeedsReplyCounts = {};
262
+ const addRepliesPagesOnLowBufferedFeedsReplyCounts = (repliesStoreState) => {
263
+ const { bufferedFeedsReplyCounts, feedsOptions } = repliesStore.getState();
264
+ const { comments } = repliesCommentsStore.getState();
265
+ // if feeds comments have changed, we must try adding them even if buffered replies counts haven't changed
266
+ const bufferedFeedsComments = getFeedsComments(feedsOptions, comments);
267
+ const _feedsCommentsChanged = feedsCommentsChanged(previousBufferedFeedsComments, bufferedFeedsComments);
268
+ const bufferedFeedsReplyCountsChanged = previousBufferedFeedsReplyCounts !== bufferedFeedsReplyCounts;
269
+ // if feeds comments havent changed and buffered replies counts also havent changed, do nothing
270
+ if (!_feedsCommentsChanged && !bufferedFeedsReplyCountsChanged) {
271
+ return;
272
+ }
273
+ previousBufferedFeedsComments = bufferedFeedsComments;
274
+ previousBufferedFeedsReplyCounts = bufferedFeedsReplyCounts;
275
+ // in case feeds comments changed, but the first page cids haven't
276
+ const bufferedFeedsReplyCountsPageCids = getFeedsCommentsFirstPageCids(bufferedFeedsComments);
277
+ const bufferedFeedsReplyCountsPageCidsChanged = bufferedFeedsReplyCountsPageCids.toString() !==
278
+ previousBufferedFeedsReplyCountsPageCids.toString();
279
+ if (!bufferedFeedsReplyCountsPageCidsChanged && !bufferedFeedsReplyCountsChanged) {
280
+ return;
281
+ }
282
+ previousBufferedFeedsReplyCountsPageCids = bufferedFeedsReplyCountsPageCids;
283
+ const { addNextRepliesPageToStore } = repliesPagesStore.getState();
284
+ const { accounts } = accountsStore.getState();
285
+ // bufferedFeedsReplyCounts have changed, check if any of them are low
286
+ for (const feedName in bufferedFeedsReplyCounts) {
287
+ if (feedsOptions[feedName].onlyIfCached) {
288
+ continue;
289
+ }
290
+ const account = accounts[feedsOptions[feedName].accountId];
291
+ const feedReplyCount = bufferedFeedsReplyCounts[feedName];
292
+ let sortType = feedsOptions[feedName].sortType;
293
+ const commentCid = feedsOptions[feedName].commentCid;
294
+ // TODO: maybe skip if comment community address, comment cid or comment author is blocked?
295
+ // comment hasn't loaded yet
296
+ if (!comments[commentCid]) {
297
+ continue;
298
+ }
299
+ sortType = getSortTypeFromComment(comments[commentCid], feedsOptions[feedName]);
300
+ // comment replies count is low, fetch next replies page
301
+ if (feedReplyCount <= commentRepliesLeftBeforeNextPage) {
302
+ addNextRepliesPageToStore(comments[commentCid], sortType, account).catch((error) => log.error("repliesStore repliesPagesStore.addNextRepliesPageToStore error", {
303
+ commentCid,
304
+ comment: comments[commentCid],
305
+ sortType,
306
+ error,
307
+ }));
308
+ }
309
+ }
310
+ };
311
+ let previousFeedsCommentsFirstPageCids = [];
312
+ let previousFeedsComments = new Map();
313
+ let previousFeedsCommentsLoadedCount = 0;
314
+ let previousFeedsCommentsRepliesPagesFirstUpdatedAts = "";
315
+ const updateFeedsOnFeedsCommentsChange = (repliesCommentsStoreState) => {
316
+ const { comments } = repliesCommentsStoreState;
317
+ const { feedsOptions, updateFeeds } = repliesStore.getState();
318
+ // feeds comments haven't changed, do nothing
319
+ const feedsComments = getFeedsComments(feedsOptions, comments);
320
+ if (!feedsCommentsChanged(previousFeedsComments, feedsComments)) {
321
+ return;
322
+ }
323
+ previousFeedsComments = feedsComments;
324
+ // decide if feeds comments have changed by looking at all feeds comments page cids
325
+ // (in case that a comment changed, but its first page cid didn't)
326
+ const feedsCommentsFirstPageCids = getFeedsCommentsFirstPageCids(feedsComments);
327
+ // first page cids haven't changed, do nothing
328
+ if (feedsCommentsFirstPageCids.toString() === previousFeedsCommentsFirstPageCids.toString()) {
329
+ // if no new feed comments have loaded, do nothing
330
+ // in case a comment loads with no first page cid and first pages cids don't change, need to trigger hasMore update
331
+ const feedsCommentsLoadedCount = getFeedsCommentsLoadedCount(feedsComments);
332
+ if (feedsCommentsLoadedCount === previousFeedsCommentsLoadedCount) {
333
+ // if comment.replies.pages haven't changed, do nothing
334
+ const feedsCommentsRepliesPagesFirstUpdatedAts = getFeedsCommentsRepliesPagesFirstUpdatedAts(feedsComments);
335
+ if (feedsCommentsRepliesPagesFirstUpdatedAts ===
336
+ previousFeedsCommentsRepliesPagesFirstUpdatedAts) {
337
+ return;
338
+ }
339
+ previousFeedsCommentsRepliesPagesFirstUpdatedAts = feedsCommentsRepliesPagesFirstUpdatedAts;
340
+ }
341
+ previousFeedsCommentsLoadedCount = feedsCommentsLoadedCount;
342
+ }
343
+ // feeds comments have changed, update feeds
344
+ previousFeedsCommentsFirstPageCids = feedsCommentsFirstPageCids;
345
+ updateFeeds();
346
+ };
347
+ let previousAccountsCommentsCount = 0;
348
+ let previousAccountsCommentsCids = "";
349
+ const updateFeedsOnAccountsCommentsChange = (accountsStoreState) => {
350
+ const { accountsComments } = accountsStoreState;
351
+ const accountsCommentsCount = Object.values(accountsComments).reduce((count, accountComments) => count + accountComments.length, 0);
352
+ // no changes, do nothing
353
+ if (accountsCommentsCount === previousAccountsCommentsCount) {
354
+ // if cids haven't changed (account comments receive cids after pending), do nothing
355
+ const accountsCommentsCids = Object.values(accountsComments).reduce((cids, accountComments) => cids + String(accountComments.map((comment) => comment.cid || "")), "");
356
+ if (accountsCommentsCids === previousAccountsCommentsCids) {
357
+ return;
358
+ }
359
+ previousAccountsCommentsCids = accountsCommentsCids;
360
+ }
361
+ previousAccountsCommentsCount = accountsCommentsCount;
362
+ // TODO: only update the feeds that are relevant to the new accountComment.parentCid/postCid
363
+ repliesStore.getState().updateFeeds();
364
+ };
365
+ // needed to view replies instantly without waiting for zustant store react rerenders. must be synchronous
366
+ export const getRepliesFirstPageSkipValidation = (comment, feedOptions) => {
367
+ const feedName = `firstPageSkipValidation-${comment === null || comment === void 0 ? void 0 : comment.cid}`;
368
+ const feedsOptions = { [feedName]: feedOptions };
369
+ const { accounts } = accountsStore.getState();
370
+ // don't use comments store, only use preloaded comment.replies.pages
371
+ const comments = { [comment.cid]: comment };
372
+ // don't use any reply pages, they can't provide instant loading like preloaded comment.replies.pages
373
+ const repliesPages = {};
374
+ const filteredSortedFeeds = getFilteredSortedFeeds(feedsOptions, comments, repliesPages, accounts);
375
+ // only get first page and put next page in buffered
376
+ const bufferedFeeds = { [feedName]: [] };
377
+ const repliesPerPage = feedOptions.repliesPerPage || defaultRepliesPerPage;
378
+ if (filteredSortedFeeds[feedName].length > repliesPerPage) {
379
+ bufferedFeeds[feedName] = filteredSortedFeeds[feedName].splice(repliesPerPage);
380
+ }
381
+ addAccountsComments(feedsOptions, filteredSortedFeeds);
382
+ const feedsHaveMore = getFeedsHaveMore(feedsOptions, bufferedFeeds, comments, repliesPages, accounts);
383
+ return { replies: filteredSortedFeeds[feedName], hasMore: feedsHaveMore[feedName] };
384
+ };
385
+ // reset store in between tests
386
+ const originalState = repliesStore.getState();
387
+ // async function because some stores have async init
388
+ export const resetRepliesStore = () => __awaiter(void 0, void 0, void 0, function* () {
389
+ previousBufferedFeedsReplyCounts = {};
390
+ previousBufferedFeedsReplyCountsPageCids = [];
391
+ previousBufferedFeedsComments = new Map();
392
+ previousFeedsCommentsFirstPageCids = [];
393
+ previousFeedsComments = new Map();
394
+ previousFeedsCommentsLoadedCount = 0;
395
+ previousFeedsCommentsRepliesPagesFirstUpdatedAts = "";
396
+ previousRepliesPages = {};
397
+ previousAccountsCommentsCount = 0;
398
+ previousAccountsCommentsCids = "";
399
+ updateFeedsPending = false;
400
+ // destroy all component subscriptions to the store
401
+ repliesStore.destroy();
402
+ // restore original state
403
+ repliesStore.setState(originalState);
404
+ repliesCommentsStore.setState(Object.assign(Object.assign({}, repliesCommentsStore.getState()), { comments: {} }));
405
+ repliesStoreInitialized = false;
406
+ });
407
+ // reset database and store in between tests
408
+ export const resetRepliesDatabaseAndStore = () => __awaiter(void 0, void 0, void 0, function* () {
409
+ yield localForageLru.createInstance({ name: "bitsocialReactHooks-repliesPages" }).clear();
410
+ yield resetRepliesStore();
411
+ });
412
+ export default repliesStore;
413
+ //# sourceMappingURL=replies-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replies-store.js","sourceRoot":"","sources":["../../../src/stores/replies/replies-store.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,MAAM,GAAG,GAAG,MAAM,CAAC,sCAAsC,CAAC,CAAC;AAY3D,OAAO,WAAW,MAAM,SAAS,CAAC;AAClC,OAAO,cAAc,MAAM,2BAA2B,CAAC;AACvD,OAAO,aAAa,M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+B;AAC/B,MAAM,aAAa,GAAG,YAAY,CAAC,QAAQ,EAAE,CAAC;AAC9C,qDAAqD;AACrD,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAS,EAAE;IAC1C,gCAAgC,GAAG,EAAE,CAAC;IACtC,wCAAwC,GAAG,EAAE,CAAC;IAC9C,6BAA6B,GAAG,IAAI,GAAG,EAAE,CAAC;IAC1C,kCAAkC,GAAG,EAAE,CAAC;IACxC,qBAAqB,GAAG,IAAI,GAAG,EAAE,CAAC;IAClC,gCAAgC,GAAG,CAAC,CAAC;IACrC,gDAAgD,GAAG,EAAE,CAAC;IACtD,oBAAoB,GAAG,EAAE,CAAC;IAC1B,6BAA6B,GAAG,CAAC,CAAC;IAClC,4BAA4B,GAAG,EAAE,CAAC;IAClC,kBAAkB,GAAG,KAAK,CAAC;IAC3B,mDAAmD;IACnD,YAAY,CAAC,OAAO,EAAE,CAAC;IACvB,yBAAyB;IACzB,YAAY,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;IACrC,oBAAoB,CAAC,QAAQ,iCAAM,oBAAoB,CAAC,QAAQ,EAAE,KAAE,QAAQ,EAAE,EAAE,IAAG,CAAC;IACpF,uBAAuB,GAAG,KAAK,CAAC;AAClC,CAAC,CAAA,CAAC;AAEF,4CAA4C;AAC5C,MAAM,CAAC,MAAM,4BAA4B,GAAG,GAAS,EAAE;IACrD,MAAM,cAAc,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,kCAAkC,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC;IAC1F,MAAM,iBAAiB,EAAE,CAAC;AAC5B,CAAC,CAAA,CAAC;AAEF,eAAe,YAAY,CAAC","sourcesContent":["import assert from \"assert\";\nimport Logger from \"@pkcprotocol/pkc-logger\";\nconst log = Logger(\"bitsocial-react-hooks:replies:stores\");\nimport {\n Feed,\n Feeds,\n Comment,\n Comments,\n Account,\n RepliesFeedOptions,\n RepliesFeedsOptions,\n RepliesPage,\n CommentsFilter,\n} from \"../../types\";\nimport createStore from \"zustand\";\nimport localForageLru from \"../../lib/localforage-lru\";\nimport accountsStore from \"../accounts\";\nimport repliesCommentsStore from \"./replies-comments-store\";\nimport repliesPagesStore from \"../replies-pages\";\nimport {\n getFeedsCommentsFirstPageCids,\n getLoadedFeeds,\n getBufferedFeedsWithoutLoadedFeeds,\n getUpdatedFeeds,\n getFeedsReplyCounts,\n getFeedsHaveMore,\n feedsCommentsChanged,\n getFeedsComments,\n getFeedsCommentsLoadedCount,\n getFeedsCommentsRepliesPagesFirstUpdatedAts,\n getFilteredSortedFeeds,\n getSortTypeFromComment,\n addAccountsComments,\n} from \"./utils\";\n\n// reddit loads approximately 25 posts per page\n// while infinite scrolling\nexport const defaultRepliesPerPage = 25;\n\n// keep large buffer because fetching cids is slow\nconst commentRepliesLeftBeforeNextPage = 50;\n\nexport type RepliesState = {\n feedsOptions: RepliesFeedsOptions;\n bufferedFeeds: Feeds;\n loadedFeeds: Feeds;\n updatedFeeds: Feeds;\n bufferedFeedsReplyCounts: { [feedName: string]: number };\n feedsHaveMore: { [feedName: string]: boolean };\n addFeedsToStore: Function;\n addFeedToStoreOrUpdateComment: Function;\n incrementFeedPageNumber: Function;\n resetFeed: Function;\n updateFeeds: Function;\n};\n\nconst defaultAccountComments = { newerThan: Infinity, append: false };\nconst addDefaultFeedOptions = (feedOptions: any) => {\n feedOptions = { ...feedOptions };\n if (feedOptions.flat === undefined || feedOptions.flat === null) {\n feedOptions.flat = false;\n }\n feedOptions.onlyIfCached = feedOptions.onlyIfCached === true;\n if (feedOptions.accountComments === undefined || feedOptions.accountComments === null) {\n feedOptions.accountComments = defaultAccountComments;\n }\n feedOptions.repliesPerPage = feedOptions.repliesPerPage || defaultRepliesPerPage;\n return feedOptions;\n};\n\nexport const feedOptionsToFeedName = (feedOptions: Partial<RepliesFeedOptions>) => {\n feedOptions = addDefaultFeedOptions(feedOptions);\n return `${feedOptions?.accountId}-${feedOptions?.commentCid}-${feedOptions?.postCid}-${feedOptions?.sortType}-${feedOptions?.flat}-${feedOptions?.onlyIfCached}-${feedOptions?.accountComments?.newerThan}-${feedOptions?.accountComments?.append}-${feedOptions?.repliesPerPage}-${feedOptions?.filter?.key}-${feedOptions?.streamPage}`;\n};\n\n// don't updateFeeds more than once per updateFeedsMinIntervalTime\nlet updateFeedsPending = false;\nlet updateFeedsAgain = false;\nconst updateFeedsMinIntervalTime = 100;\n\nconst repliesStore = createStore<RepliesState>((setState: Function, getState: Function) => ({\n feedsOptions: {},\n bufferedFeeds: {},\n loadedFeeds: {},\n updatedFeeds: {},\n bufferedFeedsReplyCounts: {},\n feedsHaveMore: {},\n\n addFeedsToStore(feedOptionsArray: RepliesFeedOptions[]) {\n if (!feedOptionsArray.length) {\n return;\n }\n const { feedsOptions: previousFeedsOptions } = getState();\n const newFeedsOptions: RepliesFeedsOptions = {};\n\n // get all newFeedsOptions\n for (let feedOptions of feedOptionsArray) {\n const feedName = feedOptionsToFeedName(feedOptions);\n // feed is in store already, do nothing\n // if the feed already exist but is at page 0, reset it to page 1\n if (previousFeedsOptions[feedName] && previousFeedsOptions[feedName].pageNumber !== 0) {\n continue;\n }\n\n feedOptions = addDefaultFeedOptions(feedOptions);\n\n // to add a buffered feed, add a feed with pageNumber 0\n feedOptions.pageNumber = 1;\n newFeedsOptions[feedName] = feedOptions;\n }\n\n // set new feedsOptions state (first loop already skips existing feeds)\n let feedsChanged = false;\n setState(({ feedsOptions }: RepliesState) => {\n if (!Object.keys(newFeedsOptions).length) return {};\n feedsChanged = true;\n return { feedsOptions: { ...feedsOptions, ...newFeedsOptions } };\n });\n if (feedsChanged) {\n log(\"repliesStore.addFeedsToStore\", newFeedsOptions);\n }\n return feedsChanged;\n },\n\n async addFeedToStoreOrUpdateComment(comment: Comment, feedOptions: RepliesFeedOptions) {\n // init here because must be called after async accounts store finished initializing\n initializeRepliesStore();\n\n // validate options\n assert(\n comment && comment.cid && typeof comment.cid === \"string\",\n `repliesStore.addFeedToStoreOrUpdateComment comment.cid '${comment?.cid}' invalid`,\n );\n assert(\n feedOptions.commentCid && typeof feedOptions.commentCid === \"string\",\n `repliesStore.addFeedToStoreOrUpdateComment feedOptions.commentCid '${feedOptions.commentCid}' invalid`,\n );\n assert(\n feedOptions.sortType && typeof feedOptions.sortType === \"string\",\n `repliesStore.addFeedToStoreOrUpdateComment feedOptions.sortType '${feedOptions.sortType}' invalid`,\n );\n const account = accountsStore.getState().accounts[feedOptions.accountId];\n assert(\n typeof account?.pkc?.createComment === \"function\",\n `repliesStore.addFeedToStoreOrUpdateComment feedOptions.accountId '${feedOptions.accountId}' invalid`,\n );\n assert(\n !feedOptions.repliesPerPage || typeof feedOptions.repliesPerPage === \"number\",\n `repliesStore.addFeedToStoreOrUpdateComment feedOptions.repliesPerPage '${feedOptions.repliesPerPage}' invalid`,\n );\n assert(\n !feedOptions.filter || typeof feedOptions.filter?.filter === \"function\",\n `repliesStore.addFeedToStoreOrUpdateComment feedOptions.filter.filter '${feedOptions.filter?.filter}' invalid`,\n );\n assert(\n !feedOptions.filter || typeof feedOptions.filter?.key === \"string\",\n `repliesStore.addFeedToStoreOrUpdateComment feedOptions.filter.key '${feedOptions.filter?.key}' invalid`,\n );\n\n // if replies feed aren't in store yet, add them recursively\n // TODO: optimize performance by only adding feeds that are in page 1, and add more on each page increase\n const commentsToAddToStoreOrUpdate: Comment[] = [];\n const feedsToAddToStore: RepliesFeedOptions[] = [];\n\n // use the sort type availabe on the comment when missing\n const sortType = getSortTypeFromComment(comment, feedOptions);\n\n const addRepliesFeedsToStoreRecursively = (comment: Comment) => {\n // NOTE: even a comment with no replies needs a feed, to know it has 0 replies and not displace the UI when a new replies appears\n commentsToAddToStoreOrUpdate.push(comment);\n feedsToAddToStore.push({\n ...feedOptions,\n commentCid: comment?.cid,\n commentDepth: comment?.depth,\n });\n\n // flat doesn't need nested feeds\n if (!feedOptions.flat) {\n for (const reply of comment.replies?.pages?.[sortType]?.comments || []) {\n addRepliesFeedsToStoreRecursively(reply);\n }\n }\n };\n addRepliesFeedsToStoreRecursively(comment);\n\n // add feeds to store and update feeds\n const { addFeedsToStore, updateFeeds } = getState();\n const feedsChanged = addFeedsToStore(feedsToAddToStore);\n // add comments to store (do it after addFeedsToStore because it can trigger updateFeeds)\n repliesCommentsStore\n .getState()\n .addCommentsToStoreOrUpdateComments(commentsToAddToStoreOrUpdate);\n if (feedsChanged) {\n log(\"repliesStore.addFeedToStoreOrUpdateComment\", {\n comment,\n feedOptions,\n sortType,\n feedsToAddToStore,\n commentsToAddToStoreOrUpdate,\n });\n updateFeeds();\n }\n },\n\n incrementFeedPageNumber(feedName: string) {\n const { feedsOptions, loadedFeeds, bufferedFeeds, updateFeeds } = getState();\n assert(\n feedsOptions[feedName],\n `repliesStore.incrementFeedPageNumber feed name '${feedName}' does not exist in feeds store`,\n );\n log(\"repliesStore.incrementFeedPageNumber\", { feedName });\n\n // TODO: fix design issue, pageNumber shouldnt be increased when loadMore is called and repliesPerPage not reached\n // assert(\n // feedsOptions[feedName].pageNumber * feedsOptions[feedName].repliesPerPage <= loadedFeeds[feedName].length,\n // `repliesStore.incrementFeedPageNumber cannot increment feed page number before current page has loaded`\n // )\n if (\n feedsOptions[feedName].pageNumber * feedsOptions[feedName].repliesPerPage <=\n loadedFeeds[feedName].length\n ) {\n assert(\n bufferedFeeds[feedName].length > 0,\n `repliesStore.incrementFeedPageNumber cannot increment feed page number before current page has loaded`,\n );\n }\n\n setState(({ feedsOptions, loadedFeeds }: any) => {\n // don't increment page number before the current page has loaded\n // if (feedsOptions[feedName].pageNumber * feedsOptions[feedName].repliesPerPage > loadedFeeds[feedName].length) {\n // return {}\n // }\n const feedOptions = {\n ...feedsOptions[feedName],\n pageNumber: feedsOptions[feedName].pageNumber + 1,\n };\n return { feedsOptions: { ...feedsOptions, [feedName]: feedOptions } };\n });\n\n // do not update feed at the same time as increment a page number or it might cause\n // a race condition, rather schedule a feed update\n updateFeeds();\n },\n\n resetFeed(feedName: string) {\n const { feedsOptions, updateFeeds } = getState();\n assert(\n feedsOptions[feedName],\n `repliesStore.resetFeed feed name '${feedName}' does not exist in feeds store`,\n );\n assert(\n feedsOptions[feedName].pageNumber >= 1,\n `repliesStore.resetFeed cannot reset feed page number '${feedsOptions[feedName].pageNumber}' lower than 1`,\n );\n log(\"repliesStore.resetFeed\", { feedName });\n\n setState(({ feedsOptions, loadedFeeds, updatedFeeds }: any) => {\n const feedOptions = {\n ...feedsOptions[feedName],\n pageNumber: 1,\n };\n return {\n feedsOptions: { ...feedsOptions, [feedName]: feedOptions },\n loadedFeeds: { ...loadedFeeds, [feedName]: [] },\n updatedFeeds: { ...updatedFeeds, [feedName]: [] },\n };\n });\n\n updateFeeds();\n },\n\n // recalculate all feeds using new comments.replies.pages, repliesPagesStore and page numbers\n updateFeeds() {\n if (updateFeedsPending) {\n updateFeedsAgain = true;\n return;\n }\n updateFeedsPending = true;\n\n // don't update feeds more than once per updateFeedsMinIntervalTime\n const timeUntilNextUpdate = Date.now() % updateFeedsMinIntervalTime;\n\n setTimeout(async () => {\n // get state from all stores\n const previousState = getState();\n const { feedsOptions, updateFeeds } = previousState;\n const { comments } = repliesCommentsStore.getState();\n const { repliesPages } = repliesPagesStore.getState();\n const { accounts } = accountsStore.getState();\n\n // calculate new feeds\n const filteredSortedFeeds = getFilteredSortedFeeds(\n feedsOptions,\n comments,\n repliesPages,\n accounts,\n );\n const bufferedFeedsWithoutPreviousLoadedFeeds = getBufferedFeedsWithoutLoadedFeeds(\n filteredSortedFeeds,\n previousState.loadedFeeds,\n );\n const loadedFeeds = await getLoadedFeeds(\n feedsOptions,\n previousState.loadedFeeds,\n bufferedFeedsWithoutPreviousLoadedFeeds,\n accounts,\n );\n // after loaded feeds are caculated, remove new loaded feeds (again) from buffered feeds\n const bufferedFeeds = getBufferedFeedsWithoutLoadedFeeds(\n bufferedFeedsWithoutPreviousLoadedFeeds,\n loadedFeeds,\n );\n const bufferedFeedsReplyCounts = getFeedsReplyCounts(feedsOptions, bufferedFeeds);\n const feedsHaveMore = getFeedsHaveMore(\n feedsOptions,\n bufferedFeeds,\n comments,\n repliesPages,\n accounts,\n );\n const updatedFeeds = await getUpdatedFeeds(\n feedsOptions,\n filteredSortedFeeds,\n previousState.updatedFeeds,\n loadedFeeds,\n accounts,\n );\n\n // set new feeds\n setState((state: any) => ({\n bufferedFeeds,\n loadedFeeds,\n bufferedFeedsReplyCounts,\n updatedFeeds,\n feedsHaveMore,\n }));\n log(\"repliesStore.updateFeeds\", {\n feedsOptions,\n bufferedFeeds,\n loadedFeeds,\n bufferedFeedsReplyCounts,\n updatedFeeds,\n feedsHaveMore,\n comments,\n repliesPages,\n });\n\n updateFeedsPending = false;\n\n // if updateFeeds was called while updateFeedsPending = true, call updateFeeds again\n if (updateFeedsAgain) {\n updateFeedsAgain = false;\n updateFeeds();\n }\n }, timeUntilNextUpdate);\n },\n}));\n\nlet repliesStoreInitialized = false;\nconst initializeRepliesStore = async () => {\n if (repliesStoreInitialized) {\n return;\n }\n repliesStoreInitialized = true;\n // TODO: optimize subscriptions e.g. updateFeedsOnFeedsCommentsChange(comment)\n // subscribe to comments store changes\n repliesCommentsStore.subscribe(updateFeedsOnFeedsCommentsChange);\n // subscribe to bufferedFeedsReplyCounts change\n repliesStore.subscribe(addRepliesPagesOnLowBufferedFeedsReplyCounts);\n // subscribe to replies pages store changes\n repliesPagesStore.subscribe(updateFeedsOnFeedsRepliesPagesChange);\n // subscribe to accounts store changes\n accountsStore.subscribe(updateFeedsOnAccountsCommentsChange);\n};\n\nlet previousRepliesPages: { [pageCid: string]: RepliesPage } = {};\nconst updateFeedsOnFeedsRepliesPagesChange = (repliesPagesStoreState: any) => {\n const { repliesPages } = repliesPagesStoreState;\n\n // no changes, do nothing\n if (repliesPages === previousRepliesPages) {\n return;\n }\n previousRepliesPages = repliesPages;\n\n // currently only the feeds use repliesPagesStore, so any change must\n // trigger a feed update, if in the future another hook uses the repliesPagesStore\n // we should check if the replies pages changed are actually used by the feeds before\n // triggering an update\n repliesStore.getState().updateFeeds();\n};\n\nlet previousBufferedFeedsReplyCountsPageCids: string[] = [];\nlet previousBufferedFeedsComments = new Map<string, Comments>();\nlet previousBufferedFeedsReplyCounts: { [feedName: string]: number } = {};\nconst addRepliesPagesOnLowBufferedFeedsReplyCounts = (repliesStoreState: any) => {\n const { bufferedFeedsReplyCounts, feedsOptions } = repliesStore.getState();\n const { comments } = repliesCommentsStore.getState();\n\n // if feeds comments have changed, we must try adding them even if buffered replies counts haven't changed\n const bufferedFeedsComments = getFeedsComments(feedsOptions, comments);\n const _feedsCommentsChanged = feedsCommentsChanged(\n previousBufferedFeedsComments,\n bufferedFeedsComments,\n );\n const bufferedFeedsReplyCountsChanged =\n previousBufferedFeedsReplyCounts !== bufferedFeedsReplyCounts;\n\n // if feeds comments havent changed and buffered replies counts also havent changed, do nothing\n if (!_feedsCommentsChanged && !bufferedFeedsReplyCountsChanged) {\n return;\n }\n previousBufferedFeedsComments = bufferedFeedsComments;\n previousBufferedFeedsReplyCounts = bufferedFeedsReplyCounts;\n\n // in case feeds comments changed, but the first page cids haven't\n const bufferedFeedsReplyCountsPageCids = getFeedsCommentsFirstPageCids(bufferedFeedsComments);\n const bufferedFeedsReplyCountsPageCidsChanged =\n bufferedFeedsReplyCountsPageCids.toString() !==\n previousBufferedFeedsReplyCountsPageCids.toString();\n if (!bufferedFeedsReplyCountsPageCidsChanged && !bufferedFeedsReplyCountsChanged) {\n return;\n }\n previousBufferedFeedsReplyCountsPageCids = bufferedFeedsReplyCountsPageCids;\n\n const { addNextRepliesPageToStore } = repliesPagesStore.getState();\n const { accounts } = accountsStore.getState();\n\n // bufferedFeedsReplyCounts have changed, check if any of them are low\n for (const feedName in bufferedFeedsReplyCounts) {\n if (feedsOptions[feedName].onlyIfCached) {\n continue;\n }\n const account = accounts[feedsOptions[feedName].accountId];\n const feedReplyCount = bufferedFeedsReplyCounts[feedName];\n let sortType = feedsOptions[feedName].sortType;\n const commentCid = feedsOptions[feedName].commentCid;\n\n // TODO: maybe skip if comment community address, comment cid or comment author is blocked?\n\n // comment hasn't loaded yet\n if (!comments[commentCid]) {\n continue;\n }\n\n sortType = getSortTypeFromComment(comments[commentCid], feedsOptions[feedName]);\n\n // comment replies count is low, fetch next replies page\n if (feedReplyCount <= commentRepliesLeftBeforeNextPage) {\n addNextRepliesPageToStore(comments[commentCid], sortType, account).catch((error: unknown) =>\n log.error(\"repliesStore repliesPagesStore.addNextRepliesPageToStore error\", {\n commentCid,\n comment: comments[commentCid],\n sortType,\n error,\n }),\n );\n }\n }\n};\n\nlet previousFeedsCommentsFirstPageCids: string[] = [];\nlet previousFeedsComments: Map<string, Comments> = new Map();\nlet previousFeedsCommentsLoadedCount = 0;\nlet previousFeedsCommentsRepliesPagesFirstUpdatedAts = \"\";\nconst updateFeedsOnFeedsCommentsChange = (repliesCommentsStoreState: any) => {\n const { comments } = repliesCommentsStoreState;\n const { feedsOptions, updateFeeds } = repliesStore.getState();\n\n // feeds comments haven't changed, do nothing\n const feedsComments = getFeedsComments(feedsOptions, comments);\n if (!feedsCommentsChanged(previousFeedsComments, feedsComments)) {\n return;\n }\n previousFeedsComments = feedsComments;\n\n // decide if feeds comments have changed by looking at all feeds comments page cids\n // (in case that a comment changed, but its first page cid didn't)\n const feedsCommentsFirstPageCids = getFeedsCommentsFirstPageCids(feedsComments);\n\n // first page cids haven't changed, do nothing\n if (feedsCommentsFirstPageCids.toString() === previousFeedsCommentsFirstPageCids.toString()) {\n // if no new feed comments have loaded, do nothing\n // in case a comment loads with no first page cid and first pages cids don't change, need to trigger hasMore update\n const feedsCommentsLoadedCount = getFeedsCommentsLoadedCount(feedsComments);\n if (feedsCommentsLoadedCount === previousFeedsCommentsLoadedCount) {\n // if comment.replies.pages haven't changed, do nothing\n const feedsCommentsRepliesPagesFirstUpdatedAts =\n getFeedsCommentsRepliesPagesFirstUpdatedAts(feedsComments);\n if (\n feedsCommentsRepliesPagesFirstUpdatedAts ===\n previousFeedsCommentsRepliesPagesFirstUpdatedAts\n ) {\n return;\n }\n\n previousFeedsCommentsRepliesPagesFirstUpdatedAts = feedsCommentsRepliesPagesFirstUpdatedAts;\n }\n previousFeedsCommentsLoadedCount = feedsCommentsLoadedCount;\n }\n\n // feeds comments have changed, update feeds\n previousFeedsCommentsFirstPageCids = feedsCommentsFirstPageCids;\n updateFeeds();\n};\n\nlet previousAccountsCommentsCount = 0;\nlet previousAccountsCommentsCids = \"\";\nconst updateFeedsOnAccountsCommentsChange = (accountsStoreState: any) => {\n const { accountsComments } = accountsStoreState;\n const accountsCommentsCount = Object.values(accountsComments as Comment[][]).reduce(\n (count, accountComments) => count + accountComments.length,\n 0,\n );\n\n // no changes, do nothing\n if (accountsCommentsCount === previousAccountsCommentsCount) {\n // if cids haven't changed (account comments receive cids after pending), do nothing\n const accountsCommentsCids = Object.values(accountsComments as Comment[][]).reduce(\n (cids, accountComments) => cids + String(accountComments.map((comment) => comment.cid || \"\")),\n \"\",\n );\n if (accountsCommentsCids === previousAccountsCommentsCids) {\n return;\n }\n previousAccountsCommentsCids = accountsCommentsCids;\n }\n previousAccountsCommentsCount = accountsCommentsCount;\n\n // TODO: only update the feeds that are relevant to the new accountComment.parentCid/postCid\n repliesStore.getState().updateFeeds();\n};\n\n// needed to view replies instantly without waiting for zustant store react rerenders. must be synchronous\nexport const getRepliesFirstPageSkipValidation = (\n comment: Comment,\n feedOptions: Partial<RepliesFeedOptions>,\n) => {\n const feedName = `firstPageSkipValidation-${comment?.cid}`;\n const feedsOptions: any = { [feedName]: feedOptions };\n const { accounts } = accountsStore.getState();\n\n // don't use comments store, only use preloaded comment.replies.pages\n const comments = { [comment.cid]: comment };\n\n // don't use any reply pages, they can't provide instant loading like preloaded comment.replies.pages\n const repliesPages = {};\n\n const filteredSortedFeeds = getFilteredSortedFeeds(\n feedsOptions,\n comments,\n repliesPages,\n accounts,\n );\n\n // only get first page and put next page in buffered\n const bufferedFeeds: Feeds = { [feedName]: [] };\n const repliesPerPage = feedOptions.repliesPerPage || defaultRepliesPerPage;\n if (filteredSortedFeeds[feedName].length > repliesPerPage) {\n bufferedFeeds[feedName] = filteredSortedFeeds[feedName].splice(repliesPerPage);\n }\n addAccountsComments(feedsOptions, filteredSortedFeeds);\n\n const feedsHaveMore = getFeedsHaveMore(\n feedsOptions,\n bufferedFeeds,\n comments,\n repliesPages,\n accounts,\n );\n return { replies: filteredSortedFeeds[feedName], hasMore: feedsHaveMore[feedName] };\n};\n\n// reset store in between tests\nconst originalState = repliesStore.getState();\n// async function because some stores have async init\nexport const resetRepliesStore = async () => {\n previousBufferedFeedsReplyCounts = {};\n previousBufferedFeedsReplyCountsPageCids = [];\n previousBufferedFeedsComments = new Map();\n previousFeedsCommentsFirstPageCids = [];\n previousFeedsComments = new Map();\n previousFeedsCommentsLoadedCount = 0;\n previousFeedsCommentsRepliesPagesFirstUpdatedAts = \"\";\n previousRepliesPages = {};\n previousAccountsCommentsCount = 0;\n previousAccountsCommentsCids = \"\";\n updateFeedsPending = false;\n // destroy all component subscriptions to the store\n repliesStore.destroy();\n // restore original state\n repliesStore.setState(originalState);\n repliesCommentsStore.setState({ ...repliesCommentsStore.getState(), comments: {} });\n repliesStoreInitialized = false;\n};\n\n// reset database and store in between tests\nexport const resetRepliesDatabaseAndStore = async () => {\n await localForageLru.createInstance({ name: \"bitsocialReactHooks-repliesPages\" }).clear();\n await resetRepliesStore();\n};\n\nexport default repliesStore;\n"]}
@@ -0,0 +1,25 @@
1
+ import { Feeds, RepliesFeedOptions, RepliesFeedsOptions, Comment, Comments, Accounts, RepliesPages } from "../../types.js";
2
+ /**
3
+ * Calculate the feeds from all the loaded replies pages, filter and sort them
4
+ */
5
+ export declare const getFilteredSortedFeeds: (feedsOptions: RepliesFeedsOptions, comments: Comments, repliesPages: RepliesPages, accounts: Accounts) => Feeds;
6
+ export declare const getLoadedFeeds: (feedsOptions: RepliesFeedsOptions, loadedFeeds: Feeds, bufferedFeeds: Feeds, accounts: Accounts) => Promise<Feeds>;
7
+ export declare const addAccountsComments: (feedsOptions: RepliesFeedsOptions, loadedFeeds: Feeds) => boolean;
8
+ export declare const getBufferedFeedsWithoutLoadedFeeds: (bufferedFeeds: Feeds, loadedFeeds: Feeds) => Feeds;
9
+ export declare const getUpdatedFeeds: (feedsOptions: RepliesFeedsOptions, filteredSortedFeeds: Feeds, updatedFeeds: Feeds, loadedFeeds: Feeds, accounts: Accounts) => Promise<Feeds>;
10
+ export declare const getFeedsReplyCounts: (feedsOptions: RepliesFeedsOptions, feeds: Feeds) => {
11
+ [feedName: string]: number;
12
+ };
13
+ /**
14
+ * Get which feeds have more replies, i.e. have not reached the final page of all comments
15
+ */
16
+ export declare const getFeedsHaveMore: (feedsOptions: RepliesFeedsOptions, bufferedFeeds: Feeds, comments: Comments, repliesPages: RepliesPages, accounts: Accounts) => {
17
+ [feedName: string]: boolean;
18
+ };
19
+ export declare const getFeedsComments: (feedsOptions: RepliesFeedsOptions, comments: Comments) => Map<string, Comment>;
20
+ export declare const feedsCommentsChanged: (previousFeedsComments: Map<string, Comment>, feedsComments: Map<string, Comment>) => boolean;
21
+ export declare const getFeedsCommentsFirstPageCids: (feedsComments: Map<string, Comment>) => string[];
22
+ export declare const getFeedsCommentsRepliesPagesFirstUpdatedAts: (feedsComments: Map<string, Comment>) => string;
23
+ export declare const getFeedsCommentsLoadedCount: (feedsComments: Map<string, Comment>) => number;
24
+ export declare const getSortTypeFromComment: (comment: Comment, feedOptions: RepliesFeedOptions) => string;
25
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/stores/replies/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,EACL,kBAAkB,EAClB,mBAAmB,EACnB,OAAO,EACP,QAAQ,EAER,QAAQ,EAER,YAAY,EACb,MAAM,aAAa,CAAC;AASrB;;GAEG;AACH,eAAO,MAAM,sBAAsB,GACjC,cAAc,mBAAmB,EACjC,UAAU,QAAQ,EAClB,cAAc,YAAY,EAC1B,UAAU,QAA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