function isSerializedFunction(value) {
  return value instanceof MessagePort;
}
function serializeFunction(func) {
  const messageChannel = new MessageChannel();
  messageChannel.port1.onmessage = ({ data: [callId, args] }) => {
    Promise.resolve().then(() => func.apply(null, args)).then((result) => messageChannel.port1.postMessage([callId, result]));
  };
  return messageChannel.port2;
}
function deserializeFunction(serializedFunction) {
  let nextCallId = 0;
  const calls = /* @__PURE__ */ new Map();
  serializedFunction.onmessage = ({ data: [callId, result] }) => {
    calls.get(callId)?.(result);
    calls.delete(callId);
  };
  return function deserializedFunction(...args) {
    return new Promise((resolve) => {
      const callId = nextCallId++;
      calls.set(callId, resolve);
      serializedFunction.postMessage([callId, args]);
    });
  };
}
export {
  deserializeFunction,
  isSerializedFunction,
  serializeFunction
};
//# sourceMappingURL=function-serialization.js.map