Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: exposing module loader to plugins #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/bagel/src/__tests__/modules/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'foo';
34 changes: 33 additions & 1 deletion packages/bagel/src/__tests__/plugins.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import bagel from '../index';
import bagel, {logger} from '../index';
import createWebsocketServer from '../websocket_io';
import wsRequest from '../utils/ws_request';
import type {Resolver} from 'bagel-module-loader';
Expand Down Expand Up @@ -30,6 +30,8 @@ const startBagelWithPlugins = async (plugins = []) => {
port,
transport: createWebsocketServer
});

logger.setGlobalLevel('info');
};

afterEach(() => {
Expand Down Expand Up @@ -325,3 +327,33 @@ it('attaches metadata in "pre job response" lifecycles', async () => {
expect(meta.vegtable).toBe('squash');
expect(batchResponseMetadata.foo).toBe('bar');
});

it('exposes module loader to plugins', async () => {
const testLoadModulePlugin = {
beforeBatch({batchResponseMetadata, loadModule}) {
expect(
loadModule({
// jobRequest,
// parentBatchRequest,
// jobResponseMetadata,
// batchResponseMetadata
}).default
).toBe('foo');
}
};

await startBagelWithPlugins([testLoadModulePlugin]);
const requestData = {
ID_0: {
name: './__tests__/example_components/simple_react_component',
metadata: {
jobId: 'ID_0'
},
props: {}
}
};

await wsRequestToUrl(requestData);

expect.assertions(1);
});
6 changes: 5 additions & 1 deletion packages/bagel/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ const init = (
withLoadModuleHooks,
withRenderHooks,
afterRequestComplete
} = withPluginHooks(config.plugins);
} = withPluginHooks({
plugins: config.plugins,
// eslint-disable-next-line no-use-before-define
loadModule: (...args) => loadModuleHandler({...args})
});

/**
* Here we wire together the various functions bagel needs. In order to
Expand Down
20 changes: 11 additions & 9 deletions packages/bagel/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ type LoadModuleHandler = JobHandlerRequest => Promise<RenderHandlerRequest>;
type LifeCycleMethod<T> = T => Promise<void> | void;

type Plugin = {
beforeBatch?: LifeCycleMethod<BatchHandlerRequest>,
afterBatch?: LifeCycleMethod<BatchHandlerResponse>,
afterRequestComplete?: LifeCycleMethod<BatchHandlerRequest>,
beforeJob?: LifeCycleMethod<JobHandlerRequest>,
afterJob?: LifeCycleMethod<JobHandlerResponse>,
beforeLoadModule?: LifeCycleMethod<JobHandlerRequest>,
afterLoadModule?: LifeCycleMethod<RenderHandlerRequest>,
beforeRender?: LifeCycleMethod<RenderHandlerRequest>,
afterRender?: LifeCycleMethod<JobHandlerResponse>
beforeBatch?: LifeCycleMethod<BatchHandlerRequest & LoadModuleHandler>,
afterBatch?: LifeCycleMethod<BatchHandlerResponse & LoadModuleHandler>,
afterRequestComplete?: LifeCycleMethod<
BatchHandlerRequest & LoadModuleHandler
>,
beforeJob?: LifeCycleMethod<JobHandlerRequest & LoadModuleHandler>,
afterJob?: LifeCycleMethod<JobHandlerResponse & LoadModuleHandler>,
beforeLoadModule?: LifeCycleMethod<JobHandlerRequest & LoadModuleHandler>,
afterLoadModule?: LifeCycleMethod<RenderHandlerRequest & LoadModuleHandler>,
beforeRender?: LifeCycleMethod<RenderHandlerRequest & LoadModuleHandler>,
afterRender?: LifeCycleMethod<JobHandlerResponse & LoadModuleHandler>
};

type Config = {
Expand Down
2 changes: 1 addition & 1 deletion packages/bagel/src/utils/plugin_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const makePromiseChain = <T: Object>(fns: Array<LifeCycleMethod<T>>) => (
* Promise.resolve will slow us down only for one tick per wrapped function.
*/
Promise.resolve(prev)
.then(next)
.then(() => next({...initialValue}))
/**
* We never care about the return value - it's always the initialValue
* passed by reference.
Expand Down
2 changes: 1 addition & 1 deletion packages/bagel/src/utils/with_plugin_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
LoadModuleHandler
} from '../types';

const withPluginHooks = (plugins: Array<Plugin>) => {
const withPluginHooks = ({plugins}: {plugins: Array<Plugin>}) => {
// Group all of the life cycle hooks by name.
const pluginLifeCycleHooks = groupObjectsByKeys(plugins);

Expand Down