import { ref } from 'vue'; import { IApplicationCode } from '@/application/Context/State/Code/IApplicationCode'; import { IEventSubscriptionCollection } from '@/infrastructure/Events/IEventSubscriptionCollection'; import { useCollectionState } from './UseCollectionState'; export function useCurrentCode( state: ReturnType, events: IEventSubscriptionCollection, ) { const { onStateChange } = state; const currentCode = ref(''); onStateChange((newState) => { updateCurrentCode(newState.code.current); subscribeToCodeChanges(newState.code); }, { immediate: true }); function subscribeToCodeChanges(code: IApplicationCode) { events.unsubscribeAllAndRegister([ code.changed.on((newCode) => updateCurrentCode(newCode.code)), ]); } function updateCurrentCode(newCode: string) { currentCode.value = newCode; } return { currentCode, }; }