This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api.js | |
// your API call helpers | |
import { ajax } from 'rxjs/observable/dom/ajax'; | |
export const fetchSomething = id => | |
ajax.getJSON(`/somethings/${id}`); | |
// the epic |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { webSocket } from 'rxjs/observable/dom/webSocket'; | |
import { timer } from 'rxjs/observable/timer'; | |
import { fromEvent } from 'rxjs/observable/fromEvent'; | |
// Lazy, doesn't connect when no one is subscribed, | |
// but to multiplex we need a single static instance | |
// so that they all use the same socket | |
const socket$ = webSocket('ws://stock/endpoint'); | |
// So this is bi-direction multiplexing; multiple concurrent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The classic AJAX call - dispatch before the request, and after it comes back | |
function myThunkActionCreator(someValue) { | |
return (dispatch, getState) => { | |
dispatch({type : "REQUEST_STARTED"}); | |
myAjaxLib.post("/someEndpoint", {data : someValue}) | |
.then(response => dispatch({type : "REQUEST_SUCCEEDED", payload : response}) | |
.catch(error => dispatch({type : "REQUEST_FAILED", error : error}); | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* README FIRST:::::::::::::::::: | |
* | |
* This isn't meant to necessarily be an example of "best practices" | |
* but rather to generally show how you might convert the redux-thunk examples | |
* here: https://gist.github.com/markerikson/ea4d0a6ce56ee479fe8b356e099f857e | |
* | |
* The redux-thunk examples used generic placeholder names, so it's possible | |
* I misunderstood the usecase/intent. | |
* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const oauthUserEpic = (action$) => | |
action$ | |
.ofType(OAUTH_USER) | |
.concatMap(({ token }) => | |
get({ | |
endpoint: 'user', | |
params: { access_token: token } | |
}) // assuming get() returns an Observable | |
.map((user) => | |
addUser( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onclick = () => { | |
// You MUST create the window on the same event | |
// tick/stack as the user-initiated event (e.g. click callback) | |
const googleWindow = window.open(); | |
// Do your async work | |
fakeAjax(response => { | |
// Change the URL of the window you created once you | |
// know what the full URL is! | |
googleWindow.location.replace(`https://google.com?q=${response}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isValidEmail(str) { | |
/** | |
* These comments use the following terms from RFC2822: | |
* local-part, domain, domain-literal and dot-atom. | |
* Does the address contain a local-part followed an @ followed by a domain? | |
* Note the use of lastIndexOf to find the last @ in the address | |
* since a valid email address may have a quoted @ in the local-part. | |
* Does the domain name have at least two parts, i.e. at least one dot, | |
* after the @? If not, is it a domain-literal? | |
* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isObject(value) { | |
// Avoid an old bug in Chrome 19-20 | |
// See https://code.google.com/p/v8/issues/detail?id=2291 | |
const type = typeof value; | |
return type === 'function' || (!!value && type === 'object'); | |
} | |
function ofPropertyChanges(obj, key) { | |
if (isObject(obj) === false) { | |
return Rx.Observable.return(undefined); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Object.resolve(document, 'body.style') | |
*/ | |
Object.resolve = function resolve(obj, propPath) { | |
return [obj] | |
.concat(propPath.split('.')) | |
.reduce((prevObj, currKey) => prevObj[currKey]); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var parseLocation = (function () { | |
var a = document.createElement('a'); | |
function parseLocation(url) { | |
a.href = url; | |
var protocol = (!a.protocol || a.protocol === ':') ? location.protocol : a.protocol; | |
// IE8 inconsistently returns leading slash | |
var pathname = (a.pathname.match(/^\//)) ? a.pathname : '/' + a.pathname; |