So I have a react front end for a project that is acting rather odd with an Observable.
It might be hard to visualize through just code but I was hoping someone would at least give me an idea how to troubleshoot this.
The setup I have an App buss that is responsible for making sure the 3d scene and the react app are in sync with their states. How its setup is each react useState gets set on the bus and so I am able to add callbacks and other things to the set states. Also on the bus objects there is a Observable that fires whenever the change of state is done.
One example is the âScreenLockâ functional component.
const [isLocked, setIsLocked] = useState<boolean>(true)
const [screenLockFade, setScreenLockFade] = useState<number>(1)
States.SetConnection(BusNames.AppBus, 'isLocked', {currentState: isLocked, set: (value:any)=>{
console.log('screenLockFade', screenLockFade)
if(!value && screenLockFade === 1){
new TimedAnimation({
duration: lockScreenTransitionDuration,
onStep: (x: number) => {
setScreenLockFade(1.0 - x)
},
onDone: () => {
setScreenLockFade(0)
setIsLocked(false)
States.AppBus.isLocked?.onChangeDoneObservable.notifyObservers(value)
}
})
}else if(value && screenLockFade === 0 ){
new TimedAnimation({
duration: lockScreenTransitionDuration,
onStart: ()=>{
setIsLocked(true)
},
onStep: (x: number) => {
setScreenLockFade(x)
},
onDone: () => {
setScreenLockFade(1)
States.AppBus.isLocked?.onChangeDoneObservable.notifyObservers(value)
}
})
}
}})
States.AppBus.isLocked?.onChangeDoneObservable.add((value:any)=>{
console.log('States.AppBus.isLocked?.onChangeDoneObservable', value)
})
This then is used in the app by calling:
States.AppBus.isLocked.set(true)
The observer and the setting of the state work perfectly, but I have a case were I am registering an addOnce callback to the States.AppBus.isLocked.onChangeDoneObservable on a button click on a different component then calling the â.set(true)â.
I have console logged the obs and when it fires off the â.notifyObserversâ, but for some reason the addOnce callback is not firing even though I see it in the obs list.
const stableButtonOnClick = ()=>{
console.log(`stableButtonOnClick`)
const guiConnection = States.AppBus.guiLayout
if(guiConnection && States.AppBus.isLocked){
States.AppBus.isLocked.onChangeDoneObservable.addOnce((value)=>{
console.log("Observer notified")
States.ChangeScene(SceneNames[Scenes.Stables])
})
//console.log(States.AppBus.isLocked.onChangeDoneObservable)
// guiConnection.set(GuiLayout.Stables)
States.AppBus.isLocked.set(true)
}
}
Everything works except the:
States.AppBus.isLocked.onChangeDoneObservable.addOnce((value)=>{
console.log("Observer notified")
States.ChangeScene(SceneNames[Scenes.Stables])
})
Is never seen or fires even though the notification from this comes through:
States.AppBus.isLocked?.onChangeDoneObservable.add((value:any)=>{
console.log('States.AppBus.isLocked?.onChangeDoneObservable', value)
})
Anybody have any clue what could be causing this?
edit
Ohh yeah this is how the Obs is created.
public static SetConnection(busName: string, routeName: string, connection: BusConnection){
const _states = States as any
if(_states[busName]!== undefined && _states[busName][routeName] !== undefined){
_states[busName][routeName] = {
currentState: connection.currentState,
set: connection.set,
onChangeDoneObservable: new Observable<any>()
}
}
}