@tanstack/angular-query-experimental 5.25.0 → 5.26.0

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.
@@ -0,0 +1,126 @@
1
+ import { describe, expect, test } from 'vitest'
2
+ import {
3
+ ChangeDetectionStrategy,
4
+ Component,
5
+ type WritableSignal,
6
+ computed,
7
+ effect,
8
+ input,
9
+ signal,
10
+ } from '@angular/core'
11
+ import { TestBed } from '@angular/core/testing'
12
+ import { flushQueue, setFixtureSignalInputs } from '../../test-utils'
13
+ import { lazyInit } from '../../../util/lazy-init/lazy-init'
14
+
15
+ describe('lazyInit', () => {
16
+ test('should init lazily in next tick when not accessing manually', async () => {
17
+ const mockFn = vi.fn()
18
+
19
+ TestBed.runInInjectionContext(() => {
20
+ lazyInit(() => {
21
+ mockFn()
22
+ return {
23
+ data: signal(true),
24
+ }
25
+ })
26
+ })
27
+
28
+ expect(mockFn).not.toHaveBeenCalled()
29
+
30
+ await new Promise(setImmediate)
31
+
32
+ expect(mockFn).toHaveBeenCalled()
33
+ })
34
+
35
+ test('should init eagerly accessing manually', async () => {
36
+ const mockFn = vi.fn()
37
+
38
+ TestBed.runInInjectionContext(() => {
39
+ const lazySignal = lazyInit(() => {
40
+ mockFn()
41
+ return {
42
+ data: signal(true),
43
+ }
44
+ })
45
+
46
+ lazySignal.data()
47
+
48
+ console.log(lazySignal)
49
+ })
50
+
51
+ expect(mockFn).toHaveBeenCalled()
52
+ })
53
+
54
+ test('should init lazily and only once', async () => {
55
+ const initCallFn = vi.fn()
56
+ const registerDataValue = vi.fn<[number]>()
57
+
58
+ let value!: { data: WritableSignal<number> }
59
+ const outerSignal = signal(0)
60
+
61
+ TestBed.runInInjectionContext(() => {
62
+ value = lazyInit(() => {
63
+ initCallFn()
64
+
65
+ void outerSignal()
66
+
67
+ return { data: signal(0) }
68
+ })
69
+
70
+ effect(() => registerDataValue(value.data()))
71
+ })
72
+
73
+ value.data()
74
+
75
+ await flushQueue()
76
+
77
+ expect(outerSignal).toBeDefined()
78
+
79
+ expect(initCallFn).toHaveBeenCalledTimes(1)
80
+
81
+ outerSignal.set(1)
82
+ await flushQueue()
83
+ outerSignal.set(2)
84
+ await flushQueue()
85
+ value.data.set(4)
86
+ await flushQueue()
87
+
88
+ expect(initCallFn).toHaveBeenCalledTimes(1)
89
+ expect(registerDataValue).toHaveBeenCalledTimes(2)
90
+ })
91
+
92
+ test('should support required signal input', async () => {
93
+ @Component({
94
+ standalone: true,
95
+ template: `{{ call }} - {{ lazySignal.data() }}`,
96
+ changeDetection: ChangeDetectionStrategy.OnPush,
97
+ })
98
+ class Test {
99
+ readonly title = input.required<string>()
100
+ call = 0
101
+
102
+ lazySignal = lazyInit(() => {
103
+ this.call++
104
+ return {
105
+ data: computed(() => this.title()),
106
+ }
107
+ })
108
+ }
109
+
110
+ const fixture = TestBed.createComponent(Test)
111
+
112
+ setFixtureSignalInputs(fixture, { title: 'newValue' })
113
+ expect(fixture.debugElement.nativeElement.textContent).toBe('0 - newValue')
114
+ await flushQueue()
115
+
116
+ setFixtureSignalInputs(fixture, { title: 'updatedValue' })
117
+ expect(fixture.debugElement.nativeElement.textContent).toBe(
118
+ '1 - updatedValue',
119
+ )
120
+
121
+ setFixtureSignalInputs(fixture, { title: 'newUpdatedValue' })
122
+ expect(fixture.debugElement.nativeElement.textContent).toBe(
123
+ '1 - newUpdatedValue',
124
+ )
125
+ })
126
+ })
@@ -0,0 +1,130 @@
1
+ import { describe, expect, test } from 'vitest'
2
+ import {
3
+ Component,
4
+ type Signal,
5
+ type WritableSignal,
6
+ effect,
7
+ input,
8
+ signal,
9
+ } from '@angular/core'
10
+ import { TestBed } from '@angular/core/testing'
11
+ import { lazySignalInitializer } from '../../../util/lazy-signal-initializer/lazy-signal-initializer'
12
+ import { flushQueue, setFixtureSignalInputs } from '../../test-utils'
13
+
14
+ describe('lazySignalInitializer', () => {
15
+ test('should init lazily in next tick when not accessing manually', async () => {
16
+ const mockFn = vi.fn()
17
+
18
+ TestBed.runInInjectionContext(() => {
19
+ lazySignalInitializer(() => {
20
+ mockFn()
21
+ return signal(true)
22
+ })
23
+ })
24
+
25
+ expect(mockFn).not.toHaveBeenCalled()
26
+
27
+ await new Promise(setImmediate)
28
+
29
+ expect(mockFn).toHaveBeenCalled()
30
+ })
31
+
32
+ test('should init eagerly accessing manually', async () => {
33
+ const mockFn = vi.fn()
34
+
35
+ TestBed.runInInjectionContext(() => {
36
+ const lazySignal = lazySignalInitializer(() => {
37
+ mockFn()
38
+ return signal(true)
39
+ })
40
+
41
+ lazySignal()
42
+ })
43
+
44
+ expect(mockFn).toHaveBeenCalled()
45
+ })
46
+
47
+ test('should init lazily and only once', async () => {
48
+ const initCallFn = vi.fn()
49
+ const registerEffectValue = vi.fn<[number]>()
50
+
51
+ let value!: Signal<number>
52
+ const outerSignal = signal(0)
53
+ let innerSignal!: WritableSignal<number>
54
+
55
+ TestBed.runInInjectionContext(() => {
56
+ value = lazySignalInitializer(() => {
57
+ initCallFn()
58
+ innerSignal = signal(0)
59
+
60
+ void outerSignal()
61
+
62
+ return innerSignal
63
+ })
64
+
65
+ effect(() => registerEffectValue(value()))
66
+ })
67
+
68
+ value()
69
+
70
+ await flushQueue()
71
+
72
+ expect(outerSignal).toBeDefined()
73
+ expect(innerSignal).toBeDefined()
74
+
75
+ expect(initCallFn).toHaveBeenCalledTimes(1)
76
+
77
+ innerSignal.set(1)
78
+ await flushQueue()
79
+ outerSignal.set(2)
80
+ await flushQueue()
81
+
82
+ expect(initCallFn).toHaveBeenCalledTimes(1)
83
+ expect(registerEffectValue).toHaveBeenCalledTimes(2)
84
+ })
85
+
86
+ test('should init lazily', async () => {
87
+ @Component({
88
+ standalone: true,
89
+ template: `{{ subscribed }}`,
90
+ })
91
+ class Test {
92
+ subscribed = false
93
+
94
+ lazySignal = lazySignalInitializer(() => {
95
+ this.subscribed = true
96
+ return signal('value')
97
+ })
98
+ }
99
+
100
+ const fixture = TestBed.createComponent(Test)
101
+ const { debugElement } = fixture
102
+ fixture.detectChanges()
103
+
104
+ expect(debugElement.nativeElement.textContent).toBe('false')
105
+
106
+ await new Promise(setImmediate)
107
+
108
+ fixture.detectChanges()
109
+
110
+ expect(debugElement.nativeElement.textContent).toBe('true')
111
+ })
112
+
113
+ test('should support required signal input', () => {
114
+ @Component({
115
+ standalone: true,
116
+ template: `{{ subscribed }}`,
117
+ })
118
+ class Test {
119
+ readonly title = input.required<string>()
120
+ subscribed = false
121
+
122
+ lazySignal = lazySignalInitializer(() => {
123
+ return signal(this.title())
124
+ })
125
+ }
126
+
127
+ const fixture = TestBed.createComponent(Test)
128
+ setFixtureSignalInputs(fixture, { title: 'newValue' })
129
+ })
130
+ })
@@ -10,7 +10,7 @@ import {
10
10
  } from '@angular/core'
