Skip to content

Instantly share code, notes, and snippets.

View joedski's full-sized avatar
💭
Ætheric

Joe joedski

💭
Ætheric
View GitHub Profile
@joedski
joedski / tieredMemoizer.storesOnly.js
Created July 16, 2017 23:35
The simplest case, which doesn't allow you to use existing memoizers, though you can use their stores if they expose that.
// Simple single-value store.
// Setting a KV pair overwrites the last.
// Follows a get/set/has interface similar to Maps.
function singleValueStore() {
let key;
let value;
let isSet = false;
function get(givenKey) {
if (!isSet) return undefined;
@joedski
joedski / hof-thunks-resource-thunk.js
Last active September 19, 2017 01:51
hof thunks: resource thunk creator
export function resource(resourceName, mapArgs) {
return (params, meta) => async (getState, dispatch) => {
const fetchOptions = mapArgs(params, meta);
dispatch(resourceActions.requestResource({
name: resourceName,
params,
meta,
}));
@joedski
joedski / hof-thunks-0-my-endpoint.js
Last active September 19, 2017 02:28
hof thunks: simple endpoint example
import { resource } from 'my-app/api/resource';
export const myEndpoint = resource('myEndpoint', (params, meta) => ({
uri: `myservice.example.com/v1/my-endpoint?${querystring.stringify(params)}`,
headers: DEFAULT_HEADERS,
// ...etc.
}));
@joedski
joedski / hof-thunks-connected-component.jsx
Last active September 19, 2017 01:54
hof thunks: A connected component
import { myEndpoint } from 'my-app/api/thunks';
// ... other sundry imports.
export default compose(
connect(
null,
(dispatch, ownProps) => ({
getMyResource: dispatch(myEndpoint({
name: ownProps.name,
order: 'asc',
@joedski
joedski / hof-thunks-0-params-decorators.js
Last active September 23, 2017 01:57
hof thunks: first decorator
// my-app/api/thunk-utils.js
// Used to dispatch the thunk.
// Rather boring compared to the rest.
export function map(fn) {
return (createThunk) => (params, meta) => fn(createThunk(params, meta));
}
// These params are enforced.
export function withParams(overrideParams) {
@joedski
joedski / hof-thunks-0-then-decorators.js
Last active September 23, 2017 02:34
hof thunks: chaining
// my-app/api/thunk-utils.js
// ... previous decorators.
export function then(mapResToCreateNextThunk) {
return (createThunk) => (params, meta) => async (dispatch, getState) => {
const res = await dispatch(createThunk(params, meta));
if (res.error) return res;
return dispatch(mapResToCreateNextThunk(res, getState())(params, meta));
};
@joedski
joedski / hof-thunks-0-mapres-decorator.js
Last active September 23, 2017 03:11
hof thunks: mapping resolutions
// my-app/api/thunk-utils.js
// ... other decorators and such.
export function mapResolution(mapResFn) {
return (createThunk) => (params, meta) => async (dispatch, getState) => {
const res = await dispatch(createThunk(params, meta));
if (res.error) return res;
return {
...res,
@joedski
joedski / comp-undo-steps-0-step-example.js
Last active September 27, 2017 00:36
comp-undo-steps 0: Example step implementation
function stepA(options) {
return (next) => async (pctx) => {
// Do stuff, allowing any errors to reject this promise,
// passing them back to previous steps.
const result = await doSomething(pctx.foo);
const nextPctx = { ...pctx, bar: result };
try {
// NOTE: Technically, you could further process the result
// before returning it.
@joedski
joedski / comp-undo-steps-1-proc-example.js
Last active September 27, 2017 00:43
comp-undo-steps 1: An example process.
const processFoo =
stepA(optionsA)(
stepB(optionsB)(
stepC(optionsC)(
// A SACRIFICE TO THE PYRAMID OF DOOM
// On a more serious note, since each step function requires
// a next function, we need to pass in a "return" function which
// doesn't expect a next function to complete the chain.
// If we didn't do this, stepC would return a function expecting a next
// function... passing that function an object doesn't cause an error,
@joedski
joedski / comp-undo-steps-2-compose.js
Last active September 27, 2017 01:15
comp-undo-steps 2: Using compose to flatten things out.
const processFoo = compose(
stepA(optionsA),
stepB(optionsB),
stepC(optionsC)
)(res => res);
async function handleSomeThing(ctx) {
// Create a pctx with the original ctx and whatever else we want.
// NOTE: in this case, we're trying to not pollute the original ctx,
// which might be something like a Koa Context.