Chain

The chaining api is responsible for operations we can do at runtime in a single call, combining queries and controlling results from drivers, state, cache and network all together.

Example#

For a fresh result from network we can do

import { fetch } from '@rebased/core';
fetch('kitty', {
silent: false,
from: 'http',
baseURL: 'https://api.thecatapi.com',
endpoint: '/v1'
})
.key('a-random-kitty') // identify this result
.cache(false) // never use cache as result
.state(false) // never use memoized state as result
.get('/images/search?size=small')
.subscribe(kitty => console.log(kitty)); // fresh result from network

Cache response#

import { fetch } from '@rebased/core';
fetch('kitty', {
silent: false,
from: 'http',
baseURL: 'https://api.thecatapi.com',
endpoint: '/v1'
})
.key('a-random-kitty') // identify this result
.cache(true) // use cache as result
.state(false) // never use memoized state as result
.get('/images/search?size=small')
// first result from this stream should be from cache
// but network is still being requested
// case network result differs from cache
// the stream will end up having 2 responses in a row
// unless the result from network is the same as in the cache
// then you should expect only one response in this subscription
.subscribe(kitty => console.log(kitty));

Except for .on calls, all observables from fetch are destroyed internally once the network response lands.

State response#

import { fetch } from '@rebased/core';
fetch('kitty', {
silent: false,
from: 'http',
baseURL: 'https://api.thecatapi.com',
endpoint: '/v1'
})
.key('a-random-kitty') // identify this result
.cache(false) // don't use cache as result
.state(true) // use memoized state as result
.get('/images/search?size=small')
// first result from this stream should be from network
// as there wasn't any memoized result yet
// but network is still being requested
// case a memoized state exists and network result differs from it
// the stream will end up having 2 responses in a row
// unless the result from network is the same as in the state
// then you should expect only one response in this subscription
.subscribe(kitty => console.log(kitty));

All together#

import { fetch } from '@rebased/core';
fetch('kitty', {
silent: false,
from: 'http',
baseURL: 'https://api.thecatapi.com',
endpoint: '/v1'
})
.key('a-random-kitty') // identify this result
.cache(true) // use cache as result
.state(true) // use memoized state as result
.get('/images/search?size=small')
// same rule above is applied in this sequence
// 1 - response from cache or state (the faster one)
// 2 - response from network (case it differs from previous result)
.subscribe(kitty => console.log(kitty));
note

Cache and state are enabled by default in case of its packages are present and running

Using rxjs toPromise()#

Another way to guarantee responses only from network, or in case you're working with fetch calls on server where there's no cache/state needs, you can take advantage of rxjs' toPromise()

import { fetch } from '@rebased/core';
fetch('kitty', {
silent: false,
from: 'http',
baseURL: 'https://api.thecatapi.com',
endpoint: '/v1'
})
.key('a-random-kitty') // identify this result
.cache(true) // use cache as result
.state(true) // use memoized state as result
.get('/images/search?size=small')
.toPromise()
// even though we have defined .cache and .state above
// this promise will only be resolved once the network call land
// and it will only have the network response as result
.then(kitty => console.log(kitty));

Chain Availability#

httpfirebasefirestoreparse
fromโœ…โœ…โœ…โœ…
networkโœ…โœ…โœ…โœ…
keyโœ…โœ…โœ…โœ…
queryโ›”๏ธโ›”๏ธโ›”๏ธโœ…
whereโœ…โœ…โœ…โœ…
sortโ›”๏ธโ›”๏ธโœ…โœ…
sizeโœ…โœ…โœ…โœ…
atโ›”๏ธโ›”๏ธโœ…โ›”๏ธ
afterโ›”๏ธโ›”๏ธโœ…โœ…
refโ›”๏ธโœ…โ›”๏ธโ›”๏ธ
httpโœ…โ›”๏ธโ›”๏ธโ›”๏ธ
includeโ›”๏ธโ›”๏ธโ›”๏ธโœ…
docโ›”๏ธโ›”๏ธโœ…โœ…
masterโ›”๏ธโ›”๏ธโ›”๏ธโœ…
tokenโ›”๏ธโ›”๏ธโ›”๏ธโœ…
objectโ›”๏ธโ›”๏ธโ›”๏ธโœ…
cache๐Ÿ›ฃ๏ธ
browser
๐Ÿ›ฃ๏ธ
browser
๐Ÿ›ฃ๏ธ
browser
๐Ÿ›ฃ๏ธ
browser
selectโ›”๏ธโ›”๏ธโ›”๏ธโœ…
stateโœ…โœ…โœ…โœ…
nearโ›”๏ธโ›”๏ธโ›”๏ธโœ…
withinKilometersโ›”๏ธโ›”๏ธโ›”๏ธโœ…
withinMilesโ›”๏ธโ›”๏ธโ›”๏ธโœ…
diffโœ…โœ…โœ…โœ…
responseโœ…โœ…โœ…โœ…
symbolmeaningdescription
โœ… available method is available for this driver
โ›”๏ธ unavailable method is not allowed for this driver
๐Ÿ›ฃ๏ธ routed method is routed to another platform