11
11
  import { notifyManager } from '@tanstack/query-core'
12
12
  import { signalProxy } from './signal-proxy'
13
- import { lazyInit } from './lazy-init'
13
+ import { lazyInit } from './util/lazy-init/lazy-init'
14
14
  import type { QueryClient, QueryKey, QueryObserver } from '@tanstack/query-core'
15
15
  import type { CreateBaseQueryOptions, CreateBaseQueryResult } from './types'
16
16
 
@@ -67,20 +67,17 @@ export function createBaseQuery<
67
67
  observer.getOptimisticResult(defaultedOptionsSignal()),
68
68
  )
69
69
 
70
- // Effects should not be called inside reactive contexts
71
- untracked(() =>
72
- effect(() => {
73
- const defaultedOptions = defaultedOptionsSignal()
74
- observer.setOptions(defaultedOptions, {
75
- // Do not notify on updates because of changes in the options because
76
- // these changes should already be reflected in the optimistic result.
77
- listeners: false,
78
- })
79
- untracked(() => {
80
- resultSignal.set(observer.getOptimisticResult(defaultedOptions))
81
- })
82
- }),
83
- )
70
+ effect(() => {
71
+ const defaultedOptions = defaultedOptionsSignal()
72
+ observer.setOptions(defaultedOptions, {
73
+ // Do not notify on updates because of changes in the options because
74
+ // these changes should already be reflected in the optimistic result.
75
+ listeners: false,
76
+ })
77
+ untracked(() => {
78
+ resultSignal.set(observer.getOptimisticResult(defaultedOptions))
79
+ })
80
+ })
84
81
 
