model.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { __assign, __spreadArray, __read } from './_virtual/_tslib.js';
  2. import { assign } from './actions.js';
  3. import { createMachine } from './Machine.js';
  4. import { mapValues } from './utils.js';
  5. function createModel(initialContext, creators) {
  6. var eventCreators = creators === null || creators === void 0 ? void 0 : creators.events;
  7. var actionCreators = creators === null || creators === void 0 ? void 0 : creators.actions;
  8. var model = {
  9. initialContext: initialContext,
  10. assign: assign,
  11. events: eventCreators ? mapValues(eventCreators, function (fn, eventType) {
  12. return function () {
  13. var args = [];
  14. for (var _i = 0; _i < arguments.length; _i++) {
  15. args[_i] = arguments[_i];
  16. }
  17. return __assign(__assign({}, fn.apply(void 0, __spreadArray([], __read(args), false))), {
  18. type: eventType
  19. });
  20. };
  21. }) : undefined,
  22. actions: actionCreators ? mapValues(actionCreators, function (fn, actionType) {
  23. return function () {
  24. var args = [];
  25. for (var _i = 0; _i < arguments.length; _i++) {
  26. args[_i] = arguments[_i];
  27. }
  28. return __assign(__assign({}, fn.apply(void 0, __spreadArray([], __read(args), false))), {
  29. type: actionType
  30. });
  31. };
  32. }) : undefined,
  33. reset: function () {
  34. return assign(initialContext);
  35. },
  36. createMachine: function (config, implementations) {
  37. return createMachine('context' in config ? config : __assign(__assign({}, config), {
  38. context: initialContext
  39. }), implementations);
  40. }
  41. };
  42. return model;
  43. }
  44. export { createModel };