mini-reactivity
really basic reactivity package
This package provides some basic reactivity functions that are modeled after the vue and vueuse functions of the same name. It was created for use in automated Node.js tests.
The following descriptions are partly taken from the corresponding original descriptions.
Usage
ref()
Takes an inner value and returns a reactive and mutable ref object, which has a single property .value
that points to the inner value.
Example
import { ref } from '@uscreen.de/mini-reactivity'
const count = ref('initial value')
console.log(count.value)
count.value = 'changed value'
console.log(count.value)
computed()
Takes a getter function and returns a readonly reactive ref object for the returned value from the getter.
import { ref, computed } from '@uscreen.de/mini-reactivity'
const msg = ref('initial value')
const message = computed(() => `Message: ${msg.value}`)
console.log(message)
msg.value = 'changed value'
console.log(message)
watchEffect
Runs a function immediately while reactively tracking its dependencies and re-runs it whenever the dependencies are changed.
import { ref, watchEffect } from '@uscreen.de/mini-reactivity'
const msg = ref('initial value')
watchEffect(() => { console.log(msg) })
msg.value = 'changed value'
until
Returns a promised one-time watch for changes of reactive refs.
import { ref, until } from '@uscreen.de/mini-reactivity'
const myRef = ref('')
await until(myRef).changed()
await until(myRef).changedTimes(3)
await until(myRef).toBe('changed')
await until(myRef).toBeNull()
await until(myRef).toBeTruthy()
await until(myRef).toMatch(r => r.value === 'changed')
setTimeout(() => { myRef.value = 'changed' }, 1000)
const resolved = await until(myRef).changed()
console.log(resolved)
await until(myRef).toBe(
'changed',
{ timeout: 1000 }
)
await until(myRef).toBe(
'changed',
{ timeout: 1000, throwOnTimeout: true }
)
License
Licensed under MIT.
Published, Supported and Sponsored by u|screen