85
82
  // observer.trackResult is not used as this optimization is not needed for Angular
86
83
  const unsubscribe = observer.subscribe(
@@ -10,6 +10,7 @@ import {
10
10
  } from '@tanstack/query-core'
11
11
  import { assertInjector } from './util/assert-injector/assert-injector'
12
12
  import { injectQueryClient } from './inject-query-client'
13
+ import { lazySignalInitializer } from './util/lazy-signal-initializer/lazy-signal-initializer'
13
14
  import type { Injector, Signal } from '@angular/core'
14
15
 
15
16
  type MutationStateOptions<TResult = MutationState> = {
@@ -49,33 +50,38 @@ export function injectMutationState<TResult = MutationState>(
49
50
 
50
51
  const mutationCache = queryClient.getMutationCache()
51
52
 
52
- const result = signal<Array<TResult>>(
53
- getResult(mutationCache, mutationStateOptionsFn()),
54
- )
53
+ return lazySignalInitializer((injector) => {
54
+ const result = signal<Array<TResult>>(
55
+ getResult(mutationCache, mutationStateOptionsFn()),
56
+ )
55
57
 
56
- effect(() => {
57
- const mutationStateOptions = mutationStateOptionsFn()
58
- untracked(() => {
59
- // Setting the signal from an effect because it's both 'computed' from options()
60
- // and needs to be set imperatively in the mutationCache listener.
61
- result.set(getResult(mutationCache, mutationStateOptions))
62
- })
63
- })
58
+ effect(
59
+ () => {
60
+ const mutationStateOptions = mutationStateOptionsFn()
61
+ untracked(() => {
62
+ // Setting the signal from an effect because it's both 'computed' from options()
63
+ // and needs to be set imperatively in the mutationCache listener.
64
+ result.set(getResult(mutationCache, mutationStateOptions))
65
+ })
66
+ },
67
+ { injector },
68
+ )
64
69
 
65
- const unsubscribe = mutationCache.subscribe(
66
- notifyManager.batchCalls(() => {
67
- const nextResult = replaceEqualDeep(
68
- result(),
69
- getResult(mutationCache, mutationStateOptionsFn()),
70
- )
71
- if (result() !== nextResult) {
72
- result.set(nextResult)
73
- }
74
- }),
75
- )
70
+ const unsubscribe = mutationCache.subscribe(
71
+ notifyManager.batchCalls(() => {
72
+ const nextResult = replaceEqualDeep(
73
+ result(),
74
+ getResult(mutationCache, mutationStateOptionsFn()),
75
+ )
76
+ if (result() !== nextResult) {
77
+ result.set(nextResult)
78
+ }
79
+ }),
80
+ )
76
81
 
77
- destroyRef.onDestroy(unsubscribe)
82
+ destroyRef.onDestroy(unsubscribe)
78
83
 
79
- return result
84
+ return result
85
+ })
80
86
  })
81
87
  }
