Page MenuHomePhabricator

D13583.id44849.diff
No OneTemporary

D13583.id44849.diff

diff --git a/lib/components/base-auto-join-community-handler.react.js b/lib/components/base-auto-join-community-handler.react.js
--- a/lib/components/base-auto-join-community-handler.react.js
+++ b/lib/components/base-auto-join-community-handler.react.js
@@ -31,16 +31,24 @@
createDefaultHTTPRequestHeaders,
} from '../utils/services-utils.js';
+type JoinStatus = 'inactive' | 'joining' | 'joined';
+
type CommunityToAutoJoin = {
+ +batch: number,
+communityID: string,
+keyserverOverride: ?KeyserverOverride,
- +joinStatus: 'inactive' | 'joining' | 'joined',
+ +joinStatus: JoinStatus,
};
-type CommunitiesToAutoJoin = {
+type CommunityDatas = {
+[communityID: string]: CommunityToAutoJoin,
};
+type CommunitiesToAutoJoin = {
+ +curBatch: number,
+ +communityDatas: CommunityDatas,
+};
+
type Props = {
+calendarQuery: () => CalendarQuery,
};
@@ -66,7 +74,7 @@
state => state.keyserverStore.keyserverInfos,
);
- const [communitiesToAutoJoin, setCommunitiesToAutoJoin] =
+ const [communitiesToAutoJoin, baseSetCommunitiesToAutoJoin] =
React.useState<?CommunitiesToAutoJoin>();
const prevCanQueryRef = React.useRef<?boolean>();
@@ -106,7 +114,9 @@
channel => channel.id,
);
- const promises: { [string]: Promise<?CommunityToAutoJoin> } = {};
+ const promises: {
+ [string]: Promise<?$Diff<CommunityToAutoJoin, { +batch: number }>>,
+ } = {};
for (const channelID of followedFarcasterChannelIDs) {
promises[channelID] = (async () => {
@@ -149,14 +159,19 @@
const filteredCommunitiesObj = _pickBy(Boolean)(communitiesObj);
- const communitesToJoin: { ...CommunitiesToAutoJoin } = {};
+ const communityDatas: { ...CommunityDatas } = {};
+ let i = 0;
for (const key in filteredCommunitiesObj) {
- const communityID = filteredCommunitiesObj[key].communityID;
- communitesToJoin[communityID] = filteredCommunitiesObj[key];
+ const communityObject = filteredCommunitiesObj[key];
+ const communityID = communityObject.communityID;
+ communityDatas[communityID] = {
+ ...communityObject,
+ batch: Math.floor(i++ / 5),
+ };
}
- setCommunitiesToAutoJoin(communitesToJoin);
+ baseSetCommunitiesToAutoJoin({ communityDatas, curBatch: 0 });
})();
}, [
threadInfos,
@@ -169,18 +184,66 @@
canQuery,
]);
+ const potentiallyIncrementBatch: (
+ ?CommunitiesToAutoJoin,
+ ) => ?CommunitiesToAutoJoin = React.useCallback(input => {
+ if (!input) {
+ return input;
+ }
+
+ let shouldIncrementBatch = false;
+ const { curBatch, communityDatas } = input;
+ for (const communityToAutoJoin of Object.values(communityDatas)) {
+ const { batch, joinStatus } = communityToAutoJoin;
+
+ if (batch !== curBatch) {
+ continue;
+ }
+
+ if (joinStatus !== 'joined') {
+ // One of the current batch isn't complete yet
+ return input;
+ }
+
+ // We have at least one complete in the current batch
+ shouldIncrementBatch = true;
+ }
+
+ // If we get here, all of the current batch is complete
+ if (shouldIncrementBatch) {
+ return { communityDatas, curBatch: curBatch + 1 };
+ }
+
+ return input;
+ }, []);
+
+ const setCommunitiesToAutoJoin: SetState<?CommunitiesToAutoJoin> =
+ React.useCallback(
+ next => {
+ if (typeof next !== 'function') {
+ baseSetCommunitiesToAutoJoin(potentiallyIncrementBatch(next));
+ return;
+ }
+ baseSetCommunitiesToAutoJoin(prev => {
+ const result = next(prev);
+ return potentiallyIncrementBatch(result);
+ });
+ },
+ [potentiallyIncrementBatch],
+ );
+
const joinHandlers = React.useMemo(() => {
if (!communitiesToAutoJoin) {
return null;
}
- return Object.keys(communitiesToAutoJoin).map(id => {
- const communityToAutoJoin = communitiesToAutoJoin[id];
+ const { curBatch, communityDatas } = communitiesToAutoJoin;
- const { communityID, keyserverOverride, joinStatus } =
- communityToAutoJoin;
+ return Object.values(communityDatas).map(communityData => {
+ const { batch, communityID, keyserverOverride, joinStatus } =
+ communityData;
- if (joinStatus === 'joined') {
+ if (batch !== curBatch || joinStatus === 'joined') {
return null;
}
@@ -190,12 +253,12 @@
communityID={communityID}
keyserverOverride={keyserverOverride}
calendarQuery={calendarQuery}
- communitiesToAutoJoin={communitiesToAutoJoin}
+ joinStatus={joinStatus}
setCommunitiesToAutoJoin={setCommunitiesToAutoJoin}
/>
);
});
- }, [calendarQuery, communitiesToAutoJoin]);
+ }, [calendarQuery, communitiesToAutoJoin, setCommunitiesToAutoJoin]);
return joinHandlers;
}
@@ -204,7 +267,7 @@
+communityID: string,
+keyserverOverride: ?KeyserverOverride,
+calendarQuery: () => CalendarQuery,
- +communitiesToAutoJoin: CommunitiesToAutoJoin,
+ +joinStatus: JoinStatus,
+setCommunitiesToAutoJoin: SetState<?CommunitiesToAutoJoin>,
};
@@ -213,7 +276,7 @@
communityID,
keyserverOverride,
calendarQuery,
- communitiesToAutoJoin,
+ joinStatus,
setCommunitiesToAutoJoin,
} = props;
@@ -233,59 +296,41 @@
defaultSubscription: defaultThreadSubscription,
});
- React.useEffect(() => {
- const joinStatus = communitiesToAutoJoin[communityID]?.joinStatus;
- if (joinStatus !== 'inactive') {
- return;
- }
-
- void joinCommunity();
- }, [
- communitiesToAutoJoin,
- communityID,
- joinCommunity,
- setCommunitiesToAutoJoin,
- ]);
-
- React.useEffect(() => {
- if (step !== 'add_keyserver') {
- return;
- }
-
- setCommunitiesToAutoJoin(prev => {
- if (!prev) {
- return null;
- }
+ const setJoinStatus = React.useCallback(
+ (newJoinStatus: JoinStatus) => {
+ setCommunitiesToAutoJoin(prev => {
+ if (!prev) {
+ return null;
+ }
- return {
- ...prev,
- [communityID]: {
- ...prev[communityID],
- joinStatus: 'joining',
- },
- };
- });
- }, [communityID, setCommunitiesToAutoJoin, step]);
+ return {
+ ...prev,
+ communityDatas: {
+ ...prev.communityDatas,
+ [communityID]: {
+ ...prev.communityDatas[communityID],
+ joinStatus: newJoinStatus,
+ },
+ },
+ };
+ });
+ },
+ [communityID, setCommunitiesToAutoJoin],
+ );
React.useEffect(() => {
- if (step !== 'finished') {
+ if (joinStatus !== 'inactive') {
return;
}
-
- setCommunitiesToAutoJoin(prev => {
- if (!prev) {
- return null;
+ void (async () => {
+ try {
+ setJoinStatus('joining');
+ await joinCommunity();
+ } finally {
+ setJoinStatus('joined');
}
-
- return {
- ...prev,
- [communityID]: {
- ...prev[communityID],
- joinStatus: 'joined',
- },
- };
- });
- }, [communityID, step, setCommunitiesToAutoJoin]);
+ })();
+ }, [joinStatus, communityID, setJoinStatus, joinCommunity]);
return null;
}

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 3, 10:44 AM (20 h, 2 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2611375
Default Alt Text
D13583.id44849.diff (7 KB)

Event Timeline