@bitsocial/bitsocial-react-hooks 0.1.64 → 0.2.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.
- package/CHANGELOG.md +23 -0
- package/dist/hooks/communities.d.ts.map +1 -1
- package/dist/hooks/communities.js +49 -11
- package/dist/hooks/communities.js.map +1 -1
- package/dist/hooks/feeds/feeds.d.ts.map +1 -1
- package/dist/hooks/feeds/feeds.js +34 -1
- package/dist/hooks/feeds/feeds.js.map +1 -1
- package/dist/lib/pkc-js/pkc-js-mock.d.ts +2 -0
- package/dist/lib/pkc-js/pkc-js-mock.d.ts.map +1 -1
- package/dist/lib/pkc-js/pkc-js-mock.js +17 -0
- package/dist/lib/pkc-js/pkc-js-mock.js.map +1 -1
- package/dist/stores/communities/communities-store.d.ts +5 -0
- package/dist/stores/communities/communities-store.d.ts.map +1 -1
- package/dist/stores/communities/communities-store.js +272 -36
- package/dist/stores/communities/communities-store.js.map +1 -1
- package/dist/stores/feeds/feeds-store.d.ts +2 -0
- package/dist/stores/feeds/feeds-store.d.ts.map +1 -1
- package/dist/stores/feeds/feeds-store.js +97 -2
- package/dist/stores/feeds/feeds-store.js.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -7,6 +7,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
+
var t = {};
|
|
12
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
+
t[p] = s[p];
|
|
14
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
+
t[p[i]] = s[p[i]];
|
|
18
|
+
}
|
|
19
|
+
return t;
|
|
20
|
+
};
|
|
10
21
|
import assert from "../../lib/assert.js";
|
|
11
22
|
import localForageLru from "../../lib/localforage-lru/index.js";
|
|
12
23
|
const communitiesDatabase = localForageLru.createInstance({
|
|
@@ -22,13 +33,27 @@ import communitiesPagesStore from "../communities-pages/index.js";
|
|
|
22
33
|
import { getCommunityLookupOptions, getCommunityRefKey } from "../../lib/community-ref.js";
|
|
23
34
|
import { createPkcCommunity, getPkcCommunity, getPkcCommunityAddresses, getPkcCreateCommunity, getPkcGetCommunity, } from "../../lib/pkc-compat.js";
|
|
24
35
|
export const COMMUNITY_UPDATE_INTERVAL_MS = 15 * 60 * 1000;
|
|
36
|
+
// How long a community keeps updating after nothing uses it anymore. pkc-js forgets the record
|
|
37
|
+
// CIDs and gateway ETags it already loaded when a community stops, so every restart downloads the
|
|
38
|
+
// whole record again, while a running loop downloads it once per publish. Communities publish
|
|
39
|
+
// between every ~20 seconds when busy and every 15 minutes when idle, so 2 minutes keeps quick
|
|
40
|
+
// back-and-forth navigation from re-downloading records and bounds what an abandoned busy
|
|
41
|
+
// community costs to about one restart's worth of downloads.
|
|
42
|
+
export const COMMUNITY_UPDATE_STOP_GRACE_MS = 2 * 60 * 1000;
|
|
25
43
|
let pkcGetCommunityPending = {};
|
|
26
|
-
|
|
27
|
-
|
|
44
|
+
const liveCommunities = {};
|
|
45
|
+
// hooks and feeds that need a community kept updating, the loop stops after the last one leaves
|
|
46
|
+
const communityUpdateSubscribers = {};
|
|
47
|
+
const communityStopTimers = {};
|
|
28
48
|
// reset all event listeners in between tests
|
|
29
49
|
const listeners = [];
|
|
30
50
|
const COMMUNITY_ERROR_UPDATE_GRACE_MS = 1000;
|
|
31
51
|
const pendingCommunityErrorTimers = {};
|
|
52
|
+
const omitLastRetriableError = (syncStatus) => {
|
|
53
|
+
const nextSyncStatus = Object.assign({}, syncStatus);
|
|
54
|
+
delete nextSyncStatus.lastRetriableError;
|
|
55
|
+
return nextSyncStatus;
|
|
56
|
+
};
|
|
32
57
|
const getNowSeconds = () => Math.floor(Date.now() / 1000);
|
|
33
58
|
const normalizeCommunitySyncState = (updatingState) => {
|
|
34
59
|
if (updatingState === "waiting-retry") {
|
|
@@ -43,7 +68,10 @@ const getNextSyncStatuses = (state, communityKey, syncState) => {
|
|
|
43
68
|
const previousStatus = state.syncStatuses[communityKey];
|
|
44
69
|
const timestamp = getNowSeconds();
|
|
45
70
|
const shouldRecordAttempt = syncState === "loading" && (previousStatus === null || previousStatus === void 0 ? void 0 : previousStatus.syncState) !== "loading";
|
|
46
|
-
|
|
71
|
+
const nextStatus = Object.assign(Object.assign(Object.assign(Object.assign({}, previousStatus), { syncState }), (shouldRecordAttempt ? { lastFetchAttemptAt: timestamp } : undefined)), (syncState === "succeeded" ? { lastSuccessfulFetchAt: timestamp } : undefined));
|
|
72
|
+
return Object.assign(Object.assign({}, state.syncStatuses), {
|
|
73
|
+
// a success ends the retries, so their error is no longer current
|
|
74
|
+
[communityKey]: syncState === "succeeded" ? omitLastRetriableError(nextStatus) : nextStatus });
|
|
47
75
|
};
|
|
48
76
|
const updateCommunitySyncStatus = (setState, communityKey, syncState) => {
|
|
49
77
|
setState((state) => (Object.assign(Object.assign({}, state), { syncStatuses: getNextSyncStatuses(state, communityKey, syncState) })));
|
|
@@ -65,35 +93,141 @@ const createCommunityWithLookupFallback = (pkc, communityLookupOptions, communit
|
|
|
65
93
|
}
|
|
66
94
|
throw Error(`communitiesStore.addCommunityToStore failed getting community '${communityKey}'`);
|
|
67
95
|
});
|
|
68
|
-
const
|
|
69
|
-
|
|
96
|
+
const getCommunityKey = (communityAddressOrRef) => typeof communityAddressOrRef === "string"
|
|
97
|
+
? communityAddressOrRef
|
|
98
|
+
: getCommunityRefKey(communityAddressOrRef);
|
|
99
|
+
const hasCommunityUpdateSubscribers = (communityKey) => { var _a; return (((_a = communityUpdateSubscribers[communityKey]) === null || _a === void 0 ? void 0 : _a.size) || 0) > 0; };
|
|
100
|
+
const updateCommunity = (communityKey, liveCommunity) => {
|
|
101
|
+
const { community, communityAddressOrRef } = liveCommunity;
|
|
102
|
+
liveCommunity.updateCall = Promise.resolve()
|
|
103
|
+
.then(() => community.update())
|
|
104
|
+
.catch((error) => log.trace("community.update error", {
|
|
70
105
|
communityAddressOrRef,
|
|
71
106
|
communityKey,
|
|
72
107
|
community,
|
|
73
108
|
error,
|
|
74
109
|
}));
|
|
75
110
|
};
|
|
76
|
-
const startCommunityUpdatePolling = (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
111
|
+
const startCommunityUpdatePolling = (communityKey, liveCommunity) => {
|
|
112
|
+
clearInterval(liveCommunity.updateInterval);
|
|
113
|
+
liveCommunity.openingUpdatePending = true;
|
|
114
|
+
liveCommunity.stopWhenOpeningUpdateEnds = false;
|
|
115
|
+
updateCommunity(communityKey, liveCommunity);
|
|
116
|
+
liveCommunity.updateInterval = setInterval(() => updateCommunity(communityKey, liveCommunity), COMMUNITY_UPDATE_INTERVAL_MS);
|
|
117
|
+
};
|
|
118
|
+
const pauseCommunityUpdatePolling = (communityKey) => {
|
|
119
|
+
const liveCommunity = liveCommunities[communityKey];
|
|
120
|
+
if (!(liveCommunity === null || liveCommunity === void 0 ? void 0 : liveCommunity.updateInterval)) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
clearInterval(liveCommunity.updateInterval);
|
|
124
|
+
liveCommunity.updateInterval = undefined;
|
|
125
|
+
liveCommunity.openingUpdatePending = false;
|
|
126
|
+
liveCommunity.stopWhenOpeningUpdateEnds = false;
|
|
127
|
+
liveCommunity.replayRecordId = undefined;
|
|
128
|
+
clearPendingCommunityErrors(communityKey);
|
|
129
|
+
const { community, updateCall } = liveCommunity;
|
|
130
|
+
// stopping while update() is still attaching to the shared updating instance would leave its
|
|
131
|
+
// listeners attached, so wait for the call to return first
|
|
132
|
+
const stopping = (() => __awaiter(void 0, void 0, void 0, function* () {
|
|
133
|
+
yield updateCall;
|
|
134
|
+
if (typeof community.stop !== "function") {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
yield community.stop();
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
log.trace("community.stop error", { communityKey, community, error });
|
|
142
|
+
}
|
|
143
|
+
}))();
|
|
144
|
+
liveCommunity.stopping = stopping;
|
|
145
|
+
stopping.finally(() => {
|
|
146
|
+
if (liveCommunity.stopping === stopping) {
|
|
147
|
+
liveCommunity.stopping = undefined;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
log("communitiesStore community update loop stopped", { communityKey });
|
|
151
|
+
};
|
|
152
|
+
const resumeCommunityUpdatePolling = (communityKey) => __awaiter(void 0, void 0, void 0, function* () {
|
|
153
|
+
var _a;
|
|
154
|
+
const liveCommunity = liveCommunities[communityKey];
|
|
155
|
+
if (!liveCommunity || liveCommunity.updateInterval) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
// update() is a no-op until a stop in progress ends
|
|
159
|
+
if (liveCommunity.stopping) {
|
|
160
|
+
yield liveCommunity.stopping;
|
|
161
|
+
}
|
|
162
|
+
if (liveCommunities[communityKey] !== liveCommunity ||
|
|
163
|
+
liveCommunity.updateInterval ||
|
|
164
|
+
!hasCommunityUpdateSubscribers(communityKey)) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
liveCommunity.replayRecordId = getCommunityRecordId(liveCommunity.community);
|
|
168
|
+
liveCommunity.replayRefreshesFetchedAt = false;
|
|
169
|
+
startCommunityUpdatePolling(communityKey, liveCommunity);
|
|
170
|
+
log("communitiesStore community update loop restarted", { communityKey });
|
|
171
|
+
// joining a shared pkc-js instance that another consumer kept updating emits no state change
|
|
172
|
+
// until its next fetch, so take its current state
|
|
173
|
+
yield liveCommunity.updateCall;
|
|
174
|
+
const { updatingState } = liveCommunity.community;
|
|
175
|
+
if (liveCommunity.updateInterval &&
|
|
176
|
+
typeof updatingState === "string" &&
|
|
177
|
+
updatingState !== "stopped" &&
|
|
178
|
+
((_a = communitiesStore.getState().syncStatuses[communityKey]) === null || _a === void 0 ? void 0 : _a.stoppedUntilUsed)) {
|
|
179
|
+
liveCommunity.onUpdatingStateChange(updatingState);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
const clearCommunityStopTimer = (communityKey) => {
|
|
183
|
+
clearTimeout(communityStopTimers[communityKey]);
|
|
184
|
+
delete communityStopTimers[communityKey];
|
|
185
|
+
};
|
|
186
|
+
const scheduleCommunityUpdateStop = (communityKey) => {
|
|
187
|
+
clearCommunityStopTimer(communityKey);
|
|
188
|
+
communityStopTimers[communityKey] = setTimeout(() => {
|
|
189
|
+
delete communityStopTimers[communityKey];
|
|
190
|
+
const liveCommunity = liveCommunities[communityKey];
|
|
191
|
+
if (hasCommunityUpdateSubscribers(communityKey) || !(liveCommunity === null || liveCommunity === void 0 ? void 0 : liveCommunity.updateInterval)) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
// stopping before the first fetch ends would leave a store-level caller without the record
|
|
195
|
+
// it loaded the community for
|
|
196
|
+
if (liveCommunity.openingUpdatePending) {
|
|
197
|
+
liveCommunity.stopWhenOpeningUpdateEnds = true;
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
pauseCommunityUpdatePolling(communityKey);
|
|
201
|
+
}, COMMUNITY_UPDATE_STOP_GRACE_MS);
|
|
202
|
+
};
|
|
203
|
+
const endOpeningCommunityUpdate = (communityKey, liveCommunity) => {
|
|
204
|
+
if (!liveCommunity.openingUpdatePending) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
liveCommunity.openingUpdatePending = false;
|
|
208
|
+
if (!liveCommunity.stopWhenOpeningUpdateEnds) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
liveCommunity.stopWhenOpeningUpdateEnds = false;
|
|
212
|
+
if (!hasCommunityUpdateSubscribers(communityKey)) {
|
|
213
|
+
pauseCommunityUpdatePolling(communityKey);
|
|
214
|
+
}
|
|
83
215
|
};
|
|
84
216
|
const stopCommunityUpdatePolling = (communityKey) => {
|
|
85
|
-
|
|
86
|
-
|
|
217
|
+
clearCommunityStopTimer(communityKey);
|
|
218
|
+
const liveCommunity = liveCommunities[communityKey];
|
|
219
|
+
if (!liveCommunity) {
|
|
87
220
|
return;
|
|
88
221
|
}
|
|
89
222
|
clearPendingCommunityErrors(communityKey);
|
|
90
|
-
clearInterval(
|
|
91
|
-
|
|
92
|
-
|
|
223
|
+
clearInterval(liveCommunity.updateInterval);
|
|
224
|
+
liveCommunity.updateInterval = undefined;
|
|
225
|
+
liveCommunity.community.removeAllListeners();
|
|
226
|
+
const listenerIndex = listeners.indexOf(liveCommunity.community);
|
|
93
227
|
if (listenerIndex >= 0) {
|
|
94
228
|
listeners.splice(listenerIndex, 1);
|
|
95
229
|
}
|
|
96
|
-
delete
|
|
230
|
+
delete liveCommunities[communityKey];
|
|
97
231
|
};
|
|
98
232
|
const clearPendingCommunityErrors = (communityKey) => {
|
|
99
233
|
var _a;
|
|
@@ -109,8 +243,15 @@ const clearStoredCommunityErrors = (state, communityKey) => {
|
|
|
109
243
|
return nextErrors;
|
|
110
244
|
};
|
|
111
245
|
// returning the same state skips notifying subscribers when there is nothing to clear
|
|
112
|
-
const clearStoredCommunityErrorsState = (state, communityKey) =>
|
|
113
|
-
|
|
246
|
+
const clearStoredCommunityErrorsState = (state, communityKey) => {
|
|
247
|
+
const syncStatus = state.syncStatuses[communityKey];
|
|
248
|
+
const hasRetriableError = (syncStatus === null || syncStatus === void 0 ? void 0 : syncStatus.lastRetriableError) !== undefined;
|
|
249
|
+
if (!state.errors[communityKey] && !hasRetriableError) {
|
|
250
|
+
return state;
|
|
251
|
+
}
|
|
252
|
+
return Object.assign(Object.assign({}, state), { errors: clearStoredCommunityErrors(state, communityKey), syncStatuses: hasRetriableError
|
|
253
|
+
? Object.assign(Object.assign({}, state.syncStatuses), { [communityKey]: omitLastRetriableError(syncStatus) }) : state.syncStatuses });
|
|
254
|
+
};
|
|
114
255
|
export const isRetriableCommunityLoadError = (error) => {
|
|
115
256
|
const details = error.details;
|
|
116
257
|
return Boolean(details &&
|
|
@@ -118,8 +259,21 @@ export const isRetriableCommunityLoadError = (error) => {
|
|
|
118
259
|
"retriableError" in details &&
|
|
119
260
|
details.retriableError === true);
|
|
120
261
|
};
|
|
262
|
+
const recordRetriableCommunityError = (setState, communityKey, error) => {
|
|
263
|
+
setState((state) => {
|
|
264
|
+
const syncStatus = state.syncStatuses[communityKey];
|
|
265
|
+
// a deleted community has no sync status left to report on
|
|
266
|
+
if (!syncStatus) {
|
|
267
|
+
return state;
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
syncStatuses: Object.assign(Object.assign({}, state.syncStatuses), { [communityKey]: Object.assign(Object.assign({}, syncStatus), { lastRetriableError: error }) }),
|
|
271
|
+
};
|
|
272
|
+
});
|
|
273
|
+
};
|
|
121
274
|
const scheduleCommunityError = (setState, communityKey, error) => {
|
|
122
275
|
if (isRetriableCommunityLoadError(error)) {
|
|
276
|
+
recordRetriableCommunityError(setState, communityKey, error);
|
|
123
277
|
return;
|
|
124
278
|
}
|
|
125
279
|
const timeout = setTimeout(() => {
|
|
@@ -247,6 +401,11 @@ const communitiesStore = createStore((setState, getState) => ({
|
|
|
247
401
|
if (!isFromCache) {
|
|
248
402
|
yield communitiesDatabase.setItem(communityKey, firstCommunityState);
|
|
249
403
|
}
|
|
404
|
+
// another load of this community, e.g. by another account, finished first and keeps it
|
|
405
|
+
// updating, so this instance is discarded before it starts updating
|
|
406
|
+
if (liveCommunities[communityKey]) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
250
409
|
log("communitiesStore.addCommunityToStore", {
|
|
251
410
|
communityAddressOrRef,
|
|
252
411
|
communityKey,
|
|
@@ -256,16 +415,46 @@ const communitiesStore = createStore((setState, getState) => ({
|
|
|
256
415
|
setState((state) => ({
|
|
257
416
|
communities: Object.assign(Object.assign({}, state.communities), { [communityKey]: firstCommunityState }),
|
|
258
417
|
}));
|
|
259
|
-
|
|
260
|
-
|
|
418
|
+
const liveCommunity = {
|
|
419
|
+
community,
|
|
420
|
+
communityAddressOrRef,
|
|
421
|
+
// pkc-js re-emits the record a community was created with when update() starts
|
|
422
|
+
replayRecordId: isFromCache ? getCommunityRecordId(firstCommunityState) : undefined,
|
|
423
|
+
replayRefreshesFetchedAt: true,
|
|
424
|
+
openingUpdatePending: false,
|
|
425
|
+
stopWhenOpeningUpdateEnds: false,
|
|
426
|
+
onUpdatingStateChange: (updatingState) => {
|
|
427
|
+
setState((state) => {
|
|
428
|
+
const syncStatuses = getNextSyncStatuses(state, communityKey, normalizeCommunitySyncState(updatingState));
|
|
429
|
+
const _a = syncStatuses[communityKey], { stoppedUntilUsed: _stoppedUntilUsed } = _a, syncStatus = __rest(_a, ["stoppedUntilUsed"]);
|
|
430
|
+
// the loop is not running when the store stopped the community itself
|
|
431
|
+
syncStatuses[communityKey] =
|
|
432
|
+
updatingState === "stopped" && !liveCommunity.updateInterval
|
|
433
|
+
? Object.assign(Object.assign({}, syncStatus), { stoppedUntilUsed: true }) : syncStatus;
|
|
434
|
+
return {
|
|
435
|
+
syncStatuses,
|
|
436
|
+
communities: Object.assign(Object.assign({}, state.communities), { [communityKey]: Object.assign(Object.assign({}, state.communities[communityKey]), { updatingState }) }),
|
|
437
|
+
};
|
|
438
|
+
});
|
|
439
|
+
if (updatingState === "succeeded" ||
|
|
440
|
+
updatingState === "failed" ||
|
|
441
|
+
updatingState === "waiting-retry") {
|
|
442
|
+
endOpeningCommunityUpdate(communityKey, liveCommunity);
|
|
443
|
+
}
|
|
444
|
+
},
|
|
445
|
+
};
|
|
261
446
|
// the community has published new posts
|
|
262
447
|
community.on("update", (updatedCommunity) => __awaiter(this, void 0, void 0, function* () {
|
|
263
448
|
clearPendingCommunityErrors(communityKey);
|
|
264
449
|
setState((state) => clearStoredCommunityErrorsState(state, communityKey));
|
|
265
|
-
const
|
|
266
|
-
getCommunityRecordId(updatedCommunity) ===
|
|
267
|
-
|
|
268
|
-
if (
|
|
450
|
+
const isRecordReplay = liveCommunity.replayRecordId !== undefined &&
|
|
451
|
+
getCommunityRecordId(updatedCommunity) === liveCommunity.replayRecordId;
|
|
452
|
+
liveCommunity.replayRecordId = undefined;
|
|
453
|
+
if (isRecordReplay && !liveCommunity.replayRefreshesFetchedAt) {
|
|
454
|
+
// a restarted loop replays the record the store already has, it was not fetched now
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
if (isRecordReplay) {
|
|
269
458
|
// the replayed record is already in the store and the database, only refresh fetchedAt
|
|
270
459
|
// like a fetched update would, without cloning and rewriting the whole record
|
|
271
460
|
setState((state) => {
|
|
@@ -287,27 +476,26 @@ const communitiesStore = createStore((setState, getState) => ({
|
|
|
287
476
|
// add fetchedAt to be able to expire the cache
|
|
288
477
|
// NOTE: fetchedAt is undefined on owner communities because never stale
|
|
289
478
|
updatedCommunity.fetchedAt = Math.floor(Date.now() / 1000);
|
|
290
|
-
|
|
479
|
+
// pkc-js reports the fetch succeeded right after this event, so store the record first:
|
|
480
|
+
// a succeeded sync state must not be shown with the previous record, e.g. a stale one
|
|
481
|
+
// kept while the community was not updating
|
|
482
|
+
setState((state) => (Object.assign(Object.assign({}, state), { communities: Object.assign(Object.assign({}, state.communities), { [communityKey]: updatedCommunity }) })));
|
|
291
483
|
log("communitiesStore community update", {
|
|
292
484
|
communityAddressOrRef,
|
|
293
485
|
communityKey,
|
|
294
486
|
updatedCommunity,
|
|
295
487
|
account,
|
|
296
488
|
});
|
|
297
|
-
|
|
489
|
+
yield communitiesDatabase.setItem(communityKey, updatedCommunity);
|
|
298
490
|
// if a community has a role with an account's address add it to the account.communities
|
|
299
491
|
accountsStore
|
|
300
492
|
.getState()
|
|
301
493
|
.accountsActionsInternal.addCommunityRoleToAccountsCommunities(updatedCommunity);
|
|
302
494
|
// add page comments to communitiesPagesStore so they can be used in useComment
|
|
303
495
|
communitiesPagesStore.getState().addCommunityPageCommentsToStore(updatedCommunity);
|
|
496
|
+
endOpeningCommunityUpdate(communityKey, liveCommunity);
|
|
304
497
|
}));
|
|
305
|
-
community.on("updatingstatechange",
|
|
306
|
-
setState((state) => ({
|
|
307
|
-
syncStatuses: getNextSyncStatuses(state, communityKey, normalizeCommunitySyncState(updatingState)),
|
|
308
|
-
communities: Object.assign(Object.assign({}, state.communities), { [communityKey]: Object.assign(Object.assign({}, state.communities[communityKey]), { updatingState }) }),
|
|
309
|
-
}));
|
|
310
|
-
});
|
|
498
|
+
community.on("updatingstatechange", liveCommunity.onUpdatingStateChange);
|
|
311
499
|
community.on("error", (error) => {
|
|
312
500
|
scheduleCommunityError(setState, communityKey, error);
|
|
313
501
|
});
|
|
@@ -334,7 +522,13 @@ const communitiesStore = createStore((setState, getState) => ({
|
|
|
334
522
|
});
|
|
335
523
|
});
|
|
336
524
|
listeners.push(community);
|
|
337
|
-
|
|
525
|
+
liveCommunities[communityKey] = liveCommunity;
|
|
526
|
+
startCommunityUpdatePolling(communityKey, liveCommunity);
|
|
527
|
+
// loaded without a subscriber, e.g. by a store-level caller: update until the grace period
|
|
528
|
+
// ends, and at least until the first fetch ends
|
|
529
|
+
if (!hasCommunityUpdateSubscribers(communityKey)) {
|
|
530
|
+
scheduleCommunityUpdateStop(communityKey);
|
|
531
|
+
}
|
|
338
532
|
}
|
|
339
533
|
catch (error) {
|
|
340
534
|
updateCommunitySyncStatus(setState, communityKey, "failed");
|
|
@@ -345,6 +539,44 @@ const communitiesStore = createStore((setState, getState) => ({
|
|
|
345
539
|
}
|
|
346
540
|
});
|
|
347
541
|
},
|
|
542
|
+
// keep a community updating while a subscriber (a mounted hook or feed) needs it, and restart
|
|
543
|
+
// its update loop if it stopped after its previous subscribers left
|
|
544
|
+
startCommunityAutoUpdate(communityAddressOrRef, subscriberId, account) {
|
|
545
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
546
|
+
const communityKey = getCommunityKey(communityAddressOrRef);
|
|
547
|
+
assert(communityKey !== "" && typeof communityKey === "string", `communitiesStore.startCommunityAutoUpdate invalid communityAddress argument '${communityAddressOrRef}'`);
|
|
548
|
+
assert(subscriberId && typeof subscriberId === "string", `communitiesStore.startCommunityAutoUpdate invalid subscriberId argument '${subscriberId}'`);
|
|
549
|
+
if (!communityUpdateSubscribers[communityKey]) {
|
|
550
|
+
communityUpdateSubscribers[communityKey] = new Set();
|
|
551
|
+
}
|
|
552
|
+
communityUpdateSubscribers[communityKey].add(subscriberId);
|
|
553
|
+
clearCommunityStopTimer(communityKey);
|
|
554
|
+
const liveCommunity = liveCommunities[communityKey];
|
|
555
|
+
if (liveCommunity) {
|
|
556
|
+
liveCommunity.stopWhenOpeningUpdateEnds = false;
|
|
557
|
+
yield resumeCommunityUpdatePolling(communityKey);
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
yield getState().addCommunityToStore(communityAddressOrRef, account);
|
|
561
|
+
});
|
|
562
|
+
},
|
|
563
|
+
// the community stops updating a grace period after its last subscriber leaves
|
|
564
|
+
stopCommunityAutoUpdate(communityAddressOrRef, subscriberId) {
|
|
565
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
566
|
+
const communityKey = getCommunityKey(communityAddressOrRef);
|
|
567
|
+
const subscribers = communityUpdateSubscribers[communityKey];
|
|
568
|
+
if (!(subscribers === null || subscribers === void 0 ? void 0 : subscribers.delete(subscriberId))) {
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
if (subscribers.size > 0) {
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
delete communityUpdateSubscribers[communityKey];
|
|
575
|
+
if (liveCommunities[communityKey]) {
|
|
576
|
+
scheduleCommunityUpdateStop(communityKey);
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
},
|
|
348
580
|
refreshCommunity(communityAddressOrRef, account) {
|
|
349
581
|
return __awaiter(this, void 0, void 0, function* () {
|
|
350
582
|
const communityLookupOptions = getCommunityLookupOptions(communityAddressOrRef);
|
|
@@ -459,7 +691,11 @@ export const resetCommunitiesStore = () => __awaiter(void 0, void 0, void 0, fun
|
|
|
459
691
|
// remove all event listeners
|
|
460
692
|
listeners.forEach((listener) => listener.removeAllListeners());
|
|
461
693
|
listeners.length = 0;
|
|
462
|
-
Object.keys(
|
|
694
|
+
Object.keys(liveCommunities).forEach(stopCommunityUpdatePolling);
|
|
695
|
+
Object.keys(communityStopTimers).forEach(clearCommunityStopTimer);
|
|
696
|
+
for (const communityKey in communityUpdateSubscribers) {
|
|
697
|
+
delete communityUpdateSubscribers[communityKey];
|
|
698
|
+
}
|
|
463
699
|
// destroy all component subscriptions to the store
|
|
464
700
|
communitiesStore.destroy();
|
|
465
701
|
// restore original state
|