utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __values = (this && this.__values) || function(o) {
  19. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  20. if (m) return m.call(o);
  21. if (o && typeof o.length === "number") return {
  22. next: function () {
  23. if (o && i >= o.length) o = void 0;
  24. return { value: o && o[i++], done: !o };
  25. }
  26. };
  27. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  28. };
  29. Object.defineProperty(exports, "__esModule", { value: true });
  30. exports.isInterpreterStateEqual = exports.isService = exports.shallowEqual = exports.getServiceSnapshot = exports.partition = void 0;
  31. var xstate_1 = require("xstate");
  32. function partition(items, predicate) {
  33. var e_1, _a;
  34. var _b = __read([[], []], 2), truthy = _b[0], falsy = _b[1];
  35. try {
  36. for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
  37. var item = items_1_1.value;
  38. if (predicate(item)) {
  39. truthy.push(item);
  40. }
  41. else {
  42. falsy.push(item);
  43. }
  44. }
  45. }
  46. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  47. finally {
  48. try {
  49. if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
  50. }
  51. finally { if (e_1) throw e_1.error; }
  52. }
  53. return [truthy, falsy];
  54. }
  55. exports.partition = partition;
  56. function getServiceSnapshot(service) {
  57. return service.status !== 0
  58. ? service.getSnapshot()
  59. : service.machine.initialState;
  60. }
  61. exports.getServiceSnapshot = getServiceSnapshot;
  62. // From https://github.com/reduxjs/react-redux/blob/master/src/utils/shallowEqual.ts
  63. function is(x, y) {
  64. if (x === y) {
  65. return x !== 0 || y !== 0 || 1 / x === 1 / y;
  66. }
  67. else {
  68. return x !== x && y !== y;
  69. }
  70. }
  71. function shallowEqual(objA, objB) {
  72. if (is(objA, objB))
  73. return true;
  74. if (typeof objA !== 'object' ||
  75. objA === null ||
  76. typeof objB !== 'object' ||
  77. objB === null) {
  78. return false;
  79. }
  80. var keysA = Object.keys(objA);
  81. var keysB = Object.keys(objB);
  82. if (keysA.length !== keysB.length)
  83. return false;
  84. for (var i = 0; i < keysA.length; i++) {
  85. if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) ||
  86. !is(objA[keysA[i]], objB[keysA[i]])) {
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. exports.shallowEqual = shallowEqual;
  93. function isService(actor) {
  94. return 'state' in actor && 'machine' in actor;
  95. }
  96. exports.isService = isService;
  97. function isInterpreterStateEqual(service, prevState, nextState) {
  98. if (service.status === xstate_1.InterpreterStatus.NotStarted) {
  99. return true;
  100. }
  101. // Only change the current state if:
  102. // - the incoming state is the "live" initial state (since it might have new actors)
  103. // - OR the incoming state actually changed.
  104. //
  105. // The "live" initial state will have .changed === undefined.
  106. var initialStateChanged = nextState.changed === undefined &&
  107. (Object.keys(nextState.children).length > 0 ||
  108. typeof prevState.changed === 'boolean');
  109. return !(nextState.changed || initialStateChanged);
  110. }
  111. exports.isInterpreterStateEqual = isInterpreterStateEqual;