'use strict';

var vue = require('vue');
require('../../../../utils/index.cjs');
var raf = require('../../../../utils/vue/raf.cjs');

function useRaf(callback) {
  const rafRef = vue.shallowRef();
  const removedRef = vue.shallowRef(false);
  function trigger(...args) {
    if (!removedRef.value) {
      raf.raf.cancel(rafRef.value);
      rafRef.value = raf.raf(() => {
        callback(...args);
      });
    }
  }
  vue.onBeforeUnmount(() => {
    removedRef.value = true;
    raf.raf.cancel(rafRef.value);
  });
  return trigger;
}
function useRafState(defaultState) {
  const batchRef = vue.shallowRef([]);
  const state = vue.shallowRef(
    typeof defaultState === "function" ? defaultState() : defaultState
  );
  const flushUpdate = useRaf(() => {
    let value = state.value;
    batchRef.value.forEach((callback) => {
      value = callback(value);
    });
    batchRef.value = [];
    state.value = value;
  });
  function updater(callback) {
    batchRef.value.push(callback);
    flushUpdate();
  }
  return [state, updater];
}

exports.useRaf = useRaf;
exports.useRafState = useRafState;
//# sourceMappingURL=useRaf.cjs.map
