@tanstack/query-core 5.0.0-alpha.0 → 5.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/build/lib/index.d.ts +1 -1
  2. package/build/lib/index.esm.js +1 -1
  3. package/build/lib/index.js +1 -0
  4. package/build/lib/index.js.map +1 -1
  5. package/build/lib/index.mjs +1 -1
  6. package/build/lib/infiniteQueryObserver.d.ts +2 -2
  7. package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
  8. package/build/lib/infiniteQueryObserver.js.map +1 -1
  9. package/build/lib/infiniteQueryObserver.mjs.map +1 -1
  10. package/build/lib/mutation.d.ts +3 -3
  11. package/build/lib/mutation.esm.js +6 -0
  12. package/build/lib/mutation.esm.js.map +1 -1
  13. package/build/lib/mutation.js +6 -0
  14. package/build/lib/mutation.js.map +1 -1
  15. package/build/lib/mutation.mjs +6 -0
  16. package/build/lib/mutation.mjs.map +1 -1
  17. package/build/lib/mutationCache.d.ts +4 -3
  18. package/build/lib/mutationCache.esm.js.map +1 -1
  19. package/build/lib/mutationCache.js.map +1 -1
  20. package/build/lib/mutationCache.mjs.map +1 -1
  21. package/build/lib/mutationObserver.d.ts +2 -2
  22. package/build/lib/mutationObserver.esm.js +1 -3
  23. package/build/lib/mutationObserver.esm.js.map +1 -1
  24. package/build/lib/mutationObserver.js +1 -3
  25. package/build/lib/mutationObserver.js.map +1 -1
  26. package/build/lib/mutationObserver.mjs +1 -3
  27. package/build/lib/mutationObserver.mjs.map +1 -1
  28. package/build/lib/queriesObserver.esm.js +1 -5
  29. package/build/lib/queriesObserver.esm.js.map +1 -1
  30. package/build/lib/queriesObserver.js +1 -5
  31. package/build/lib/queriesObserver.js.map +1 -1
  32. package/build/lib/queriesObserver.mjs +1 -5
  33. package/build/lib/queriesObserver.mjs.map +1 -1
  34. package/build/lib/query.d.ts +4 -4
  35. package/build/lib/query.esm.js +2 -0
  36. package/build/lib/query.esm.js.map +1 -1
  37. package/build/lib/query.js +2 -0
  38. package/build/lib/query.js.map +1 -1
  39. package/build/lib/query.mjs +2 -0
  40. package/build/lib/query.mjs.map +1 -1
  41. package/build/lib/queryCache.d.ts +4 -3
  42. package/build/lib/queryCache.esm.js.map +1 -1
  43. package/build/lib/queryCache.js.map +1 -1
  44. package/build/lib/queryCache.mjs.map +1 -1
  45. package/build/lib/queryClient.d.ts +7 -7
  46. package/build/lib/queryClient.esm.js.map +1 -1
  47. package/build/lib/queryClient.js.map +1 -1
  48. package/build/lib/queryClient.mjs.map +1 -1
  49. package/build/lib/queryObserver.d.ts +2 -2
  50. package/build/lib/queryObserver.esm.js.map +1 -1
  51. package/build/lib/queryObserver.js.map +1 -1
  52. package/build/lib/queryObserver.mjs.map +1 -1
  53. package/build/lib/retryer.d.ts +5 -5
  54. package/build/lib/retryer.esm.js.map +1 -1
  55. package/build/lib/retryer.js.map +1 -1
  56. package/build/lib/retryer.mjs.map +1 -1
  57. package/build/lib/subscribable.esm.js.map +1 -1
  58. package/build/lib/subscribable.js.map +1 -1
  59. package/build/lib/subscribable.mjs.map +1 -1
  60. package/build/lib/types.d.ts +38 -38
  61. package/build/umd/index.development.js +11 -8
  62. package/build/umd/index.development.js.map +1 -1
  63. package/build/umd/index.production.js +1 -1
  64. package/build/umd/index.production.js.map +1 -1
  65. package/package.json +1 -1
  66. package/src/index.ts +7 -1
  67. package/src/infiniteQueryObserver.ts +2 -2
  68. package/src/mutation.ts +22 -4
  69. package/src/mutationCache.ts +10 -3
  70. package/src/mutationObserver.ts +3 -5
  71. package/src/queriesObserver.ts +3 -7
  72. package/src/query.ts +15 -5
  73. package/src/queryCache.ts +8 -3
  74. package/src/queryClient.ts +7 -7
  75. package/src/queryObserver.ts +4 -4
  76. package/src/retryer.ts +5 -5
  77. package/src/subscribable.ts +1 -1
  78. package/src/tests/mutationCache.test.tsx +54 -10
  79. package/src/tests/queryCache.test.tsx +18 -6
  80. package/src/types.ts +44 -36
