@koordinates/xstate-tree
Version:
Build UIs with Actors using xstate and React
49 lines (48 loc) • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useService = exports.loggingMetaOptions = void 0;
const react_1 = require("react");
/**
* @public
*/
function loggingMetaOptions(ignoredEvents, ignoreContext = undefined) {
const ignoredEventMap = new Map();
ignoredEvents.forEach((event) => {
ignoredEventMap.set(event, true);
});
return {
xstateTree: {
ignoredEvents: ignoredEventMap,
ignoreContext,
},
};
}
exports.loggingMetaOptions = loggingMetaOptions;
/**
* @internal
*/
function useService(service) {
const [current, setCurrent] = (0, react_1.useState)(service.getSnapshot());
const [children, setChildren] = (0, react_1.useState)(service.getSnapshot().children);
const childrenRef = (0, react_1.useRef)({});
(0, react_1.useEffect)(() => {
childrenRef.current = children;
}, [children]);
(0, react_1.useEffect)(function () {
// Set to current service state as there is a possibility
// of a transition occurring between the initial useState()
// initialization and useEffect() commit.
setCurrent(service.getSnapshot());
setChildren(service.getSnapshot().children);
const listener = function (snapshot) {
setCurrent(snapshot);
setChildren(service.getSnapshot().children);
};
const sub = service.subscribe(listener);
return function () {
sub.unsubscribe();
};
}, [service, setChildren]);
return [current, children];
}
exports.useService = useService;