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AY,CAAC,CAAI,GAAA,KAAA,CAAM,CAAC,CAAA,CAAED,EAAe,EAAE,CAAA;AAAA;AAC7C;AAGF,IAAA,OAAO,MAAM;AACX,MAAK,EAAA,GAAAE,qBAAA;AAEL,MAAA,KAAA,IAAS,CAAI,GAAA,CAAA,EAAG,CAAI,GAAA,CAAA,EAAG,CAAK,EAAA,EAAA;AAC1B,QAAA,WAAA,CAAY,CAAC,CAAE,EAAA;AAAA;AACjB,KACF;AAAA;AAGF,EAAO,OAAA,KAAA,CAAMF,EAAe,EAAE,CAAA;AAChC,CAAA;AAoCA,IAAO,qBAAQ,GAAA","file":"chunk-OOTMIK4E.cjs","sourcesContent":["import noop from 'lodash.noop';\nimport type { AsyncState, StateBase as State } from '../types';\nimport { postBatchCallbacksPush } from '../utils/batching';\n\nconst onValueChange = ((\n  state: State | State[],\n  cb: (values?: any[]) => void\n): (() => void) => {\n  if ('length' in state) {\n    let isAvailable = true;\n\n    const l = state.length;\n\n    const unlisteners: Array<() => void> = new Array(l);\n\n    if (cb.length) {\n      const values = state.map((state) => state.get());\n\n      for (let i = 0; i < l; i++) {\n        unlisteners[i] = state[i]._onValueChange((value) => {\n          values[i] = value;\n\n          if (isAvailable) {\n            isAvailable = false;\n\n            postBatchCallbacksPush(() => {\n              cb(values);\n\n              isAvailable = true;\n            });\n          }\n        });\n      }\n    } else {\n      const fn = () => {\n        if (isAvailable) {\n          isAvailable = false;\n\n          postBatchCallbacksPush(() => {\n            cb();\n\n            isAvailable = true;\n          });\n        }\n      };\n\n      for (let i = 0; i < l; i++) {\n        unlisteners[i] = state[i]._onValueChange(fn);\n      }\n    }\n\n    return () => {\n      cb = noop;\n\n      for (let i = 0; i < l; i++) {\n        unlisteners[i]();\n      }\n    };\n  }\n\n  return state._onValueChange(cb);\n}) as {\n  /**\n   * Registers a callback to be invoked when the value of a single {@link state} changes.\n   *\n   * @param state - The state to monitor for changes.\n   * @param cb - The callback function invoked with the new value of the state.\n   * @returns a function to unsubscribe from the value change event.\n   *\n   */\n  <T>(state: AsyncState<T>, cb: (value: T | undefined) => void): () => void;\n  /**\n   * Registers a callback to be invoked when the value of a single {@link state} changes.\n   *\n   * @param state - The state to monitor for changes.\n   * @param cb - The callback function invoked with the new value of the state.\n   * @returns a function to unsubscribe from the value change event.\n   *\n   */\n  <T>(state: State<T>, cb: (value: T) => void): () => void;\n  /**\n   * Registers a callback to be invoked when the values of multiple {@link states} change.\n   *\n   * @param states - The states to monitor for changes.\n   * @param cb - The callback function invoked with the new values of the states.\n   * @returns a function to unsubscribe from the values change event.\n   */\n  <const S extends State[]>(\n    states: S,\n    cb: (values: {\n      [index in keyof S]: S[index] extends State<infer K>\n        ? K | (S[index] extends AsyncState ? undefined : never)\n        : never;\n    }) => void\n  ): () => void;\n};\n\nexport default onValueChange;\n"]}