-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
create.ts
42 lines (37 loc) · 1.25 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import * as actions from './actions';
import { swap } from '@dbeining/react-atom';
import { buildName, Extend } from 'piral-core';
import { withPiletState } from './withPiletState';
import { PiletContainersApi } from './types';
/**
* Available configuration options for the container extension.
*/
export interface ContainersConfig {}
/**
* Creates a new Piral API extension for supporting Pilet global state containers.
*/
export function createContainersApi(config: ContainersConfig = {}): Extend<PiletContainersApi> {
return context => {
context.defineActions(actions);
swap(context.state, state => ({
...state,
containers: {},
}));
return (api, target) => {
let containers = 0;
return {
createState(options) {
const actions = {};
const id = buildName(target.name, containers++);
const cb = dispatch => context.replaceState(id, dispatch);
context.createState(id, options.state);
Object.keys(options.actions).forEach(key => {
const action = options.actions[key];
actions[key] = (...args) => action.call(api, cb, ...args);
});
return component => withPiletState(component, id, actions) as any;
},
};
};
};
}