Skip to content

Commit cb4a0df

Browse files
authored
Merge pull request #382 from lightningrodlabs/fix/unsubscribe-from-assetstore
Fix unsubscribe from assetstore
2 parents 076ea41 + fb70f4e commit cb4a0df

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

web/src/components/ExpandedViewMode/ExpandedViewMode.connector.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function mapStateToProps(
5959
const ExpandedViewMode = connect(mapStateToProps)(ExpandedViewModeComponent)
6060

6161
/*
62-
We do this in order to 'connect' in the other
62+
We do this in order to 'connect' in the other
6363
redux connected sub-components
6464
*/
6565

@@ -113,11 +113,10 @@ const ConnectedExpandedViewMode: React.FC<ConnectedExpandedViewModeProps> = ({
113113
// Run only once on mount when initial props are provided
114114
}, [initialProjectId, initialOutcomeActionHash, dispatch, projectId])
115115

116-
// Always call the hook, but only with valid data when outcome exists and projectId is determined
117-
const { attachmentsInfo } = useAttachments({
116+
const { attachmentsInfo } = outcome && projectId ? useAttachments({
118117
projectId: projectId, // Use the determined projectId
119-
outcome: outcome || ({ actionHash: '' } as ComputedOutcome),
120-
})
118+
outcome: outcome,
119+
}) : { attachmentsInfo: [] };
121120

122121
// Function to add attachments
123122
const addAttachment = async () => {

web/src/hooks/useAttachments.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ export function useAttachments({
4747
// Clean up any existing subscription before creating a new one
4848
if (
4949
subscriptionRef.current &&
50-
typeof subscriptionRef.current.unsubscribe === 'function'
50+
typeof subscriptionRef.current === 'function'
5151
) {
52-
subscriptionRef.current.unsubscribe()
52+
const unsubscribe = subscriptionRef.current;
53+
unsubscribe()
5354
}
5455
subscriptionRef.current = null
5556

@@ -93,9 +94,10 @@ export function useAttachments({
9394
return () => {
9495
if (
9596
subscriptionRef.current &&
96-
typeof subscriptionRef.current.unsubscribe === 'function'
97+
typeof subscriptionRef.current === 'function'
9798
) {
98-
subscriptionRef.current.unsubscribe()
99+
const unsubscribe = subscriptionRef.current;
100+
unsubscribe()
99101
}
100102
subscriptionRef.current = null
101103
}

web/src/hooks/useProjectAttachments.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function useProjectAttachments({
2424
projectId: string
2525
projectMeta: WithActionHash<ProjectMeta>
2626
}) {
27-
// Call hooks unconditionally at the top level
27+
// Call hooks unconditionally at the top level
2828
const [attachmentWALs, setAttachmentWALs] = useState<WAL[] | null>(null)
2929
const [attachmentsInfo, setAttachmentsInfo] = useState<ProjectAssetMeta[]>([])
3030
const [error, setError] = useState(null)
@@ -43,9 +43,10 @@ export function useProjectAttachments({
4343
// Ensure cleanup if subscription exists from a previous valid run
4444
if (
4545
subscriptionRef.current &&
46-
typeof subscriptionRef.current.unsubscribe === 'function'
46+
typeof subscriptionRef.current === 'function'
4747
) {
48-
subscriptionRef.current.unsubscribe()
48+
const unsubscribe = subscriptionRef.current;
49+
unsubscribe()
4950
}
5051
subscriptionRef.current = null
5152
return // Exit effect early
@@ -66,14 +67,15 @@ export function useProjectAttachments({
6667
// Clean up any existing subscription before creating a new one
6768
if (
6869
subscriptionRef.current &&
69-
typeof subscriptionRef.current.unsubscribe === 'function'
70+
typeof subscriptionRef.current === 'function'
7071
) {
71-
subscriptionRef.current.unsubscribe()
72+
const unsubscribe = subscriptionRef.current;
73+
unsubscribe()
7274
}
7375
subscriptionRef.current = null
7476

7577
try {
76-
const subscription = weaveClient.assets
78+
const unsubscribe = weaveClient.assets
7779
.assetStore(wal)
7880
.subscribe(async (store) => {
7981
if (store.status === 'complete') {
@@ -102,7 +104,7 @@ export function useProjectAttachments({
102104
})
103105

104106
// Store the subscription object properly
105-
subscriptionRef.current = subscription
107+
subscriptionRef.current = unsubscribe
106108
} catch (err) {
107109
setError(err)
108110
}
@@ -114,9 +116,10 @@ export function useProjectAttachments({
114116
return () => {
115117
if (
116118
subscriptionRef.current &&
117-
typeof subscriptionRef.current.unsubscribe === 'function'
119+
typeof subscriptionRef.current === 'function'
118120
) {
119-
subscriptionRef.current.unsubscribe()
121+
const unsubscribe = subscriptionRef.current;
122+
unsubscribe();
120123
}
121124
subscriptionRef.current = null
122125
}

0 commit comments

Comments
 (0)