Skip to content

Instantly share code, notes, and snippets.

const combineMiddlewares = middlewares => (store) => {
const propagationFunctions = middlewares.map(middleware => (
middleware(store)
));
const invokeMiddlewareChain = propagationFunctions.reduceRight((nextFn, propagationFunction) => (
propagationFunction(nextFn)
), null);
return invokeMiddlewareChain;
};
const createStore = ({ middlewares = [] }) => {
const store = {};
store.dispatch = combineMiddlewares(middlewares)(store);
return store;
};
const reducerMiddleware = (reducer, initialState) => store => {
let state = initialState;
store.getState = () => state;
return () => (action) => { state = reducer(state, action); };
};
const createStore = ({
reducer = state => state,
middlewares = [],
initialState = undefined,
}) => {
const store = {};
store.dispatch = combineMiddlewares([
...middlewares,
reducerMiddleware(reducer, initialState)
const subscribeMiddleware = () => store => {
let handlers = [];
store.subscribe = (cb) => {
handlers = handlers.concat(cb);
const removeSubscriber = () => { handlers = handlers.filter(fn => fn !== cb) };
return removeSubscriber;
};
return next => (action) => {
if condition {
doSomething();
}
export default function () {
return {
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("jsx");
}
};
}
const { plugins } = require('babylon/lib/parser');
const pluginName = 'swift-blocks';
const plugin = createSwiftBlocksPlugin();
plugins[pluginName] = 'swift-blocks';
module.exports = () => ({
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push(pluginName);
},
pp.parseStatement = function (declaration, topLevel) {
let node = this.startNode();
if (this.match(tokenTypes._if)) {
return this.parseIfStatement(node);
} else {
...
}
}
const plugin = (instance) => {
instance.extend('parseIfStatement', inner => function (node) {
this.next();
const isParenFree = this.match(tokenTypes.parenL);
node.test = isParenFree
? this.parseExpression()
: this.parseParenExpression();
node.consequent = isParenFree
? this.parseBlock(false)