0.28.4 • Published 2 years ago

retil-source v0.28.4

Weekly downloads
44
License
MIT
Repository
-
Last release
2 years ago

Create and combine asynchronous data sources that work great with React Concurrent Mode.

Installation

# For npm users...
npm install --save retil-source

# For yarn users...
yarn add retil-source

Sources don't always have values

Why should a data source return a value before it has one?

import { createState, hasSnapshot } from 'retil'

const [stateSource, setState] = createState()
const [getState, subscribe] = stateSource

getState() // throws a promise that resolves to the first value.

setState('yo!')

getState() // now it returns 'yo!'

Examples

Say you have a source that switches between having a value, and not having a value -- e.g. because it maps an id to data on a remote server. But you still want to display the latest value.

import { Source, fuse } from 'retil'

export const fallbackToMostRecent = <T>(source: Source<T>): Source<T> => {
  let fallback: [T] | [] = []
  return fuse(use => {
    const value = use(source, ...fallback)
    fallback = [value]
    return value
  })
}

Maybe as well as remembering the most recent value, you'd also like to set a flag indicating whether the source is missing a current value. To do this, you can use your source twice, with different fallback values. Don't worry, fuse will only subscribe to the source once!

const missingFlag = Symbol('missing')

export function addBusyFlag<T, U>(
  source: Source<T>,
  build: (snapshot: T, flag: boolean) => U,
): Source<U> {
  let fallback: [T] | [] = []
  return fuse((use) => {
    const value = use(source, ...fallback)
    const valueOrFlag = use(source, missingFlag)
    fallback = [value]
    return build(value, valueOrFlag === missingFlag)
  })
}
0.28.4

2 years ago

0.28.3

2 years ago

0.27.0

2 years ago

0.28.1

2 years ago

0.28.0

2 years ago

0.26.2

2 years ago

0.26.0

2 years ago

0.28.2

2 years ago

0.25.0

2 years ago

0.24.3

3 years ago

0.24.2

3 years ago

0.24.1

3 years ago

0.24.0

3 years ago

0.23.8

3 years ago

0.23.4

3 years ago

0.23.3

3 years ago

0.23.2

3 years ago

0.23.1

3 years ago

0.23.0

3 years ago

0.21.5

3 years ago

0.22.0

3 years ago

0.21.4

3 years ago

0.21.0

3 years ago

0.20.1

3 years ago

0.19.1

3 years ago

0.19.0

3 years ago

0.18.5

3 years ago

0.18.3

3 years ago

0.18.0

3 years ago

0.17.9

3 years ago

0.17.2

3 years ago

0.17.1

3 years ago

0.17.0

3 years ago

0.16.0

3 years ago

0.15.0

3 years ago

0.14.0

3 years ago

0.13.0

3 years ago

0.11.8

3 years ago

0.11.1

4 years ago

0.11.0

4 years ago

0.10.0

4 years ago

0.10.1

4 years ago

0.9.12

4 years ago

0.9.0

4 years ago

0.8.0

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago