motion-utils.dev.js 5.1 KB

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