Skip to content

Instantly share code, notes, and snippets.

@dtothefp
Last active March 23, 2016 13:56
Show Gist options
  • Save dtothefp/1b1e074a1ac720878477 to your computer and use it in GitHub Desktop.
Save dtothefp/1b1e074a1ac720878477 to your computer and use it in GitHub Desktop.
import reactor from '../modules/bootstrap';
import serverMod from '@hfa/sos-server/lib/modules/server';
import formMetaMod from '../modules/form-meta';
import {Getters as FormGetters, makeActions as makeFormActions} from '@hfa/form-components';
import {analytics} from '../config';
import {isFunction} from '../utils/lodash';
class BootStrap {
constructor() {
const {_reactor = '{}'} = global.hfaGlobals || {};
if (!_reactor) {
this.init({env: 'server'});
} else {
this.init({env: 'client', data: _reactor});
}
}
init({env, data}) {
if (env === 'client' && data) {
reactor.loadState(data);
}
const {Actions: ServerActions, Getters: ServerGetters} = serverMod(reactor);
const {Actions: FormMetaActions, Getters: FormMetaGetters} = formMetaMod(reactor);
const FormActions = makeFormActions(reactor, {analytics, legacy: true});
const Actions = {
...ServerActions,
...FormMetaActions,
...FormActions
};
const Getters = {
ServerGetters,
FormMetaGetters,
FormGetters
};
this.props = {
reactor,
Actions,
Getters
};
}
run(fn) {
const requiredKeys = ['Actions', 'Getters', 'reactor'];
fn = isFunction(fn) ? fn : (props) => props;
const newProps = fn(this.props);
if (_.isPlainObject(newProps)) {
//add missing props if they aren't there
//you could also do a deeper check on Actions/Getters to see if someone did not merge properly and you lost keys
//but this probably isn't necessary
if (_.intersection(requiredKeys, Object.keys(newProps)).length !== requiredKeys.length) {
requiredKeys.forEach(key => {
const prop = newProps[key];
if (_.isUndefined(prop)) newProps[key] = prop;
});
}
this.props = newProps;
}
return this.props;
}
}
if (!global.bootStrap) {
global.bootStrap = new BootStrap(opts, fn);
}
return global.bootStrap;
import bs from './bootstrap';
import SomeMod from './my-mod';
const {Actions, Getters, reactor} = bs.run(props => {
const {reactor} = props;
const {Actions: MyActions, Getters: MyGetters} = SomeMod(reactor);
return _.merge({}, props, {
Actions: {MyActions},
Getters: {MyGetters}
})
})
Actions.MyActions.register('bloop');
comp = <SomeComp {Actions, Getters, reactor} />;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment