@bitsocial/bitsocial-react-hooks 0.1.12 → 0.1.13
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
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## [0.1.13](https://github.com/bitsocialnet/bitsocial-react-hooks/compare/v0.1.12...v0.1.13) (2026-05-27)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **communities:** suppress retriable load errors ([33c3dc1](https://github.com/bitsocialnet/bitsocial-react-hooks/commit/33c3dc1f870f56728e496906b0b2e60ce98128c2))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
1
10
|
## [0.1.12](https://github.com/bitsocialnet/bitsocial-react-hooks/compare/v0.1.11...v0.1.12) (2026-05-26)
|
|
2
11
|
|
|
3
12
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"communities-store.d.ts","sourceRoot":"","sources":["../../../src/stores/communities/communities-store.ts"],"names":[],"mappings":"AAQA,OAAO,EAEL,WAAW,EAIZ,MAAM,aAAa,CAAC;AAcrB,eAAO,MAAM,4BAA4B,QAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"communities-store.d.ts","sourceRoot":"","sources":["../../../src/stores/communities/communities-store.ts"],"names":[],"mappings":"AAQA,OAAO,EAEL,WAAW,EAIZ,MAAM,aAAa,CAAC;AAcrB,eAAO,MAAM,4BAA4B,QAAiB,CAAC;AA6I3D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,EAAE;QAAE,CAAC,gBAAgB,EAAE,MAAM,GAAG,KAAK,EAAE,CAAA;KAAE,CAAC;IAChD,mBAAmB,EAAE,QAAQ,CAAC;IAC9B,gBAAgB,EAAE,QAAQ,CAAC;IAC3B,aAAa,EAAE,QAAQ,CAAC;IACxB,eAAe,EAAE,QAAQ,CAAC;IAC1B,eAAe,EAAE,QAAQ,CAAC;CAC3B,CAAC;AAEF,QAAA,MAAM,gBAAgB,+EAmVrB,CAAC;AAKF,eAAO,MAAM,qBAAqB,qBAWjC,CAAC;AAGF,eAAO,MAAM,gCAAgC,qBAG5C,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
|
|
@@ -80,12 +80,29 @@ const clearStoredCommunityErrors = (state, communityKey) => {
|
|
|
80
80
|
delete nextErrors[communityKey];
|
|
81
81
|
return nextErrors;
|
|
82
82
|
};
|
|
83
|
+
const isRetriableCommunityLoadError = (error) => {
|
|
84
|
+
const details = error.details;
|
|
85
|
+
return Boolean(details &&
|
|
86
|
+
typeof details === "object" &&
|
|
87
|
+
"retriableError" in details &&
|
|
88
|
+
details.retriableError === true);
|
|
89
|
+
};
|
|
83
90
|
const scheduleCommunityError = (setState, communityKey, error) => {
|
|
91
|
+
if (isRetriableCommunityLoadError(error)) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
84
94
|
const timeout = setTimeout(() => {
|
|
85
|
-
|
|
86
|
-
if ((
|
|
95
|
+
const pendingTimeouts = pendingCommunityErrorTimers[communityKey];
|
|
96
|
+
if (!(pendingTimeouts === null || pendingTimeouts === void 0 ? void 0 : pendingTimeouts.includes(timeout))) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const remainingTimeouts = pendingTimeouts.filter((pendingTimeout) => pendingTimeout !== timeout);
|
|
100
|
+
if (remainingTimeouts.length === 0) {
|
|
87
101
|
delete pendingCommunityErrorTimers[communityKey];
|
|
88
102
|
}
|
|
103
|
+
else {
|
|
104
|
+
pendingCommunityErrorTimers[communityKey] = remainingTimeouts;
|
|
105
|
+
}
|
|
89
106
|
setState((state) => {
|
|
90
107
|
const communityErrors = state.errors[communityKey] || [];
|
|
91
108
|
return Object.assign(Object.assign({}, state), { errors: Object.assign(Object.assign({}, state.errors), { [communityKey]: [...communityErrors, error] }) });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3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ib/localforage-lru\";\nconst communitiesDatabase = localForageLru.createInstance({\n name: \"bitsocialReactHooks-communities\",\n size: 500,\n});\nimport Logger from \"@pkcprotocol/pkc-logger\";\nconst log = Logger(\"bitsocial-react-hooks:communities:stores\");\nimport {\n Community,\n Communities,\n Account,\n CommunityIdentifier,\n CreateCommunityOptions,\n} from \"../../types\";\nimport utils from \"../../lib/utils\";\nimport createStore from \"zustand\";\nimport accountsStore from \"../accounts\";\nimport communitiesPagesStore from \"../communities-pages\";\nimport { getCommunityLookupOptions, getCommunityRefKey } from \"../../lib/community-ref\";\nimport {\n createPkcCommunity,\n getPkcCommunity,\n getPkcCommunityAddresses,\n getPkcCreateCommunity,\n getPkcGetCommunity,\n} from \"../../lib/pkc-compat\";\n\nexport const COMMUNITY_UPDATE_INTERVAL_MS = 15 * 60 * 1000;\n\nlet pkcGetCommunityPending: { [key: string]: boolean } = {};\n// Key pollers by community so delete/reset can stop the matching live instance.\nconst communityUpdatePollers: {\n [communityKey: string]: {\n community: any;\n updateInterval: ReturnType<typeof setInterval>;\n };\n} = {};\n\n// reset all event listeners in between tests\nconst listeners: any = [];\n\nconst COMMUNITY_ERROR_UPDATE_GRACE_MS = 1000;\nconst pendingCommunityErrorTimers: {\n [communityKey: string]: ReturnType<typeof setTimeout>[];\n} = {};\n\nconst createCommunityWithLookupFallback = async (\n pkc: any,\n communityLookupOptions: { address?: string; name?: string; publicKey?: string },\n communityKey: string,\n) => {\n const supportsAddressLookup = \"address\" in communityLookupOptions;\n const community = await createPkcCommunity(pkc, communityLookupOptions);\n if (community?.address || supportsAddressLookup) {\n return community;\n }\n throw Error(`communitiesStore.addCommunityToStore failed getting community '${communityKey}'`);\n};\n\nconst updateCommunity = (\n community: Community,\n {\n communityAddressOrRef,\n communityKey,\n }: { communityAddressOrRef: string | CommunityIdentifier; communityKey: string },\n) => {\n community.update().catch((error: unknown) =>\n log.trace(\"community.update error\", {\n communityAddressOrRef,\n communityKey,\n community,\n error,\n }),\n );\n};\n\nconst startCommunityUpdatePolling = (\n community: Community,\n {\n communityAddressOrRef,\n communityKey,\n }: { communityAddressOrRef: string | CommunityIdentifier; communityKey: string },\n) => {\n stopCommunityUpdatePolling(communityKey);\n updateCommunity(community, { communityAddressOrRef, communityKey });\n communityUpdatePollers[communityKey] = {\n community,\n updateInterval: setInterval(\n () => updateCommunity(community, { communityAddressOrRef, communityKey }),\n COMMUNITY_UPDATE_INTERVAL_MS,\n ),\n };\n};\n\nconst stopCommunityUpdatePolling = (communityKey: string) => {\n const polling = communityUpdatePollers[communityKey];\n if (!polling) {\n return;\n }\n\n clearPendingCommunityErrors(communityKey);\n clearInterval(polling.updateInterval);\n polling.community.removeAllListeners();\n const listenerIndex = listeners.indexOf(polling.community);\n if (listenerIndex >= 0) {\n listeners.splice(listenerIndex, 1);\n }\n delete communityUpdatePollers[communityKey];\n};\n\nconst clearPendingCommunityErrors = (communityKey: string) => {\n pendingCommunityErrorTimers[communityKey]?.forEach((timeout) => clearTimeout(timeout));\n delete pendingCommunityErrorTimers[communityKey];\n};\n\nconst clearStoredCommunityErrors = (state: CommunitiesState, communityKey: string) => {\n if (!state.errors[communityKey]) {\n return state.errors;\n }\n const nextErrors = { ...state.errors };\n delete nextErrors[communityKey];\n return nextErrors;\n};\n\nconst scheduleCommunityError = (setState: Function, communityKey: string, error: Error) => {\n const timeout = setTimeout(() => {\n pendingCommunityErrorTimers[communityKey] = (\n pendingCommunityErrorTimers[communityKey] || []\n ).filter((pendingTimeout) => pendingTimeout !== timeout);\n if ((pendingCommunityErrorTimers[communityKey] || []).length === 0) {\n delete pendingCommunityErrorTimers[communityKey];\n }\n setState((state: CommunitiesState) => {\n const communityErrors = state.errors[communityKey] || [];\n return {\n ...state,\n errors: { ...state.errors, [communityKey]: [...communityErrors, error] },\n };\n });\n }, COMMUNITY_ERROR_UPDATE_GRACE_MS);\n\n pendingCommunityErrorTimers[communityKey] = [\n ...(pendingCommunityErrorTimers[communityKey] || []),\n timeout,\n ];\n};\n\nexport type CommunitiesState = {\n communities: Communities;\n errors: { [communityAddress: string]: Error[] };\n addCommunityToStore: Function;\n refreshCommunity: Function;\n editCommunity: Function;\n createCommunity: Function;\n deleteCommunity: Function;\n};\n\nconst communitiesStore = createStore<CommunitiesState>(\n (setState: Function, getState: Function) => ({\n communities: {},\n errors: {},\n\n async addCommunityToStore(\n communityAddressOrRef: string | CommunityIdentifier,\n account: Account,\n ) {\n const communityLookupOptions = getCommunityLookupOptions(communityAddressOrRef);\n const communityKey =\n typeof communityAddressOrRef === \"string\"\n ? communityAddressOrRef\n : getCommunityRefKey(communityAddressOrRef);\n assert(\n communityKey !== \"\" && typeof communityKey === \"string\",\n `communitiesStore.addCommunityToStore invalid communityAddress argument '${communityAddressOrRef}'`,\n );\n assert(\n typeof getPkcCreateCommunity(account?.pkc) === \"function\",\n `communitiesStore.addCommunityToStore invalid account argument '${account}'`,\n );\n\n // community is in store already, do nothing\n const { communities } = getState();\n let community: Community | undefined = communities[communityKey];\n const pendingKey = communityKey + account.id;\n if (community || pkcGetCommunityPending[pendingKey]) {\n return;\n }\n\n // start trying to get community\n pkcGetCommunityPending[pendingKey] = true;\n let errorGettingCommunity: any;\n try {\n // try to find community in owner communities\n if (getPkcCommunityAddresses(account.pkc).includes(communityKey)) {\n try {\n community = await createCommunityWithLookupFallback(\n account.pkc,\n communityLookupOptions,\n communityKey,\n );\n } catch (e) {\n errorGettingCommunity = e;\n }\n }\n\n // try to find community in database\n let fetchedAt: number | undefined;\n if (!community) {\n const communityData: any = await communitiesDatabase.getItem(communityKey);\n if (communityData) {\n fetchedAt = communityData.fetchedAt;\n delete communityData.fetchedAt; // not part of pkc-js schema\n try {\n community = await createPkcCommunity(account.pkc, communityData);\n } catch (e) {\n fetchedAt = undefined;\n // need to log this always or it could silently fail in production and cache never be used\n console.error(\"failed pkc.createCommunity(cachedCommunity)\", {\n cachedCommunity: communityData,\n error: e,\n });\n }\n }\n if (community) {\n // add page comments to communitiesPagesStore so they can be used in useComment\n communitiesPagesStore.getState().addCommunityPageCommentsToStore(community);\n }\n }\n\n // community not in database, try to fetch from pkc-js\n if (!community) {\n try {\n community = await createCommunityWithLookupFallback(\n account.pkc,\n communityLookupOptions,\n communityKey,\n );\n } catch (e) {\n errorGettingCommunity = e;\n }\n }\n\n // failure getting community\n if (!community) {\n if (errorGettingCommunity) {\n setState((state: CommunitiesState) => {\n let communityErrors = state.errors[communityKey] || [];\n communityErrors = [...communityErrors, errorGettingCommunity];\n return { ...state, errors: { ...state.errors, [communityKey]: communityErrors } };\n });\n }\n\n throw (\n errorGettingCommunity ||\n Error(`communitiesStore.addCommunityToStore failed getting community '${communityKey}'`)\n );\n }\n\n // success getting community\n const firstCommunityState = utils.clone({ ...community, fetchedAt });\n await communitiesDatabase.setItem(communityKey, firstCommunityState);\n log(\"communitiesStore.addCommunityToStore\", {\n communityAddressOrRef,\n communityKey,\n community,\n account,\n });\n setState((state: any) => ({\n communities: { ...state.communities, [communityKey]: firstCommunityState },\n }));\n\n // the community has published new posts\n community.on(\"update\", async (updatedCommunity: Community) => {\n clearPendingCommunityErrors(communityKey);\n setState((state: CommunitiesState) => ({\n ...state,\n errors: clearStoredCommunityErrors(state, communityKey),\n }));\n updatedCommunity = utils.clone(updatedCommunity);\n\n // add fetchedAt to be able to expire the cache\n // NOTE: fetchedAt is undefined on owner communities because never stale\n updatedCommunity.fetchedAt = Math.floor(Date.now() / 1000);\n\n await communitiesDatabase.setItem(communityKey, updatedCommunity);\n log(\"communitiesStore community update\", {\n communityAddressOrRef,\n communityKey,\n updatedCommunity,\n account,\n });\n setState((state: any) => ({\n ...state,\n communities: { ...state.communities, [communityKey]: updatedCommunity },\n }));\n\n // if a community has a role with an account's address add it to the account.communities\n accountsStore\n .getState()\n .accountsActionsInternal.addCommunityRoleToAccountsCommunities(updatedCommunity);\n\n // add page comments to communitiesPagesStore so they can be used in useComment\n communitiesPagesStore.getState().addCommunityPageCommentsToStore(updatedCommunity);\n });\n\n community.on(\"updatingstatechange\", (updatingState: string) => {\n setState((state: CommunitiesState) => ({\n communities: {\n ...state.communities,\n [communityKey]: { ...state.communities[communityKey], updatingState },\n },\n }));\n });\n\n community.on(\"error\", (error: Error) => {\n scheduleCommunityError(setState, communityKey, error);\n });\n\n // set clients on community so the frontend can display it, dont persist in db because a reload cancels updating\n utils.clientsOnStateChange(\n community?.clients,\n (clientState: string, clientType: string, clientUrl: string, chainTicker?: string) => {\n setState((state: CommunitiesState) => {\n // make sure not undefined, sometimes happens in e2e tests\n if (!state.communities[communityKey]) {\n return {};\n }\n const clients = { ...state.communities[communityKey]?.clients };\n const client = { state: clientState };\n if (chainTicker) {\n const chainProviders = { ...clients[clientType][chainTicker], [clientUrl]: client };\n clients[clientType] = { ...clients[clientType], [chainTicker]: chainProviders };\n } else {\n clients[clientType] = { ...clients[clientType], [clientUrl]: client };\n }\n return {\n communities: {\n ...state.communities,\n [communityKey]: { ...state.communities[communityKey], clients },\n },\n };\n });\n },\n );\n\n listeners.push(community);\n startCommunityUpdatePolling(community, { communityAddressOrRef, communityKey });\n } finally {\n pkcGetCommunityPending[pendingKey] = false;\n }\n },\n\n async refreshCommunity(communityAddressOrRef: string | CommunityIdentifier, account: Account) {\n const communityLookupOptions = getCommunityLookupOptions(communityAddressOrRef);\n const communityKey =\n typeof communityAddressOrRef === \"string\"\n ? communityAddressOrRef\n : getCommunityRefKey(communityAddressOrRef);\n assert(\n communityKey !== \"\" && typeof communityKey === \"string\",\n `communitiesStore.refreshCommunity invalid communityAddress argument '${communityAddressOrRef}'`,\n );\n assert(\n typeof getPkcGetCommunity(account?.pkc) === \"function\",\n `communitiesStore.refreshCommunity invalid account argument '${account}'`,\n );\n\n const refreshedCommunity = utils.clone(\n await getPkcCommunity(account.pkc, communityLookupOptions),\n );\n refreshedCommunity.fetchedAt = Math.floor(Date.now() / 1000);\n\n await communitiesDatabase.setItem(communityKey, refreshedCommunity);\n log(\"communitiesStore.refreshCommunity\", {\n communityAddressOrRef,\n communityKey,\n refreshedCommunity,\n account,\n });\n setState((state: any) => ({\n communities: { ...state.communities, [communityKey]: refreshedCommunity },\n }));\n\n communitiesPagesStore.getState().addCommunityPageCommentsToStore(refreshedCommunity);\n\n return refreshedCommunity;\n },\n\n // user is the owner of the community and can edit it locally\n async editCommunity(communityAddress: string, communityEditOptions: any, account: Account) {\n assert(\n communityAddress !== \"\" && typeof communityAddress === \"string\",\n `communitiesStore.editCommunity invalid communityAddress argument '${communityAddress}'`,\n );\n assert(\n communityEditOptions && typeof communityEditOptions === \"object\",\n `communitiesStore.editCommunity invalid communityEditOptions argument '${communityEditOptions}'`,\n );\n assert(\n typeof getPkcCreateCommunity(account?.pkc) === \"function\",\n `communitiesStore.editCommunity invalid account argument '${account}'`,\n );\n\n // if not added to store first, community.update() is never called\n await getState().addCommunityToStore(communityAddress, account);\n\n // `communityAddress` is different from `communityEditOptions.address` when editing the community address\n const community = await createPkcCommunity(account.pkc, {\n address: communityAddress,\n });\n\n // could fix some test issues\n community.on(\"error\", console.log);\n\n await community.edit(communityEditOptions);\n\n const updatedCommunity = utils.clone(community);\n // edit db of both old and new community address to not break the UI\n await communitiesDatabase.setItem(communityAddress, updatedCommunity);\n await communitiesDatabase.setItem(community.address, updatedCommunity);\n log(\"communitiesStore.editCommunity\", {\n communityAddress,\n communityEditOptions,\n community,\n account,\n });\n setState((state: any) => ({\n communities: {\n ...state.communities,\n // edit react state of both old and new community address to not break the UI\n [communityAddress]: updatedCommunity,\n [community.address]: updatedCommunity,\n },\n }));\n },\n\n // internal action called by accountsActions.createCommunity\n async createCommunity(createCommunityOptions: CreateCommunityOptions, account: Account) {\n assert(\n !createCommunityOptions || typeof createCommunityOptions === \"object\",\n `communitiesStore.createCommunity invalid createCommunityOptions argument '${createCommunityOptions}'`,\n );\n if (!createCommunityOptions?.signer) {\n assert(\n !createCommunityOptions?.address,\n `communitiesStore.createCommunity createCommunityOptions.address '${createCommunityOptions?.address}' must be undefined to create a community`,\n );\n }\n assert(\n typeof getPkcCreateCommunity(account?.pkc) === \"function\",\n `communitiesStore.createCommunity invalid account argument '${account}'`,\n );\n\n const community = await createPkcCommunity(account.pkc, createCommunityOptions);\n\n // could fix some test issues\n community.on(\"error\", console.log);\n\n // if not added to store first, community.update() is never called\n await getState().addCommunityToStore(community.address, account);\n\n await communitiesDatabase.setItem(community.address, utils.clone(community));\n log(\"communitiesStore.createCommunity\", { createCommunityOptions, community, account });\n setState((state: any) => ({\n communities: { ...state.communities, [community.address]: utils.clone(community) },\n }));\n return community;\n },\n\n // internal action called by accountsActions.deleteCommunity\n async deleteCommunity(communityAddress: string, account: Account) {\n assert(\n communityAddress && typeof communityAddress === \"string\",\n `communitiesStore.deleteCommunity invalid communityAddress argument '${communityAddress}'`,\n );\n assert(\n typeof getPkcCreateCommunity(account?.pkc) === \"function\",\n `communitiesStore.deleteCommunity invalid account argument '${account}'`,\n );\n\n const community = await createPkcCommunity(account.pkc, {\n address: communityAddress,\n });\n\n // could fix some test issues\n community.on(\"error\", console.log);\n\n await community.delete();\n stopCommunityUpdatePolling(communityAddress);\n await communitiesDatabase.removeItem(communityAddress);\n log(\"communitiesStore.deleteCommunity\", { communityAddress, community, account });\n setState((state: any) => ({\n communities: { ...state.communities, [communityAddress]: undefined },\n }));\n },\n }),\n);\n\n// reset store in between tests\nconst originalState = communitiesStore.getState();\n// async function because some stores have async init\nexport const resetCommunitiesStore = async () => {\n pkcGetCommunityPending = {};\n Object.keys(pendingCommunityErrorTimers).forEach(clearPendingCommunityErrors);\n // remove all event listeners\n listeners.forEach((listener: any) => listener.removeAllListeners());\n listeners.length = 0;\n Object.keys(communityUpdatePollers).forEach(stopCommunityUpdatePolling);\n // destroy all component subscriptions to the store\n communitiesStore.destroy();\n // restore original state\n communitiesStore.setState(originalState);\n};\n\n// reset database and store in between tests\nexport const resetCommunitiesDatabaseAndStore = async () => {\n await localForageLru.createInstance({ name: \"bitsocialReactHooks-communities\" }).clear();\n await resetCommunitiesStore();\n};\n\nexport default communitiesStore;\n"]}
|
|
1
|
+
{"version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AAC,sBAAsB,CAAC,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC;IACxE,mDAAmD;IACnD,gBAAgB,CAAC,OAAO,EAAE,CAAC;IAC3B,yBAAyB;IACzB,gBAAgB,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;AAC3C,CAAC,CAAA,CAAC;AAEF,4CAA4C;AAC5C,MAAM,CAAC,MAAM,gCAAgC,GAAG,GAAS,EAAE;IACzD,MAAM,cAAc,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,iCAAiC,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC;IACzF,MAAM,qBAAqB,EAAE,CAAC;AAChC,CAAC,CAAA,CAAC;AAEF,eAAe,gBAAgB,CAAC","sourcesContent":["import assert from \"assert\";\nimport localForageLru from \"../../lib/localforage-lru\";\nconst communitiesDatabase = localForageLru.createInstance({\n name: \"bitsocialReactHooks-communities\",\n size: 500,\n});\nimport Logger from \"@pkcprotocol/pkc-logger\";\nconst log = Logger(\"bitsocial-react-hooks:communities:stores\");\nimport {\n Community,\n Communities,\n Account,\n CommunityIdentifier,\n CreateCommunityOptions,\n} from \"../../types\";\nimport utils from \"../../lib/utils\";\nimport createStore from \"zustand\";\nimport accountsStore from \"../accounts\";\nimport communitiesPagesStore from \"../communities-pages\";\nimport { getCommunityLookupOptions, getCommunityRefKey } from \"../../lib/community-ref\";\nimport {\n createPkcCommunity,\n getPkcCommunity,\n getPkcCommunityAddresses,\n getPkcCreateCommunity,\n getPkcGetCommunity,\n} from \"../../lib/pkc-compat\";\n\nexport const COMMUNITY_UPDATE_INTERVAL_MS = 15 * 60 * 1000;\n\nlet pkcGetCommunityPending: { [key: string]: boolean } = {};\n// Key pollers by community so delete/reset can stop the matching live instance.\nconst communityUpdatePollers: {\n [communityKey: string]: {\n community: any;\n updateInterval: ReturnType<typeof setInterval>;\n };\n} = {};\n\n// reset all event listeners in between tests\nconst listeners: any = [];\n\nconst COMMUNITY_ERROR_UPDATE_GRACE_MS = 1000;\nconst pendingCommunityErrorTimers: {\n [communityKey: string]: ReturnType<typeof setTimeout>[];\n} = {};\n\nconst createCommunityWithLookupFallback = async (\n pkc: any,\n communityLookupOptions: { address?: string; name?: string; publicKey?: string },\n communityKey: string,\n) => {\n const supportsAddressLookup = \"address\" in communityLookupOptions;\n const community = await createPkcCommunity(pkc, communityLookupOptions);\n if (community?.address || supportsAddressLookup) {\n return community;\n }\n throw Error(`communitiesStore.addCommunityToStore failed getting community '${communityKey}'`);\n};\n\nconst updateCommunity = (\n community: Community,\n {\n communityAddressOrRef,\n communityKey,\n }: { communityAddressOrRef: string | CommunityIdentifier; communityKey: string },\n) => {\n community.update().catch((error: unknown) =>\n log.trace(\"community.update error\", {\n communityAddressOrRef,\n communityKey,\n community,\n error,\n }),\n );\n};\n\nconst startCommunityUpdatePolling = (\n community: Community,\n {\n communityAddressOrRef,\n communityKey,\n }: { communityAddressOrRef: string | CommunityIdentifier; communityKey: string },\n) => {\n stopCommunityUpdatePolling(communityKey);\n updateCommunity(community, { communityAddressOrRef, communityKey });\n communityUpdatePollers[communityKey] = {\n community,\n updateInterval: setInterval(\n () => updateCommunity(community, { communityAddressOrRef, communityKey }),\n COMMUNITY_UPDATE_INTERVAL_MS,\n ),\n };\n};\n\nconst stopCommunityUpdatePolling = (communityKey: string) => {\n const polling = communityUpdatePollers[communityKey];\n if (!polling) {\n return;\n }\n\n clearPendingCommunityErrors(communityKey);\n clearInterval(polling.updateInterval);\n polling.community.removeAllListeners();\n const listenerIndex = listeners.indexOf(polling.community);\n if (listenerIndex >= 0) {\n listeners.splice(listenerIndex, 1);\n }\n delete communityUpdatePollers[communityKey];\n};\n\nconst clearPendingCommunityErrors = (communityKey: string) => {\n pendingCommunityErrorTimers[communityKey]?.forEach((timeout) => clearTimeout(timeout));\n delete pendingCommunityErrorTimers[communityKey];\n};\n\nconst clearStoredCommunityErrors = (state: CommunitiesState, communityKey: string) => {\n if (!state.errors[communityKey]) {\n return state.errors;\n }\n const nextErrors = { ...state.errors };\n delete nextErrors[communityKey];\n return nextErrors;\n};\n\nconst isRetriableCommunityLoadError = (error: Error) => {\n const details = (error as Error & { details?: unknown }).details;\n return Boolean(\n details &&\n typeof details === \"object\" &&\n \"retriableError\" in details &&\n (details as { retriableError?: unknown }).retriableError === true,\n );\n};\n\nconst scheduleCommunityError = (setState: Function, communityKey: string, error: Error) => {\n if (isRetriableCommunityLoadError(error)) {\n return;\n }\n\n const timeout = setTimeout(() => {\n const pendingTimeouts = pendingCommunityErrorTimers[communityKey];\n if (!pendingTimeouts?.includes(timeout)) {\n return;\n }\n\n const remainingTimeouts = pendingTimeouts.filter(\n (pendingTimeout) => pendingTimeout !== timeout,\n );\n if (remainingTimeouts.length === 0) {\n delete pendingCommunityErrorTimers[communityKey];\n } else {\n pendingCommunityErrorTimers[communityKey] = remainingTimeouts;\n }\n setState((state: CommunitiesState) => {\n const communityErrors = state.errors[communityKey] || [];\n return {\n ...state,\n errors: { ...state.errors, [communityKey]: [...communityErrors, error] },\n };\n });\n }, COMMUNITY_ERROR_UPDATE_GRACE_MS);\n\n pendingCommunityErrorTimers[communityKey] = [\n ...(pendingCommunityErrorTimers[communityKey] || []),\n timeout,\n ];\n};\n\nexport type CommunitiesState = {\n communities: Communities;\n errors: { [communityAddress: string]: Error[] };\n addCommunityToStore: Function;\n refreshCommunity: Function;\n editCommunity: Function;\n createCommunity: Function;\n deleteCommunity: Function;\n};\n\nconst communitiesStore = createStore<CommunitiesState>(\n (setState: Function, getState: Function) => ({\n communities: {},\n errors: {},\n\n async addCommunityToStore(\n communityAddressOrRef: string | CommunityIdentifier,\n account: Account,\n ) {\n const communityLookupOptions = getCommunityLookupOptions(communityAddressOrRef);\n const communityKey =\n typeof communityAddressOrRef === \"string\"\n ? communityAddressOrRef\n : getCommunityRefKey(communityAddressOrRef);\n assert(\n communityKey !== \"\" && typeof communityKey === \"string\",\n `communitiesStore.addCommunityToStore invalid communityAddress argument '${communityAddressOrRef}'`,\n );\n assert(\n typeof getPkcCreateCommunity(account?.pkc) === \"function\",\n `communitiesStore.addCommunityToStore invalid account argument '${account}'`,\n );\n\n // community is in store already, do nothing\n const { communities } = getState();\n let community: Community | undefined = communities[communityKey];\n const pendingKey = communityKey + account.id;\n if (community || pkcGetCommunityPending[pendingKey]) {\n return;\n }\n\n // start trying to get community\n pkcGetCommunityPending[pendingKey] = true;\n let errorGettingCommunity: any;\n try {\n // try to find community in owner communities\n if (getPkcCommunityAddresses(account.pkc).includes(communityKey)) {\n try {\n community = await createCommunityWithLookupFallback(\n account.pkc,\n communityLookupOptions,\n communityKey,\n );\n } catch (e) {\n errorGettingCommunity = e;\n }\n }\n\n // try to find community in database\n let fetchedAt: number | undefined;\n if (!community) {\n const communityData: any = await communitiesDatabase.getItem(communityKey);\n if (communityData) {\n fetchedAt = communityData.fetchedAt;\n delete communityData.fetchedAt; // not part of pkc-js schema\n try {\n community = await createPkcCommunity(account.pkc, communityData);\n } catch (e) {\n fetchedAt = undefined;\n // need to log this always or it could silently fail in production and cache never be used\n console.error(\"failed pkc.createCommunity(cachedCommunity)\", {\n cachedCommunity: communityData,\n error: e,\n });\n }\n }\n if (community) {\n // add page comments to communitiesPagesStore so they can be used in useComment\n communitiesPagesStore.getState().addCommunityPageCommentsToStore(community);\n }\n }\n\n // community not in database, try to fetch from pkc-js\n if (!community) {\n try {\n community = await createCommunityWithLookupFallback(\n account.pkc,\n communityLookupOptions,\n communityKey,\n );\n } catch (e) {\n errorGettingCommunity = e;\n }\n }\n\n // failure getting community\n if (!community) {\n if (errorGettingCommunity) {\n setState((state: CommunitiesState) => {\n let communityErrors = state.errors[communityKey] || [];\n communityErrors = [...communityErrors, errorGettingCommunity];\n return { ...state, errors: { ...state.errors, [communityKey]: communityErrors } };\n });\n }\n\n throw (\n errorGettingCommunity ||\n Error(`communitiesStore.addCommunityToStore failed getting community '${communityKey}'`)\n );\n }\n\n // success getting community\n const firstCommunityState = utils.clone({ ...community, fetchedAt });\n await communitiesDatabase.setItem(communityKey, firstCommunityState);\n log(\"communitiesStore.addCommunityToStore\", {\n communityAddressOrRef,\n communityKey,\n community,\n account,\n });\n setState((state: any) => ({\n communities: { ...state.communities, [communityKey]: firstCommunityState },\n }));\n\n // the community has published new posts\n community.on(\"update\", async (updatedCommunity: Community) => {\n clearPendingCommunityErrors(communityKey);\n setState((state: CommunitiesState) => ({\n ...state,\n errors: clearStoredCommunityErrors(state, communityKey),\n }));\n updatedCommunity = utils.clone(updatedCommunity);\n\n // add fetchedAt to be able to expire the cache\n // NOTE: fetchedAt is undefined on owner communities because never stale\n updatedCommunity.fetchedAt = Math.floor(Date.now() / 1000);\n\n await communitiesDatabase.setItem(communityKey, updatedCommunity);\n log(\"communitiesStore community update\", {\n communityAddressOrRef,\n communityKey,\n updatedCommunity,\n account,\n });\n setState((state: any) => ({\n ...state,\n communities: { ...state.communities, [communityKey]: updatedCommunity },\n }));\n\n // if a community has a role with an account's address add it to the account.communities\n accountsStore\n .getState()\n .accountsActionsInternal.addCommunityRoleToAccountsCommunities(updatedCommunity);\n\n // add page comments to communitiesPagesStore so they can be used in useComment\n communitiesPagesStore.getState().addCommunityPageCommentsToStore(updatedCommunity);\n });\n\n community.on(\"updatingstatechange\", (updatingState: string) => {\n setState((state: CommunitiesState) => ({\n communities: {\n ...state.communities,\n [communityKey]: { ...state.communities[communityKey], updatingState },\n },\n }));\n });\n\n community.on(\"error\", (error: Error) => {\n scheduleCommunityError(setState, communityKey, error);\n });\n\n // set clients on community so the frontend can display it, dont persist in db because a reload cancels updating\n utils.clientsOnStateChange(\n community?.clients,\n (clientState: string, clientType: string, clientUrl: string, chainTicker?: string) => {\n setState((state: CommunitiesState) => {\n // make sure not undefined, sometimes happens in e2e tests\n if (!state.communities[communityKey]) {\n return {};\n }\n const clients = { ...state.communities[communityKey]?.clients };\n const client = { state: clientState };\n if (chainTicker) {\n const chainProviders = { ...clients[clientType][chainTicker], [clientUrl]: client };\n clients[clientType] = { ...clients[clientType], [chainTicker]: chainProviders };\n } else {\n clients[clientType] = { ...clients[clientType], [clientUrl]: client };\n }\n return {\n communities: {\n ...state.communities,\n [communityKey]: { ...state.communities[communityKey], clients },\n },\n };\n });\n },\n );\n\n listeners.push(community);\n startCommunityUpdatePolling(community, { communityAddressOrRef, communityKey });\n } finally {\n pkcGetCommunityPending[pendingKey] = false;\n }\n },\n\n async refreshCommunity(communityAddressOrRef: string | CommunityIdentifier, account: Account) {\n const communityLookupOptions = getCommunityLookupOptions(communityAddressOrRef);\n const communityKey =\n typeof communityAddressOrRef === \"string\"\n ? communityAddressOrRef\n : getCommunityRefKey(communityAddressOrRef);\n assert(\n communityKey !== \"\" && typeof communityKey === \"string\",\n `communitiesStore.refreshCommunity invalid communityAddress argument '${communityAddressOrRef}'`,\n );\n assert(\n typeof getPkcGetCommunity(account?.pkc) === \"function\",\n `communitiesStore.refreshCommunity invalid account argument '${account}'`,\n );\n\n const refreshedCommunity = utils.clone(\n await getPkcCommunity(account.pkc, communityLookupOptions),\n );\n refreshedCommunity.fetchedAt = Math.floor(Date.now() / 1000);\n\n await communitiesDatabase.setItem(communityKey, refreshedCommunity);\n log(\"communitiesStore.refreshCommunity\", {\n communityAddressOrRef,\n communityKey,\n refreshedCommunity,\n account,\n });\n setState((state: any) => ({\n communities: { ...state.communities, [communityKey]: refreshedCommunity },\n }));\n\n communitiesPagesStore.getState().addCommunityPageCommentsToStore(refreshedCommunity);\n\n return refreshedCommunity;\n },\n\n // user is the owner of the community and can edit it locally\n async editCommunity(communityAddress: string, communityEditOptions: any, account: Account) {\n assert(\n communityAddress !== \"\" && typeof communityAddress === \"string\",\n `communitiesStore.editCommunity invalid communityAddress argument '${communityAddress}'`,\n );\n assert(\n communityEditOptions && typeof communityEditOptions === \"object\",\n `communitiesStore.editCommunity invalid communityEditOptions argument '${communityEditOptions}'`,\n );\n assert(\n typeof getPkcCreateCommunity(account?.pkc) === \"function\",\n `communitiesStore.editCommunity invalid account argument '${account}'`,\n );\n\n // if not added to store first, community.update() is never called\n await getState().addCommunityToStore(communityAddress, account);\n\n // `communityAddress` is different from `communityEditOptions.address` when editing the community address\n const community = await createPkcCommunity(account.pkc, {\n address: communityAddress,\n });\n\n // could fix some test issues\n community.on(\"error\", console.log);\n\n await community.edit(communityEditOptions);\n\n const updatedCommunity = utils.clone(community);\n // edit db of both old and new community address to not break the UI\n await communitiesDatabase.setItem(communityAddress, updatedCommunity);\n await communitiesDatabase.setItem(community.address, updatedCommunity);\n log(\"communitiesStore.editCommunity\", {\n communityAddress,\n communityEditOptions,\n community,\n account,\n });\n setState((state: any) => ({\n communities: {\n ...state.communities,\n // edit react state of both old and new community address to not break the UI\n [communityAddress]: updatedCommunity,\n [community.address]: updatedCommunity,\n },\n }));\n },\n\n // internal action called by accountsActions.createCommunity\n async createCommunity(createCommunityOptions: CreateCommunityOptions, account: Account) {\n assert(\n !createCommunityOptions || typeof createCommunityOptions === \"object\",\n `communitiesStore.createCommunity invalid createCommunityOptions argument '${createCommunityOptions}'`,\n );\n if (!createCommunityOptions?.signer) {\n assert(\n !createCommunityOptions?.address,\n `communitiesStore.createCommunity createCommunityOptions.address '${createCommunityOptions?.address}' must be undefined to create a community`,\n );\n }\n assert(\n typeof getPkcCreateCommunity(account?.pkc) === \"function\",\n `communitiesStore.createCommunity invalid account argument '${account}'`,\n );\n\n const community = await createPkcCommunity(account.pkc, createCommunityOptions);\n\n // could fix some test issues\n community.on(\"error\", console.log);\n\n // if not added to store first, community.update() is never called\n await getState().addCommunityToStore(community.address, account);\n\n await communitiesDatabase.setItem(community.address, utils.clone(community));\n log(\"communitiesStore.createCommunity\", { createCommunityOptions, community, account });\n setState((state: any) => ({\n communities: { ...state.communities, [community.address]: utils.clone(community) },\n }));\n return community;\n },\n\n // internal action called by accountsActions.deleteCommunity\n async deleteCommunity(communityAddress: string, account: Account) {\n assert(\n communityAddress && typeof communityAddress === \"string\",\n `communitiesStore.deleteCommunity invalid communityAddress argument '${communityAddress}'`,\n );\n assert(\n typeof getPkcCreateCommunity(account?.pkc) === \"function\",\n `communitiesStore.deleteCommunity invalid account argument '${account}'`,\n );\n\n const community = await createPkcCommunity(account.pkc, {\n address: communityAddress,\n });\n\n // could fix some test issues\n community.on(\"error\", console.log);\n\n await community.delete();\n stopCommunityUpdatePolling(communityAddress);\n await communitiesDatabase.removeItem(communityAddress);\n log(\"communitiesStore.deleteCommunity\", { communityAddress, community, account });\n setState((state: any) => ({\n communities: { ...state.communities, [communityAddress]: undefined },\n }));\n },\n }),\n);\n\n// reset store in between tests\nconst originalState = communitiesStore.getState();\n// async function because some stores have async init\nexport const resetCommunitiesStore = async () => {\n pkcGetCommunityPending = {};\n Object.keys(pendingCommunityErrorTimers).forEach(clearPendingCommunityErrors);\n // remove all event listeners\n listeners.forEach((listener: any) => listener.removeAllListeners());\n listeners.length = 0;\n Object.keys(communityUpdatePollers).forEach(stopCommunityUpdatePolling);\n // destroy all component subscriptions to the store\n communitiesStore.destroy();\n // restore original state\n communitiesStore.setState(originalState);\n};\n\n// reset database and store in between tests\nexport const resetCommunitiesDatabaseAndStore = async () => {\n await localForageLru.createInstance({ name: \"bitsocialReactHooks-communities\" }).clear();\n await resetCommunitiesStore();\n};\n\nexport default communitiesStore;\n"]}
|