Skip to content

Instantly share code, notes, and snippets.

@MaxMotovilov
MaxMotovilov / gist:6f2ea3ff6615c85a806e022c2ed97c9d
Created April 16, 2020 02:27
Middleware & reducer decorator providing cross-cutting concern solution via selectors
export function injectRootState({getState}) {
return next => action => {
action = next(action);
(action.meta || (action.meta = {})).rootState = getState();
return action;
}
}
export function withSelectors(...selectors) {
const reducer = selectors.pop();
@MaxMotovilov
MaxMotovilov / gist:780cdcb57433f254f62020e8c032f968
Last active April 16, 2020 16:18
Triggers for Redux - perform imperative actions & dispatches on specific changes in the store w/o touching React components
import {bindActionCreators} from 'redux';
export function withSelectors(...selectors) {
const bound = selectors.pop();
return (...args) => {
const state = args[args.length-1].getState();
return bound(...selectors.map( sel => sel(state) ), ...args);
}
}
@MaxMotovilov
MaxMotovilov / chart.html
Last active March 30, 2020 04:36
Covid-19 positive cases & hospitalization vs. testing density per 1M (uses covidtracking.com API)
<!DOCTYPE html>
<html>
<head>
<title>COVID-19 testing progress based on covidtracking.com</title>
<style type="text/css">
body {
font-family: sans-serif;
}
.pane {
@MaxMotovilov
MaxMotovilov / a.js
Created August 8, 2017 22:28
Running Uglify twice results in correct optimization!
export default function() { console.log( "A" ) }
@MaxMotovilov
MaxMotovilov / eval-in-closure-ext.js
Last active December 14, 2015 03:59
eval() in closure with subsequent read-write access to closure
function bindClosureSlot( name ) {
return "function(v){ if( arguments.length<1 ) return " + name + "; else return " + name + "=v; }";
}
function evalInClosureExt( code, closure ) {
return Function.apply(
null,
Object.keys( closure ).concat(
"return [" +
code + "," +
@MaxMotovilov
MaxMotovilov / binders.js
Last active December 12, 2015 12:39
Property, method, slot binders: eager and late
// One-way property binders
function bindNowGetProp( propname, obj ) {
return function() { return obj[propname]; }
}
function bindNowSetProp( propname, obj ) {
return function(v){ return obj[propname] = v; }
}
function Def( arg ) {
if( arg instanceof Def )
this.parent = arg;
else if( typeof arg !== "undefined" )
this.v = arg;
}
Def.prototype = {
chain: function(v){