Last active
May 16, 2023 07:13
-
-
Save ThomasDeutsch/0f318b56398ee8581a765d9c9f231f5c to your computer and use it in GitHub Desktop.
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
import { Signal, signal } from '@angular/core'; | |
import { Event } from '@flowcards/core'; | |
export function getSignal<P, V>(event: Event<P, V>) { | |
const eventSignal = signal<Event<P, V>>(event); | |
event.registerCallback(() => { | |
eventSignal.set(this._internalEvent); | |
}); | |
return eventSignal.asReadonly; | |
} | |
type MapToSignal<T> = { | |
[K in keyof T]: T[K] extends Event<infer V, infer B> | |
? Signal<Event<V, B>> | |
: MapToSignal<T[K]>; | |
}; | |
export function mapToSignals<T>(obj: T): MapToSignal<T> { | |
const result: any = {}; | |
for (const key in obj) { | |
const item = obj[key]; | |
if (item instanceof Event) { | |
result[key] = getSignal(item); | |
} else { | |
result[key] = mapToSignals(item); | |
} | |
} | |
return result; | |
} | |
const obj = { | |
a: new Event<number>('a'), | |
b: new Event<number>('b'), | |
c: { | |
d: new Event<string>('d'), | |
}, | |
}; | |
const signalsObj = mapToSignals(obj); | |
// JUHUUUU!!!! x ist ein string! | |
const x = signalsObj.c.d().value; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment