Skip to content

Instantly share code, notes, and snippets.

@TheLucifurry
Last active November 24, 2023 04:09
Show Gist options
  • Select an option

  • Save TheLucifurry/64df4c7a09b00106c24141cd6b131afd to your computer and use it in GitHub Desktop.

Select an option

Save TheLucifurry/64df4c7a09b00106c24141cd6b131afd to your computer and use it in GitHub Desktop.
function useRefsMap<K extends string | number | symbol, V>() {
const mapRefs = new Map<K, Ref<V | undefined>>()
return {
...mapRefs,
set(key: K, value: V) {
if (mapRefs.has(key)) {
const r = mapRefs.get(key)!
r.value = value
} else {
mapRefs.set(key, ref(value) as Ref<V>)
}
},
get(key: K) {
if (mapRefs.has(key))
return mapRefs.get(key)!
const newRef = ref(undefined)
mapRefs.set(key, newRef)
return newRef
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment