index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function addUniqueItem(arr, item) {
  4. if (arr.indexOf(item) === -1)
  5. arr.push(item);
  6. }
  7. function removeItem(arr, item) {
  8. const index = arr.indexOf(item);
  9. if (index > -1)
  10. arr.splice(index, 1);
  11. }
  12. // Adapted from array-move
  13. function moveItem([...arr], fromIndex, toIndex) {
  14. const startIndex = fromIndex < 0 ? arr.length + fromIndex : fromIndex;
  15. if (startIndex >= 0 && startIndex < arr.length) {
  16. const endIndex = toIndex < 0 ? arr.length + toIndex : toIndex;
  17. const [item] = arr.splice(fromIndex, 1);
  18. arr.splice(endIndex, 0, item);
  19. }
  20. return arr;
  21. }
  22. exports.warning = () => { };
  23. exports.invariant = () => { };
  24. if (process.env.NODE_ENV !== "production") {
  25. exports.warning = (check, message) => {
  26. if (!check && typeof console !== "undefined") {
  27. console.warn(message);
  28. }
  29. };
  30. exports.invariant = (check, message) => {
  31. if (!check) {
  32. throw new Error(message);
  33. }
  34. };
  35. }
  36. const MotionGlobalConfig = {
  37. skipAnimations: false,
  38. useManualTiming: false,
  39. };
  40. /*#__NO_SIDE_EFFECTS__*/
  41. function memo(callback) {
  42. let result;
  43. return () => {
  44. if (result === undefined)
  45. result = callback();
  46. return result;
  47. };
  48. }
  49. /*#__NO_SIDE_EFFECTS__*/
  50. const noop = (any) => any;
  51. /*
  52. Progress within given range
  53. Given a lower limit and an upper limit, we return the progress
  54. (expressed as a number 0-1) represented by the given value, and
  55. limit that progress to within 0-1.
  56. @param [number]: Lower limit
  57. @param [number]: Upper limit
  58. @param [number]: Value to find progress within given range
  59. @return [number]: Progress of value within range as expressed 0-1
  60. */
  61. /*#__NO_SIDE_EFFECTS__*/
  62. const progress = (from, to, value) => {
  63. const toFromDifference = to - from;
  64. return toFromDifference === 0 ? 1 : (value - from) / toFromDifference;
  65. };
  66. class SubscriptionManager {
  67. constructor() {
  68. this.subscriptions = [];
  69. }
  70. add(handler) {
  71. addUniqueItem(this.subscriptions, handler);
  72. return () => removeItem(this.subscriptions, handler);
  73. }
  74. notify(a, b, c) {
  75. const numSubscriptions = this.subscriptions.length;
  76. if (!numSubscriptions)
  77. return;
  78. if (numSubscriptions === 1) {
  79. /**
  80. * If there's only a single handler we can just call it without invoking a loop.
  81. */
  82. this.subscriptions[0](a, b, c);
  83. }
  84. else {
  85. for (let i = 0; i < numSubscriptions; i++) {
  86. /**
  87. * Check whether the handler exists before firing as it's possible
  88. * the subscriptions were modified during this loop running.
  89. */
  90. const handler = this.subscriptions[i];
  91. handler && handler(a, b, c);
  92. }
  93. }
  94. }
  95. getSize() {
  96. return this.subscriptions.length;
  97. }
  98. clear() {
  99. this.subscriptions.length = 0;
  100. }
  101. }
  102. /**
  103. * Converts seconds to milliseconds
  104. *
  105. * @param seconds - Time in seconds.
  106. * @return milliseconds - Converted time in milliseconds.
  107. */
  108. /*#__NO_SIDE_EFFECTS__*/
  109. const secondsToMilliseconds = (seconds) => seconds * 1000;
  110. /*#__NO_SIDE_EFFECTS__*/
  111. const millisecondsToSeconds = (milliseconds) => milliseconds / 1000;
  112. /*
  113. Convert velocity into velocity per second
  114. @param [number]: Unit per frame
  115. @param [number]: Frame duration in ms
  116. */
  117. function velocityPerSecond(velocity, frameDuration) {
  118. return frameDuration ? velocity * (1000 / frameDuration) : 0;
  119. }
  120. const warned = new Set();
  121. function hasWarned(message) {
  122. return warned.has(message);
  123. }
  124. function warnOnce(condition, message, element) {
  125. if (condition || warned.has(message))
  126. return;
  127. console.warn(message);
  128. if (element)
  129. console.warn(element);
  130. warned.add(message);
  131. }
  132. exports.MotionGlobalConfig = MotionGlobalConfig;
  133. exports.SubscriptionManager = SubscriptionManager;
  134. exports.addUniqueItem = addUniqueItem;
  135. exports.hasWarned = hasWarned;
  136. exports.memo = memo;
  137. exports.millisecondsToSeconds = millisecondsToSeconds;
  138. exports.moveItem = moveItem;
  139. exports.noop = noop;
  140. exports.progress = progress;
  141. exports.removeItem = removeItem;
  142. exports.secondsToMilliseconds = secondsToMilliseconds;
  143. exports.velocityPerSecond = velocityPerSecond;
  144. exports.warnOnce = warnOnce;