Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,956 Bytes
5cc7f12 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
'use client'
import { create } from 'zustand'
import { Bellhop } from 'bellhop-iframe'
export const useChildController = create<{
bellhop: Bellhop
/**
* Whether the communication pipeline seems to be working or not
*
* Initially we assume that it is, but this can be invalidated
* if we know for certain it is not, or in case of exception
*/
canUseBellhop: boolean
/**
*
*/
hasLoadedBellhop: boolean
/**
* Whether Clapper is ready
*
* This will be set upon reception of the event
*/
clapperIsReady: boolean
setDomElement: (domElement: HTMLIFrameElement, origin?: string) => void
setCanUseBellhop: (canUseBellhop: boolean) => void
setHasLoadedBellhop: (hasLoadedBellhop: boolean) => void
onMessage: (name: string, callback: Function, priority?: number) => void
sendMessage: (type: string, data?: any) => void
}>((set, get) => ({
bellhop: undefined as unknown as Bellhop,
canUseBellhop: true,
hasLoadedBellhop: false,
clapperIsReady: false,
setDomElement: (domElement: HTMLIFrameElement, origin?: string) => {
const bellhop = new Bellhop()
bellhop.connect(domElement, origin)
set({
bellhop,
})
},
setCanUseBellhop: (canUseBellhop: boolean) => {
set({
canUseBellhop,
})
},
setHasLoadedBellhop: (hasLoadedBellhop: boolean) => {
set({
bellhop: hasLoadedBellhop ? new Bellhop() : (undefined as unknown as Bellhop),
hasLoadedBellhop,
})
},
onMessage: (name: string, callback: Function, priority?: number) => {
const { bellhop } = get()
try {
bellhop.on(name, callback, priority)
} catch (err) {
console.log(`failed to subscribe to parent iframe messages:`, err)
}
},
sendMessage: (type: string, data?: any) => {
const { bellhop } = get()
try {
bellhop.send(type, data)
} catch (err) {
console.log(`failed to send a message to parent iframe:`, err)
}
},
}))
|