useInterpret.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. var __rest = (this && this.__rest) || function (s, e) {
  13. var t = {};
  14. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  15. t[p] = s[p];
  16. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  17. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  18. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  19. t[p[i]] = s[p[i]];
  20. }
  21. return t;
  22. };
  23. var __read = (this && this.__read) || function (o, n) {
  24. var m = typeof Symbol === "function" && o[Symbol.iterator];
  25. if (!m) return o;
  26. var i = m.call(o), r, ar = [], e;
  27. try {
  28. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  29. }
  30. catch (error) { e = { error: error }; }
  31. finally {
  32. try {
  33. if (r && !r.done && (m = i["return"])) m.call(i);
  34. }
  35. finally { if (e) throw e.error; }
  36. }
  37. return ar;
  38. };
  39. import { useEffect, useState } from 'react';
  40. import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
  41. import { interpret, InterpreterStatus, State, toObserver } from 'xstate';
  42. import useConstant from './useConstant';
  43. export function useIdleInterpreter(getMachine, options) {
  44. var machine = useConstant(function () {
  45. return typeof getMachine === 'function' ? getMachine() : getMachine;
  46. });
  47. if (process.env.NODE_ENV !== 'production' &&
  48. typeof getMachine !== 'function') {
  49. var _a = __read(useState(machine), 1), initialMachine = _a[0];
  50. if (getMachine !== initialMachine) {
  51. console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' +
  52. 'Please make sure that you pass the same Machine as argument each time.');
  53. }
  54. }
  55. var context = options.context, guards = options.guards, actions = options.actions, activities = options.activities, services = options.services, delays = options.delays, rehydratedState = options.state, interpreterOptions = __rest(options, ["context", "guards", "actions", "activities", "services", "delays", "state"]);
  56. var service = useConstant(function () {
  57. var machineConfig = {
  58. context: context,
  59. guards: guards,
  60. actions: actions,
  61. activities: activities,
  62. services: services,
  63. delays: delays
  64. };
  65. var machineWithConfig = machine.withConfig(machineConfig, function () { return (__assign(__assign({}, machine.context), context)); });
  66. return interpret(machineWithConfig, interpreterOptions);
  67. });
  68. // Make sure options are kept updated when they change.
  69. // This mutation assignment is safe because the service instance is only used
  70. // in one place -- this hook's caller.
  71. useIsomorphicLayoutEffect(function () {
  72. Object.assign(service.machine.options.actions, actions);
  73. Object.assign(service.machine.options.guards, guards);
  74. Object.assign(service.machine.options.activities, activities);
  75. Object.assign(service.machine.options.services, services);
  76. Object.assign(service.machine.options.delays, delays);
  77. }, [actions, guards, activities, services, delays]);
  78. return service;
  79. }
  80. export function useInterpret(getMachine) {
  81. var _a = [];
  82. for (var _i = 1; _i < arguments.length; _i++) {
  83. _a[_i - 1] = arguments[_i];
  84. }
  85. var _b = __read(_a, 2), _c = _b[0], options = _c === void 0 ? {} : _c, observerOrListener = _b[1];
  86. var service = useIdleInterpreter(getMachine, options);
  87. useEffect(function () {
  88. if (!observerOrListener) {
  89. return;
  90. }
  91. var sub = service.subscribe(toObserver(observerOrListener));
  92. return function () {
  93. sub.unsubscribe();
  94. };
  95. }, [observerOrListener]);
  96. useEffect(function () {
  97. var rehydratedState = options.state;
  98. service.start(rehydratedState ? State.create(rehydratedState) : undefined);
  99. return function () {
  100. service.stop();
  101. service.status = InterpreterStatus.NotStarted;
  102. };
  103. }, []);
  104. return service;
  105. }