{"version":3,"file":"bufferLatest.cjs","names":["_createStore","require","bufferLatest","publish","subscribe","store","createStore","isReactAppReadyForProps","unsubscribe","setState","getState","hasState","publishWhenReady","value","exports"],"sources":["../../../src/tools/bufferLatest.ts"],"sourcesContent":["/*!\n * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.\n * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the \"License.\")\n *\n * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *\n * See the License for the specific language governing permissions and limitations under the License.\n */\n\nimport { createStore } from \"./createStore.js\";\n\n/**\n * Buffers the values passed to a publisher, keeping only the latest value, until the subscriber emits.\n *\n * This is useful in restricting publishers from firing events until specific subscribers are active. Once active, the `publish` function reverts to its previous behavior.\n */\nexport const bufferLatest = <Value>({\n  publish,\n  subscribe,\n}: {\n  /**\n   * A function that publishes values to a subscriber, but not the same subscriber as the one passed to this function.\n   *\n   * Only the latest value published by this publisher is kept until the subscriber emits.\n   */\n  publish: (value: Value) => void;\n  /**\n   * A subscription that listens for emissions from a separate publisher.\n   *\n   * When that separate publisher emits, the latest value from the passed-in publish function is emitted as are all future values.\n   */\n  subscribe: (subscriber: () => void) => () => void;\n}) => {\n  const store = createStore<{\n    bufferedValue?: Value;\n    isReactAppReadyForProps: boolean;\n  }>({\n    isReactAppReadyForProps: false,\n  });\n\n  const unsubscribe = subscribe(() => {\n    unsubscribe();\n\n    store.setState(\"isReactAppReadyForProps\", true);\n    store.getState(\"bufferedValue\");\n    if (store.hasState(\"bufferedValue\")) {\n      // If we have a state, then the value in here is what we want. TypeScript expects this to possibly be `undefined` because the type we sent to `createStore` listed `bufferedValue` as optional. TypeScript doesn't seem to have any way of knowing we passed in the value at some point.\n      publish(store.getState(\"bufferedValue\")!);\n    }\n  });\n\n  const publishWhenReady: typeof publish = (value) => {\n    if (store.getState(\"isReactAppReadyForProps\")) {\n      publish(value);\n    } else {\n      store.setState(\"bufferedValue\", value);\n    }\n  };\n\n  return publishWhenReady;\n};\n"],"mappings":";;;;;;AAYA,IAAAA,YAAA,GAAAC,OAAA;AAZA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GASO,MAAMC,YAAY,GAAGA,CAAQ;EAClCC,OAAO;EACPC;AAcF,CAAC,KAAK;EACJ,MAAMC,KAAK,GAAG,IAAAC,wBAAW,EAGtB;IACDC,uBAAuB,EAAE;EAC3B,CAAC,CAAC;EAEF,MAAMC,WAAW,GAAGJ,SAAS,CAAC,MAAM;IAClCI,WAAW,CAAC,CAAC;IAEbH,KAAK,CAACI,QAAQ,CAAC,yBAAyB,EAAE,IAAI,CAAC;IAC/CJ,KAAK,CAACK,QAAQ,CAAC,eAAe,CAAC;IAC/B,IAAIL,KAAK,CAACM,QAAQ,CAAC,eAAe,CAAC,EAAE;MAEnCR,OAAO,CAACE,KAAK,CAACK,QAAQ,CAAC,eAAe,CAAE,CAAC;IAC3C;EACF,CAAC,CAAC;EAEF,MAAME,gBAAgC,GAAIC,KAAK,IAAK;IAClD,IAAIR,KAAK,CAACK,QAAQ,CAAC,yBAAyB,CAAC,EAAE;MAC7CP,OAAO,CAACU,KAAK,CAAC;IAChB,CAAC,MAAM;MACLR,KAAK,CAACI,QAAQ,CAAC,eAAe,EAAEI,KAAK,CAAC;IACxC;EACF,CAAC;EAED,OAAOD,gBAAgB;AACzB,CAAC;AAACE,OAAA,CAAAZ,YAAA,GAAAA,YAAA","ignoreList":[]}