{"version":3,"file":"watcher.mjs","sources":["../../../../../src/query/watcher.ts"],"sourcesContent":["import { type Store, attach, createEvent, sample, scopeBind } from \"effector\"\n\nimport { type ApolloClient, Cache, type OperationVariables } from \"@apollo/client\"\nimport type { QueryManager } from \"@apollo/client/core/QueryManager\"\n\nimport { storify } from \"../lib/storify\"\nimport { setupSubscription } from \"../setup_subscription\"\n\nimport type { Query } from \"./query\"\n\ninterface WatchQueryOptions {\n  /**\n   * Your Apollo Client instance that'll be used for watching the cache.\n   * By default, the same client you use to make requests will be used\n   * to watch the cache.\n   */\n  client?: ApolloClient<unknown> | Store<ApolloClient<unknown>>\n\n  /**\n   * Controls when `watchQuery` will listen to updates in cache.\n   *\n   * By default, this is when the {@link Query} has succeeded.\n   * (i.e. run successfully at least once and not reset).\n   *\n   * ---\n   *\n   * Note: `enabled` being `true` is required but not sufficient for `Query`\n   * to subscribe to cache. The query needs to aquire variables\n   */\n  enabled?: Store<boolean>\n\n  /** Watch for optimistic updates? */\n  optimistic?: boolean\n}\n\n/**\n * Watch for Apollo Cache changes and update your `Query` to match.\n *\n * Subscribes your query to Apollo Cache to always keep `$data`\n * in sync with Apollo Cache. Usually, `Query` only updates its\n * `$data` upon request. `watchQuery` changes that so your `Query`\n * behaves more like Apollo Client's React hooks.\n */\nexport function watchQuery<Data, Variables extends OperationVariables = OperationVariables>(\n  query: Query<Data, Variables>,\n  {\n    client = query.meta.client,\n\n    enabled: $enabled = query.$succeeded,\n\n    optimistic = true,\n  }: WatchQueryOptions = {},\n): void {\n  type WatchOptions = Omit<Cache.WatchOptions<Data, Variables>, \"callback\">\n\n  const name = `${query.meta.name}.watch`\n  const options: WatchOptions = { query: query.meta.document, optimistic }\n\n  const $client = storify(client, { name: `${name}.client`, sid: `apollo.${name}.$client` })\n\n  const updated = createEvent<Cache.DiffResult<Data>>({ name: `${name}.updated` })\n\n  const subscribeFx = attach({\n    name: `${name}.subscribe`,\n    source: { variables: query.__.$variables, client: $client },\n    effect({ variables, client }) {\n      const callback = scopeBind(updated, { safe: true })\n\n      return client.cache.watch({ ...options, variables, callback })\n    },\n  })\n\n  const maskFx = attach({\n    name: `${name}.mask`,\n    source: { client: $client },\n    effect: ({ client }, data: Data) => {\n      // @ts-expect-error: private member\n      const manager = client.queryManager as QueryManager<unknown>\n\n      return manager.maskOperation({ data, document: query.meta.document, id: name })\n    },\n  })\n\n  /** Fires when we _can_ connect */\n  const ready = sample({ clock: $enabled, filter: (enabled) => enabled })\n  const teardown = sample({ clock: $enabled, filter: (enabled) => !enabled })\n\n  const setup = sample({\n    clock: [ready, query.finished.finally, query.__.$variables],\n    filter: $enabled,\n    fn: (): void => undefined,\n  })\n\n  /**\n   * When cache is updated, attempt to mask it\n   * using client settings.\n   */\n  sample({\n    clock: updated,\n    filter: ({ complete }) => Boolean(complete),\n    fn: ({ result }) => result!,\n    target: maskFx,\n  })\n\n  /**\n   * Once extraction & masking is complete,\n   * push data into the query.\n   */\n  sample({\n    clock: maskFx.doneData,\n    target: query.__.push,\n  })\n\n  setupSubscription({ subscribe: subscribeFx, setup, teardown, name })\n}\n"],"names":["client"],"mappings":";;;;AA2CO,SAAS,WACd,OACA;AAAA,EACE,SAAS,MAAM,KAAK;AAAA,EAEpB,SAAS,WAAW,MAAM;AAAA,EAE1B,aAAa;AACf,IAAuB,IACjB;AAGN,QAAM,OAAO,GAAG,MAAM,KAAK,IAAI;AAC/B,QAAM,UAAwB,EAAE,OAAO,MAAM,KAAK,UAAU,WAAW;AAEvE,QAAM,UAAU,QAAQ,QAAQ,EAAE,MAAM,GAAG,IAAI,WAAW,KAAK,UAAU,IAAI,WAAA,CAAY;AAEzF,QAAM,UAAU,YAAoC,EAAE,MAAM,GAAG,IAAI,YAAY;AAE/E,QAAM,cAAc,OAAO;AAAA,IACzB,MAAM,GAAG,IAAI;AAAA,IACb,QAAQ,EAAE,WAAW,MAAM,GAAG,YAAY,QAAQ,QAAQ;AAAA,IAC1D,OAAO,EAAE,WAAW,QAAAA,WAAU;AAC5B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