@@ -1,12 +1,20 @@
1
- import { DestroyRef, computed, effect, inject, signal } from '@angular/core'
1
+ import {
2
+ DestroyRef,
3
+ Injector,
4
+ computed,
5
+ effect,
6
+ inject,
7
+ runInInjectionContext,
8
+ signal,
9
+ } from '@angular/core'
2
10
  import { MutationObserver, notifyManager } from '@tanstack/query-core'
3
11
  import { assertInjector } from './util/assert-injector/assert-injector'
4
12
  import { signalProxy } from './signal-proxy'
5
13
  import { injectQueryClient } from './inject-query-client'
6
14
  import { noop } from './util'
7
- import type { DefaultError, QueryClient } from '@tanstack/query-core'
8
- import type { Injector } from '@angular/core'
9
15
 
16
+ import { lazyInit } from './util/lazy-init/lazy-init'
17
+ import type { DefaultError, QueryClient } from '@tanstack/query-core'
10
18
  import type {
11
19
  CreateMutateFunction,
12
20
  CreateMutationOptions,
@@ -26,42 +34,51 @@ export function injectMutation<
26
34
  ): CreateMutationResult<TData, TError, TVariables, TContext> {
27
35
  return assertInjector(injectMutation, injector, () => {
28
36
  const queryClient = injectQueryClient()
37
+ const currentInjector = inject(Injector)
29
38
  const destroyRef = inject(DestroyRef)
30
39
 
31
- const observer = new MutationObserver<TData, TError, TVariables, TContext>(
32
- queryClient,
33
- options(queryClient),
34
- )
35
- const mutate: CreateMutateFunction<TData, TError, TVariables, TContext> = (
36
- variables,
37
- mutateOptions,
38
- ) => {
39
- observer.mutate(variables, mutateOptions).catch(noop)
40
- }
40
+ return lazyInit(() =>
41
+ runInInjectionContext(currentInjector, () => {
42
+ const observer = new MutationObserver<
43
+ TData,
44
+ TError,
45
+ TVariables,
46
+ TContext
47
+ >(queryClient, options(queryClient))
48
+ const mutate: CreateMutateFunction<
49
+ TData,
50
+ TError,
51
+ TVariables,
52
+ TContext
53
+ > = (variables, mutateOptions) => {
54
+ observer.mutate(variables, mutateOptions).catch(noop)
55
+ }
41
56
 
42
- effect(() => {
43
- observer.setOptions(options(queryClient))
44
- })
57
+ effect(() => {
58
+ observer.setOptions(options(queryClient))
59
+ })
45
60
 
46
- const result = signal(observer.getCurrentResult())
61
+ const result = signal(observer.getCurrentResult())
47
62
 
48
- const unsubscribe = observer.subscribe(
49
- notifyManager.batchCalls((val) => result.set(val)),
50
- )
63
+ const unsubscribe = observer.subscribe(
64
+ notifyManager.batchCalls((val) => result.set(val)),
65
+ )
51
66
 
52
- destroyRef.onDestroy(unsubscribe)
67
+ destroyRef.onDestroy(unsubscribe)
53
68
 
54
- const resultSignal = computed(() => ({
55
- ...result(),
56
- mutate,
57
- mutateAsync: result().mutate,
58
- }))
69
+ const resultSignal = computed(() => ({
70
+ ...result(),
71
+ mutate,
72
+ mutateAsync: result().mutate,
73
+ }))
59
74
 
60
- return signalProxy(resultSignal) as unknown as CreateMutationResult<
61
- TData,
62
- TError,
63
- TVariables,
64
- TContext
65
- >
75
+ return signalProxy(resultSignal) as unknown as CreateMutationResult<
76
+ TData,
77
+ TError,
78
+ TVariables,
79
+ TContext
80
+ >
81
+ }),
82
+ )
66
83
  })
67
84
  }
