piral-core
Version:
The core library for creating a Piral instance.
61 lines • 1.87 kB
JavaScript
import { runPilet } from 'piral-base';
import { withKey, replaceOrAddItem, removeNested, withProvider, withRoute, noop } from '../utils';
export function initialize(ctx, loading, error, modules) {
ctx.dispatch((state) => ({
...state,
app: {
...state.app,
error,
loading,
},
modules,
}));
}
export function addPilet(ctx, meta) {
return ctx.options
.loadPilet(meta)
.then((pilet) => ctx.injectPilet(pilet))
.then((pilet) => runPilet(ctx.options.createApi, pilet, ctx.options.hooks))
.then(noop);
}
export function removePilet(ctx, name) {
ctx.dispatch((state) => ({
...state,
modules: state.modules.filter((m) => m.name !== name),
registry: removeNested(state.registry, (m) => m.pilet === name),
}));
ctx.emit('unload-pilet', {
name,
});
return Promise.resolve();
}
export function injectPilet(ctx, pilet) {
ctx.dispatch((state) => ({
...state,
modules: replaceOrAddItem(state.modules, pilet, (m) => m.name === pilet.name),
registry: removeNested(state.registry, (m) => m.pilet === pilet.name),
}));
ctx.emit('unload-pilet', {
name: pilet.name,
});
return pilet;
}
export function setComponent(ctx, name, component) {
ctx.dispatch((state) => ({
...state,
components: withKey(state.components, name, component),
}));
}
export function setErrorComponent(ctx, type, component) {
ctx.dispatch((state) => ({
...state,
errorComponents: withKey(state.errorComponents, type, component),
}));
}
export function setRoute(ctx, path, component) {
ctx.dispatch(withRoute(path, component));
}
export function includeProvider(ctx, provider) {
ctx.dispatch(withProvider(provider));
}
//# sourceMappingURL=app.js.map