This file contains 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 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
// WARNING: Completely untested code. it might not work and/or it might have | |
// things that don't work well. Just made for illustrational purposes | |
// redux-observable shines the most with complex async stuff, like WebSockets | |
// but many of us will still use it for more modest things like AJAX requests. | |
// In these cases, there can be a ton of repetitive boilerplate. So this is a | |
// simple example of applying some abstractions and conventions to make it easier. | |
// THAT SAID, since abstractions cause indirection it can make it harder for | |
// someone to come along later and know how something works. Weigh the costs | |
// and remember, this example isn't a suggestion of the actual code you should |
This file contains 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 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 v8 = require('v8'); | |
const fs = require('fs'); | |
function writeDemo() { | |
const module = new WebAssembly.Module(new Uint8Array([ | |
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 | |
])); | |
const buffer = v8.serialize(module); | |
fs.writeFileSync('cached-wasm-module.buffer', buffer); | |
} |
This file contains 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 dispatchSwallowedError(e) { | |
window.dispatchEvent(new ErrorEvent('error', { | |
error: e, | |
message: e.message | |
})); | |
console.error(e); | |
} |
This file contains 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 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 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 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 get = Ember.get; | |
var HistoryScrollLocation = Ember.HistoryLocation.extend({ | |
document: document, | |
window: window, | |
_storeScrollPos: function () { | |
var state = history.state, | |
window = get(this, 'window'), | |
doc = get(this, 'document.documentElement'); |
This file contains 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 isValidURL = (function () { | |
var input = document.createElement('input'); | |
input.type = 'url'; | |
function isValidURL(url) { | |
input.value = url; | |
return input.validity.valid; | |
} | |
return isValidURL; |