Skip to content

Instantly share code, notes, and snippets.

💭
on sabbatical, I may be slow to respond.

Jay Phelps jayphelps

💭
on sabbatical, I may be slow to respond.
Block or report user

Report or block jayphelps

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jayphelps
jayphelps / redux-observable-typescript-testing-example.ts
Created Feb 24, 2019
Example using redux-observable + typescript + TestScheduler, with a run helper written
View redux-observable-typescript-testing-example.ts
import { map, delay } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { Action } from 'redux';
import { Epic, ofType, ActionsObservable, StateObservable } from 'redux-observable';
const scheduler = new TestScheduler((actual, expected) => {
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
throw new Error(`Failing test
actual: ${JSON.stringify(actual, null, 2)}
@jayphelps
jayphelps / gist:cfc66b39cdd45187df936fec545df447
Last active Jan 20, 2018
WebAssembly structured clone node.js v8 serialize deserialize "Unable to deserialize cloned data"
View gist:cfc66b39cdd45187df936fec545df447
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);
}
@jayphelps
jayphelps / a.js
Last active Apr 26, 2018
Making abstractions for redux and redux-observable
View a.js
// 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
@jayphelps
jayphelps / dispatchSwallowedError.js
Created Oct 17, 2017
in situations like tc39 Observable when it should "swallow" errors, but we still want to report them to window.onerror (HostReportErrors) and in the console
View dispatchSwallowedError.js
function dispatchSwallowedError(e) {
window.dispatchEvent(new ErrorEvent('error', {
error: e,
message: e.message
}));
console.error(e);
}
@jayphelps
jayphelps / package.json
Last active Jul 16, 2019
TypeScript output es2015, esm (ES Modules), CJS, UMD, UMD + Min + Gzip. Assumes you install typescript (tsc), rollup, uglifyjs either globally or included as devDependencies
View package.json
{
"scripts": {
"build": "npm run build:es2015 && npm run build:esm && npm run build:cjs && npm run build:umd && npm run build:umd:min",
"build:es2015": "tsc --module es2015 --target es2015 --outDir dist/es2015",
"build:esm": "tsc --module es2015 --target es5 --outDir dist/esm",
"build:cjs": "tsc --module commonjs --target es5 --outDir dist/cjs",
"build:umd": "rollup dist/esm/index.js --format umd --name YourLibrary --sourceMap --output dist/umd/yourlibrary.js",
"build:umd:min": "cd dist/umd && uglifyjs --compress --mangle --source-map --screw-ie8 --comments --o yourlibrary.min.js -- yourlibrary.js && gzip yourlibrary.min.js -c > yourlibrary.min.js.gz",
}
}
@jayphelps
jayphelps / batchsampling.js
Last active Feb 27, 2018
Batch Sampling Example from my talk, Real-time Insights, powered by Reactive Programming
View batchsampling.js
let buffer = getWebSocket()
.bufferTime(1000);
let gate = new BehaviorSubject(true);
let batchSize = 50;
let batchSizeCounter = 0;
let results = gate
.switchMap(enabled => enabled ? buffer : Observable.never())
.do(buffer => {
@jayphelps
jayphelps / stuff.js
Created Feb 11, 2017
testing epics by mocking
View stuff.js
// api.js
// your API call helpers
import { ajax } from 'rxjs/observable/dom/ajax';
export const fetchSomething = id =>
ajax.getJSON(`/somethings/${id}`);
// the epic
View stockTickerEpic.js
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
View redux-thunk-examples.js
// 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});
};
}
View redux-observable-examples.js
/**
* 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.
*
You can’t perform that action at this time.