@@ -1,9 +1,11 @@
1
+ import { untracked } from '@angular/core'
2
+
1
3
  export function lazyInit<T extends object>(initializer: () => T): T {
2
4
  let object: T | null = null
3
5
 
4
6
  const initializeObject = () => {
5
7
  if (!object) {
6
- object = initializer()
8
+ object = untracked(() => initializer())
7
9
  }
8
10
  }
9
11
 
@@ -0,0 +1,28 @@
1
+ import {
2
+ Injector,
3
+ type Signal,
4
+ computed,
5
+ inject,
6
+ untracked,
7
+ } from '@angular/core'
8
+
9
+ type SignalInitializerFn<T> = (injector: Injector) => Signal<T>
10
+
11
+ export function lazySignalInitializer<T>(
12
+ initializerFn: SignalInitializerFn<T>,
13
+ ) {
14
+ const injector = inject(Injector)
15
+
16
+ let source: Signal<T> | null = null
17
+
18
+ const unwrapSignal = () => {
19
+ if (!source) {
20
+ source = untracked(() => initializerFn(injector))
21
+ }
22
+ return source()
23
+ }
24
+
25
+ queueMicrotask(() => unwrapSignal())
26
+
27
+ return computed(unwrapSignal)
28
+ }
@@ -1,30 +0,0 @@
1
- export function lazyInit(initializer) {
2
- let object = null;
3
- const initializeObject = () => {
4
- if (!object) {
5
- object = initializer();
6
- }
7
- };
8
- queueMicrotask(() => initializeObject());
9
- return new Proxy({}, {
10
- get(_, prop, receiver) {
11
- initializeObject();
12
- return Reflect.get(object, prop, receiver);
13
- },
14
- has(_, prop) {
15
- initializeObject();
16
- return Reflect.has(object, prop);
17
- },
18
- ownKeys() {
19
- initializeObject();
20
- return Reflect.ownKeys(object);
21
- },
22
- getOwnPropertyDescriptor() {
23
- return {
24
- enumerable: true,
25
- configurable: true,
26
- };
27
- },
28
- });
29
- }
30
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibGF6eS1pbml0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2xhenktaW5pdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxNQUFNLFVBQVUsUUFBUSxDQUFtQixXQUFvQjtJQUM3RCxJQUFJLE1BQU0sR0FBYSxJQUFJLENBQUE7SUFFM0IsTUFBTSxnQkFBZ0IsR0FBRyxHQUFHLEVBQUU7UUFDNUIsSUFBSSxDQUFDLE1BQU0sRUFBRTtZQUNYLE1BQU0sR0FBRyxXQUFXLEVBQUUsQ0FBQTtTQUN2QjtJQUNILENBQUMsQ0FBQTtJQUVELGNBQWMsQ0FBQyxHQUFHLEVBQUUsQ0FBQyxnQkFBZ0IsRUFBRSxDQUFDLENBQUE7SUFFeEMsT0FBTyxJQUFJLEtBQUssQ0FBSSxFQUFPLEVBQUU7UUFDM0IsR0FBRyxDQUFDLENBQUMsRUFBRSxJQUFJLEVBQUUsUUFBUTtZQUNu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