Experiments with API
Opened this issue · 0 comments
pvorona commented
Caching observe
that doesn't call get
on each dependency and instead memoizes last seen values.
- What's the use case and why is it useful?
Unify eager
and lazy
interfaces
What's the purpose?
- Expose new
observable.lazy
interface observable.lazy
interface can be used to implementanimatable
and reduce code duplication- Symmetrical APIs for
eager
andlazy
Variants:
- References:
observable.eager
andobservable.lazy
- Reference with default to eager:
observable
andobservable.lazy
- Options:
observable(1, { lazy: true })
andobservable(1, { lazy: false })
Initialize observable
with function.
What's the purpose?
- Hiding side effects
- Scoping related logic
Negative side effects:
Observable
initialized without value => boxed type isT | undefined
instead ofT
. This is an implementation detail, the public interface is unchanged.
Examples:
- Sync function, Immediate invocation + interval:
const time = observable(set => {
set(Date.now())
setInterval(() => {
set(Date.now())
}, 1000)
})
- Async function + fetch:
const data = observable(async (set) => {
set({ state: 'loading' })
try {
const response = await fetch('api')
const data = await response.json()
set({ state: 'success', data })
} catch (error) {
set({ state: 'failure', error })
}
})
- Async:
const time = observable(async (set) => {
while (true) {
set(Date.now())
await delay(1000)
}
})
- Async generator:
const time = observable(() * => {
while (true) {
yield Date.now()
await delay(1000)
}
})
Lazy as option:
computeLazy
->compute
with{ lazy: true }
observe
starts having lazy as well. What are the use cases for lazy observation?
Done:
Observe
with options
observe
with or without values:{ collectValues: boolean }
observe
with or without immediate invocation:{ fireImmediate: boolean }