|
1 | | -import { computed } from 'vue' |
2 | | -import { useStore } from 'vuex' |
| 1 | +import { computed, watch } from 'vue' |
| 2 | +import { GetterTree, useStore } from 'vuex' |
3 | 3 | import { NodeStatusResult } from '@/lib/nodes/abstract.node' |
| 4 | +import { RootState } from '@/store/types' |
4 | 5 |
|
5 | | -export function useConsiderOffline() { |
6 | | - const store = useStore() |
| 6 | +export function useConsiderOffline(options?: { getters: GetterTree<any, RootState> }) { |
| 7 | + const getters = options?.getters ?? useStore().getters |
7 | 8 |
|
8 | | - const isOnline = computed(() => store.getters['isOnline']) |
| 9 | + const isOnline = computed(() => getters['isOnline']) |
9 | 10 |
|
10 | | - const admNodes = computed<NodeStatusResult[]>(() => store.getters['nodes/adm']) |
11 | | - const coinNodes = computed<NodeStatusResult[]>(() => store.getters['nodes/coins']) |
12 | | - const servicesNodes = computed<NodeStatusResult[]>(() => store.getters['services/services']) |
13 | | - const ipfsNodes = computed<NodeStatusResult[]>(() => store.getters['nodes/ipfs']) |
| 11 | + const admNodes = computed<NodeStatusResult[]>(() => getters['nodes/adm']) |
| 12 | + const coinNodes = computed<NodeStatusResult[]>(() => getters['nodes/coins']) |
| 13 | + const servicesNodes = computed<NodeStatusResult[]>(() => getters['services/services']) |
| 14 | + const ipfsNodes = computed<NodeStatusResult[]>(() => getters['nodes/ipfs']) |
14 | 15 |
|
15 | 16 | const allNodes = computed<NodeStatusResult[]>(() => [ |
16 | 17 | ...admNodes.value, |
@@ -43,7 +44,29 @@ export function useConsiderOffline() { |
43 | 44 | return !anyNodeOnline.value || everyNodeDisabled.value |
44 | 45 | }) |
45 | 46 |
|
| 47 | + const offlineHandlers = new Set<() => void>() |
| 48 | + |
| 49 | + const subscribeOffline = (callback: () => void) => { |
| 50 | + offlineHandlers.add(callback) |
| 51 | + |
| 52 | + return () => { |
| 53 | + offlineHandlers.delete(callback) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + const unsubscribeOffline = (callback: () => void) => { |
| 58 | + offlineHandlers.delete(callback) |
| 59 | + } |
| 60 | + |
| 61 | + watch(consideredOffline, (isOffline) => { |
| 62 | + if (isOffline) { |
| 63 | + offlineHandlers.forEach((cb) => cb()) |
| 64 | + } |
| 65 | + }) |
| 66 | + |
46 | 67 | return { |
47 | | - consideredOffline |
| 68 | + consideredOffline, |
| 69 | + subscribeOffline, |
| 70 | + unsubscribeOffline |
48 | 71 | } |
49 | 72 | } |
0 commit comments