Skip to content

Instantly share code, notes, and snippets.

View BerkeleyTrue's full-sized avatar
💭
Baby Yoda is my spirit animal

Berkeley Martinez BerkeleyTrue

💭
Baby Yoda is my spirit animal
View GitHub Profile
function epic(actions){
return actions.ofType('FOO')
.mergeMap(action => {
const transformed = transform(action); // may throw collapsing the pipe
return makeThingDoAsyncy(transform)
.catch(e => [foo(e)]); // won't catch above
});
}
// imperative
@BerkeleyTrue
BerkeleyTrue / routes.jsx
Last active June 20, 2017 16:43
Fyfe.js
import { createServer, at } from 'fyfe';
import { GET, composeRoutes, composeMiddleware } from 'fyfe-compose-routes';
import fyfeDefaults from 'fyfe-defaults';
// composes routes together
// first route to return something other then Void will win
const routes = composeRoutes(
// use a route handler
// Request => Responce|String|Stream|Void
GET('/', renderIndex),
@BerkeleyTrue
BerkeleyTrue / fetch-git-pr.bash
Created October 9, 2015 07:58 — forked from vojtajina/fetch-git-pr.bash
Bash script for fetching a pull request from github...
# fetching a single pull request from github
# put it into ~/.profile or ~/.bashrc
function fetch_pr() {
PR=$1
BRANCH=$2
if [ -z $PR ]; then
echo "Missing pull request number"
return 1
fi
@BerkeleyTrue
BerkeleyTrue / np.sh
Created April 7, 2017 17:19 — forked from stevemao/np.sh
Publish node module
# npm publish with goodies
# prerequisites:
# `npm install -g trash conventional-recommended-bump conventional-changelog conventional-github-releaser conventional-commits-detector json`
# `np` with optional argument `patch`/`minor`/`major`/`<version>`
# defaults to conventional-recommended-bump
# and optional argument preset `angular`/ `jquery` ...
# defaults to conventional-commits-detector
np() {
travis status --no-interactive &&
trash node_modules &>/dev/null;
export class BadFunComp extends React.PureComponent {
handleFun() {
const { funApi } = this.props;
funAPi();
}
render() {
// displayCoolness.props.handleFun will always have a new ref value
// causes child to re-render on ever BadFunComp render
return (
<DisplayCoolness onFun={ () => this.handleFun() } />
const serviceValues = [ 2, 3, 4 ];
// convert values array into a stream of individual values: Obs[...num]
return Observable.of(serviceValues)
// call service on each value and get Observable in return: Obs[Obs[newValFromService]]
.map(val => serviceApi(val))
// convert stream of observables into a stream of a single array of observables Obs[ [Obs[newVal]] ]
.toArray()
// trigger all the service calls with maximum concurrenct
.switchMap((serviceCalls) => Observable.combineLatest(serviceCalls))
.subscribe(newVals => console.log('new values from server: ', newVals)) // [...{id: 2, ...data}, {id: 3, ...dat}]
@BerkeleyTrue
BerkeleyTrue / thunkToObservable.js
Last active November 17, 2016 00:52
Convert thunk to observable
function thunkToObservable(thunk) {
return (getState, ...args) => {
return new Observable(observer => {
const dispatchProxy = new Subject();
const dispatch = action => dispatchProxy.next(action);
thunk(...args)(dispatch, getState);
return dispatchProxy.subscribe(observer);
});
};
}
import { ActionsObservable } from 'redux-observale/lib/ActionsObservable';
import rootEpic from './epics';
const flux = new Reactor({ /*...*/ });
const input$ = new Subject();
const actions$ = new ActionsObservable(input$);
const epic$ = new Subject();
epic$
@BerkeleyTrue
BerkeleyTrue / actions.js
Last active September 20, 2016 21:19
redux create types
import createTypes from 'redux-create-types';
export const ns = 'app';
export const types = createTypes(
[
'openModal',
'onClick',
'updateEmailSettings',
createAsyncTypes('fetch')
],
@BerkeleyTrue
BerkeleyTrue / timed-out-epic.js
Created September 19, 2016 00:18
Time out logout
import { Observable } from 'rxjs';
export default function logOutEpic(actions, { getState }) {
return Observable
.interval(500)
.takeUntil(actions.ofType('LOG_OUT').first())
.map(() => {
const { app: { accessToken } = {} } = getState();
return accessToken;
})