{"version":3,"file":"keep_fresh.mjs","sources":["../../../../src/keep_fresh.ts"],"sourcesContent":["import { type Event, type EventCallable, type Store, createStore, is, sample } from \"effector\"\n\nimport type { OperationVariables } from \"@apollo/client\"\n\nimport { divide } from \"./lib/divide\"\nimport { not } from \"./lib/not\"\nimport type { Optional } from \"./lib/optional\"\nimport { storify } from \"./lib/storify\"\nimport type { Query } from \"./query/query\"\n\nexport interface TriggerProtocol {\n  \"@@trigger\": () => {\n    setup: EventCallable<void>\n    teardown: EventCallable<void>\n    fired: Event<unknown> | Event<void>\n  }\n}\n\ntype Trigger = Event<any> | TriggerProtocol\n\ninterface KeepFreshOptions {\n  /**\n   * Controls whether the automatic refresh is enabled.\n   *\n   * By default, `keepFresh` will be always enabled.\n   */\n  enabled?: Store<boolean>\n\n  /**\n   * A list of triggers to start the query and launch a network request.\n   *\n   * Can either be a `Event` or a `TriggerProtocol`\n   * ({@link https://withease.pages.dev/protocols/trigger | see documentation}).\n   */\n  triggers: Trigger[]\n}\n\n/**\n * Enables automatic refreshes for your query,\n * ensuring that the data stays up-to-date\n * in response to specific events or triggers.\n *\n * @remarks\n *  - By default, `keepFresh` is always enabled. You can optionally control its enabled state using the `enabled` option.\n *  - The query will be refetched when any of the specified triggers fire.\n *\n * @param query - The Query you want to keep fresh.\n * @param options - Options for customizing the refresh behavior.\n */\nexport function keepFresh<Data, Variables extends OperationVariables = OperationVariables>(\n  query: Query<Data, Variables>,\n  { enabled, triggers }: KeepFreshOptions,\n) {\n  const name = `${query.meta.name}.fresh`\n\n  const $enabled = storify(enabled ?? true, {\n    name: `${name}.enabled`,\n    sid: `apollo.${name}.$enabled`,\n  })\n\n  // eslint-disable-next-line @typescript-eslint/unbound-method\n  const [events, protocolSources] = divide<Event<any>, TriggerProtocol>(triggers, is.event)\n\n  if (protocolSources.length > 0) {\n    const protocols = protocolSources.map((proto) => proto[\"@@trigger\"]())\n\n    const $setup = createStore(false, {\n      name: `${name}.setup`,\n      sid: `apollo.${name}.$setup`,\n      serialize: \"ignore\",\n      skipVoid: false,\n    })\n\n    sample({\n      clock: query.finished.success,\n      filter: not($setup),\n      fn: () => true,\n      target: [...protocols.map(({ setup }) => setup), $setup],\n    })\n\n    sample({\n      clock: $enabled.updates,\n      filter: not($enabled),\n      fn: () => false,\n      target: [...protocols.map(({ teardown }) => teardown), $setup],\n    })\n\n    events.push(...protocols.map(({ fired }) => fired))\n  }\n\n  const refresh = sample({ clock: events, filter: $enabled })\n\n  sample({\n    clock: refresh,\n    source: query.__.$variables as Store<Optional<Variables>>,\n    filter: not(query.$idle),\n    // query.refresh would read cache, but we want to force a request\n    target: query.start,\n  })\n}\n"],"names":[],"mappings":";;;;AAiDO,SAAS,UACd,OACA,EAAE,SAAS,YACX;AACA,QAAM,OAAO,GAAG,MAAM,KAAK,IAAI;AAEzB,QAAA,WAAW,QAAQ,WAAW,MAAM;AAAA,IACxC,MAAM,GAAG,IAAI;AAAA,IACb,KAAK,UAAU,IAAI;AAAA,EAAA,CACpB;AAGD,QAAM,CAAC,QAAQ,eAAe,IAAI,OAAoC,UAAU,GAAG,KAAK;AAEpF,MAAA,gBAAgB,SAAS,GAAG;AACxB,UAAA,YAAY,gBAAgB,IAAI,CAAC,UAAU,MAAM,WAAW,GAAG;AAE/D,UAAA,SAAS,YAAY,OAAO;AAAA,MAChC,MAAM,GAAG,IAAI;AAAA,MACb,KAAK,UAAU,IAAI;AAAA,MACnB,WAAW;AAAA,MACX,UAAU;AAAA,IAAA,CACX;AAEM,WAAA;AAAA,MACL,OAAO,MAAM,SAAS;AAAA,MACtB,QAAQ,IAAI,MAAM;AAAA,MAClB,IAAI,MAAM;AAAA,MACV,QAAQ,CAAC,GAAG,UAAU,IAAI,CAAC,EAAE,MAAM,MAAM,KAAK,GAAG,MAAM;AAAA,IAAA,CACxD;AAEM,WAAA;AAAA,MACL,OAAO,SAAS;AAAA,MAChB,QAAQ,IAAI,QAAQ;AAAA,MACpB,IAAI,MAAM;AAAA,MACV,QAAQ,CAAC,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