'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var raf = require('../../../_util/raf.js');

function useRaf(callback) {
  const rafRef = vue.shallowRef();
  const removedRef = vue.shallowRef(false);
  function trigger(...args) {
    if (!removedRef.value) {
      raf.default.cancel(rafRef.value);
      rafRef.value = raf.default(() => {
        callback(...args);
      });
    }
  }
  vue.onBeforeUnmount(() => {
    removedRef.value = true;
    raf.default.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.default = useRaf;
exports.useRafState = useRafState;
//# sourceMappingURL=useRaf.js.map
