@data-client/core 0.1.0 → 0.2.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.
- package/dist/index.js +133 -409
- package/dist/index.umd.min.js +1 -1
- package/dist/next.js +61 -101
- package/legacy/actionTypes.js +2 -2
- package/legacy/controller/Controller.js +276 -6
- package/legacy/controller/createFetch.js +3 -14
- package/legacy/controller/createInvalidate.js +1 -1
- package/legacy/controller/createInvalidateAll.js +1 -1
- package/legacy/controller/createOptimistic.js +1 -7
- package/legacy/controller/createReset.js +1 -1
- package/legacy/controller/createSet.js +31 -0
- package/legacy/controller/createSubscription.js +3 -7
- package/legacy/index.js +2 -5
- package/legacy/internal.js +2 -2
- package/legacy/manager/DevtoolsManager.js +2 -2
- package/legacy/manager/LogoutManager.js +2 -2
- package/legacy/manager/NetworkManager.js +28 -59
- package/legacy/manager/PollingSubscription.js +18 -27
- package/legacy/manager/SubscriptionManager.js +16 -42
- package/legacy/middlewareTypes.js +1 -1
- package/legacy/newActions.js +1 -1
- package/legacy/next/index.js +3 -3
- package/legacy/state/reducer/createReducer.js +2 -6
- package/legacy/state/reducer/fetchReducer.js +3 -13
- package/legacy/state/reducer/invalidateReducer.js +1 -1
- package/legacy/state/reducer/setReducer.js +9 -16
- package/legacy/types.js +2 -6
- package/lib/actionTypes.d.ts +1 -0
- package/lib/actionTypes.d.ts.map +1 -1
- package/lib/actionTypes.js +2 -2
- package/lib/controller/Controller.d.ts +126 -6
- package/lib/controller/Controller.d.ts.map +1 -1
- package/lib/controller/Controller.js +276 -6
- package/lib/controller/createFetch.d.ts +2 -2
- package/lib/controller/createFetch.d.ts.map +1 -1
- package/lib/controller/createFetch.js +3 -14
- package/lib/controller/createInvalidate.d.ts +1 -1
- package/lib/controller/createInvalidate.d.ts.map +1 -1
- package/lib/controller/createInvalidate.js +1 -1
- package/lib/controller/createInvalidateAll.d.ts +1 -1
- package/lib/controller/createInvalidateAll.d.ts.map +1 -1
- package/lib/controller/createInvalidateAll.js +1 -1
- package/lib/controller/createOptimistic.d.ts +2 -2
- package/lib/controller/createOptimistic.d.ts.map +1 -1
- package/lib/controller/createOptimistic.js +1 -7
- package/lib/controller/createReset.d.ts +1 -1
- package/lib/controller/createReset.d.ts.map +1 -1
- package/lib/controller/createReset.js +1 -1
- package/lib/controller/{createReceive.d.ts → createSet.d.ts} +6 -6
- package/lib/controller/createSet.d.ts.map +1 -0
- package/lib/controller/createSet.js +31 -0
- package/lib/controller/createSubscription.d.ts +3 -3
- package/lib/controller/createSubscription.d.ts.map +1 -1
- package/lib/controller/createSubscription.js +3 -7
- package/lib/index.d.ts +2 -6
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -5
- package/lib/internal.d.ts +1 -1
- package/lib/internal.d.ts.map +1 -1
- package/lib/internal.js +2 -2
- package/lib/manager/DevtoolsManager.d.ts +1 -1
- package/lib/manager/DevtoolsManager.js +2 -2
- package/lib/manager/LogoutManager.d.ts +4 -5
- package/lib/manager/LogoutManager.d.ts.map +1 -1
- package/lib/manager/LogoutManager.js +2 -2
- package/lib/manager/NetworkManager.d.ts +4 -5
- package/lib/manager/NetworkManager.d.ts.map +1 -1
- package/lib/manager/NetworkManager.js +28 -61
- package/lib/manager/PollingSubscription.d.ts +9 -9
- package/lib/manager/PollingSubscription.d.ts.map +1 -1
- package/lib/manager/PollingSubscription.js +18 -27
- package/lib/manager/SubscriptionManager.d.ts +10 -15
- package/lib/manager/SubscriptionManager.d.ts.map +1 -1
- package/lib/manager/SubscriptionManager.js +16 -42
- package/lib/middlewareTypes.d.ts +5 -5
- package/lib/middlewareTypes.d.ts.map +1 -1
- package/lib/middlewareTypes.js +1 -1
- package/lib/newActions.d.ts +26 -18
- package/lib/newActions.d.ts.map +1 -1
- package/lib/newActions.js +1 -1
- package/lib/next/index.d.ts +2 -2
- package/lib/next/index.d.ts.map +1 -1
- package/lib/next/index.js +3 -3
- package/lib/state/reducer/createReducer.d.ts.map +1 -1
- package/lib/state/reducer/createReducer.js +2 -6
- package/lib/state/reducer/fetchReducer.d.ts +1 -2
- package/lib/state/reducer/fetchReducer.d.ts.map +1 -1
- package/lib/state/reducer/fetchReducer.js +3 -14
- package/lib/state/reducer/invalidateReducer.d.ts +3 -4
- package/lib/state/reducer/invalidateReducer.d.ts.map +1 -1
- package/lib/state/reducer/invalidateReducer.js +1 -1
- package/lib/state/reducer/setReducer.d.ts +2 -38
- package/lib/state/reducer/setReducer.d.ts.map +1 -1
- package/lib/state/reducer/setReducer.js +9 -17
- package/lib/types.d.ts +9 -30
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +2 -6
- package/package.json +3 -3
- package/src/actionTypes.ts +1 -1
- package/src/controller/Controller.ts +491 -10
- package/src/controller/createFetch.ts +4 -19
- package/src/controller/createInvalidate.ts +1 -1
- package/src/controller/createInvalidateAll.ts +1 -1
- package/src/controller/createOptimistic.ts +3 -9
- package/src/controller/createReset.ts +1 -1
- package/src/controller/{createReceive.ts → createSet.ts} +9 -17
- package/src/controller/createSubscription.ts +3 -10
- package/src/index.ts +2 -16
- package/src/internal.ts +1 -1
- package/src/manager/DevtoolsManager.ts +1 -1
- package/src/manager/LogoutManager.ts +4 -5
- package/src/manager/NetworkManager.ts +31 -80
- package/src/manager/PollingSubscription.ts +26 -27
- package/src/manager/SubscriptionManager.ts +22 -54
- package/src/manager/__tests__/__snapshots__/pollingSubscription.ts.snap +10 -8
- package/src/manager/__tests__/logoutManager.ts +5 -5
- package/src/manager/__tests__/manager.ts +2 -4
- package/src/manager/__tests__/networkManager.ts +8 -42
- package/src/manager/__tests__/pollingSubscription.ts +259 -151
- package/src/manager/__tests__/subscriptionManager.ts +7 -13
- package/src/middlewareTypes.ts +5 -10
- package/src/newActions.ts +33 -23
- package/src/next/index.ts +2 -2
- package/src/state/__tests__/reducer.ts +59 -297
- package/src/state/reducer/createReducer.ts +3 -11
- package/src/state/reducer/fetchReducer.ts +5 -18
- package/src/state/reducer/invalidateReducer.ts +2 -2
- package/src/state/reducer/setReducer.ts +10 -22
- package/src/types.ts +9 -78
- package/ts3.4/actionTypes.d.ts +1 -0
- package/ts3.4/controller/Controller.d.ts +168 -6
- package/ts3.4/controller/createFetch.d.ts +2 -2
- package/ts3.4/controller/createInvalidate.d.ts +1 -1
- package/ts3.4/controller/createInvalidateAll.d.ts +1 -1
- package/ts3.4/controller/createOptimistic.d.ts +1 -1
- package/ts3.4/controller/createReset.d.ts +1 -1
- package/ts3.4/controller/{createReceive.d.ts → createSet.d.ts} +6 -6
- package/ts3.4/controller/createSubscription.d.ts +3 -3
- package/ts3.4/index.d.ts +2 -7
- package/ts3.4/internal.d.ts +1 -1
- package/ts3.4/manager/DevtoolsManager.d.ts +1 -1
- package/ts3.4/manager/LogoutManager.d.ts +4 -5
- package/ts3.4/manager/NetworkManager.d.ts +4 -5
- package/ts3.4/manager/PollingSubscription.d.ts +9 -9
- package/ts3.4/manager/SubscriptionManager.d.ts +10 -15
- package/ts3.4/middlewareTypes.d.ts +5 -5
- package/ts3.4/newActions.d.ts +26 -18
- package/ts3.4/next/index.d.ts +2 -2
- package/ts3.4/state/reducer/fetchReducer.d.ts +1 -2
- package/ts3.4/state/reducer/invalidateReducer.d.ts +3 -4
- package/ts3.4/state/reducer/setReducer.d.ts +2 -38
- package/ts3.4/types.d.ts +8 -30
- package/legacy/compatibleActions.js +0 -2
- package/legacy/controller/BaseController.js +0 -289
- package/legacy/controller/createReceive.js +0 -36
- package/legacy/endpoint/index.js +0 -2
- package/legacy/endpoint/shapes.js +0 -2
- package/legacy/endpoint/types.js +0 -2
- package/legacy/legacyActions.js +0 -2
- package/legacy/next/Controller.js +0 -24
- package/legacy/previousActions.js +0 -2
- package/legacy/state/applyUpdatersToResults.js +0 -4
- package/legacy/state/legacy-actions/createFetch.js +0 -62
- package/legacy/state/legacy-actions/createReceive.js +0 -37
- package/legacy/state/legacy-actions/createReceiveError.js +0 -28
- package/legacy/state/legacy-actions/index.js +0 -4
- package/legacy/state/reducerInstance.js +0 -9
- package/lib/compatibleActions.d.ts +0 -47
- package/lib/compatibleActions.d.ts.map +0 -1
- package/lib/compatibleActions.js +0 -2
- package/lib/controller/BaseController.d.ts +0 -128
- package/lib/controller/BaseController.d.ts.map +0 -1
- package/lib/controller/BaseController.js +0 -289
- package/lib/controller/createReceive.d.ts.map +0 -1
- package/lib/controller/createReceive.js +0 -36
- package/lib/endpoint/index.d.ts +0 -3
- package/lib/endpoint/index.d.ts.map +0 -1
- package/lib/endpoint/index.js +0 -2
- package/lib/endpoint/shapes.d.ts +0 -25
- package/lib/endpoint/shapes.d.ts.map +0 -1
- package/lib/endpoint/shapes.js +0 -2
- package/lib/endpoint/types.d.ts +0 -45
- package/lib/endpoint/types.d.ts.map +0 -1
- package/lib/endpoint/types.js +0 -2
- package/lib/legacyActions.d.ts +0 -92
- package/lib/legacyActions.d.ts.map +0 -1
- package/lib/legacyActions.js +0 -2
- package/lib/next/Controller.d.ts +0 -14
- package/lib/next/Controller.d.ts.map +0 -1
- package/lib/next/Controller.js +0 -24
- package/lib/previousActions.d.ts +0 -91
- package/lib/previousActions.d.ts.map +0 -1
- package/lib/previousActions.js +0 -2
- package/lib/state/applyUpdatersToResults.d.ts +0 -13
- package/lib/state/applyUpdatersToResults.d.ts.map +0 -1
- package/lib/state/applyUpdatersToResults.js +0 -7
- package/lib/state/legacy-actions/createFetch.d.ts +0 -19
- package/lib/state/legacy-actions/createFetch.d.ts.map +0 -1
- package/lib/state/legacy-actions/createFetch.js +0 -62
- package/lib/state/legacy-actions/createReceive.d.ts +0 -14
- package/lib/state/legacy-actions/createReceive.d.ts.map +0 -1
- package/lib/state/legacy-actions/createReceive.js +0 -37
- package/lib/state/legacy-actions/createReceiveError.d.ts +0 -9
- package/lib/state/legacy-actions/createReceiveError.d.ts.map +0 -1
- package/lib/state/legacy-actions/createReceiveError.js +0 -28
- package/lib/state/legacy-actions/index.d.ts +0 -4
- package/lib/state/legacy-actions/index.d.ts.map +0 -1
- package/lib/state/legacy-actions/index.js +0 -4
- package/lib/state/reducerInstance.d.ts +0 -7
- package/lib/state/reducerInstance.d.ts.map +0 -1
- package/lib/state/reducerInstance.js +0 -9
- package/src/compatibleActions.ts +0 -96
- package/src/controller/BaseController.ts +0 -508
- package/src/endpoint/index.ts +0 -14
- package/src/endpoint/shapes.ts +0 -53
- package/src/endpoint/types.ts +0 -72
- package/src/legacyActions.ts +0 -163
- package/src/manager/__tests__/__snapshots__/pollingSubscription-endpoint.ts.snap +0 -49
- package/src/manager/__tests__/networkManager-legacy.ts +0 -394
- package/src/manager/__tests__/pollingSubscription-endpoint.ts +0 -423
- package/src/next/Controller.ts +0 -39
- package/src/previousActions.ts +0 -159
- package/src/state/__tests__/applyUpdatersToResults.ts +0 -40
- package/src/state/applyUpdatersToResults.ts +0 -29
- package/src/state/legacy-actions/createFetch.ts +0 -95
- package/src/state/legacy-actions/createReceive.ts +0 -68
- package/src/state/legacy-actions/createReceiveError.ts +0 -43
- package/src/state/legacy-actions/index.ts +0 -3
- package/src/state/reducerInstance.ts +0 -14
- package/ts3.4/compatibleActions.d.ts +0 -47
- package/ts3.4/controller/BaseController.d.ts +0 -170
- package/ts3.4/endpoint/index.d.ts +0 -3
- package/ts3.4/endpoint/shapes.d.ts +0 -25
- package/ts3.4/endpoint/types.d.ts +0 -45
- package/ts3.4/legacyActions.d.ts +0 -95
- package/ts3.4/next/Controller.d.ts +0 -14
- package/ts3.4/previousActions.d.ts +0 -94
- package/ts3.4/state/applyUpdatersToResults.d.ts +0 -13
- package/ts3.4/state/legacy-actions/createFetch.d.ts +0 -19
- package/ts3.4/state/legacy-actions/createReceive.d.ts +0 -14
- package/ts3.4/state/legacy-actions/createReceiveError.d.ts +0 -9
- package/ts3.4/state/legacy-actions/index.d.ts +0 -4
- package/ts3.4/state/reducerInstance.d.ts +0 -7
|
@@ -1,423 +0,0 @@
|
|
|
1
|
-
import { Article, PollingArticleResource } from '__tests__/new';
|
|
2
|
-
|
|
3
|
-
import { initialState } from '../../state/reducer/createReducer';
|
|
4
|
-
import ConnectionListener from '../ConnectionListener';
|
|
5
|
-
import DefaultConnectionListener from '../DefaultConnectionListener';
|
|
6
|
-
import PollingSubscription from '../PollingSubscription';
|
|
7
|
-
|
|
8
|
-
class MockConnectionListener implements ConnectionListener {
|
|
9
|
-
declare online: boolean;
|
|
10
|
-
declare onlineHandlers: (() => void)[];
|
|
11
|
-
declare offlineHandlers: (() => void)[];
|
|
12
|
-
|
|
13
|
-
constructor(online: boolean) {
|
|
14
|
-
this.online = online;
|
|
15
|
-
this.onlineHandlers = [];
|
|
16
|
-
this.offlineHandlers = [];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
isOnline() {
|
|
20
|
-
return this.online;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
addOnlineListener(handler: () => void) {
|
|
24
|
-
this.onlineHandlers.push(handler);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
removeOnlineListener(handler: () => void) {
|
|
28
|
-
this.onlineHandlers = this.onlineHandlers.filter(h => h !== handler);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
addOfflineListener(handler: () => void) {
|
|
32
|
-
this.offlineHandlers.push(handler);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
removeOfflineListener(handler: () => void) {
|
|
36
|
-
this.offlineHandlers = this.offlineHandlers.filter(h => h !== handler);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
trigger(event: 'offline' | 'online') {
|
|
40
|
-
switch (event) {
|
|
41
|
-
case 'offline':
|
|
42
|
-
this.online = false;
|
|
43
|
-
this.offlineHandlers.forEach(t => t());
|
|
44
|
-
break;
|
|
45
|
-
case 'online':
|
|
46
|
-
this.online = true;
|
|
47
|
-
this.onlineHandlers.forEach(t => t());
|
|
48
|
-
break;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
reset() {
|
|
53
|
-
this.offlineHandlers = [];
|
|
54
|
-
this.onlineHandlers = [];
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const makeState = (key: string, time: number) => () => ({
|
|
59
|
-
...initialState,
|
|
60
|
-
meta: { [key]: { date: time, expiresAt: Infinity } },
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
function onError(e: any) {
|
|
64
|
-
e.preventDefault();
|
|
65
|
-
}
|
|
66
|
-
beforeEach(() => {
|
|
67
|
-
if (typeof addEventListener === 'function')
|
|
68
|
-
addEventListener('error', onError);
|
|
69
|
-
});
|
|
70
|
-
afterEach(() => {
|
|
71
|
-
if (typeof removeEventListener === 'function')
|
|
72
|
-
removeEventListener('error', onError);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
describe('PollingSubscription', () => {
|
|
76
|
-
const a = () => Promise.resolve({ id: 5, title: 'hi' });
|
|
77
|
-
const fetch = jest.fn(a);
|
|
78
|
-
|
|
79
|
-
describe('existing data', () => {
|
|
80
|
-
it('should wait to call for fresh data', () => {
|
|
81
|
-
const dispatch = jest.fn();
|
|
82
|
-
jest.useFakeTimers();
|
|
83
|
-
const sub2 = new PollingSubscription(
|
|
84
|
-
{
|
|
85
|
-
key: 'test.com',
|
|
86
|
-
schema: Article,
|
|
87
|
-
fetch,
|
|
88
|
-
frequency: 5000,
|
|
89
|
-
getState: makeState('test.com', Date.now()),
|
|
90
|
-
},
|
|
91
|
-
dispatch,
|
|
92
|
-
);
|
|
93
|
-
expect(dispatch.mock.calls.length).toBe(0);
|
|
94
|
-
jest.advanceTimersByTime(4990);
|
|
95
|
-
expect(dispatch.mock.calls.length).toBe(0);
|
|
96
|
-
jest.advanceTimersByTime(20);
|
|
97
|
-
expect(dispatch.mock.calls.length).toBe(1);
|
|
98
|
-
jest.advanceTimersByTime(5000);
|
|
99
|
-
expect(dispatch.mock.calls.length).toBe(2);
|
|
100
|
-
jest.advanceTimersByTime(2000);
|
|
101
|
-
expect(dispatch.mock.calls.length).toBe(2);
|
|
102
|
-
sub2.cleanup();
|
|
103
|
-
jest.useRealTimers();
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('should only run once with multiple simultaneous starts', () => {
|
|
107
|
-
const dispatch = jest.fn();
|
|
108
|
-
jest.useFakeTimers();
|
|
109
|
-
const sub2 = new PollingSubscription(
|
|
110
|
-
{
|
|
111
|
-
key: 'test.com',
|
|
112
|
-
schema: Article,
|
|
113
|
-
fetch,
|
|
114
|
-
frequency: 5000,
|
|
115
|
-
getState: () => initialState,
|
|
116
|
-
},
|
|
117
|
-
dispatch,
|
|
118
|
-
);
|
|
119
|
-
sub2.add(1000);
|
|
120
|
-
sub2.add(1000);
|
|
121
|
-
sub2.add(1000);
|
|
122
|
-
jest.advanceTimersByTime(1);
|
|
123
|
-
expect(dispatch.mock.calls.length).toBe(1);
|
|
124
|
-
jest.advanceTimersByTime(999);
|
|
125
|
-
expect(dispatch.mock.calls.length).toBe(2);
|
|
126
|
-
jest.advanceTimersByTime(10);
|
|
127
|
-
sub2.remove(1000);
|
|
128
|
-
sub2.remove(1000);
|
|
129
|
-
sub2.remove(1000);
|
|
130
|
-
sub2.cleanup();
|
|
131
|
-
jest.useRealTimers();
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
describe('fresh data', () => {
|
|
136
|
-
const dispatch = jest.fn();
|
|
137
|
-
let sub: PollingSubscription;
|
|
138
|
-
|
|
139
|
-
beforeAll(() => {
|
|
140
|
-
jest.useFakeTimers();
|
|
141
|
-
sub = new PollingSubscription(
|
|
142
|
-
{
|
|
143
|
-
key: 'test.com',
|
|
144
|
-
schema: Article,
|
|
145
|
-
fetch,
|
|
146
|
-
frequency: 5000,
|
|
147
|
-
getState: makeState('test.com', 0),
|
|
148
|
-
},
|
|
149
|
-
dispatch,
|
|
150
|
-
);
|
|
151
|
-
});
|
|
152
|
-
afterAll(() => {
|
|
153
|
-
sub.cleanup();
|
|
154
|
-
jest.useRealTimers();
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('should throw on undefined frequency in construction', () => {
|
|
158
|
-
expect(
|
|
159
|
-
() =>
|
|
160
|
-
new PollingSubscription(
|
|
161
|
-
{
|
|
162
|
-
key: 'test.com',
|
|
163
|
-
schema: Article,
|
|
164
|
-
fetch,
|
|
165
|
-
getState: makeState('test.com', 0),
|
|
166
|
-
},
|
|
167
|
-
dispatch,
|
|
168
|
-
),
|
|
169
|
-
).toThrow();
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it('should call immediately', () => {
|
|
173
|
-
jest.advanceTimersByTime(1);
|
|
174
|
-
expect(dispatch.mock.calls.length).toBe(1);
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it('should call after period', () => {
|
|
178
|
-
dispatch.mockReset();
|
|
179
|
-
jest.advanceTimersByTime(5000);
|
|
180
|
-
expect(dispatch.mock.calls.length).toBe(1);
|
|
181
|
-
dispatch.mock.calls[0].forEach((element: any) => {
|
|
182
|
-
delete element?.meta?.createdAt;
|
|
183
|
-
});
|
|
184
|
-
expect(dispatch.mock.calls[0]).toMatchSnapshot();
|
|
185
|
-
jest.advanceTimersByTime(5000);
|
|
186
|
-
expect(dispatch.mock.calls.length).toBe(2);
|
|
187
|
-
dispatch.mock.calls[1].forEach((element: any) => {
|
|
188
|
-
delete element?.meta?.createdAt;
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
expect(dispatch.mock.calls[1]).toMatchSnapshot();
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
describe('add()', () => {
|
|
195
|
-
it('should take smaller frequency when another is added', () => {
|
|
196
|
-
sub.add(1000);
|
|
197
|
-
jest.advanceTimersByTime(1000 * 4);
|
|
198
|
-
expect(dispatch.mock.calls.length).toBe(2 + 4);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it('should not change frequency if same is added', () => {
|
|
202
|
-
dispatch.mockClear();
|
|
203
|
-
sub.add(1000);
|
|
204
|
-
jest.advanceTimersByTime(1000 * 13);
|
|
205
|
-
expect(dispatch.mock.calls.length).toBe(13);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
it('should not change frequency if larger is added', () => {
|
|
209
|
-
dispatch.mockClear();
|
|
210
|
-
sub.add(7000);
|
|
211
|
-
sub.add(8000);
|
|
212
|
-
jest.advanceTimersByTime(1000 * 13);
|
|
213
|
-
expect(dispatch.mock.calls.length).toBe(13);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it('should do nothing if frequency is undefined', () => {
|
|
217
|
-
dispatch.mockClear();
|
|
218
|
-
sub.add(undefined);
|
|
219
|
-
jest.advanceTimersByTime(1000 * 13);
|
|
220
|
-
expect(dispatch.mock.calls.length).toBe(13);
|
|
221
|
-
});
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
describe('remove()', () => {
|
|
225
|
-
it('should not change frequency if only partially removed', () => {
|
|
226
|
-
dispatch.mockClear();
|
|
227
|
-
sub.remove(1000);
|
|
228
|
-
jest.advanceTimersByTime(1000 * 13);
|
|
229
|
-
expect(dispatch.mock.calls.length).toBe(13);
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
it('should not change frequency if only larger removed', () => {
|
|
233
|
-
dispatch.mockClear();
|
|
234
|
-
sub.remove(7000);
|
|
235
|
-
jest.advanceTimersByTime(1000 * 13);
|
|
236
|
-
expect(dispatch.mock.calls.length).toBe(13);
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
it('should go back to fastest if smallest frequency is removed completely', () => {
|
|
240
|
-
sub.remove(1000);
|
|
241
|
-
jest.advanceTimersByTime(1000);
|
|
242
|
-
dispatch.mockClear();
|
|
243
|
-
jest.advanceTimersByTime(5000 * 13);
|
|
244
|
-
expect(dispatch.mock.calls.length).toBe(13);
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
it('should do nothing if frequency is not registered', () => {
|
|
248
|
-
const oldError = console.error;
|
|
249
|
-
const spy = (console.error = jest.fn());
|
|
250
|
-
|
|
251
|
-
sub.remove(1000);
|
|
252
|
-
dispatch.mockClear();
|
|
253
|
-
jest.advanceTimersByTime(5000 * 13);
|
|
254
|
-
expect(dispatch.mock.calls.length).toBe(13);
|
|
255
|
-
|
|
256
|
-
expect(spy.mock.calls[0]).toMatchInlineSnapshot(`
|
|
257
|
-
[
|
|
258
|
-
"Mismatched remove: 1000 is not subscribed for test.com",
|
|
259
|
-
]
|
|
260
|
-
`);
|
|
261
|
-
console.error = oldError;
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
it('should do nothing if frequency is undefined', () => {
|
|
265
|
-
sub.remove(undefined);
|
|
266
|
-
dispatch.mockClear();
|
|
267
|
-
jest.advanceTimersByTime(5000 * 13);
|
|
268
|
-
expect(dispatch.mock.calls.length).toBe(13);
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
it('should return false until completely empty, then return true', () => {
|
|
272
|
-
const sub = new PollingSubscription(
|
|
273
|
-
{
|
|
274
|
-
key: 'test.com',
|
|
275
|
-
schema: Article,
|
|
276
|
-
fetch,
|
|
277
|
-
frequency: 5000,
|
|
278
|
-
getState: makeState('test.com', 0),
|
|
279
|
-
},
|
|
280
|
-
dispatch,
|
|
281
|
-
);
|
|
282
|
-
sub.add(5000);
|
|
283
|
-
sub.add(7000);
|
|
284
|
-
expect(sub.remove(5000)).toBe(false);
|
|
285
|
-
expect(sub.remove(5000)).toBe(false);
|
|
286
|
-
expect(sub.remove(7000)).toBe(true);
|
|
287
|
-
});
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
describe('cleanup()', () => {
|
|
291
|
-
let warnSpy: jest.SpyInstance;
|
|
292
|
-
afterEach(() => {
|
|
293
|
-
warnSpy.mockRestore();
|
|
294
|
-
});
|
|
295
|
-
beforeEach(() => (warnSpy = jest.spyOn(console, 'warn')));
|
|
296
|
-
|
|
297
|
-
it('should stop all timers', () => {
|
|
298
|
-
dispatch.mockClear();
|
|
299
|
-
sub.cleanup();
|
|
300
|
-
jest.advanceTimersByTime(5000 * 13);
|
|
301
|
-
expect(dispatch.mock.calls.length).toBe(0);
|
|
302
|
-
});
|
|
303
|
-
it('should be idempotent', () => {
|
|
304
|
-
sub.cleanup();
|
|
305
|
-
});
|
|
306
|
-
it('should not run even if interval not cancelled', () => {
|
|
307
|
-
sub.cleanup();
|
|
308
|
-
sub = new PollingSubscription(
|
|
309
|
-
{
|
|
310
|
-
key: 'test.com2',
|
|
311
|
-
schema: Article,
|
|
312
|
-
fetch,
|
|
313
|
-
frequency: 5000,
|
|
314
|
-
getState: makeState('test.com', 0),
|
|
315
|
-
},
|
|
316
|
-
dispatch,
|
|
317
|
-
);
|
|
318
|
-
sub.add(5000);
|
|
319
|
-
jest.runOnlyPendingTimers();
|
|
320
|
-
delete (sub as any).intervalId;
|
|
321
|
-
jest.advanceTimersByTime(5000 * 13);
|
|
322
|
-
|
|
323
|
-
expect(dispatch.mock.calls.length).toBe(1);
|
|
324
|
-
expect(warnSpy.mock.calls.length).toBe(13);
|
|
325
|
-
expect(warnSpy.mock.calls[0]).toMatchSnapshot();
|
|
326
|
-
});
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
describe('offline support', () => {
|
|
331
|
-
beforeAll(() => {
|
|
332
|
-
jest.useFakeTimers();
|
|
333
|
-
});
|
|
334
|
-
afterAll(() => {
|
|
335
|
-
jest.useRealTimers();
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
function createMocks(listener: ConnectionListener) {
|
|
339
|
-
const dispatch = jest.fn();
|
|
340
|
-
const a = () => Promise.resolve({ id: 5, title: 'hi' });
|
|
341
|
-
const fetch = jest.fn(a);
|
|
342
|
-
|
|
343
|
-
const pollingSubscription = new PollingSubscription(
|
|
344
|
-
{
|
|
345
|
-
key: 'test.com',
|
|
346
|
-
schema: Article,
|
|
347
|
-
fetch,
|
|
348
|
-
frequency: 5000,
|
|
349
|
-
getState: makeState('test.com', 0),
|
|
350
|
-
},
|
|
351
|
-
dispatch,
|
|
352
|
-
listener,
|
|
353
|
-
);
|
|
354
|
-
jest.advanceTimersByTime(1);
|
|
355
|
-
return { dispatch, fetch, pollingSubscription };
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
it('should not dispatch when offline', () => {
|
|
359
|
-
jest.spyOn(navigator, 'onLine', 'get').mockReturnValue(false);
|
|
360
|
-
const listener = new DefaultConnectionListener();
|
|
361
|
-
const offlineSpy = jest.spyOn(listener, 'addOfflineListener');
|
|
362
|
-
const onlineSpy = jest.spyOn(listener, 'addOnlineListener');
|
|
363
|
-
const { dispatch } = createMocks(listener);
|
|
364
|
-
jest.advanceTimersByTime(50000);
|
|
365
|
-
jest.spyOn(navigator, 'onLine', 'get').mockReturnValue(true);
|
|
366
|
-
expect(dispatch.mock.calls.length).toBe(0);
|
|
367
|
-
expect(offlineSpy.mock.calls.length).toBe(0);
|
|
368
|
-
expect(onlineSpy.mock.calls.length).toBe(1);
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
it('should not dispatch when onLine is undefined (default to true)', () => {
|
|
372
|
-
jest.spyOn(navigator, 'onLine', 'get').mockReturnValue(undefined as any);
|
|
373
|
-
const listener = new DefaultConnectionListener();
|
|
374
|
-
const offlineSpy = jest.spyOn(listener, 'addOfflineListener');
|
|
375
|
-
const onlineSpy = jest.spyOn(listener, 'addOnlineListener');
|
|
376
|
-
const { dispatch } = createMocks(listener);
|
|
377
|
-
expect(dispatch.mock.calls.length).toBe(1);
|
|
378
|
-
expect(offlineSpy.mock.calls.length).toBe(1);
|
|
379
|
-
expect(onlineSpy.mock.calls.length).toBe(0);
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
it('should immediately start fetching when online', () => {
|
|
383
|
-
const listener = new MockConnectionListener(false);
|
|
384
|
-
const { dispatch } = createMocks(listener);
|
|
385
|
-
expect(dispatch.mock.calls.length).toBe(0);
|
|
386
|
-
|
|
387
|
-
listener.trigger('online');
|
|
388
|
-
jest.advanceTimersByTime(1);
|
|
389
|
-
expect(dispatch.mock.calls.length).toBe(1);
|
|
390
|
-
jest.advanceTimersByTime(5000);
|
|
391
|
-
expect(dispatch.mock.calls.length).toBe(2);
|
|
392
|
-
expect(listener.offlineHandlers.length).toBe(1);
|
|
393
|
-
expect(listener.onlineHandlers.length).toBe(0);
|
|
394
|
-
});
|
|
395
|
-
|
|
396
|
-
it('should not run when timeoutId is deleted after coming online', () => {
|
|
397
|
-
const listener = new MockConnectionListener(false);
|
|
398
|
-
const { dispatch, pollingSubscription } = createMocks(listener);
|
|
399
|
-
expect(dispatch.mock.calls.length).toBe(0);
|
|
400
|
-
|
|
401
|
-
listener.trigger('online');
|
|
402
|
-
delete (pollingSubscription as any).startId;
|
|
403
|
-
jest.advanceTimersByTime(1);
|
|
404
|
-
expect(dispatch.mock.calls.length).toBe(0);
|
|
405
|
-
jest.advanceTimersByTime(5000);
|
|
406
|
-
expect(dispatch.mock.calls.length).toBe(0);
|
|
407
|
-
expect(listener.offlineHandlers.length).toBe(1);
|
|
408
|
-
expect(listener.onlineHandlers.length).toBe(0);
|
|
409
|
-
});
|
|
410
|
-
|
|
411
|
-
it('should stop dispatching when offline again', () => {
|
|
412
|
-
const listener = new MockConnectionListener(true);
|
|
413
|
-
const { dispatch } = createMocks(listener);
|
|
414
|
-
expect(dispatch.mock.calls.length).toBe(1);
|
|
415
|
-
|
|
416
|
-
listener.trigger('offline');
|
|
417
|
-
jest.advanceTimersByTime(50000);
|
|
418
|
-
expect(dispatch.mock.calls.length).toBe(1);
|
|
419
|
-
expect(listener.offlineHandlers.length).toBe(0);
|
|
420
|
-
expect(listener.onlineHandlers.length).toBe(1);
|
|
421
|
-
});
|
|
422
|
-
});
|
|
423
|
-
});
|
package/src/next/Controller.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { EndpointInterface, Denormalize } from '@data-client/normalizr';
|
|
2
|
-
import { denormalize } from '@data-client/normalizr';
|
|
3
|
-
|
|
4
|
-
import BaseController, {
|
|
5
|
-
CompatibleDispatch,
|
|
6
|
-
GenericDispatch,
|
|
7
|
-
} from '../controller/BaseController.js';
|
|
8
|
-
import createFetch from '../controller/createFetch.js';
|
|
9
|
-
import type { EndpointUpdateFunction } from '../controller/types.js';
|
|
10
|
-
|
|
11
|
-
export default class Controller<
|
|
12
|
-
D extends GenericDispatch = CompatibleDispatch,
|
|
13
|
-
> extends BaseController<D> {
|
|
14
|
-
/**
|
|
15
|
-
* Fetches the endpoint with given args, updating the Rest Hooks cache with the response or error upon completion.
|
|
16
|
-
* @see https://resthooks.io/docs/api/Controller#fetch
|
|
17
|
-
*/
|
|
18
|
-
fetch = <
|
|
19
|
-
E extends EndpointInterface & { update?: EndpointUpdateFunction<E> },
|
|
20
|
-
>(
|
|
21
|
-
endpoint: E,
|
|
22
|
-
...args: readonly [...Parameters<E>]
|
|
23
|
-
): E['schema'] extends undefined | null
|
|
24
|
-
? ReturnType<E>
|
|
25
|
-
: Promise<Denormalize<E['schema']>> => {
|
|
26
|
-
const action = createFetch(endpoint, {
|
|
27
|
-
args,
|
|
28
|
-
});
|
|
29
|
-
this.dispatch(action);
|
|
30
|
-
|
|
31
|
-
if (endpoint.schema) {
|
|
32
|
-
return action.meta.promise.then(input =>
|
|
33
|
-
denormalize(input, endpoint.schema, {}, args),
|
|
34
|
-
) as any;
|
|
35
|
-
}
|
|
36
|
-
return action.meta.promise as any;
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
export * from '../controller/BaseController.js';
|
package/src/previousActions.ts
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EndpointExtraOptions,
|
|
3
|
-
EndpointInterface,
|
|
4
|
-
Schema,
|
|
5
|
-
} from '@data-client/normalizr';
|
|
6
|
-
import type { UpdateFunction } from '@data-client/normalizr';
|
|
7
|
-
import { FSAWithMeta, FSAWithPayloadAndMeta } from 'flux-standard-action';
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
FETCH_TYPE,
|
|
11
|
-
GC_TYPE,
|
|
12
|
-
INVALIDATE_TYPE,
|
|
13
|
-
OPTIMISTIC_TYPE,
|
|
14
|
-
RECEIVE_TYPE,
|
|
15
|
-
RESET_TYPE,
|
|
16
|
-
SUBSCRIBE_TYPE,
|
|
17
|
-
UNSUBSCRIBE_TYPE,
|
|
18
|
-
} from './actionTypes.js';
|
|
19
|
-
import { EndpointUpdateFunction } from './controller/types.js';
|
|
20
|
-
import { FetchShape } from './endpoint/index.js';
|
|
21
|
-
import { ErrorableFSAWithPayloadAndMeta } from './fsa.js';
|
|
22
|
-
import type { InvalidateAction, InvalidateAllAction } from './newActions.js';
|
|
23
|
-
|
|
24
|
-
export interface ReceiveMeta<S extends Schema | undefined> {
|
|
25
|
-
schema?: S;
|
|
26
|
-
key: string;
|
|
27
|
-
args?: readonly any[];
|
|
28
|
-
updaters?: Record<string, UpdateFunction<S, any>>;
|
|
29
|
-
update?: (result: any, ...args: any) => Record<string, (...args: any) => any>;
|
|
30
|
-
fetchedAt?: number;
|
|
31
|
-
date: number;
|
|
32
|
-
expiresAt: number;
|
|
33
|
-
errorPolicy?: (error: any) => 'hard' | 'soft' | undefined;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export type ReceiveAction<
|
|
37
|
-
Payload extends object | string | number | null =
|
|
38
|
-
| object
|
|
39
|
-
| string
|
|
40
|
-
| number
|
|
41
|
-
| null,
|
|
42
|
-
S extends Schema | undefined = any,
|
|
43
|
-
> = ErrorableFSAWithPayloadAndMeta<
|
|
44
|
-
typeof RECEIVE_TYPE,
|
|
45
|
-
Payload,
|
|
46
|
-
ReceiveMeta<S>
|
|
47
|
-
> & { endpoint?: EndpointInterface };
|
|
48
|
-
|
|
49
|
-
export type OptimisticAction<
|
|
50
|
-
E extends EndpointInterface & {
|
|
51
|
-
update?: EndpointUpdateFunction<E>;
|
|
52
|
-
} = EndpointInterface & {
|
|
53
|
-
update?: EndpointUpdateFunction<EndpointInterface>;
|
|
54
|
-
},
|
|
55
|
-
> = {
|
|
56
|
-
type: typeof OPTIMISTIC_TYPE;
|
|
57
|
-
meta: {
|
|
58
|
-
schema: E['schema'];
|
|
59
|
-
key: string;
|
|
60
|
-
args: readonly any[];
|
|
61
|
-
update?: (
|
|
62
|
-
result: any,
|
|
63
|
-
...args: any
|
|
64
|
-
) => Record<string, (...args: any) => any>;
|
|
65
|
-
fetchedAt: number;
|
|
66
|
-
date: number;
|
|
67
|
-
expiresAt: number;
|
|
68
|
-
errorPolicy?: (error: any) => 'hard' | 'soft' | undefined;
|
|
69
|
-
};
|
|
70
|
-
endpoint: E;
|
|
71
|
-
error?: undefined;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export interface ResetAction {
|
|
75
|
-
type: typeof RESET_TYPE;
|
|
76
|
-
date: number | Date;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
interface FetchMeta<
|
|
80
|
-
Payload extends object | string | number | null =
|
|
81
|
-
| object
|
|
82
|
-
| string
|
|
83
|
-
| number
|
|
84
|
-
| null,
|
|
85
|
-
S extends Schema | undefined = any,
|
|
86
|
-
> {
|
|
87
|
-
type: FetchShape<any, any>['type'];
|
|
88
|
-
schema?: S;
|
|
89
|
-
key: string;
|
|
90
|
-
args?: readonly any[];
|
|
91
|
-
updaters?: Record<string, UpdateFunction<S, any>>;
|
|
92
|
-
update?: (result: any, ...args: any) => Record<string, (...args: any) => any>;
|
|
93
|
-
options?: EndpointExtraOptions;
|
|
94
|
-
throttle: boolean;
|
|
95
|
-
resolve: (value?: any | PromiseLike<any>) => void;
|
|
96
|
-
reject: (reason?: any) => void;
|
|
97
|
-
promise: PromiseLike<any>;
|
|
98
|
-
createdAt: number | Date;
|
|
99
|
-
optimisticResponse?: Payload;
|
|
100
|
-
// indicates whether network manager processed it
|
|
101
|
-
nm?: boolean;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export interface FetchAction<
|
|
105
|
-
Payload extends object | string | number | null =
|
|
106
|
-
| object
|
|
107
|
-
| string
|
|
108
|
-
| number
|
|
109
|
-
| null,
|
|
110
|
-
S extends Schema | undefined = any,
|
|
111
|
-
> extends FSAWithPayloadAndMeta<
|
|
112
|
-
typeof FETCH_TYPE,
|
|
113
|
-
() => Promise<Payload>,
|
|
114
|
-
FetchMeta<any, any>
|
|
115
|
-
> {
|
|
116
|
-
meta: FetchMeta<Payload, S>;
|
|
117
|
-
endpoint?: EndpointInterface;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export interface SubscribeAction
|
|
121
|
-
extends FSAWithMeta<typeof SUBSCRIBE_TYPE, undefined, any> {
|
|
122
|
-
endpoint?: EndpointInterface;
|
|
123
|
-
meta: {
|
|
124
|
-
args?: readonly any[];
|
|
125
|
-
schema: Schema | undefined;
|
|
126
|
-
fetch: () => Promise<any>;
|
|
127
|
-
key: string;
|
|
128
|
-
options: EndpointExtraOptions | undefined;
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export interface UnsubscribeAction
|
|
133
|
-
extends FSAWithMeta<typeof UNSUBSCRIBE_TYPE, undefined, any> {
|
|
134
|
-
endpoint?: EndpointInterface;
|
|
135
|
-
meta: {
|
|
136
|
-
args?: readonly any[];
|
|
137
|
-
key: string;
|
|
138
|
-
options: EndpointExtraOptions | undefined;
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export type { InvalidateAction, InvalidateAllAction };
|
|
143
|
-
|
|
144
|
-
export interface GCAction {
|
|
145
|
-
type: typeof GC_TYPE;
|
|
146
|
-
entities: [string, string][];
|
|
147
|
-
results: string[];
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export type ActionTypes =
|
|
151
|
-
| FetchAction
|
|
152
|
-
| OptimisticAction
|
|
153
|
-
| ReceiveAction
|
|
154
|
-
| SubscribeAction
|
|
155
|
-
| UnsubscribeAction
|
|
156
|
-
| InvalidateAction
|
|
157
|
-
| InvalidateAllAction
|
|
158
|
-
| ResetAction
|
|
159
|
-
| GCAction;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { ArticleResourceWithOtherListUrl } from '__tests__/new';
|
|
2
|
-
|
|
3
|
-
import applyUpdatersToResults from '../applyUpdatersToResults';
|
|
4
|
-
|
|
5
|
-
describe('applyUpdatersToResults', () => {
|
|
6
|
-
const results = {
|
|
7
|
-
[ArticleResourceWithOtherListUrl.getList.key({})]: ['1', '2'],
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
it('handles a single updater', () => {
|
|
11
|
-
const newResults = applyUpdatersToResults(results, '3', {
|
|
12
|
-
[ArticleResourceWithOtherListUrl.getList.key({})]: (
|
|
13
|
-
article: string,
|
|
14
|
-
articles: string[] | undefined,
|
|
15
|
-
) => [article, ...(articles || [])],
|
|
16
|
-
});
|
|
17
|
-
expect(newResults).toStrictEqual({
|
|
18
|
-
...results,
|
|
19
|
-
[ArticleResourceWithOtherListUrl.getList.key({})]: ['3', '1', '2'],
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('handles multiple updaters sequentially', () => {
|
|
24
|
-
const newResults = applyUpdatersToResults(results, '3', {
|
|
25
|
-
[ArticleResourceWithOtherListUrl.getList.key({})]: (
|
|
26
|
-
article: string,
|
|
27
|
-
articles: string[] | undefined,
|
|
28
|
-
) => [article, ...(articles || [])],
|
|
29
|
-
[ArticleResourceWithOtherListUrl.otherList.key({})]: (
|
|
30
|
-
article: string,
|
|
31
|
-
articles: string[] | undefined,
|
|
32
|
-
) => [...(articles || []), article],
|
|
33
|
-
});
|
|
34
|
-
expect(newResults).toStrictEqual({
|
|
35
|
-
...results,
|
|
36
|
-
[ArticleResourceWithOtherListUrl.getList.key({})]: ['3', '1', '2'],
|
|
37
|
-
[ArticleResourceWithOtherListUrl.otherList.key({})]: ['3'],
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
});
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { Schema, Normalize, UpdateFunction } from '@data-client/normalizr';
|
|
2
|
-
|
|
3
|
-
type ResultStateFromUpdateFunctions<
|
|
4
|
-
SourceSchema extends Schema,
|
|
5
|
-
UpdateFunctions extends {
|
|
6
|
-
[key: string]: UpdateFunction<SourceSchema, any>;
|
|
7
|
-
},
|
|
8
|
-
> = { [K in keyof UpdateFunctions]: any };
|
|
9
|
-
|
|
10
|
-
export default function applyUpdatersToResults<
|
|
11
|
-
SourceSchema extends Schema,
|
|
12
|
-
UpdateFunctions extends {
|
|
13
|
-
[key: string]: UpdateFunction<SourceSchema, any>;
|
|
14
|
-
},
|
|
15
|
-
>(
|
|
16
|
-
results: ResultStateFromUpdateFunctions<SourceSchema, UpdateFunctions>,
|
|
17
|
-
result: Normalize<SourceSchema>,
|
|
18
|
-
updaters: UpdateFunctions,
|
|
19
|
-
) {
|
|
20
|
-
return {
|
|
21
|
-
...results,
|
|
22
|
-
...Object.fromEntries(
|
|
23
|
-
Object.entries(updaters).map(([fetchKey, updater]) => [
|
|
24
|
-
fetchKey,
|
|
25
|
-
updater(result, results[fetchKey]),
|
|
26
|
-
]),
|
|
27
|
-
),
|
|
28
|
-
};
|
|
29
|
-
}
|