@@ -1 +1 @@
1
- {"version":3,"file":"mutationCache.js","sources":["../../src/mutationCache.ts"],"sourcesContent":["import type { MutationObserver } from './mutationObserver'\nimport type { NotifyEvent, MutationOptions, RegisteredError } from './types'\nimport type { QueryClient } from './queryClient'\nimport { notifyManager } from './notifyManager'\nimport type { Action, MutationState } from './mutation'\nimport { Mutation } from './mutation'\nimport type { MutationFilters } from './utils'\nimport { matchMutation, noop } from './utils'\nimport { Subscribable } from './subscribable'\n\n// TYPES\n\ninterface MutationCacheConfig {\n onError?: (\n error: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onSuccess?: (\n data: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onMutate?: (\n variables: unknown,\n mutation: Mutation<unknown, unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n}\n\ninterface NotifyEventMutationAdded extends NotifyEvent {\n type: 'added'\n mutation: Mutation<any, any, any, any>\n}\ninterface NotifyEventMutationRemoved extends NotifyEvent {\n type: 'removed'\n mutation: Mutation<any, any, any, any>\n}\n\ninterface NotifyEventMutationObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n mutation?: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any, any>\n}\n\ninterface NotifyEventMutationUpdated extends NotifyEvent {\n type: 'updated'\n mutation: Mutation<any, any, any, any>\n action: Action<any, any, any, any>\n}\n\ntype MutationCacheNotifyEvent =\n | NotifyEventMutationAdded\n | NotifyEventMutationRemoved\n | NotifyEventMutationObserverAdded\n | NotifyEventMutationObserverRemoved\n | NotifyEventMutationObserverOptionsUpdated\n | NotifyEventMutationUpdated\n\ntype MutationCacheListener = (event: MutationCacheNotifyEvent) => void\n\n// CLASS\n\nexport class MutationCache extends Subscribable<MutationCacheListener> {\n #mutations: Mutation<any, any, any, any>[]\n #mutationId: number\n #resuming: Promise<unknown> | undefined\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = []\n this.#mutationId = 0\n }\n\n build<TData, TError, TVariables, TContext>(\n client: QueryClient,\n options: MutationOptions<TData, TError, TVariables, TContext>,\n state?: MutationState<TData, TError, TVariables, TContext>,\n ): Mutation<TData, TError, TVariables, TContext> {\n const mutation = new Mutation({\n mutationCache: this,\n mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n this.#mutations.push(mutation)\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n this.#mutations = this.#mutations.filter((x) => x !== mutation)\n this.notify({ type: 'removed', mutation })\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.#mutations.forEach((mutation) => {\n this.remove(mutation)\n })\n })\n }\n\n getAll(): Mutation[] {\n return this.#mutations\n }\n\n find<\n TData = unknown,\n TError = RegisteredError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n if (typeof filters.exact === 'undefined') {\n filters.exact = true\n }\n\n return this.#mutations.find((mutation) => matchMutation(filters, mutation))\n }\n\n findAll(filters: MutationFilters = {}): Mutation[] {\n return this.#mutations.filter((mutation) =>\n matchMutation(filters, mutation),\n )\n }\n\n notify(event: MutationCacheNotifyEvent) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n resumePausedMutations(): Promise<unknown> {\n this.#resuming = (this.#resuming ?? Promise.resolve())\n .then(() => {\n const pausedMutations = this.#mutations.filter((x) => x.state.isPaused)\n return notifyManager.batch(() =>\n pausedMutations.reduce(\n (promise, mutation) =>\n promise.then(() => mutation.continue().catch(noop)),\n Promise.resolve() as Promise<unknown>,\n ),\n )\n })\n .then(() => {\n this.#resuming = undefined\n })\n\n return this.#resuming\n }\n}\n"],"names":["MutationCache","Subscribable","constructor","config","build","client","options","state","mutation","Mutation","mutationCache","mutationId","defaultMutationOptions","add","push","notify","type","remove","filter","x","clear","notifyManager","batch","forEach","getAll","find","filters","exact","matchMutation","findAll","event","listeners","listener","resumePausedMutations","Promise","resolve","then","pausedMutations","isPaused","reduce","promise","continue","catch","noop","undefined"],"mappings":";;;;;;;AAUA;;AAgEA;;AAEO,MAAMA,aAAa,SAASC,yBAAY,CAAwB;AACrE,EAAA,UAAU,CAAA;AACV,EAAA,WAAW,CAAA;AACX,EAAA,SAAS,CAAA;AAETC,EAAAA,WAAW,CAAQC,MAA2B,GAAG,EAAE,EAAE;AACnD,IAAA,KAAK,EAAE,CAAA;IAAA,IADUA,CAAAA,MAA2B,GAA3BA,MAA2B,CAAA;AAE5C,IAAA,IAAI,CAAC,UAAU,GAAG,EAAE,CAAA;AACpB,IAAA,IAAI,CAAC,WAAW,GAAG,CAAC,CAAA;AACtB,GAAA;AAEAC,EAAAA,KAAK,CACHC,MAAmB,EACnBC,OAA6D,EAC7DC,KAA0D,EACX;AAC/C,IAAA,MAAMC,UAAQ,GAAG,IAAIC,iBAAQ,CAAC;AAC5BC,MAAAA,aAAa,EAAE,IAAI;AACnBC,MAAAA,UAAU,EAAE,EAAE,IAAI,CAAC,WAAW;AAC9BL,MAAAA,OAAO,EAAED,MAAM,CAACO,sBAAsB,CAACN,OAAO,CAAC;AAC/CC,MAAAA,KAAAA;AACF,KAAC,CAAC,CAAA;AAEF,IAAA,IAAI,CAACM,GAAG,CAACL,UAAQ,CAAC,CAAA;AAElB,IAAA,OAAOA,UAAQ,CAAA;AACjB,GAAA;EAEAK,GAAG,CAACL,QAAsC,EAAQ;AAChD,IAAA,IA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ACH,GAAA;EAEAO,MAAM,CAACe,KAA+B,EAAE;IACtCT,2BAAa,CAACC,KAAK,CAAC,MAAM;AACxB,MAAA,IAAI,CAACS,SAAS,CAACR,OAAO,CAAES,QAAQ,IAAK;QACnCA,QAAQ,CAACF,KAAK,CAAC,CAAA;AACjB,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AAEAG,EAAAA,qBAAqB,GAAqB;AACxC,IAAA,IAAI,CAAC,SAAS,GAAG,CAAC,IAAI,CAAC,SAAS,IAAIC,OAAO,CAACC,OAAO,EAAE,EAClDC,IAAI,CAAC,MAAM;AACV,MAAA,MAAMC,eAAe,GAAG,IAAI,CAAC,UAAU,CAACnB,MAAM,CAAEC,CAAC,IAAKA,CAAC,CAACZ,KAAK,CAAC+B,QAAQ,CAAC,CAAA;AACvE,MAAA,OAAOjB,2BAAa,CAACC,KAAK,CAAC,MACzBe,eAAe,CAACE,MAAM,CACpB,CAACC,OAAO,EAAEhC,QAAQ,KAChBgC,OAAO,CAACJ,IAAI,CAAC,MAAM5B,QAAQ,CAACiC,QAAQ,EAAE,CAACC,KAAK,CAACC,UAAI,CAAC,CAAC,EACrDT,OAAO,CAACC,OAAO,EAAE,CAClB,CACF,CAAA;AACH,KAAC,CAAC,CACDC,IAAI,CAAC,MAAM;AACV,MAAA,IAAI,CAAC,SAAS,GAAGQ,SAAS,CAAA;AAC5B,KAAC,CAAC,CAAA;IAEJ,OAAO,IAAI,CAAC,SAAS,CAAA;AACvB,GAAA;AACF;;;;"}
1
+ {"version":3,"file":"mutationCache.js","sources":["../../src/mutationCache.ts"],"sourcesContent":["import type { MutationObserver } from './mutationObserver'\nimport type { NotifyEvent, MutationOptions, DefaultError } from './types'\nimport type { QueryClient } from './queryClient'\nimport { notifyManager } from './notifyManager'\nimport type { Action, MutationState } from './mutation'\nimport { Mutation } from './mutation'\nimport type { MutationFilters } from './utils'\nimport { matchMutation, noop } from './utils'\nimport { Subscribable } from './subscribable'\n\n// TYPES\n\ninterface MutationCacheConfig {\n onError?: (\n error: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onSuccess?: (\n data: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onMutate?: (\n variables: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onSettled?: (\n data: unknown | undefined,\n error: unknown | null,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n}\n\ninterface NotifyEventMutationAdded extends NotifyEvent {\n type: 'added'\n mutation: Mutation<any, any, any, any>\n}\ninterface NotifyEventMutationRemoved extends NotifyEvent {\n type: 'removed'\n mutation: Mutation<any, any, any, any>\n}\n\ninterface NotifyEventMutationObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n mutation?: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any, any>\n}\n\ninterface NotifyEventMutationUpdated extends NotifyEvent {\n type: 'updated'\n mutation: Mutation<any, any, any, any>\n action: Action<any, any, any, any>\n}\n\ntype MutationCacheNotifyEvent =\n | NotifyEventMutationAdded\n | NotifyEventMutationRemoved\n | NotifyEventMutationObserverAdded\n | NotifyEventMutationObserverRemoved\n | NotifyEventMutationObserverOptionsUpdated\n | NotifyEventMutationUpdated\n\ntype MutationCacheListener = (event: MutationCacheNotifyEvent) => void\n\n// CLASS\n\nexport class MutationCache extends Subscribable<MutationCacheListener> {\n #mutations: Mutation<any, any, any, any>[]\n #mutationId: number\n #resuming: Promise<unknown> | undefined\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = []\n this.#mutationId = 0\n }\n\n build<TData, TError, TVariables, TContext>(\n client: QueryClient,\n options: MutationOptions<TData, TError, TVariables, TContext>,\n state?: MutationState<TData, TError, TVariables, TContext>,\n ): Mutation<TData, TError, TVariables, TContext> {\n const mutation = new Mutation({\n mutationCache: this,\n mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n this.#mutations.push(mutation)\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n this.#mutations = this.#mutations.filter((x) => x !== mutation)\n this.notify({ type: 'removed', mutation })\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.#mutations.forEach((mutation) => {\n this.remove(mutation)\n })\n })\n }\n\n getAll(): Mutation[] {\n return this.#mutations\n }\n\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n if (typeof filters.exact === 'undefined') {\n filters.exact = true\n }\n\n return this.#mutations.find((mutation) => matchMutation(filters, mutation))\n }\n\n findAll(filters: MutationFilters = {}): Mutation[] {\n return this.#mutations.filter((mutation) =>\n matchMutation(filters, mutation),\n )\n }\n\n notify(event: MutationCacheNotifyEvent) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n resumePausedMutations(): Promise<unknown> {\n this.#resuming = (this.#resuming ?? Promise.resolve())\n .then(() => {\n const pausedMutations = this.#mutations.filter((x) => x.state.isPaused)\n return notifyManager.batch(() =>\n pausedMutations.reduce(\n (promise, mutation) =>\n promise.then(() => mutation.continue().catch(noop)),\n Promise.resolve() as Promise<unknown>,\n ),\n )\n })\n .then(() => {\n this.#resuming = undefined\n })\n\n return this.#resuming\n }\n}\n"],"names":["MutationCache","Subscribable","constructor","config","build","client","options","state","mutation","Mutation","mutationCache","mutationId","defaultMutationOptions","add","push","notify","type","remove","filter","x","clear","notifyManager","batch","forEach","getAll","find","filters","exact","matchMutation","findAll","event","listeners","listener","resumePausedMutations","Promise","resolve","then","pausedMutations","isPaused","reduce","promise","continue","catch","noop","undefined"],"mappings":";;;;;;;AAUA;;AAuEA;;AAEO,MAAMA,aAAa,SAASC,yBAAY,CAAwB;AACrE,EAAA,UAAU,CAAA;AACV,EAAA,WAAW,CAAA;AACX,EAAA,SAAS,CAAA;AAETC,EAAAA,WAAW,CAAQC,MAA2B,GAAG,EAAE,EAAE;AACnD,IAAA,KAAK,EAAE,CAAA;IAAA,IADUA,CAAAA,MAA2B,GAA3BA,MAA2B,CAAA;AAE5C,IAAA,IAAI,CAAC,UAAU,GAAG,EAAE,CAAA;AACpB,IAAA,IAAI,CAAC,WAAW,GAAG,CAAC,CAAA;AACtB,GAAA;AAEAC,EAAAA,KAAK,CACHC,MAAmB,EACnBC,OAA6D,EAC7DC,KAA0D,EACX;AAC/C,IAAA,MAAMC,UAAQ,GAAG,IAAIC,iBAAQ,CAAC;AAC5BC,MAAAA,aAAa,EAAE,IAAI;AACnBC,MAAAA,UAAU,EAAE,EAAE,IAAI,CAAC,WAAW;AAC9BL,MAAAA,OAAO,EAAED,MAAM,CAACO,sBAAsB,CAACN,OAAO,CAAC;AAC/CC,MAAAA,KAAAA;AACF,KAAC,CAAC,CAAA;AAEF,IAAA,IAAI,CAACM,GAAG,CAACL,UAAQ,CAAC,CAAA;AAElB,IAAA,OAAOA,UAAQ,CAAA;AACjB,GAAA;EAEAK,GAAG,CAACL,QAAsC,EAAQ;AAChD,IAAA,IAAI,CAAC,UAAU,CAACM,IAAI,CAACN,QAAQ,CAAC,CAAA;IAC9B,IAAI,CAACO,MAAM,CAAC;AAAEC,MAAAA,IAAI,EAAE,OAAO;AAAER,MAAAA,QAAAA;AAAS,KAAC,CAAC,CAAA;AAC1C,GAAA;EAEAS,MAA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,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AAEAG,EAAAA,qBAAqB,GAAqB;AACxC,IAAA,IAAI,CAAC,SAAS,GAAG,CAAC,IAAI,CAAC,SAAS,IAAIC,OAAO,CAACC,OAAO,EAAE,EAClDC,IAAI,CAAC,MAAM;AACV,MAAA,MAAMC,eAAe,GAAG,IAAI,CAAC,UAAU,CAACnB,MAAM,CAAEC,CAAC,IAAKA,CAAC,CAACZ,KAAK,CAAC+B,QAAQ,CAAC,CAAA;AACvE,MAAA,OAAOjB,2BAAa,CAACC,KAAK,CAAC,MACzBe,eAAe,CAACE,MAAM,CACpB,CAACC,OAAO,EAAEhC,QAAQ,KAChBgC,OAAO,CAACJ,IAAI,CAAC,MAAM5B,QAAQ,CAACiC,QAAQ,EAAE,CAACC,KAAK,CAACC,UAAI,CAAC,CAAC,EACrDT,OAAO,CAACC,OAAO,EAAE,CAClB,CACF,CAAA;AACH,KAAC,CAAC,CACDC,IAAI,CAAC,MAAM;AACV,MAAA,IAAI,CAAC,SAAS,GAAGQ,SAAS,CAAA;AAC5B,KAAC,CAAC,CAAA;IAEJ,OAAO,IAAI,CAAC,SAAS,CAAA;AACvB,GAAA;AACF;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"mutationCache.mjs","sources":["../../src/mutationCache.ts"],"sourcesContent":["import type { MutationObserver } from './mutationObserver'\nimport type { NotifyEvent, MutationOptions, RegisteredError } from './types'\nimport type { QueryClient } from './queryClient'\nimport { notifyManager } from './notifyManager'\nimport type { Action, MutationState } from './mutation'\nimport { Mutation } from './mutation'\nimport type { MutationFilters } from './utils'\nimport { matchMutation, noop } from './utils'\nimport { Subscribable } from './subscribable'\n\n// TYPES\n\ninterface MutationCacheConfig {\n onError?: (\n error: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onSuccess?: (\n data: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onMutate?: (\n variables: unknown,\n mutation: Mutation<unknown, unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n}\n\ninterface NotifyEventMutationAdded extends NotifyEvent {\n type: 'added'\n mutation: Mutation<any, any, any, any>\n}\ninterface NotifyEventMutationRemoved extends NotifyEvent {\n type: 'removed'\n mutation: Mutation<any, any, any, any>\n}\n\ninterface NotifyEventMutationObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n mutation?: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any, any>\n}\n\ninterface NotifyEventMutationUpdated extends NotifyEvent {\n type: 'updated'\n mutation: Mutation<any, any, any, any>\n action: Action<any, any, any, any>\n}\n\ntype MutationCacheNotifyEvent =\n | NotifyEventMutationAdded\n | NotifyEventMutationRemoved\n | NotifyEventMutationObserverAdded\n | NotifyEventMutationObserverRemoved\n | NotifyEventMutationObserverOptionsUpdated\n | NotifyEventMutationUpdated\n\ntype MutationCacheListener = (event: MutationCacheNotifyEvent) => void\n\n// CLASS\n\nexport class MutationCache extends Subscribable<MutationCacheListener> {\n #mutations: Mutation<any, any, any, any>[]\n #mutationId: number\n #resuming: Promise<unknown> | undefined\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = []\n this.#mutationId = 0\n }\n\n build<TData, TError, TVariables, TContext>(\n client: QueryClient,\n options: MutationOptions<TData, TError, TVariables, TContext>,\n state?: MutationState<TData, TError, TVariables, TContext>,\n ): Mutation<TData, TError, TVariables, TContext> {\n const mutation = new Mutation({\n mutationCache: this,\n mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n this.#mutations.push(mutation)\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n this.#mutations = this.#mutations.filter((x) => x !== mutation)\n this.notify({ type: 'removed', mutation })\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.#mutations.forEach((mutation) => {\n this.remove(mutation)\n })\n })\n }\n\n getAll(): Mutation[] {\n return this.#mutations\n }\n\n find<\n TData = unknown,\n TError = RegisteredError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n if (typeof filters.exact === 'undefined') {\n filters.exact = true\n }\n\n return this.#mutations.find((mutation) => matchMutation(filters, mutation))\n }\n\n findAll(filters: MutationFilters = {}): Mutation[] {\n return this.#mutations.filter((mutation) =>\n matchMutation(filters, mutation),\n )\n }\n\n notify(event: MutationCacheNotifyEvent) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n resumePausedMutations(): Promise<unknown> {\n this.#resuming = (this.#resuming ?? Promise.resolve())\n .then(() => {\n const pausedMutations = this.#mutations.filter((x) => x.state.isPaused)\n return notifyManager.batch(() =>\n pausedMutations.reduce(\n (promise, mutation) =>\n promise.then(() => mutation.continue().catch(noop)),\n Promise.resolve() as Promise<unknown>,\n ),\n )\n })\n .then(() => {\n this.#resuming = undefined\n })\n\n return this.#resuming\n }\n}\n"],"names":["MutationCache","Subscribable","constructor","config","build","client","options","state","mutation","Mutation","mutationCache","mutationId","defaultMutationOptions","add","push","notify","type","remove","filter","x","clear","notifyManager","batch","forEach","getAll","find","filters","exact","matchMutation","findAll","event","listeners","listener","resumePausedMutations","Promise","resolve","then","pausedMutations","isPaused","reduce","promise","continue","catch","noop","undefined"],"mappings":";;;;;AAUA;;AAgEA;;AAEO,MAAMA,aAAa,SAASC,YAAY,CAAwB;AACr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AKA,CAAC,CAACZ,KAAK,CAAC+B,QAAQ,CAAC,CAAA;AACvE,MAAA,OAAOjB,aAAa,CAACC,KAAK,CAAC,MACzBe,eAAe,CAACE,MAAM,CACpB,CAACC,OAAO,EAAEhC,QAAQ,KAChBgC,OAAO,CAACJ,IAAI,CAAC,MAAM5B,QAAQ,CAACiC,QAAQ,EAAE,CAACC,KAAK,CAACC,IAAI,CAAC,CAAC,EACrDT,OAAO,CAACC,OAAO,EAAE,CAClB,CACF,CAAA;AACH,KAAC,CAAC,CACDC,IAAI,CAAC,MAAM;AACV,MAAA,IAAI,CAAC,SAAS,GAAGQ,SAAS,CAAA;AAC5B,KAAC,CAAC,CAAA;IAEJ,OAAO,IAAI,CAAC,SAAS,CAAA;AACvB,GAAA;AACF;;;;"}
1
+ {"version":3,"file":"mutationCache.mjs","sources":["../../src/mutationCache.ts"],"sourcesContent":["import type { MutationObserver } from './mutationObserver'\nimport type { NotifyEvent, MutationOptions, DefaultError } from './types'\nimport type { QueryClient } from './queryClient'\nimport { notifyManager } from './notifyManager'\nimport type { Action, MutationState } from './mutation'\nimport { Mutation } from './mutation'\nimport type { MutationFilters } from './utils'\nimport { matchMutation, noop } from './utils'\nimport { Subscribable } from './subscribable'\n\n// TYPES\n\ninterface MutationCacheConfig {\n onError?: (\n error: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onSuccess?: (\n data: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onMutate?: (\n variables: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onSettled?: (\n data: unknown | undefined,\n error: unknown | null,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n}\n\ninterface NotifyEventMutationAdded extends NotifyEvent {\n type: 'added'\n mutation: Mutation<any, any, any, any>\n}\ninterface NotifyEventMutationRemoved extends NotifyEvent {\n type: 'removed'\n mutation: Mutation<any, any, any, any>\n}\n\ninterface NotifyEventMutationObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n mutation?: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any, any>\n}\n\ninterface NotifyEventMutationUpdated extends NotifyEvent {\n type: 'updated'\n mutation: Mutation<any, any, any, any>\n action: Action<any, any, any, any>\n}\n\ntype MutationCacheNotifyEvent =\n | NotifyEventMutationAdded\n | NotifyEventMutationRemoved\n | NotifyEventMutationObserverAdded\n | NotifyEventMutationObserverRemoved\n | NotifyEventMutationObserverOptionsUpdated\n | NotifyEventMutationUpdated\n\ntype MutationCacheListener = (event: MutationCacheNotifyEvent) => void\n\n// CLASS\n\nexport class MutationCache extends Subscribable<MutationCacheListener> {\n #mutations: Mutation<any, any, any, any>[]\n #mutationId: number\n #resuming: Promise<unknown> | undefined\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = []\n this.#mutationId = 0\n }\n\n build<TData, TError, TVariables, TContext>(\n client: QueryClient,\n options: MutationOptions<TData, TError, TVariables, TContext>,\n state?: MutationState<TData, TError, TVariables, TContext>,\n ): Mutation<TData, TError, TVariables, TContext> {\n const mutation = new Mutation({\n mutationCache: this,\n mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n this.#mutations.push(mutation)\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n this.#mutations = this.#mutations.filter((x) => x !== mutation)\n this.notify({ type: 'removed', mutation })\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.#mutations.forEach((mutation) => {\n this.remove(mutation)\n })\n })\n }\n\n getAll(): Mutation[] {\n return this.#mutations\n }\n\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n if (typeof filters.exact === 'undefined') {\n filters.exact = true\n }\n\n return this.#mutations.find((mutation) => matchMutation(filters, mutation))\n }\n\n findAll(filters: MutationFilters = {}): Mutation[] {\n return this.#mutations.filter((mutation) =>\n matchMutation(filters, mutation),\n )\n }\n\n notify(event: MutationCacheNotifyEvent) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n resumePausedMutations(): Promise<unknown> {\n this.#resuming = (this.#resuming ?? Promise.resolve())\n .then(() => {\n const pausedMutations = this.#mutations.filter((x) => x.state.isPaused)\n return notifyManager.batch(() =>\n pausedMutations.reduce(\n (promise, mutation) =>\n promise.then(() => mutation.continue().catch(noop)),\n Promise.resolve() as Promise<unknown>,\n ),\n )\n })\n .then(() => {\n this.#resuming = undefined\n })\n\n return this.#resuming\n }\n}\n"],"names":["MutationCache","Subscribable","constructor","config","build","client","options","state","mutation","Mutation","mutationCache","mutationId","defaultMutationOptions","add","push","notify","type","remove","filter","x","clear","notifyManager","batch","forEach","getAll","find","filters","exact","matchMutation","findAll","event","listeners","listener","resumePausedMutations","Promise","resolve","then","pausedMutations","isPaused","reduce","promise","continue","catch","noop","undefined"],"mappings":";;;;;AAUA;;AAuEA;;AAEO,MAAMA,aAAa,SAASC,YAAY,CAAwB;AACrE,EAAA,UAAU,CAAA;AACV,EAAA,WAAW,CAAA;AACX,EAAA,SAAS,CAAA;AAETC,EAAAA,WAAW,CAAQC,MAA2B,GAAG,EAAE,EAAE;AACnD,IAAA,KAAK,EAAE,CAAA;IAAA,IADUA,CAAAA,MAA2B,GAA3BA,MAA2B,CAA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5B,QAAQ,CAACiC,QAAQ,EAAE,CAACC,KAAK,CAACC,IAAI,CAAC,CAAC,EACrDT,OAAO,CAACC,OAAO,EAAE,CAClB,CACF,CAAA;AACH,KAAC,CAAC,CACDC,IAAI,CAAC,MAAM;AACV,MAAA,IAAI,CAAC,SAAS,GAAGQ,SAAS,CAAA;AAC5B,KAAC,CAAC,CAAA;IAEJ,OAAO,IAAI,CAAC,SAAS,CAAA;AACvB,GAAA;AACF;;;;"}
@@ -1,9 +1,9 @@
1
1
  import type { Action } from './mutation';
2
2
  import type { QueryClient } from './queryClient';
3
3
  import { Subscribable } from './subscribable';
4
- import type { MutateOptions, MutationObserverResult, MutationObserverOptions, RegisteredError } from './types';
4
+ import type { MutateOptions, MutationObserverResult, MutationObserverOptions, DefaultError } from './types';
5
5
  declare type MutationObserverListener<TData, TError, TVariables, TContext> = (result: MutationObserverResult<TData, TError, TVariables, TContext>) => void;
6
- export declare class MutationObserver<TData = unknown, TError = RegisteredError, TVariables = void, TContext = unknown> extends Subscribable<MutationObserverListener<TData, TError, TVariables, TContext>> {
6
+ export declare class MutationObserver<TData = unknown, TError = DefaultError, TVariables = void, TContext = unknown> extends Subscribable<MutationObserverListener<TData, TError, TVariables, TContext>> {
7
7
  #private;
8
8
  options: MutationObserverOptions<TData, TError, TVariables, TContext>;
9
9
  constructor(client: QueryClient, options: MutationObserverOptions<TData, TError, TVariables, TContext>);
@@ -54,9 +54,7 @@ class MutationObserver extends Subscribable {
54
54
  mutate(variables, options) {
55
55
  this.#mutateOptions = options;
56
56
  this.#currentMutation?.removeObserver(this);
57
- this.#currentMutation = this.#client.getMutationCache().build(this.#client, {
58
- ...this.options
59
- });
57
+ this.#currentMutation = this.#client.getMutationCache().build(this.#client, this.options);
60
58
  this.#currentMutation.addObserver(this);
61
59
  return this.#currentMutation.execute(variables);
62
60
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mutationObserver.esm.js","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import type { Action, Mutation } from './mutation'\nimport { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverResult,\n MutationObserverOptions,\n RegisteredError,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = RegisteredError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\n }\n\n reset(): void {\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, {\n ...this.options,\n })\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TContext>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","undefined","constructor","client","options","setOptions","bindMethods","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","getCurrentResult","variables","build","addObserver","execute","state","getDefaultState","isPending","status","isSuccess","isError","isIdle","notifyManager","batch","hasListeners","onSuccess","data","context","onSettled","onError","error","forEach","listener"],"mappings":";;;;;AAaA;;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Q,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;AAC/B,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
1
+ {"version":3,"file":"mutationObserver.esm.js","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import type { Action, Mutation } from './mutation'\nimport { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverResult,\n MutationObserverOptions,\n DefaultError,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\n }\n\n reset(): void {\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TContext>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","undefined","constructor","client","options","setOptions","bindMethods","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","getCurrentResult","variables","build","addObserver","execute","state","getDefaultState","isPending","status","isSuccess","isError","isIdle","notifyManager","batch","hasListeners","onSuccess","data","context","onSettled","onError","error","forEach","listener"],"mappings":";;;;;AAaA;;AAMA;;AAEO,MAAMA,gBAAgB,SAKnBC,YAAY,CAEpB;AAGA,EAAA,OAAO,CAAA;EACP,cAAc,GACZC,SAAS,CAAA;AACX,EAAA,gBAAgB,CAAA;AAChB,EAAA,cAAc,CAAA;AAEdC,EAAAA,WAAW,CACTC,MAAmB,EACnBC,OAAqE,EACrE;AACA,IAAA,KAAK,EAAE,CAAA;AAEP,IAAA,IAAI,CAAC,OAAO,GAAGD,MAAM,CAAA;AACrB,IAAA,IAAI,CAACE,UAAU,CAACD,OAAO,CAAC,CAAA;IACxB,IAAI,CAACE,WAAW,EAAE,CAAA;IAClB,IAAI,CAAC,aAAa,EAAE,CAAA;AACtB,GAAA;AAEUA,EAAAA,WAAW,GAAS;IAC5B,IAAI,CAACC,MAAM,GAAG,IAAI,CAACA,MAAM,CAACC,IAAI,CAAC,IAAI,CAAC,CAAA;IACpC,IAAI,CAACC,KAAK,GAAG,IAAI,CAACA,KAAK,CAACD,IAAI,CAAC,IAAI,CAAC,CAAA;AACpC,GAAA;EAEAH,UAAU,CACRD,OAAsE,EACtE;AACA,IAAA,MAAMM,WAAW,GAAG,IAAI,CAACN,OAAO,CAAA;IAChC,IAAI,CAACA,OAAO,GAAG,IAAI,CAAC,OAAO,CAACO,sBAAsB,CAACP,OAAO,CAAC,CAAA;IAC3D,IAAI,CAACQ,mBAAmB,CAACF,WAAW,EAAE,IAAI,CAACN,OAAO,CAAC,EAAE;MACnD,IAAI,CAAC,OAAO,CAACS,gBAAgB,EAAE,CAACC,MAAM,CAAC;AACrCC,QAAAA,IAAI,EAAE,wBAAwB;AAC9BC,QAAAA,QAAQ,EAAE,IAAI,CAAC,gBAAgB;AAC/BC,QAAAA,QAAQ,EA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Ac,CAACgB,OAAO,CAC5B,CAAA;AACH,SAAA;AACF,OAAA;;AAEA;AACA,MAAA,IAAI,CAACtB,SAAS,CAAC0B,OAAO,CAAEC,QAAQ,IAAK;AACnCA,QAAAA,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;AAC/B,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
@@ -56,9 +56,7 @@ class MutationObserver extends subscribable.Subscribable {
56
56
  mutate(variables, options) {
57
57
  this.#mutateOptions = options;
58
58
  this.#currentMutation?.removeObserver(this);
59
- this.#currentMutation = this.#client.getMutationCache().build(this.#client, {
60
- ...this.options
61
- });
59
+ this.#currentMutation = this.#client.getMutationCache().build(this.#client, this.options);
62
60
  this.#currentMutation.addObserver(this);
63
61
  return this.#currentMutation.execute(variables);
64
62
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mutationObserver.js","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import type { Action, Mutation } from './mutation'\nimport { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverResult,\n MutationObserverOptions,\n RegisteredError,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = RegisteredError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\n }\n\n reset(): void {\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, {\n ...this.options,\n })\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TContext>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","undefined","constructor","client","options","setOptions","bindMethods","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","getCurrentResult","variables","build","addObserver","execute","state","getDefaultState","isPending","status","isSuccess","isError","isIdle","notifyManager","batch","hasListeners","onSuccess","data","context","onSettled","onError","error","forEach","listener"],"mappings":";;;;;;;AAaA;;AAMA;;AAEO,MAAMA,gBAAgB,SAKnBC,yBAAY,CAEpB;AAGA,EAAA,OAAO,CAAA;EACP,cAAc,GACZC,SAAS,CAAA;AACX,EAAA,gBAAgB,CAAA;AAChB,EAAA,cAAc,CAAA;AAEdC,EAAAA,WAAW,CACTC,MAAmB,EACnBC,OAAqE,EACrE;AACA,IAAA,KAAK,EAAE,CAAA;AAEP,IAAA,IAAI,CAAC,OAAO,GAAGD,MAAM,CAAA;AACrB,IAAA,IAAI,CAACE,UAAU,CAACD,OAAO,CAAC,CAAA;IACxB,IAAI,CAACE,WAAW,EAAE,CAAA;IAClB,IAAI,CAAC,aAAa,EAAE,CAAA;AACtB,GAAA;AAEUA,EAAAA,WAAW,GAAS;IAC5B,IAAI,CAACC,MAAM,GAAG,IAAI,CAACA,MAAM,CAACC,IAAI,CAAC,IAAI,CAAC,CAAA;IACpC,IAAI,CAACC,KAAK,GAAG,IAAI,CAACA,KAAK,CAACD,IAAI,CAAC,IAAI,CA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S,GAC3BnB,MAAM,CAACiB,IAAI,EACX,IAAI,EACJ,IAAI,CAAC,cAAc,CAACf,SAAS,EAC7B,IAAI,CAAC,cAAc,CAACgB,OAAO,CAC5B,CAAA;AACH,SAAC,MAAM,IAAIlB,MAAM,EAAER,IAAI,KAAK,OAAO,EAAE;UACnC,IAAI,CAAC,cAAc,CAAC4B,OAAO,GACzBpB,MAAM,CAACqB,KAAK,EACZ,IAAI,CAAC,cAAc,CAACnB,SAAS,EAC7B,IAAI,CAAC,cAAc,CAACgB,OAAO,CAC5B,CAAA;UACD,IAAI,CAAC,cAAc,CAACC,SAAS,GAC3BzC,SAAS,EACTsB,MAAM,CAACqB,KAAK,EACZ,IAAI,CAAC,cAAc,CAACnB,SAAS,EAC7B,IAAI,CAAC,cAAc,CAACgB,OAAO,CAC5B,CAAA;AACH,SAAA;AACF,OAAA;;AAEA;AACA,MAAA,IAAI,CAACtB,SAAS,CAAC0B,OAAO,CAAEC,QAAQ,IAAK;AACnCA,QAAAA,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;AAC/B,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
1
+ {"version":3,"file":"mutationObserver.js","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import type { Action, Mutation } from './mutation'\nimport { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverResult,\n MutationObserverOptions,\n DefaultError,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\n }\n\n reset(): void {\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TContext>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","undefined","constructor","client","options","setOptions","bindMethods","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","getCurrentResult","variables","build","addObserver","execute","state","getDefaultState","isPending","status","isSuccess","isError","isIdle","notifyManager","batch","hasListeners","onSuccess","data","context","onSettled","onError","error","forEach","listener"],"mappings":";;;;;;;AAaA;;AAMA;;AAEO,MAAMA,gBAAgB,SAKnBC,yBAAY,CAEpB;AAGA,EAAA,OAAO,CAAA;EACP,cAAc,GACZC,SAAS,CAAA;AACX,EAAA,gBAAgB,CAAA;AAChB,EAAA,cAAc,CAAA;AAEdC,EAAAA,WAAW,CACTC,MAAmB,EACnBC,OAAqE,EACrE;AACA,IAAA,KAAK,EAAE,CAAA;AAEP,IAAA,IAAI,CAAC,OAAO,GAAGD,MAAM,CAAA;AACrB,IAAA,IAAI,CAACE,UAAU,CAACD,OAAO,CAAC,CAAA;IACxB,IAAI,CAACE,WAAW,EAAE,CAAA;IAClB,IAAI,CAAC,aAAa,EAAE,CAAA;AACtB,GAAA;AAEUA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3BhB,MAAM,CAACiB,IAAI,EACX,IAAI,CAAC,cAAc,CAACf,SAAS,EAC7B,IAAI,CAAC,cAAc,CAACgB,OAAO,CAC5B,CAAA;UACD,IAAI,CAAC,cAAc,CAACC,SAAS,GAC3BnB,MAAM,CAACiB,IAAI,EACX,IAAI,EACJ,IAAI,CAAC,cAAc,CAACf,SAAS,EAC7B,IAAI,CAAC,cAAc,CAACgB,OAAO,CAC5B,CAAA;AACH,SAAC,MAAM,IAAIlB,MAAM,EAAER,IAAI,KAAK,OAAO,EAAE;UACnC,IAAI,CAAC,cAAc,CAAC4B,OAAO,GACzBpB,MAAM,CAACqB,KAAK,EACZ,IAAI,CAAC,cAAc,CAACnB,SAAS,EAC7B,IAAI,CAAC,cAAc,CAACgB,OAAO,CAC5B,CAAA;UACD,IAAI,CAAC,cAAc,CAACC,SAAS,GAC3BzC,SAAS,EACTsB,MAAM,CAACqB,KAAK,EACZ,IAAI,CAAC,cAAc,CAACnB,SAAS,EAC7B,IAAI,CAAC,cAAc,CAACgB,OAAO,CAC5B,CAAA;AACH,SAAA;AACF,OAAA;;AAEA;AACA,MAAA,IAAI,CAACtB,SAAS,CAAC0B,OAAO,CAAEC,QAAQ,IAAK;AACnCA,QAAAA,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;AAC/B,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
@@ -54,9 +54,7 @@ class MutationObserver extends Subscribable {
54
54
  mutate(variables, options) {
55
55
  this.#mutateOptions = options;
56
56
  this.#currentMutation?.removeObserver(this);
57
- this.#currentMutation = this.#client.getMutationCache().build(this.#client, {
58
- ...this.options
59
- });
57
+ this.#currentMutation = this.#client.getMutationCache().build(this.#client, this.options);
60
58
  this.#currentMutation.addObserver(this);
61
59
  return this.#currentMutation.execute(variables);
62
60
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mutationObserver.mjs","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import type { Action, Mutation } from './mutation'\nimport { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverResult,\n MutationObserverOptions,\n RegisteredError,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = RegisteredError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\n }\n\n reset(): void {\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, {\n ...this.options,\n })\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TContext>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","undefined","constructor","client","options","setOptions","bindMethods","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","getCurrentResult","variables","build","addObserver","execute","state","getDefaultState","isPending","status","isSuccess","isError","isIdle","notifyManager","batch","hasListeners","onSuccess","data","context","onSettled","onError","error","forEach","listener"],"mappings":";;;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AAAA,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;AAC/B,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
1
+ {"version":3,"file":"mutationObserver.mjs","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import type { Action, Mutation } from './mutation'\nimport { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverResult,\n MutationObserverOptions,\n DefaultError,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\n }\n\n reset(): void {\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TContext>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","undefined","constructor","client","options","setOptions","bindMethods","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","getCurrentResult","variables","build","addObserver","execute","state","getDefaultState","isPending","status","isSuccess","isError","isIdle","notifyManager","batch","hasListeners","onSuccess","data","context","onSettled","onError","error","forEach","listener"],"mappings":";;;;;AAaA;;AAMA;;AAEO,MAAMA,gBAAgB,SAKnBC,YAAY,CAEpB;AAGA,EAAA,OAAO,CAAA;EACP,cAAc,GACZC,SAAS,CAAA;AACX,EAAA,gBAAgB,CAAA;AAChB,EAAA,cAAc,CAAA;AAEdC,EAAAA,WAAW,CACTC,MAAmB,EACnBC,OAAqE,EACrE;AACA,IAAA,KAAK,EAAE,CAAA;AAEP,IAAA,IAAI,CAAC,OAAO,GAAGD,MAAM,CAAA;AACrB,IAAA,IAAI,CAACE,UAAU,CAACD,OAAO,CAAC,CAAA;IACxB,IAAI,CAACE,WAAW,EAAE,CAAA;IAClB,IAAI,CAAC,aAAa,EAAE,CAAA;AACtB,GAAA;AAEUA,EAAAA,WAAW,GAAS;IAC5B,IAAI,CAACC,MAAM,GAAG,IAAI,CAACA,MAAM,CAACC,IAAI,CAAC,IAAI,CAAC,CAAA;IACpC,IAAI,CAACC,KAAK,GAAG,IAAI,CAACA,KAAK,CAACD,IAAI,CAAC,IAAI,CAAC,CAAA;AACpC,GAAA;EAEAH,UAAU,CACRD,OAAsE,EACtE;AACA,IAAA,MAAMM,WAAW,GAAG,IAAI,CAACN,OAAO,CAAA;IAChC,IAAI,CAACA,OAAO,GAAG,IAAI,CAAC,OAAO,CAACO,sBAAsB,CAACP,OAAO,CAAC,CAAA;IAC3D,IAAI,CAACQ,mBAAmB,CAACF,WAAW,EAAE,IAAI,CAACN,OAAO,CAAC,EAAE;MACnD,IAAI,CAAC,OAAO,CAACS,gBAAgB,EAAE,CAACC,MAAM,CAAC;AACrCC,QAAAA,IAAI,EAAE,wBAAwB;AAC9BC,QAAAA,QAAQ,EAAE,IAAI,CAAC,gBAAgB;AAC/BC,QAAAA,Q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CAAC,cAAc,CAACgB,OAAO,CAC5B,CAAA;AACH,SAAA;AACF,OAAA;;AAEA;AACA,MAAA,IAAI,CAACtB,SAAS,CAAC0B,OAAO,CAAEC,QAAQ,IAAK;AACnCA,QAAAA,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;AAC/B,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
@@ -15,14 +15,12 @@ class QueriesObserver extends Subscribable {
15
15
  #result;
16
16
  #queries;
17
17
  #observers;
18
- #observersMap;
19
18
  constructor(client, queries) {
20
19
  super();
21
20
  this.#client = client;
22
21
  this.#queries = [];
23
22
  this.#result = [];
24
23
  this.#observers = [];
25
- this.#observersMap = {};
26
24
  if (queries) {
27
25
  this.setQueries(queries);
28
26
  }
@@ -56,14 +54,12 @@ class QueriesObserver extends Subscribable {
56
54
  // set options for the new observers to notify of changes
57
55
  newObserverMatches.forEach(match => match.observer.setOptions(match.defaultedQueryOptions, notifyOptions));
58
56
  const newObservers = newObserverMatches.map(match => match.observer);
59
- const newObserversMap = Object.fromEntries(newObservers.map(observer => [observer.options.queryHash, observer]));
60
57
  const newResult = newObservers.map(observer => observer.getCurrentResult());
61
58
  const hasIndexChange = newObservers.some((observer, index) => observer !== prevObservers[index]);
62
59
  if (prevObservers.length === newObservers.length && !hasIndexChange) {
63
60
  return;
64
61
  }
65
62
  this.#observers = newObservers;
66
- this.#observersMap = newObserversMap;
67
63
  this.#result = newResult;
68
64
  if (!this.hasListeners()) {
69
65
  return;
@@ -108,7 +104,7 @@ class QueriesObserver extends Subscribable {
108
104
  const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.includes(defaultedOptions.queryHash));
109
105
  const getObserver = options => {
110
106
  const defaultedOptions = this.#client.defaultQueryOptions(options);
111
- const currentObserver = this.#observersMap[defaultedOptions.queryHash];
107
+ const currentObserver = this.#observers.find(o => o.options.queryHash === defaultedOptions.queryHash);
112
108
  return currentObserver ?? new QueryObserver(this.#client, defaultedOptions);
113
109
  };
114
110
  const newOrReusedObservers = unmatchedQueries.map(options => {
@@ -1 +1 @@
1
- {"version":3,"file":"queriesObserver.esm.js","sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport type {\n QueryObserverOptions,\n QueryObserverResult,\n DefaultedQueryObserverOptions,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { NotifyOptions } from './queryObserver'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\n\nfunction difference<T>(array1: T[], array2: T[]): T[] {\n return array1.filter((x) => array2.indexOf(x) === -1)\n}\n\nfunction replaceAt<T>(array: T[], index: number, value: T): T[] {\n const copy = array.slice(0)\n copy[index] = value\n return copy\n}\n\ntype QueriesObserverListener = (result: QueryObserverResult[]) => void\n\nexport class QueriesObserver extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result: QueryObserverResult[]\n #queries: QueryObserverOptions[]\n #observers: QueryObserver[]\n #observersMap: Record<string, QueryObserver>\n\n constructor(client: QueryClient, queries?: QueryObserverOptions[]) {\n super()\n\n this.#client = client\n this.#queries = []\n this.#result = []\n this.#observers = []\n this.#observersMap = {}\n\n if (queries) {\n this.setQueries(queries)\n }\n }\n\n protected onSubscribe(): void {\n if (this.listeners.length === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.destroy()\n }\n }\n\n destroy(): void {\n this.listeners = []\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n setQueries(\n queries: QueryObserverOptions[],\n notifyOptions?: NotifyOptions,\n ): void {\n this.#queries = queries\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions, notifyOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newObserversMap = Object.fromEntries(\n newObservers.map((observer) => [observer.options.queryHash, observer]),\n )\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n if (prevObservers.length === newObservers.length && !hasIndexChange) {\n return\n }\n\n this.#observers = newObservers\n this.#observersMap = newObserversMap\n this.#result = newResult\n\n if (!this.hasListeners()) {\n return\n }\n\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n\n this.#notify()\n })\n }\n\n getCurrentResult(): QueryObserverResult[] {\n return this.#result\n }\n\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n getObservers() {\n return this.#observers\n }\n\n getOptimisticResult(queries: QueryObserverOptions[]): QueryObserverResult[] {\n return this.#findMatchingObservers(queries).map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n }\n\n #findMatchingObservers(\n queries: QueryObserverOptions[],\n ): QueryObserverMatch[] {\n const prevObservers = this.#observers\n const defaultedQueryOptions = queries.map((options) =>\n this.#client.defaultQueryOptions(options),\n )\n\n const matchingObservers: QueryObserverMatch[] =\n defaultedQueryOptions.flatMap((defaultedOptions) => {\n const match = prevObservers.find(\n (observer) =>\n observer.options.queryHash === defaultedOptions.queryHash,\n )\n if (match != null) {\n return [{ defaultedQueryOptions: defaultedOptions, observer: match }]\n }\n return []\n })\n\n const matchedQueryHashes = matchingObservers.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n const unmatchedQueries = defaultedQueryOptions.filter(\n (defaultedOptions) =>\n !matchedQueryHashes.includes(defaultedOptions.queryHash),\n )\n\n const getObserver = (options: QueryObserverOptions): QueryObserver => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const currentObserver = this.#observersMap[defaultedOptions.queryHash!]\n return (\n currentObserver ?? new QueryObserver(this.#client, defaultedOptions)\n )\n }\n\n const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map(\n (options) => {\n return {\n defaultedQueryOptions: options,\n observer: getObserver(options),\n }\n },\n )\n\n const sortMatchesByOrderOfQueries = (\n a: QueryObserverMatch,\n b: QueryObserverMatch,\n ): number =>\n defaultedQueryOptions.indexOf(a.defaultedQueryOptions) -\n defaultedQueryOptions.indexOf(b.defaultedQueryOptions)\n\n return matchingObservers\n .concat(newOrReusedObservers)\n .sort(sortMatchesByOrderOfQueries)\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = replaceAt(this.#result, index, result)\n this.#notify()\n }\n }\n\n #notify(): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"names":["difference","array1","array2","filter","x","indexOf","replaceAt","array","index","value","copy","slice","QueriesObserver","Subscribable","constructor","client","queries","setQueries","onSubscribe","listeners","length","forEach","observer","subscribe","result","onUnsubscribe","destroy","notifyOptions","notifyManager","batch","prevObservers","newObserverMatches","match","setOptions","defaultedQueryOptions","newObservers","map","newObserversMap","Object","fromEntries","options","queryHash","newResult","getCurrentResult","hasIndexChange","some","hasListeners","getQueries","getCurrentQuery","getObservers","getOptimisticResult","defaultQueryOptions","matchingObservers","flatMap","defaultedOptions","find","matchedQueryHashes","unmatchedQueries","includes","getObserver","currentObserver","QueryObserver","newOrReusedObservers","sortMatchesByOrderOfQueries","a","b","concat","sort","listener"],"mappings":";;;;AAWA,SAASA,UAAU,CAAIC,MAAW,EAAEC,MAAW,EAAO;AACpD,EAAA,OAAOD,MAAM,CAACE,MAAM,CAAEC,CAAC,IAAKF,MAAM,CAACG,OAAO,CAACD,CAAC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A,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AACxB,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
1
+ {"version":3,"file":"queriesObserver.esm.js","sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport type {\n QueryObserverOptions,\n QueryObserverResult,\n DefaultedQueryObserverOptions,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { NotifyOptions } from './queryObserver'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\n\nfunction difference<T>(array1: T[], array2: T[]): T[] {\n return array1.filter((x) => array2.indexOf(x) === -1)\n}\n\nfunction replaceAt<T>(array: T[], index: number, value: T): T[] {\n const copy = array.slice(0)\n copy[index] = value\n return copy\n}\n\ntype QueriesObserverListener = (result: QueryObserverResult[]) => void\n\nexport class QueriesObserver extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result: QueryObserverResult[]\n #queries: QueryObserverOptions[]\n #observers: QueryObserver[]\n\n constructor(client: QueryClient, queries?: QueryObserverOptions[]) {\n super()\n\n this.#client = client\n this.#queries = []\n this.#result = []\n this.#observers = []\n\n if (queries) {\n this.setQueries(queries)\n }\n }\n\n protected onSubscribe(): void {\n if (this.listeners.length === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.destroy()\n }\n }\n\n destroy(): void {\n this.listeners = []\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n setQueries(\n queries: QueryObserverOptions[],\n notifyOptions?: NotifyOptions,\n ): void {\n this.#queries = queries\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions, notifyOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n if (prevObservers.length === newObservers.length && !hasIndexChange) {\n return\n }\n\n this.#observers = newObservers\n this.#result = newResult\n\n if (!this.hasListeners()) {\n return\n }\n\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n\n this.#notify()\n })\n }\n\n getCurrentResult(): QueryObserverResult[] {\n return this.#result\n }\n\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n getObservers() {\n return this.#observers\n }\n\n getOptimisticResult(queries: QueryObserverOptions[]): QueryObserverResult[] {\n return this.#findMatchingObservers(queries).map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n }\n\n #findMatchingObservers(\n queries: QueryObserverOptions[],\n ): QueryObserverMatch[] {\n const prevObservers = this.#observers\n const defaultedQueryOptions = queries.map((options) =>\n this.#client.defaultQueryOptions(options),\n )\n\n const matchingObservers: QueryObserverMatch[] =\n defaultedQueryOptions.flatMap((defaultedOptions) => {\n const match = prevObservers.find(\n (observer) =>\n observer.options.queryHash === defaultedOptions.queryHash,\n )\n if (match != null) {\n return [{ defaultedQueryOptions: defaultedOptions, observer: match }]\n }\n return []\n })\n\n const matchedQueryHashes = matchingObservers.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n const unmatchedQueries = defaultedQueryOptions.filter(\n (defaultedOptions) =>\n !matchedQueryHashes.includes(defaultedOptions.queryHash),\n )\n\n const getObserver = (options: QueryObserverOptions): QueryObserver => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const currentObserver = this.#observers.find(\n (o) => o.options.queryHash === defaultedOptions.queryHash,\n )\n return (\n currentObserver ?? new QueryObserver(this.#client, defaultedOptions)\n )\n }\n\n const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map(\n (options) => {\n return {\n defaultedQueryOptions: options,\n observer: getObserver(options),\n }\n },\n )\n\n const sortMatchesByOrderOfQueries = (\n a: QueryObserverMatch,\n b: QueryObserverMatch,\n ): number =>\n defaultedQueryOptions.indexOf(a.defaultedQueryOptions) -\n defaultedQueryOptions.indexOf(b.defaultedQueryOptions)\n\n return matchingObservers\n .concat(newOrReusedObservers)\n .sort(sortMatchesByOrderOfQueries)\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = replaceAt(this.#result, index, result)\n this.#notify()\n }\n }\n\n #notify(): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"names":["difference","array1","array2","filter","x","indexOf","replaceAt","array","index","value","copy","slice","QueriesObserver","Subscribable","constructor","client","queries","setQueries","onSubscribe","listeners","length","forEach","observer","subscribe","result","onUnsubscribe","destroy","notifyOptions","notifyManager","batch","prevObservers","newObserverMatches","match","setOptions","defaultedQueryOptions","newObservers","map","newResult","getCurrentResult","hasIndexChange","some","hasListeners","getQueries","getCurrentQuery","getObservers","getOptimisticResult","options","defaultQueryOptions","matchingObservers","flatMap","defaultedOptions","find","queryHash","matchedQueryHashes","unmatchedQueries","includes","getObserver","currentObserver","o","QueryObserver","newOrReusedObservers","sortMatchesByOrderOfQueries","a","b","concat","sort","listener"],"mappings":";;;;AAWA,SAASA,UAAU,CAAIC,MAAW,EAAEC,MAAW,EAAO;AACpD,EAAA,OAAOD,MAAM,CAACE,MAAM,CAAEC,CAAC,IAAKF,MAAM,CAACG,OAAO,CAACD,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AACvD,CAAA;AAEA,SAASE,SAAS,CAAIC,KAAU,EAAEC,K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,qBAAqB,EAAEY,OAAO;QAC9BxB,QAAQ,EAAEkC,WAAW,CAACV,OAAO,CAAA;OAC9B,CAAA;AACH,KAAC,CACF,CAAA;IAED,MAAMe,2BAA2B,GAAG,CAClCC,CAAqB,EACrBC,CAAqB,KAErB7B,qBAAqB,CAAC7B,OAAO,CAACyD,CAAC,CAAC5B,qBAAqB,CAAC,GACtDA,qBAAqB,CAAC7B,OAAO,CAAC0D,CAAC,CAAC7B,qBAAqB,CAAC,CAAA;IAExD,OAAOc,iBAAiB,CACrBgB,MAAM,CAACJ,oBAAoB,CAAC,CAC5BK,IAAI,CAACJ,2BAA2B,CAAC,CAAA;AACtC,GAAA;AAEA,EAAA,SAAS,CAACvC,QAAuB,EAAEE,MAA2B,EAAQ;IACpE,MAAMhB,KAAK,GAAG,IAAI,CAAC,UAAU,CAACH,OAAO,CAACiB,QAAQ,CAAC,CAAA;AAC/C,IAAA,IAAId,KAAK,KAAK,CAAC,CAAC,EAAE;AAChB,MAAA,IAAI,CAAC,OAAO,GAAGF,SAAS,CAAC,IAAI,CAAC,OAAO,EAAEE,KAAK,EAAEgB,MAAM,CAAC,CAAA;MACrD,IAAI,CAAC,OAAO,EAAE,CAAA;AAChB,KAAA;AACF,GAAA;AAEA,EAAA,OAAO,GAAS;IACdI,aAAa,CAACC,KAAK,CAAC,MAAM;AACxB,MAAA,IAAI,CAACV,SAAS,CAACE,OAAO,CAAE6C,QAAQ,IAAK;AACnCA,QAAAA,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AACxB,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
@@ -17,14 +17,12 @@ class QueriesObserver extends subscribable.Subscribable {
17
17
  #result;
18
18
  #queries;
19
19
  #observers;
20
- #observersMap;
21
20
  constructor(client, queries) {
22
21
  super();
23
22
  this.#client = client;
24
23
  this.#queries = [];
25
24
  this.#result = [];
26
25
  this.#observers = [];
27
- this.#observersMap = {};
28
26
  if (queries) {
29
27
  this.setQueries(queries);
30
28
  }
@@ -58,14 +56,12 @@ class QueriesObserver extends subscribable.Subscribable {
58
56
  // set options for the new observers to notify of changes
59
57
  newObserverMatches.forEach(match => match.observer.setOptions(match.defaultedQueryOptions, notifyOptions));
60
58
  const newObservers = newObserverMatches.map(match => match.observer);
61
- const newObserversMap = Object.fromEntries(newObservers.map(observer => [observer.options.queryHash, observer]));
62
59
  const newResult = newObservers.map(observer => observer.getCurrentResult());
63
60
  const hasIndexChange = newObservers.some((observer, index) => observer !== prevObservers[index]);
64
61
  if (prevObservers.length === newObservers.length && !hasIndexChange) {
65
62
  return;
66
63
  }
67
64
  this.#observers = newObservers;
68
- this.#observersMap = newObserversMap;
69
65
  this.#result = newResult;
70
66
  if (!this.hasListeners()) {
71
67
  return;
@@ -110,7 +106,7 @@ class QueriesObserver extends subscribable.Subscribable {
110
106
  const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.includes(defaultedOptions.queryHash));
111
107
  const getObserver = options => {
112
108
  const defaultedOptions = this.#client.defaultQueryOptions(options);
113
- const currentObserver = this.#observersMap[defaultedOptions.queryHash];
109
+ const currentObserver = this.#observers.find(o => o.options.queryHash === defaultedOptions.queryHash);
114
110
  return currentObserver ?? new queryObserver.QueryObserver(this.#client, defaultedOptions);
115
111
  };
116
112
  const newOrReusedObservers = unmatchedQueries.map(options => {
@@ -1 +1 @@
1
- {"version":3,"file":"queriesObserver.js","sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport type {\n QueryObserverOptions,\n QueryObserverResult,\n DefaultedQueryObserverOptions,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { NotifyOptions } from './queryObserver'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\n\nfunction difference<T>(array1: T[], array2: T[]): T[] {\n return array1.filter((x) => array2.indexOf(x) === -1)\n}\n\nfunction replaceAt<T>(array: T[], index: number, value: T): T[] {\n const copy = array.slice(0)\n copy[index] = value\n return copy\n}\n\ntype QueriesObserverListener = (result: QueryObserverResult[]) => void\n\nexport class QueriesObserver extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result: QueryObserverResult[]\n #queries: QueryObserverOptions[]\n #observers: QueryObserver[]\n #observersMap: Record<string, QueryObserver>\n\n constructor(client: QueryClient, queries?: QueryObserverOptions[]) {\n super()\n\n this.#client = client\n this.#queries = []\n this.#result = []\n this.#observers = []\n this.#observersMap = {}\n\n if (queries) {\n this.setQueries(queries)\n }\n }\n\n protected onSubscribe(): void {\n if (this.listeners.length === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.destroy()\n }\n }\n\n destroy(): void {\n this.listeners = []\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n setQueries(\n queries: QueryObserverOptions[],\n notifyOptions?: NotifyOptions,\n ): void {\n this.#queries = queries\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions, notifyOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newObserversMap = Object.fromEntries(\n newObservers.map((observer) => [observer.options.queryHash, observer]),\n )\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n if (prevObservers.length === newObservers.length && !hasIndexChange) {\n return\n }\n\n this.#observers = newObservers\n this.#observersMap = newObserversMap\n this.#result = newResult\n\n if (!this.hasListeners()) {\n return\n }\n\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n\n this.#notify()\n })\n }\n\n getCurrentResult(): QueryObserverResult[] {\n return this.#result\n }\n\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n getObservers() {\n return this.#observers\n }\n\n getOptimisticResult(queries: QueryObserverOptions[]): QueryObserverResult[] {\n return this.#findMatchingObservers(queries).map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n }\n\n #findMatchingObservers(\n queries: QueryObserverOptions[],\n ): QueryObserverMatch[] {\n const prevObservers = this.#observers\n const defaultedQueryOptions = queries.map((options) =>\n this.#client.defaultQueryOptions(options),\n )\n\n const matchingObservers: QueryObserverMatch[] =\n defaultedQueryOptions.flatMap((defaultedOptions) => {\n const match = prevObservers.find(\n (observer) =>\n observer.options.queryHash === defaultedOptions.queryHash,\n )\n if (match != null) {\n return [{ defaultedQueryOptions: defaultedOptions, observer: match }]\n }\n return []\n })\n\n const matchedQueryHashes = matchingObservers.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n const unmatchedQueries = defaultedQueryOptions.filter(\n (defaultedOptions) =>\n !matchedQueryHashes.includes(defaultedOptions.queryHash),\n )\n\n const getObserver = (options: QueryObserverOptions): QueryObserver => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const currentObserver = this.#observersMap[defaultedOptions.queryHash!]\n return (\n currentObserver ?? new QueryObserver(this.#client, defaultedOptions)\n )\n }\n\n const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map(\n (options) => {\n return {\n defaultedQueryOptions: options,\n observer: getObserver(options),\n }\n },\n )\n\n const sortMatchesByOrderOfQueries = (\n a: QueryObserverMatch,\n b: QueryObserverMatch,\n ): number =>\n defaultedQueryOptions.indexOf(a.defaultedQueryOptions) -\n defaultedQueryOptions.indexOf(b.defaultedQueryOptions)\n\n return matchingObservers\n .concat(newOrReusedObservers)\n .sort(sortMatchesByOrderOfQueries)\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = replaceAt(this.#result, index, result)\n this.#notify()\n }\n }\n\n #notify(): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"names":["difference","array1","array2","filter","x","indexOf","replaceAt","array","index","value","copy","slice","QueriesObserver","Subscribable","constructor","client","queries","setQueries","onSubscribe","listeners","length","forEach","observer","subscribe","result","onUnsubscribe","destroy","notifyOptions","notifyManager","batch","prevObservers","newObserverMatches","match","setOptions","defaultedQueryOptions","newObservers","map","newObserversMap","Object","fromEntries","options","queryHash","newResult","getCurrentResult","hasIndexChange","some","hasListeners","getQueries","getCurrentQuery","getObservers","getOptimisticResult","defaultQueryOptions","matchingObservers","flatMap","defaultedOptions","find","matchedQueryHashes","unmatchedQueries","includes","getObserver","currentObserver","QueryObserver","newOrReusedObservers","sortMatchesByOrderOfQueries","a","b","concat","sort","listener"],"mappings":";;;;;;AAWA,SAASA,UAAU,CAAIC,MAAW,EAAEC,MAAW,EAAO;AACpD,EAAA,OAAOD,MAAM,CAACE,MAAM,CAAEC,CAAC,IAAKF,MAAM,CAACG,OAAO,CAACD,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AACvD,CAAA;AAEA,SAASE,SAAS,CAAIC,KAAU,EAAEC,KAAa,EAAEC,KAAQ,EAAO;AAC9D,EAAA,MAAMC,IAAI,GAAGH,KAAK,CAACI,KAAK,CAAC,CAAC,CAAC,CAAA;AAC3BD,EAAAA,IAAI,CAACF,KAAK,CAAC,GAAGC,KAAK,CAAA;AACnB,EAAA,OAAOC,IAAI,CAAA;AACb,CAAA;AAIO,MAAME,eAAe,SAASC,yBAAY,CAA0B;AACzE,EAAA,OAAO,CAAA;AACP,EAAA,OAAO,CAAA;AACP,EAAA,QAAQ,CAAA;AACR,EAAA,UAAU,CAAA;AACV,EAAA,aAAa,CAAA;AAE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2BAA2B,GAAG,CAClCC,CAAqB,EACrBC,CAAqB,KAErB/B,qBAAqB,CAAC7B,OAAO,CAAC2D,CAAC,CAAC9B,qBAAqB,CAAC,GACtDA,qBAAqB,CAAC7B,OAAO,CAAC4D,CAAC,CAAC/B,qBAAqB,CAAC,CAAA;IAExD,OAAOkB,iBAAiB,CACrBc,MAAM,CAACJ,oBAAoB,CAAC,CAC5BK,IAAI,CAACJ,2BAA2B,CAAC,CAAA;AACtC,GAAA;AAEA,EAAA,SAAS,CAACzC,QAAuB,EAAEE,MAA2B,EAAQ;IACpE,MAAMhB,KAAK,GAAG,IAAI,CAAC,UAAU,CAACH,OAAO,CAACiB,QAAQ,CAAC,CAAA;AAC/C,IAAA,IAAId,KAAK,KAAK,CAAC,CAAC,EAAE;AAChB,MAAA,IAAI,CAAC,OAAO,GAAGF,SAAS,CAAC,IAAI,CAAC,OAAO,EAAEE,KAAK,EAAEgB,MAAM,CAAC,CAAA;MACrD,IAAI,CAAC,OAAO,EAAE,CAAA;AAChB,KAAA;AACF,GAAA;AAEA,EAAA,OAAO,GAAS;IACdI,2BAAa,CAACC,KAAK,CAAC,MAAM;AACxB,MAAA,IAAI,CAACV,SAAS,CAACE,OAAO,CAAE+C,QAAQ,IAAK;AACnCA,QAAAA,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AACxB,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
1
+ {"version":3,"file":"queriesObserver.js","sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport type {\n QueryObserverOptions,\n QueryObserverResult,\n DefaultedQueryObserverOptions,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { NotifyOptions } from './queryObserver'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\n\nfunction difference<T>(array1: T[], array2: T[]): T[] {\n return array1.filter((x) => array2.indexOf(x) === -1)\n}\n\nfunction replaceAt<T>(array: T[], index: number, value: T): T[] {\n const copy = array.slice(0)\n copy[index] = value\n return copy\n}\n\ntype QueriesObserverListener = (result: QueryObserverResult[]) => void\n\nexport class QueriesObserver extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result: QueryObserverResult[]\n #queries: QueryObserverOptions[]\n #observers: QueryObserver[]\n\n constructor(client: QueryClient, queries?: QueryObserverOptions[]) {\n super()\n\n this.#client = client\n this.#queries = []\n this.#result = []\n this.#observers = []\n\n if (queries) {\n this.setQueries(queries)\n }\n }\n\n protected onSubscribe(): void {\n if (this.listeners.length === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.destroy()\n }\n }\n\n destroy(): void {\n this.listeners = []\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n setQueries(\n queries: QueryObserverOptions[],\n notifyOptions?: NotifyOptions,\n ): void {\n this.#queries = queries\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions, notifyOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n if (prevObservers.length === newObservers.length && !hasIndexChange) {\n return\n }\n\n this.#observers = newObservers\n this.#result = newResult\n\n if (!this.hasListeners()) {\n return\n }\n\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n\n this.#notify()\n })\n }\n\n getCurrentResult(): QueryObserverResult[] {\n return this.#result\n }\n\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n getObservers() {\n return this.#observers\n }\n\n getOptimisticResult(queries: QueryObserverOptions[]): QueryObserverResult[] {\n return this.#findMatchingObservers(queries).map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n }\n\n #findMatchingObservers(\n queries: QueryObserverOptions[],\n ): QueryObserverMatch[] {\n const prevObservers = this.#observers\n const defaultedQueryOptions = queries.map((options) =>\n this.#client.defaultQueryOptions(options),\n )\n\n const matchingObservers: QueryObserverMatch[] =\n defaultedQueryOptions.flatMap((defaultedOptions) => {\n const match = prevObservers.find(\n (observer) =>\n observer.options.queryHash === defaultedOptions.queryHash,\n )\n if (match != null) {\n return [{ defaultedQueryOptions: defaultedOptions, observer: match }]\n }\n return []\n })\n\n const matchedQueryHashes = matchingObservers.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n const unmatchedQueries = defaultedQueryOptions.filter(\n (defaultedOptions) =>\n !matchedQueryHashes.includes(defaultedOptions.queryHash),\n )\n\n const getObserver = (options: QueryObserverOptions): QueryObserver => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const currentObserver = this.#observers.find(\n (o) => o.options.queryHash === defaultedOptions.queryHash,\n )\n return (\n currentObserver ?? new QueryObserver(this.#client, defaultedOptions)\n )\n }\n\n const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map(\n (options) => {\n return {\n defaultedQueryOptions: options,\n observer: getObserver(options),\n }\n },\n )\n\n const sortMatchesByOrderOfQueries = (\n a: QueryObserverMatch,\n b: QueryObserverMatch,\n ): number =>\n defaultedQueryOptions.indexOf(a.defaultedQueryOptions) -\n defaultedQueryOptions.indexOf(b.defaultedQueryOptions)\n\n return matchingObservers\n .concat(newOrReusedObservers)\n .sort(sortMatchesByOrderOfQueries)\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = replaceAt(this.#result, index, result)\n this.#notify()\n }\n }\n\n #notify(): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"names":["difference","array1","array2","filter","x","indexOf","replaceAt","array","index","value","copy","slice","QueriesObserver","Subscribable","constructor","client","queries","setQueries","onSubscribe","listeners","length","forEach","observer","subscribe","result","onUnsubscribe","destroy","notifyOptions","notifyManager","batch","prevObservers","newObserverMatches","match","setOptions","defaultedQueryOptions","newObservers","map","newResult","getCurrentResult","hasIndexChange","some","hasListeners","getQueries","getCurrentQuery","getObservers","getOptimisticResult","options","defaultQueryOptions","matchingObservers","flatMap","defaultedOptions","find","queryHash","matchedQueryHashes","unmatchedQueries","includes","getObserver","currentObserver","o","QueryObserver","newOrReusedObservers","sortMatchesByOrderOfQueries","a","b","concat","sort","listener"],"mappings":";;;;;;AAWA,SAASA,UAAU,CAAIC,MAAW,EAAEC,MAAW,EAAO;AACpD,EAAA,OAAOD,MAAM,CAACE,MAAM,CAAEC,CAAC,IAAKF,MAAM,CAACG,OAAO,CAACD,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AACvD,CAAA;AAEA,SAASE,SAAS,CAAIC,KAAU,EAAEC,KAAa,EAAEC,KAAQ,EAAO;AAC9D,EAAA,MAAMC,IAAI,GAAGH,KAAK,CAACI,KAAK,CAAC,CAAC,CAAC,CAAA;AAC3BD,EAAAA,IAAI,CAACF,KAAK,CAAC,GAAGC,KAAK,CAAA;AACnB,EAAA,OAAOC,IAAI,CAAA;AACb,CAAA;AAIO,MAAME,eAAe,SAASC,yBAAY,CAA0B;AACzE,EAAA,OAAO,CAAA;AACP,EAAA,OAAO,CAAA;AACP,EAAA,QAAQ,CAAA;AACR,EAAA,UAAU,CAAA;AAEVC,EAAAA,WAAW,CAACC,MAAmB,EAAEC,OAAgC,EAAE;AACjE,IAAA,KAAK,EAAE,CAAA;AAEP,IAAA,IAAI,CAAC,OAA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AuB,EAAEE,MAA2B,EAAQ;IACpE,MAAMhB,KAAK,GAAG,IAAI,CAAC,UAAU,CAACH,OAAO,CAACiB,QAAQ,CAAC,CAAA;AAC/C,IAAA,IAAId,KAAK,KAAK,CAAC,CAAC,EAAE;AAChB,MAAA,IAAI,CAAC,OAAO,GAAGF,SAAS,CAAC,IAAI,CAAC,OAAO,EAAEE,KAAK,EAAEgB,MAAM,CAAC,CAAA;MACrD,IAAI,CAAC,OAAO,EAAE,CAAA;AAChB,KAAA;AACF,GAAA;AAEA,EAAA,OAAO,GAAS;IACdI,2BAAa,CAACC,KAAK,CAAC,MAAM;AACxB,MAAA,IAAI,CAACV,SAAS,CAACE,OAAO,CAAE6C,QAAQ,IAAK;AACnCA,QAAAA,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AACxB,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
@@ -15,14 +15,12 @@ class QueriesObserver extends Subscribable {
15
15
  #result;
16
16
  #queries;
17
17
  #observers;
18
- #observersMap;
19
18
  constructor(client, queries) {
20
19
  super();
21
20
  this.#client = client;
22
21
  this.#queries = [];
23
22
  this.#result = [];
24
23
  this.#observers = [];
25
- this.#observersMap = {};
26
24
  if (queries) {
27
25
  this.setQueries(queries);
28
26
  }
@@ -56,14 +54,12 @@ class QueriesObserver extends Subscribable {
56
54
  // set options for the new observers to notify of changes
57
55
  newObserverMatches.forEach(match => match.observer.setOptions(match.defaultedQueryOptions, notifyOptions));
58
56
  const newObservers = newObserverMatches.map(match => match.observer);
59
- const newObserversMap = Object.fromEntries(newObservers.map(observer => [observer.options.queryHash, observer]));
60
57
  const newResult = newObservers.map(observer => observer.getCurrentResult());
61
58
  const hasIndexChange = newObservers.some((observer, index) => observer !== prevObservers[index]);
62
59
  if (prevObservers.length === newObservers.length && !hasIndexChange) {
63
60
  return;
64
61
  }
65
62
  this.#observers = newObservers;
66
- this.#observersMap = newObserversMap;
67
63
  this.#result = newResult;
68
64
  if (!this.hasListeners()) {
69
65
  return;
@@ -108,7 +104,7 @@ class QueriesObserver extends Subscribable {
108
104
  const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.includes(defaultedOptions.queryHash));
109
105
  const getObserver = options => {
110
106
  const defaultedOptions = this.#client.defaultQueryOptions(options);
111
- const currentObserver = this.#observersMap[defaultedOptions.queryHash];
107
+ const currentObserver = this.#observers.find(o => o.options.queryHash === defaultedOptions.queryHash);
112
108
  return currentObserver ?? new QueryObserver(this.#client, defaultedOptions);
113
109
  };
114
110
  const newOrReusedObservers = unmatchedQueries.map(options => {
@@ -1 +1 @@
1
- {"version":3,"file":"queriesObserver.mjs","sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport type {\n QueryObserverOptions,\n QueryObserverResult,\n DefaultedQueryObserverOptions,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { NotifyOptions } from './queryObserver'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\n\nfunction difference<T>(array1: T[], array2: T[]): T[] {\n return array1.filter((x) => array2.indexOf(x) === -1)\n}\n\nfunction replaceAt<T>(array: T[], index: number, value: T): T[] {\n const copy = array.slice(0)\n copy[index] = value\n return copy\n}\n\ntype QueriesObserverListener = (result: QueryObserverResult[]) => void\n\nexport class QueriesObserver extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result: QueryObserverResult[]\n #queries: QueryObserverOptions[]\n #observers: QueryObserver[]\n #observersMap: Record<string, QueryObserver>\n\n constructor(client: QueryClient, queries?: QueryObserverOptions[]) {\n super()\n\n this.#client = client\n this.#queries = []\n this.#result = []\n this.#observers = []\n this.#observersMap = {}\n\n if (queries) {\n this.setQueries(queries)\n }\n }\n\n protected onSubscribe(): void {\n if (this.listeners.length === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.destroy()\n }\n }\n\n destroy(): void {\n this.listeners = []\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n setQueries(\n queries: QueryObserverOptions[],\n notifyOptions?: NotifyOptions,\n ): void {\n this.#queries = queries\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions, notifyOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newObserversMap = Object.fromEntries(\n newObservers.map((observer) => [observer.options.queryHash, observer]),\n )\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n if (prevObservers.length === newObservers.length && !hasIndexChange) {\n return\n }\n\n this.#observers = newObservers\n this.#observersMap = newObserversMap\n this.#result = newResult\n\n if (!this.hasListeners()) {\n return\n }\n\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n\n this.#notify()\n })\n }\n\n getCurrentResult(): QueryObserverResult[] {\n return this.#result\n }\n\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n getObservers() {\n return this.#observers\n }\n\n getOptimisticResult(queries: QueryObserverOptions[]): QueryObserverResult[] {\n return this.#findMatchingObservers(queries).map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n }\n\n #findMatchingObservers(\n queries: QueryObserverOptions[],\n ): QueryObserverMatch[] {\n const prevObservers = this.#observers\n const defaultedQueryOptions = queries.map((options) =>\n this.#client.defaultQueryOptions(options),\n )\n\n const matchingObservers: QueryObserverMatch[] =\n defaultedQueryOptions.flatMap((defaultedOptions) => {\n const match = prevObservers.find(\n (observer) =>\n observer.options.queryHash === defaultedOptions.queryHash,\n )\n if (match != null) {\n return [{ defaultedQueryOptions: defaultedOptions, observer: match }]\n }\n return []\n })\n\n const matchedQueryHashes = matchingObservers.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n const unmatchedQueries = defaultedQueryOptions.filter(\n (defaultedOptions) =>\n !matchedQueryHashes.includes(defaultedOptions.queryHash),\n )\n\n const getObserver = (options: QueryObserverOptions): QueryObserver => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const currentObserver = this.#observersMap[defaultedOptions.queryHash!]\n return (\n currentObserver ?? new QueryObserver(this.#client, defaultedOptions)\n )\n }\n\n const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map(\n (options) => {\n return {\n defaultedQueryOptions: options,\n observer: getObserver(options),\n }\n },\n )\n\n const sortMatchesByOrderOfQueries = (\n a: QueryObserverMatch,\n b: QueryObserverMatch,\n ): number =>\n defaultedQueryOptions.indexOf(a.defaultedQueryOptions) -\n defaultedQueryOptions.indexOf(b.defaultedQueryOptions)\n\n return matchingObservers\n .concat(newOrReusedObservers)\n .sort(sortMatchesByOrderOfQueries)\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = replaceAt(this.#result, index, result)\n this.#notify()\n }\n }\n\n #notify(): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"names":["difference","array1","array2","filter","x","indexOf","replaceAt","array","index","value","copy","slice","QueriesObserver","Subscribable","constructor","client","queries","setQueries","onSubscribe","listeners","length","forEach","observer","subscribe","result","onUnsubscribe","destroy","notifyOptions","notifyManager","batch","prevObservers","newObserverMatches","match","setOptions","defaultedQueryOptions","newObservers","map","newObserversMap","Object","fromEntries","options","queryHash","newResult","getCurrentResult","hasIndexChange","some","hasListeners","getQueries","getCurrentQuery","getObservers","getOptimisticResult","defaultQueryOptions","matchingObservers","flatMap","defaultedOptions","find","matchedQueryHashes","unmatchedQueries","includes","getObserver","currentObserver","QueryObserver","newOrReusedObservers","sortMatchesByOrderOfQueries","a","b","concat","sort","listener"],"mappings":";;;;AAWA,SAASA,UAAU,CAAIC,MAAW,EAAEC,MAAW,EAAO;AACpD,EAAA,OAAOD,MAAM,CAACE,MAAM,CAAEC,CAAC,IAAKF,MAAM,CAACG,OAAO,CAACD,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AACvD,CAAA;AAEA,SAASE,SAAS,CAAIC,KAAU,EAAEC,KAAa,EAAEC,KAAQ,EAAO;AAC9D,EAAA,MAAMC,IAAI,GAAGH,KAAK,CAACI,KAAK,CAAC,CAAC,CAAC,CAAA;AAC3BD,EAAAA,IAAI,CAACF,KAAK,CAAC,GAAGC,KAAK,CAAA;AACnB,EAAA,OAAOC,IAAI,CAAA;AACb,CAAA;AAIO,MAAME,eAAe,SAASC,YAAY,CAA0B;AACzE,EAAA,OAAO,CAAA;AACP,EAAA,OAAO,CAAA;AACP,EAAA,QAAQ,CAAA;AACR,EAAA,UAAU,CAAA;AACV,EAAA,aAAa,CAAA;AAEbC,EAAAA,WAAW,CAACC,MAAmB,EAAEC,OAAgC,EAAE;AACjE,IAAA,KAAK,EAAE,CAAA;AAEP,IAAA,IAAI,CAAC,OAAO,GAAGD,MAAM,CAAA;AACrB,IAAA,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAA;AAClB,IAAA,IAAI,CAAC,OAAO,GAAG,EAAE,CAAA;AACjB,IAAA,IAAI,CAAC,UAAU,GAAG,EAAE,CAAA;AACpB,IAAA,IAAI,CAAC,aAAa,GAAG,EAAE,CAAA;AAEvB,IAAA,IAAIC,OAAO,EAAE;AACX,MAAA,IAAI,CAACC,UAAU,CAACD,OAAO,CAAC,CAAA;AAC1B,KAAA;AACF,GAAA;AAEUE,EAAAA,WAAW,GAAS;AAC5B,I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,CAAC,EAAE;AAChB,MAAA,IAAI,CAAC,OAAO,GAAGF,SAAS,CAAC,IAAI,CAAC,OAAO,EAAEE,KAAK,EAAEgB,MAAM,CAAC,CAAA;MACrD,IAAI,CAAC,OAAO,EAAE,CAAA;AAChB,KAAA;AACF,GAAA;AAEA,EAAA,OAAO,GAAS;IACdI,aAAa,CAACC,KAAK,CAAC,MAAM;AACxB,MAAA,IAAI,CAACV,SAAS,CAACE,OAAO,CAAE+C,QAAQ,IAAK;AACnCA,QAAAA,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AACxB,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
1
+ {"version":3,"file":"queriesObserver.mjs","sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport type {\n QueryObserverOptions,\n QueryObserverResult,\n DefaultedQueryObserverOptions,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { NotifyOptions } from './queryObserver'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\n\nfunction difference<T>(array1: T[], array2: T[]): T[] {\n return array1.filter((x) => array2.indexOf(x) === -1)\n}\n\nfunction replaceAt<T>(array: T[], index: number, value: T): T[] {\n const copy = array.slice(0)\n copy[index] = value\n return copy\n}\n\ntype QueriesObserverListener = (result: QueryObserverResult[]) => void\n\nexport class QueriesObserver extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result: QueryObserverResult[]\n #queries: QueryObserverOptions[]\n #observers: QueryObserver[]\n\n constructor(client: QueryClient, queries?: QueryObserverOptions[]) {\n super()\n\n this.#client = client\n this.#queries = []\n this.#result = []\n this.#observers = []\n\n if (queries) {\n this.setQueries(queries)\n }\n }\n\n protected onSubscribe(): void {\n if (this.listeners.length === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.destroy()\n }\n }\n\n destroy(): void {\n this.listeners = []\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n setQueries(\n queries: QueryObserverOptions[],\n notifyOptions?: NotifyOptions,\n ): void {\n this.#queries = queries\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions, notifyOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n if (prevObservers.length === newObservers.length && !hasIndexChange) {\n return\n }\n\n this.#observers = newObservers\n this.#result = newResult\n\n if (!this.hasListeners()) {\n return\n }\n\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n\n this.#notify()\n })\n }\n\n getCurrentResult(): QueryObserverResult[] {\n return this.#result\n }\n\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n getObservers() {\n return this.#observers\n }\n\n getOptimisticResult(queries: QueryObserverOptions[]): QueryObserverResult[] {\n return this.#findMatchingObservers(queries).map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n }\n\n #findMatchingObservers(\n queries: QueryObserverOptions[],\n ): QueryObserverMatch[] {\n const prevObservers = this.#observers\n const defaultedQueryOptions = queries.map((options) =>\n this.#client.defaultQueryOptions(options),\n )\n\n const matchingObservers: QueryObserverMatch[] =\n defaultedQueryOptions.flatMap((defaultedOptions) => {\n const match = prevObservers.find(\n (observer) =>\n observer.options.queryHash === defaultedOptions.queryHash,\n )\n if (match != null) {\n return [{ defaultedQueryOptions: defaultedOptions, observer: match }]\n }\n return []\n })\n\n const matchedQueryHashes = matchingObservers.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n const unmatchedQueries = defaultedQueryOptions.filter(\n (defaultedOptions) =>\n !matchedQueryHashes.includes(defaultedOptions.queryHash),\n )\n\n const getObserver = (options: QueryObserverOptions): QueryObserver => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const currentObserver = this.#observers.find(\n (o) => o.options.queryHash === defaultedOptions.queryHash,\n )\n return (\n currentObserver ?? new QueryObserver(this.#client, defaultedOptions)\n )\n }\n\n const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map(\n (options) => {\n return {\n defaultedQueryOptions: options,\n observer: getObserver(options),\n }\n },\n )\n\n const sortMatchesByOrderOfQueries = (\n a: QueryObserverMatch,\n b: QueryObserverMatch,\n ): number =>\n defaultedQueryOptions.indexOf(a.defaultedQueryOptions) -\n defaultedQueryOptions.indexOf(b.defaultedQueryOptions)\n\n return matchingObservers\n .concat(newOrReusedObservers)\n .sort(sortMatchesByOrderOfQueries)\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = replaceAt(this.#result, index, result)\n this.#notify()\n }\n }\n\n #notify(): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"names":["difference","array1","array2","filter","x","indexOf","replaceAt","array","index","value","copy","slice","QueriesObserver","Subscribable","constructor","client","queries","setQueries","onSubscribe","listeners","length","forEach","observer","subscribe","result","onUnsubscribe","destroy","notifyOptions","notifyManager","batch","prevObservers","newObserverMatches","match","setOptions","defaultedQueryOptions","newObservers","map","newResult","getCurrentResult","hasIndexChange","some","hasListeners","getQueries","getCurrentQuery","getObservers","getOptimisticResult","options","defaultQueryOptions","matchingObservers","flatMap","defaultedOptions","find","queryHash","matchedQueryHashes","unmatchedQueries","includes","getObserver","currentObserver","o","QueryObserver","newOrReusedObservers","sortMatchesByOrderOfQueries","a","b","concat","sort","listener"],"mappings":";;;;AAWA,SAASA,UAAU,CAAIC,MAAW,EAAEC,MAAW,EAAO;AACpD,EAAA,OAAOD,MAAM,CAACE,MAAM,CAAEC,CAAC,IAAKF,MAAM,CAACG,OAAO,CAACD,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AACvD,CAAA;AAEA,SAASE,SAAS,CAAIC,KAAU,EAAEC,KAAa,EAAEC,KAAQ,EAAO;AAC9D,EAAA,MAAMC,IAAI,GAAGH,KAAK,CAACI,KAAK,CAAC,CAAC,CAAC,CAAA;AAC3BD,EAAAA,IAAI,CAACF,KAAK,CAAC,GAAGC,KAAK,CAAA;AACnB,EAAA,OAAOC,IAAI,CAAA;AACb,CAAA;AAIO,MAAME,eAAe,SAASC,YAAY,CAA0B;AACzE,EAAA,OAAO,CAAA;AACP,EAAA,OAAO,CAAA;AACP,EAAA,QAAQ,CAAA;AACR,EAAA,UAAU,CAAA;AAEVC,EAAAA,WAAW,CAACC,MAAmB,EAAEC,OAAgC,EAAE;AACjE,IAAA,KAAK,EAAE,CAAA;AAEP,IAAA,IAAI,CAAC,OAAO,GAAGD,MAAM,CAAA;AACrB,IAAA,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAA;AAClB,IAAA,IAAI,CAAC,OAAO,GAAG,EAAE,CAAA;AACjB,IAAA,IAAI,CAAC,UAAU,GAAG,EAAE,CAAA;AAEpB,IAAA,IAAIC,OAAO,EAAE;AACX,MAAA,IAAI,CAACC,UAAU,CAACD,OAAO,CAAC,CAAA;AAC1B,KAAA;AACF,GAAA;AAEUE,EAAAA,WAAW,GAAS;AAC5B,IAAA,IAAI,IAAI,CAACC,SAAS,CAACC,MAAM,KAAK,CAAC,EAAE;AAC/B,MAAA,IAAI,CAAC,UAAU,CAACC,OAAO,CAAEC,QAAQ,IAAK;AACpCA,QAAAA,QAAQ,CAACC,SAAS,CAAE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