-
-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10905 from DestinyItemManager/cross-tab
Communicate store refresh and item moves across tabs
- Loading branch information
Showing
8 changed files
with
199 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { infoLog } from 'app/utils/log'; | ||
import { BucketHashes } from 'data/d2/generated-enums'; | ||
import { useCallback, useEffect } from 'react'; | ||
|
||
export const crossTabChannel = | ||
'BroadcastChannel' in globalThis ? new BroadcastChannel('dim') : undefined; | ||
|
||
export interface StoreUpdatedMessage { | ||
type: 'stores-updated'; | ||
} | ||
|
||
export interface ItemMovedMessage { | ||
type: 'item-moved'; | ||
itemHash: number; | ||
itemId: string; | ||
itemLocation: BucketHashes; | ||
sourceId: string; | ||
targetId: string; | ||
equip: boolean; | ||
amount: number; | ||
} | ||
|
||
// TODO: other inventory changes, dim api changes, etc. | ||
|
||
export type CrossTabMessage = StoreUpdatedMessage | ItemMovedMessage; | ||
|
||
export function useCrossTabUpdates(callback: (m: CrossTabMessage) => void) { | ||
const onMsg = useCallback( | ||
(m: MessageEvent<CrossTabMessage>) => { | ||
const message = m.data; | ||
infoLog('cross-tab', 'message', message.type, message); | ||
if (message.type) { | ||
callback(message); | ||
} | ||
}, | ||
[callback], | ||
); | ||
useEffect(() => { | ||
if (!crossTabChannel) { | ||
return; | ||
} | ||
crossTabChannel.addEventListener('message', onMsg); | ||
return () => crossTabChannel.removeEventListener('message', onMsg); | ||
}, [onMsg]); | ||
} | ||
|
||
export function notifyOtherTabsStoreUpdated() { | ||
if (!crossTabChannel) { | ||
return; | ||
} | ||
crossTabChannel.postMessage({ type: 'stores-updated' } satisfies StoreUpdatedMessage); | ||
} | ||
|
||
export function notifyOtherTabsItemMoved(args: Omit<ItemMovedMessage, 'type'>) { | ||
if (!crossTabChannel) { | ||
return; | ||
} | ||
crossTabChannel.postMessage({ type: 'item-moved', ...args } satisfies ItemMovedMessage); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters