@data-client/core 0.15.3 → 0.15.7
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/CHANGELOG.md +43 -0
- package/dist/index.js +23 -19
- package/dist/index.umd.min.js +1 -1
- package/dist/mock.js +22 -19
- package/legacy/controller/Controller.js +16 -12
- package/legacy/controller/actions/createOptimistic.js +3 -2
- package/legacy/manager/ConnectionListener.js +1 -1
- package/legacy/manager/NetworkManager.js +2 -2
- package/legacy/state/reducer/setResponseReducer.js +2 -2
- package/lib/controller/Controller.d.ts.map +1 -1
- package/lib/controller/Controller.js +16 -12
- package/lib/controller/actions/createOptimistic.d.ts.map +1 -1
- package/lib/controller/actions/createOptimistic.js +3 -2
- package/lib/manager/ConnectionListener.d.ts +12 -0
- package/lib/manager/ConnectionListener.d.ts.map +1 -1
- package/lib/manager/ConnectionListener.js +1 -1
- package/lib/manager/NetworkManager.d.ts +1 -1
- package/lib/manager/NetworkManager.js +2 -2
- package/lib/state/reducer/setResponseReducer.d.ts.map +1 -1
- package/lib/state/reducer/setResponseReducer.js +2 -2
- package/package.json +3 -3
- package/src/controller/Controller.ts +16 -17
- package/src/controller/__tests__/get.ts +8 -8
- package/src/controller/actions/createOptimistic.ts +2 -1
- package/src/manager/ConnectionListener.ts +12 -0
- package/src/manager/NetworkManager.ts +1 -1
- package/src/state/reducer/setResponseReducer.ts +1 -3
- package/ts3.4/manager/ConnectionListener.d.ts +12 -0
- package/ts3.4/manager/NetworkManager.d.ts +1 -1
|
@@ -40,7 +40,7 @@ import type { GCInterface } from '../state/GCPolicy.js';
|
|
|
40
40
|
import { ImmortalGCPolicy } from '../state/GCPolicy.js';
|
|
41
41
|
import { initialState } from '../state/reducer/createReducer.js';
|
|
42
42
|
import selectMeta from '../state/selectMeta.js';
|
|
43
|
-
import type { ActionTypes,
|
|
43
|
+
import type { ActionTypes, State } from '../types.js';
|
|
44
44
|
|
|
45
45
|
export type GenericDispatch = (value: any) => Promise<void>;
|
|
46
46
|
export type DataClientDispatch = (value: ActionTypes) => Promise<void>;
|
|
@@ -496,12 +496,7 @@ export default class Controller<
|
|
|
496
496
|
expiresAt: number;
|
|
497
497
|
countRef: () => () => void;
|
|
498
498
|
} {
|
|
499
|
-
const state = rest
|
|
500
|
-
// this is typescript generics breaking
|
|
501
|
-
const args: any = rest
|
|
502
|
-
.slice(0, rest.length - 1)
|
|
503
|
-
// handle FormData
|
|
504
|
-
.map(ensurePojo);
|
|
499
|
+
const [state, args] = extractStateAndArgs(rest);
|
|
505
500
|
const isActive = args.length !== 1 || args[0] !== null;
|
|
506
501
|
const key = isActive ? endpoint.key(...args) : '';
|
|
507
502
|
const cacheEndpoints = isActive ? state.endpoints[key] : undefined;
|
|
@@ -581,11 +576,7 @@ export default class Controller<
|
|
|
581
576
|
Pick<State<unknown>, 'entities' | 'indexes'>,
|
|
582
577
|
]
|
|
583
578
|
): DenormalizeNullable<S> | undefined {
|
|
584
|
-
const state = rest
|
|
585
|
-
// this is typescript generics breaking
|
|
586
|
-
const args: any = rest
|
|
587
|
-
.slice(0, rest.length - 1)
|
|
588
|
-
.map(ensurePojo) as SchemaArgs<S>;
|
|
579
|
+
const [state, args] = extractStateAndArgs(rest);
|
|
589
580
|
|
|
590
581
|
const { data } = this.memo.query(schema, args, state);
|
|
591
582
|
return typeof data === 'symbol' ? undefined : data;
|
|
@@ -605,11 +596,7 @@ export default class Controller<
|
|
|
605
596
|
data: DenormalizeNullable<S> | undefined;
|
|
606
597
|
countRef: () => () => void;
|
|
607
598
|
} {
|
|
608
|
-
const state = rest
|
|
609
|
-
// this is typescript generics breaking
|
|
610
|
-
const args: any = rest
|
|
611
|
-
.slice(0, rest.length - 1)
|
|
612
|
-
.map(ensurePojo) as SchemaArgs<S>;
|
|
599
|
+
const [state, args] = extractStateAndArgs(rest);
|
|
613
600
|
|
|
614
601
|
const { data, paths } = this.memo.query(schema, args, state);
|
|
615
602
|
|
|
@@ -823,3 +810,15 @@ class Snapshot<T = unknown> implements SnapshotInterface {
|
|
|
823
810
|
return this.controller.getQueryMeta(schema, ...args, this.state);
|
|
824
811
|
}
|
|
825
812
|
}
|
|
813
|
+
|
|
814
|
+
/** Extract state and args from rest params, applying ensurePojo to args */
|
|
815
|
+
function extractStateAndArgs(rest: readonly unknown[]): [State<any>, any[]] {
|
|
816
|
+
const l = rest.length;
|
|
817
|
+
const args: any = new Array(l - 1);
|
|
818
|
+
for (let i = 0; i < l - 1; i++) {
|
|
819
|
+
// handle FormData
|
|
820
|
+
args[i] = ensurePojo(rest[i]);
|
|
821
|
+
}
|
|
822
|
+
// this is typescript generics breaking
|
|
823
|
+
return [rest[l - 1] as State<any>, args];
|
|
824
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Entity,
|
|
1
|
+
import { Entity, Collection, All, Query, Union } from '@data-client/endpoint';
|
|
2
2
|
|
|
3
3
|
import { initialState } from '../../state/reducer/createReducer';
|
|
4
4
|
import { State } from '../../types';
|
|
@@ -8,7 +8,7 @@ class Tacos extends Entity {
|
|
|
8
8
|
type = '';
|
|
9
9
|
id = '';
|
|
10
10
|
}
|
|
11
|
-
const TacoList = new
|
|
11
|
+
const TacoList = new Collection([Tacos]);
|
|
12
12
|
const entities = {
|
|
13
13
|
Tacos: {
|
|
14
14
|
1: { id: '1', type: 'foo' },
|
|
@@ -188,7 +188,7 @@ describe('Controller.get()', () => {
|
|
|
188
188
|
...initialState,
|
|
189
189
|
entities,
|
|
190
190
|
};
|
|
191
|
-
const AllTacos = new
|
|
191
|
+
const AllTacos = new All(Tacos);
|
|
192
192
|
|
|
193
193
|
it('should get all entities', () => {
|
|
194
194
|
const allTacos = controller.get(AllTacos, state);
|
|
@@ -229,8 +229,8 @@ describe('Controller.get()', () => {
|
|
|
229
229
|
...initialState,
|
|
230
230
|
entities,
|
|
231
231
|
};
|
|
232
|
-
const queryTacos = new
|
|
233
|
-
new
|
|
232
|
+
const queryTacos = new Query(
|
|
233
|
+
new All(Tacos),
|
|
234
234
|
(tacos, { type }: { type?: string } = {}) => {
|
|
235
235
|
if (!type) return tacos;
|
|
236
236
|
return tacos.filter(taco => taco.type === type);
|
|
@@ -281,7 +281,7 @@ describe('Controller.get()', () => {
|
|
|
281
281
|
...initialState,
|
|
282
282
|
entities,
|
|
283
283
|
};
|
|
284
|
-
const tacoCount = new
|
|
284
|
+
const tacoCount = new Query(TacoList, tacos => {
|
|
285
285
|
return tacos.length ?? 0;
|
|
286
286
|
});
|
|
287
287
|
|
|
@@ -303,7 +303,7 @@ describe('Controller.get()', () => {
|
|
|
303
303
|
groupname: string = '';
|
|
304
304
|
memberCount = 0;
|
|
305
305
|
}
|
|
306
|
-
const queryPerson = new
|
|
306
|
+
const queryPerson = new Union(
|
|
307
307
|
{
|
|
308
308
|
users: User,
|
|
309
309
|
groups: Group,
|
|
@@ -363,7 +363,7 @@ describe('Controller.get()', () => {
|
|
|
363
363
|
groupname: string = '';
|
|
364
364
|
memberCount = 0;
|
|
365
365
|
}
|
|
366
|
-
const queryPerson = new
|
|
366
|
+
const queryPerson = new Union(
|
|
367
367
|
{
|
|
368
368
|
users: User,
|
|
369
369
|
groups: Group,
|
|
@@ -3,6 +3,7 @@ import type { EndpointInterface } from '@data-client/normalizr';
|
|
|
3
3
|
import { createMeta } from './createMeta.js';
|
|
4
4
|
import { OPTIMISTIC } from '../../actionTypes.js';
|
|
5
5
|
import type { OptimisticAction } from '../../types.js';
|
|
6
|
+
import ensurePojo from '../ensurePojo.js';
|
|
6
7
|
import type { EndpointUpdateFunction } from '../types.js';
|
|
7
8
|
|
|
8
9
|
export function createOptimistic<
|
|
@@ -25,7 +26,7 @@ export function createOptimistic<
|
|
|
25
26
|
return {
|
|
26
27
|
type: OPTIMISTIC,
|
|
27
28
|
key: endpoint.key(...args),
|
|
28
|
-
args,
|
|
29
|
+
args: args.map(ensurePojo),
|
|
29
30
|
endpoint,
|
|
30
31
|
meta: createMeta(endpoint.dataExpiryLength ?? 60000, fetchedAt),
|
|
31
32
|
};
|
|
@@ -1,7 +1,19 @@
|
|
|
1
|
+
/** Listens to online/offline events for triggering re-fetches on reconnect.
|
|
2
|
+
*
|
|
3
|
+
* Implement this interface to provide custom connectivity detection
|
|
4
|
+
* (e.g., for React Native or Node.js environments).
|
|
5
|
+
*
|
|
6
|
+
* @see https://dataclient.io/docs/api/PollingSubscription
|
|
7
|
+
*/
|
|
1
8
|
export default interface ConnectionListener {
|
|
9
|
+
/** Returns whether the client is currently connected to the network. */
|
|
2
10
|
isOnline: () => boolean;
|
|
11
|
+
/** Register a handler to be called when the client comes back online. */
|
|
3
12
|
addOnlineListener: (handler: () => void) => void;
|
|
13
|
+
/** Remove a previously registered online handler. */
|
|
4
14
|
removeOnlineListener: (handler: () => void) => void;
|
|
15
|
+
/** Register a handler to be called when the client goes offline. */
|
|
5
16
|
addOfflineListener: (handler: () => void) => void;
|
|
17
|
+
/** Remove a previously registered offline handler. */
|
|
6
18
|
removeOfflineListener: (handler: () => void) => void;
|
|
7
19
|
}
|
|
@@ -251,7 +251,7 @@ export default class NetworkManager implements Manager {
|
|
|
251
251
|
* create a new promise and call fetch.
|
|
252
252
|
*
|
|
253
253
|
* Note: The new promise is not actually tied to fetch at all,
|
|
254
|
-
* but is resolved when the expected '
|
|
254
|
+
* but is resolved when the expected 'receive' action is processed.
|
|
255
255
|
* This ensures promises are resolved only once their data is processed
|
|
256
256
|
* by the reducer.
|
|
257
257
|
*/
|
|
@@ -62,9 +62,7 @@ export function setResponseReducer(
|
|
|
62
62
|
// no reason to completely fail because of user-code error
|
|
63
63
|
// integrity of this state update is still guaranteed
|
|
64
64
|
} catch (error) {
|
|
65
|
-
console.error(
|
|
66
|
-
`The following error occured during Endpoint.update() for ${action.key}`,
|
|
67
|
-
);
|
|
65
|
+
console.error(`Endpoint.update() error: ${action.key}`);
|
|
68
66
|
console.error(error);
|
|
69
67
|
}
|
|
70
68
|
return {
|
|
@@ -1,8 +1,20 @@
|
|
|
1
|
+
/** Listens to online/offline events for triggering re-fetches on reconnect.
|
|
2
|
+
*
|
|
3
|
+
* Implement this interface to provide custom connectivity detection
|
|
4
|
+
* (e.g., for React Native or Node.js environments).
|
|
5
|
+
*
|
|
6
|
+
* @see https://dataclient.io/docs/api/PollingSubscription
|
|
7
|
+
*/
|
|
1
8
|
export default interface ConnectionListener {
|
|
9
|
+
/** Returns whether the client is currently connected to the network. */
|
|
2
10
|
isOnline: () => boolean;
|
|
11
|
+
/** Register a handler to be called when the client comes back online. */
|
|
3
12
|
addOnlineListener: (handler: () => void) => void;
|
|
13
|
+
/** Remove a previously registered online handler. */
|
|
4
14
|
removeOnlineListener: (handler: () => void) => void;
|
|
15
|
+
/** Register a handler to be called when the client goes offline. */
|
|
5
16
|
addOfflineListener: (handler: () => void) => void;
|
|
17
|
+
/** Remove a previously registered offline handler. */
|
|
6
18
|
removeOfflineListener: (handler: () => void) => void;
|
|
7
19
|
}
|
|
8
20
|
//# sourceMappingURL=ConnectionListener.d.ts.map
|
|
@@ -62,7 +62,7 @@ export default class NetworkManager implements Manager {
|
|
|
62
62
|
* create a new promise and call fetch.
|
|
63
63
|
*
|
|
64
64
|
* Note: The new promise is not actually tied to fetch at all,
|
|
65
|
-
* but is resolved when the expected '
|
|
65
|
+
* but is resolved when the expected 'receive' action is processed.
|
|
66
66
|
* This ensures promises are resolved only once their data is processed
|
|
67
67
|
* by the reducer.
|
|
68
68
|
*/
|