Blame view

天文台pc/tianwentai-ui/node_modules/motion-dom/dist/es/value/transform-value.mjs 1.17 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  import { collectMotionValues, motionValue } from './index.mjs';
  import { subscribeValue } from './subscribe-value.mjs';
  
  /**
   * Create a `MotionValue` that transforms the output of other `MotionValue`s by
   * passing their latest values through a transform function.
   *
   * Whenever a `MotionValue` referred to in the provided function is updated,
   * it will be re-evaluated.
   *
   * ```jsx
   * const x = motionValue(0)
   * const y = transformValue(() => x.get() * 2) // double x
   * ```
   *
   * @param transformer - A transform function. This function must be pure with no side-effects or conditional statements.
   * @returns `MotionValue`
   *
   * @public
   */
  function transformValue(transform) {
      const collectedValues = [];
      /**
       * Open session of collectMotionValues. Any MotionValue that calls get()
       * inside transform will be saved into this array.
       */
      collectMotionValues.current = collectedValues;
      const initialValue = transform();
      collectMotionValues.current = undefined;
      const value = motionValue(initialValue);
      subscribeValue(collectedValues, value, transform);
      return value;
  }
  
  export { transformValue };
  //# sourceMappingURL=transform-value.mjs.map