Skip to content

Instantly share code, notes, and snippets.

@bradennapier
Created June 19, 2017 11:26
Show Gist options
  • Save bradennapier/280e3bcd4d51b7b6ac4abac758b8423a to your computer and use it in GitHub Desktop.
Save bradennapier/280e3bcd4d51b7b6ac4abac758b8423a to your computer and use it in GitHub Desktop.
Process Shell example for Widgets
import { Process } from 'redux-saga-process';
import { call, put, select } from 'redux-saga/effects';
import { createSelector } from 'reselect';
import _ from 'lodash';
/**
* GET_DEFAULT_WIDGET_STATE
*/
const GET_DEFAULT_WIDGET_STATE = () => ({
rehydrated: false,
});
/**
* GET_WIDGET_CONFIG
* @param {[type]} config received user configuration
* @return {[type]} configuration merged with defaults
*/
const GET_WIDGET_CONFIG = config => ({
reduces: 'widget::processSelect',
pid: 'widget::processSelect',
exports: ['actions', 'selectors'],
log: false,
...config,
});
/**
* processLoadsOnAction
* @param {[type]} action Dispatched Action
* @return {[type]} true / false indicating if the process should load
*
* processLoadsOnAction takes either a string (the type dispatched) or a
* function which resolves a true/false value of whether or not the process
* should start loading it's dependencies and startup.
*
*/
const processLoadsOnAction = action =>
action.type === 'WIDGET_MOUNTS' && action.widgetID === 'projectSelect';
/**
* processLoadsWithScope
* @param {[type]} promises A PromiseMap which resolves to our scope
* @param {[type]} defaulted module => module.default (.then(defaulted))
* @param {[type]} action The action that caused the load to occur.
* @return {[type]} undefined or a Promise that resolves to scope.
*
* processLoadsWithScope is used to asynchronously load the dependencies
* of a process at the time that it has been requested to startup. It can
* either return a promise which resolves to the desired scope or use the
* provided PromiseMap and set the promises on it.
*
* promises.set('firebase', import('firebase').then(defaulted))
*/
const processLoadsWithScope = (promises, defaulted, action) => {};
/**
* processActionRoutes
* @type {Object}
*
* Our Widgets Action Routes define what actions
* it responds to and routes the action to the
* appropriate saga.
*
*/
const processActionRoutes = {};
/**
* processActionCreators
* @type {Object}
* Our widgets api which can be imported by
* components through the connectProcesses
* module.
*/
const processActionCreators = {};
/**
* processInitialState
* @type {Object}
*
* Each process may optionally reduce one or more keys in the
* Here we provide the default redux state for the widget.
*/
const processInitialState = GET_DEFAULT_WIDGET_STATE();
/**
* processReducer
* @type {Object}
*/
const processReducer = {};
/**
* configureWidgetProjectSelectProcess
* @param {Object} _config user-provided configuration to merge
* @return {Process || ProcessSnapshot}
*
* Our (optional) factory function is used to aid in the configuration of
* our processes logic and handling. It is called when the store is being
* built and returns everything that we need to know to build and maintain
* a given process.
*
* It may either return a ProcessSnapshot or a Process with the configuration
* set as it's static properties.
*/
export default function configureWidgetProjectSelectProcess(_config) {
const config = build_config(_config);
const processConfig = {
pid: config.pid,
reduces: config.reduces,
exports: config.exports,
};
/**
* processSelectors
* @type {Object}
* Our Process may export state selectors that our
* components may use to listen to the processes
* state and it's changes.
*/
const processSelectors = {};
/**
* widget State Selector
* @param {Object} state receives the redux state
* @return {Object} returns this widgets entire
* value in the state.
*/
processSelectors.widget = state => state[config.reduces];
/**
* widgetState State Selector
* @type {Function}
*/
processSelectors.widgetState = createSelector(
processSelectors.widget,
widget => widget.state,
);
/**
* scope
* @type {Object}
*/
let scope;
class WidgetProjectSelectProcess extends Process {
/**
* Process constructor
* @param {String} processID
* @param {Any} prev_state Used for hot reloaded processes, receives the
* value of this.state if it existed in a previous
* instance of the Process.
* @param {Object} proc Contains the currently known data about the process
* including its compiled reducer, schema, ID, path,
* and more.
*/
constructor(processID, prev_state, proc) {
super(processID, prev_state, proc);
}
/**
* processStarts
* @param {String} processID
* When the Process starts for the first time, processStarts is
* called if defined with the processes ID.
*/
*processStarts(processID) {}
}
return {
process: WidgetProjectSelectProcess,
config: processConfig,
initialState: processInitialState,
actionCreators: processActionCreators,
loadProcess: processLoadsWithScope,
actionRoutes: processActionRoutes,
selectors: processSelectors,
reducer: processReducer,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment