Skip to content

Instantly share code, notes, and snippets.

View jayphelps's full-sized avatar
💭
I may be slow to respond.

Jay Phelps jayphelps

💭
I may be slow to respond.
View GitHub Profile
@jayphelps
jayphelps / ofPropertyPathChanges.js
Last active June 15, 2021 18:09
RxJS observable of property value changes, given an object and property path
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);
@jayphelps
jayphelps / isValidEmail.js
Last active September 24, 2015 05:03
Validates an email, error on the side of letting bad ones through (best practice). Accepts emails with plus signs.
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?
*
@jayphelps
jayphelps / index.js
Created May 24, 2016 23:47
async window.open()
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}`);
const oauthUserEpic = (action$) =>
action$
.ofType(OAUTH_USER)
.concatMap(({ token }) =>
get({
endpoint: 'user',
params: { access_token: token }
}) // assuming get() returns an Observable
.map((user) =>
addUser(
/**
* 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.
*
@jayphelps
jayphelps / redux-thunk-examples.js
Created February 7, 2017 05:44 — forked from markerikson/redux-thunk-examples.js
Redux-Thunk examples
// 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});
};
}
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
@jayphelps
jayphelps / stuff.js
Created February 11, 2017 21:54
testing epics by mocking
// api.js
// your API call helpers
import { ajax } from 'rxjs/observable/dom/ajax';
export const fetchSomething = id =>
ajax.getJSON(`/somethings/${id}`);
// the epic
@jayphelps
jayphelps / batchsampling.js
Last active August 18, 2022 23:27
Batch Sampling Example from my talk, Real-time Insights, powered by Reactive Programming
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 / package.json
Last active March 20, 2024 09:41
TypeScript output es2015, esm (ES Modules), CJS, UMD, UMD + Min + Gzip. Assumes you install typescript (tsc), rollup, uglifyjs either globally or included as devDependencies
{
"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",
}
}