Skip to content

Instantly share code, notes, and snippets.

@BerkeleyTrue
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BerkeleyTrue/6a9180c4dc447b8aa171 to your computer and use it in GitHub Desktop.
Save BerkeleyTrue/6a9180c4dc447b8aa171 to your computer and use it in GitHub Desktop.
Reactive Fetcher anyone?

ProFetchRx

A Universal(read: isomorphic) JavaScript library for fetching data from remote datasources.

This describes what the api would look like for a fetcher based on RxJs.

// server setup
import { net, registrar } from 'profetchrx';
import postService from './postService';
import catService from './catService';
var services = registrar(postService, catService);
// or
var services = registrar(postService)(catService);
// or
var postServices = registrar(postService);
var postAndCatServices = postService(catService);
// express
app.use(net.middleware.express(registrar /* or */ postAndCatServices));
// koa
app.use(net.middleware.koa(registrar /* or */ postAndCatServices));
// how da hell does hapi work?
import fetchrx from 'profecthrx';
const catImageService = fetchrx({
corsPath: 'http://thecatapi.com',
service: 'catImages',
endpoint: '/api/images/get',
headers: { Awesome: 'X' }
});
gatImageService()
.subscibe(
function(catImage) {
console.log('Got cats', uri);
}
)
// Universal;
import fetchrx from 'profetchrx';
import { Actions } from 'thundercats';
const postService = fetchrx({
service: 'postService',
endpoint: '/api',
headers: { Awesome: 'X' }
});
class PostActions extends Actions {
constructor() {
super();
}
static displayName = 'PostActions'
createPost(data) {
postService
.create({ data })
.subscribe(
);
}
getPosts({ slug }) {
postService
.read({ slug })
.subscribe(
::this.setPosts
// this.onError
);
}
setPosts(posts) {
return { posts };
}
}
import Rx from 'rx';
export default function() {
return {
serviceName: 'postService',
read: (ctx) {
const slug = ctx
.pluck('params')
.pluck('slug');
const isAuthor = ctx
.pluck('req')
.pluck('userId')
.map(userId => !!userId);
retrun Rx.Observable.combineLatest(
slug,
isAuthor,
(slug, isAuthor) => ({ slug, isAuthor })
).flatMap(queryCtx => {
return getPostsFromDB(queryCtx);
});
}
};
}
// or
export default service('postService')
.map(create(ctx => {
return ctx
.pluck('body')
.pluck('data')
.do(validateData)
.flatMap(data => {
createPost(data);
});
}))
.map(read(ctx => {
const slug = ctx
.pluck('params')
.pluck('slug');
const isAuthor = ctx
.pluck('req')
.pluck('userId')
.map(userId => !!userId);
return Rx.Observable.combineLatest(
slug,
isAuthor,
(slug, isAuthor) => ({ slug, isAuthor })
).flatMap(queryCtx => {
return getPostsFromDB(queryCtx);
});
}))
.map(subService('list'))
.map(read(ctx => {
return ctx
.pluck('params')
.flatMap(({ catagory }) => {
return getPostsFromDB({ catagory });
});
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment