@bemedev/app-solid
Version:
Middleware between @bemedev/app-ts and solidjs
98 lines (95 loc) • 3.21 kB
JavaScript
import { interpret as interpret$1, getByKey } from '@bemedev/app-ts';
import { DEFAULT_DELIMITER } from '@bemedev/app-ts/lib/constants/index.js';
import { INIT_EVENT } from '@bemedev/app-ts/lib/events/index.js';
import { decomposeSV, replaceAll } from '@bemedev/app-ts/lib/utils/index.js';
import { from, createRoot, createMemo } from 'solid-js';
import { defaultSelector } from './default.js';
const interpret = (...[machine, config]) => {
const service = interpret$1(machine, config);
const initialState = {
context: service.context,
status: 'idle',
value: service.initialValue,
event: INIT_EVENT,
};
const _store = from(service);
const store = () => _store() ?? initialState;
const start = service.start;
const stop = service.stop;
const pause = service.pause;
const resume = service.resume;
const state = (...[accessor = defaultSelector, equals]) => {
const out = createRoot(() => createMemo(() => accessor(store()), accessor(initialState), {
equals,
}));
return out;
};
const reducer = (accessor) => {
const stateAccessor = accessor;
const reduceS = (...[_accessor = defaultSelector, equals]) => state(_state => {
const step1 = stateAccessor(_state);
const step2 = _accessor(step1);
return step2;
}, equals);
return reduceS;
};
const context = reducer(state => state.context);
const send = service.send;
const value = () => state(state => state.value)();
const mapper = (entry) => replaceAll({ entry, match: '.', replacement: DEFAULT_DELIMITER });
const dps = () => decomposeSV(value()).map(mapper);
const status = () => state(state => state.status)();
const tags = () => state(state => state.tags)();
const _select = (selector, equals) => {
const initial = getByKey(initialState, selector);
const out = createRoot(() => createMemo(() => {
const _state = store();
const _out = getByKey(_state, selector);
return _out;
}, initial, {
equals,
}));
return out;
};
const select = (typeof service.context !== 'object' ? undefined : _select);
// #endregion
const matches = (...values) => {
return () => {
const out = values.every(value => dps().includes(value));
return out;
};
};
const contains = (...values) => {
return () => {
const out = values.every(value => dps().every(dp => dp.includes(value)));
return out;
};
};
const values = Object.keys(machine.flat);
const subscribe = service.subscribe;
const dispose = service[Symbol.asyncDispose];
const addOptions = service.addOptions;
return {
contains,
context,
dispose,
dps,
matches,
pause,
reducer,
resume,
select,
send,
start,
state,
status,
stop,
subscribe,
tags,
value,
values,
addOptions,
};
};
export { interpret };
//# sourceMappingURL=interpret.js.map