@data-client/core 0.9.7 → 0.10.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.
Files changed (40) hide show
  1. package/README.md +22 -0
  2. package/dist/index.js +10 -7
  3. package/dist/index.umd.min.js +1 -1
  4. package/legacy/controller/Controller.js +3 -3
  5. package/legacy/controller/createSet.js +1 -1
  6. package/legacy/controller/ensurePojo.js +2 -3
  7. package/legacy/controller/types.js +1 -1
  8. package/legacy/manager/DevtoolsManager.js +6 -2
  9. package/legacy/middlewareTypes.js +1 -1
  10. package/legacy/state/RIC.js +1 -1
  11. package/legacy/state/reducer/createReducer.js +3 -3
  12. package/legacy/state/reducer/setReducer.js +1 -1
  13. package/lib/controller/Controller.d.ts.map +1 -1
  14. package/lib/controller/Controller.js +3 -3
  15. package/lib/controller/createSet.js +1 -1
  16. package/lib/controller/ensurePojo.d.ts.map +1 -1
  17. package/lib/controller/ensurePojo.js +2 -3
  18. package/lib/controller/types.d.ts.map +1 -1
  19. package/lib/controller/types.js +1 -1
  20. package/lib/manager/DevtoolsManager.d.ts +1 -0
  21. package/lib/manager/DevtoolsManager.d.ts.map +1 -1
  22. package/lib/manager/DevtoolsManager.js +6 -2
  23. package/lib/middlewareTypes.d.ts.map +1 -1
  24. package/lib/middlewareTypes.js +1 -1
  25. package/lib/state/RIC.d.ts.map +1 -1
  26. package/lib/state/RIC.js +1 -1
  27. package/lib/state/reducer/createReducer.js +3 -3
  28. package/lib/state/reducer/setReducer.js +1 -1
  29. package/package.json +2 -2
  30. package/src/controller/Controller.ts +33 -36
  31. package/src/controller/createSet.ts +3 -2
  32. package/src/controller/ensurePojo.ts +6 -7
  33. package/src/controller/types.ts +2 -4
  34. package/src/manager/DevtoolsManager.ts +8 -2
  35. package/src/manager/__tests__/subscriptionManager.ts +3 -2
  36. package/src/middlewareTypes.ts +4 -12
  37. package/src/state/RIC.ts +3 -4
  38. package/src/state/__tests__/reducer.ts +7 -9
  39. package/src/state/reducer/setReducer.ts +3 -3
  40. package/ts3.4/manager/DevtoolsManager.d.ts +1 -0
@@ -103,9 +103,8 @@ export default class Controller<
103
103
  >(
104
104
  endpoint: E,
105
105
  ...args: readonly [...Parameters<E>]
106
- ): E['schema'] extends undefined | null
107
- ? ReturnType<E>
108
- : Promise<Denormalize<E['schema']>> => {
106
+ ): E['schema'] extends undefined | null ? ReturnType<E>
107
+ : Promise<Denormalize<E['schema']>> => {
109
108
  const action = createFetch(endpoint, {
110
109
  args,
111
110
  });
@@ -128,9 +127,8 @@ export default class Controller<
128
127
  >(
129
128
  endpoint: E,
130
129
  ...args: readonly [...Parameters<E>]
131
- ): E['schema'] extends undefined | null
132
- ? ReturnType<E> | ResolveType<E>
133
- : Promise<Denormalize<E['schema']>> | Denormalize<E['schema']> => {
130
+ ): E['schema'] extends undefined | null ? ReturnType<E> | ResolveType<E>
131
+ : Promise<Denormalize<E['schema']>> | Denormalize<E['schema']> => {
134
132
  const { data, expiresAt, expiryStatus } = this.getResponse(
135
133
  endpoint,
136
134
  ...args,
@@ -149,13 +147,13 @@ export default class Controller<
149
147
  endpoint: E,
150
148
  ...args: readonly [...Parameters<E>] | readonly [null]
151
149
  ): Promise<void> =>
152
- args[0] !== null
153
- ? this.dispatch(
154
- createInvalidate(endpoint, {
155
- args: args as readonly [...Parameters<E>],
156
- }),
157
- )
158
- : Promise.resolve();
150
+ args[0] !== null ?
151
+ this.dispatch(
152
+ createInvalidate(endpoint, {
153
+ args: args as readonly [...Parameters<E>],
154
+ }),
155
+ )
156
+ : Promise.resolve();
159
157
 
160
158
  /**
161
159
  * Forces refetching and suspense on useSuspense on all matching endpoint result keys.
@@ -261,13 +259,13 @@ export default class Controller<
261
259
  endpoint: E,
262
260
  ...args: readonly [...Parameters<E>] | readonly [null]
263
261
  ): Promise<void> =>
264
- args[0] !== null
265
- ? this.dispatch(
266
- createSubscription(endpoint, {
267
- args: args as readonly [...Parameters<E>],
268
- }),
269
- )
270
- : Promise.resolve();
262
+ args[0] !== null ?
263
+ this.dispatch(
264
+ createSubscription(endpoint, {
265
+ args: args as readonly [...Parameters<E>],
266
+ }),
267
+ )
268
+ : Promise.resolve();
271
269
 
272
270
  /**
273
271
  * Marks completion of subscription to a given Endpoint.
@@ -283,13 +281,13 @@ export default class Controller<
283
281
  endpoint: E,
284
282
  ...args: readonly [...Parameters<E>] | readonly [null]
285
283
  ): Promise<void> =>
286
- args[0] !== null
287
- ? this.dispatch(
288
- createUnsubscription(endpoint, {
289
- args: args as readonly [...Parameters<E>],
290
- }),
291
- )
292
- : Promise.resolve();
284
+ args[0] !== null ?
285
+ this.dispatch(
286
+ createUnsubscription(endpoint, {
287
+ args: args as readonly [...Parameters<E>],
288
+ }),
289
+ )
290
+ : Promise.resolve();
293
291
 
294
292
  /*************** More ***************/
295
293
 
@@ -386,10 +384,9 @@ export default class Controller<
386
384
  if (!endpoint.schema || !schemaHasEntity(endpoint.schema)) {
387
385
  return {
388
386
  data: results,
389
- expiryStatus: meta?.invalidated
390
- ? ExpiryStatus.Invalid
391
- : cacheResults && !endpoint.invalidIfStale
392
- ? ExpiryStatus.Valid
387
+ expiryStatus:
388
+ meta?.invalidated ? ExpiryStatus.Invalid
389
+ : cacheResults && !endpoint.invalidIfStale ? ExpiryStatus.Valid
393
390
  : ExpiryStatus.InvalidIfStale,
394
391
  expiresAt: expiresAt || 0,
395
392
  } as {
@@ -423,11 +420,11 @@ export default class Controller<
423
420
  // we don't track the difference between stale or fresh because that is tied to triggering
424
421
  // conditions
425
422
  const expiryStatus =
426
- meta?.invalidated || (invalidDenormalize && !meta?.error)
427
- ? ExpiryStatus.Invalid
428
- : invalidDenormalize || endpoint.invalidIfStale || invalidResults
429
- ? ExpiryStatus.InvalidIfStale
430
- : ExpiryStatus.Valid;
423
+ meta?.invalidated || (invalidDenormalize && !meta?.error) ?
424
+ ExpiryStatus.Invalid
425
+ : invalidDenormalize || endpoint.invalidIfStale || invalidResults ?
426
+ ExpiryStatus.InvalidIfStale
427
+ : ExpiryStatus.Valid;
431
428
 
432
429
  return { data, expiryStatus, expiresAt };
433
430
  };
@@ -51,8 +51,9 @@ export default function createSet<
51
51
  error?: boolean;
52
52
  },
53
53
  ): SetAction<E> {
54
- const expiryLength: number = error
55
- ? endpoint.errorExpiryLength ?? 1000
54
+ const expiryLength: number =
55
+ error ?
56
+ endpoint.errorExpiryLength ?? 1000
56
57
  : endpoint.dataExpiryLength ?? 60000;
57
58
  /* istanbul ignore next */
58
59
  if (process.env.NODE_ENV === 'development' && expiryLength < 0) {
@@ -1,11 +1,10 @@
1
1
  export const ensurePojo =
2
2
  // FormData doesn't exist in node
3
3
  /* istanbul ignore else we don't run coverage when we test node*/
4
- typeof FormData !== 'undefined'
5
- ? (body: any) =>
6
- body instanceof FormData
7
- ? Object.fromEntries((body as any).entries())
8
- : body
9
- : /* istanbul ignore next */
10
- (body: any) => body;
4
+ typeof FormData !== 'undefined' ?
5
+ (body: any) =>
6
+ body instanceof FormData ?
7
+ Object.fromEntries((body as any).entries())
8
+ : body
9
+ : /* istanbul ignore next */ (body: any) => body;
11
10
  export default ensurePojo;
@@ -4,10 +4,8 @@ import type {
4
4
  ResolveType,
5
5
  } from '@data-client/normalizr';
6
6
 
7
- export type ResultEntry<E extends EndpointInterface> = E['schema'] extends
8
- | undefined
9
- | null
10
- ? ResolveType<E>
7
+ export type ResultEntry<E extends EndpointInterface> =
8
+ E['schema'] extends undefined | null ? ResolveType<E>
11
9
  : Normalize<E['schema']>;
12
10
 
13
11
  export type EndpointUpdateFunction<
@@ -47,8 +47,9 @@ if (process.env.NODE_ENV !== 'production') {
47
47
  serialize: {
48
48
  options: undefined,
49
49
  /* istanbul ignore next */
50
- replacer: HASINTL
51
- ? (key: string | number | symbol, value: unknown) => {
50
+ replacer:
51
+ HASINTL ?
52
+ (key: string | number | symbol, value: unknown) => {
52
53
  if (
53
54
  typeof value === 'number' &&
54
55
  typeof key === 'string' &&
@@ -81,6 +82,7 @@ export default class DevToolsManager implements Manager {
81
82
  protected started = false;
82
83
  protected actions: [ActionTypes, State<unknown>][] = [];
83
84
  protected declare controller: Controller;
85
+ maxBufferLength = 100;
84
86
 
85
87
  constructor(
86
88
  config?: DevToolsConfig,
@@ -150,6 +152,10 @@ export default class DevToolsManager implements Manager {
150
152
  if (this.started) {
151
153
  this.devTools.send(action, state, undefined, 'RDC');
152
154
  } else {
155
+ // avoid this getting too big in case this is long running
156
+ // we cut in half so we aren't constantly reallocating
157
+ if (this.actions.length > this.maxBufferLength)
158
+ this.actions = this.actions.slice(this.maxBufferLength / 2);
153
159
  // queue actions
154
160
  this.actions.push([action, state]);
155
161
  }
@@ -51,8 +51,9 @@ describe('SubscriptionManager', () => {
51
51
  payload: Record<string, any>,
52
52
  reject = false,
53
53
  ): SubscribeAction {
54
- const fetch = reject
55
- ? () => Promise.reject(new Error('Failed'))
54
+ const fetch =
55
+ reject ?
56
+ () => Promise.reject(new Error('Failed'))
56
57
  : () => Promise.resolve(payload);
57
58
  return {
58
59
  type: SUBSCRIBE_TYPE,
@@ -25,15 +25,7 @@ export type Dispatch<R extends Reducer<any, any>> = (
25
25
  ) => Promise<void>;
26
26
 
27
27
  export type Reducer<S, A> = (prevState: S, action: A) => S;
28
- export type ReducerState<R extends Reducer<any, any>> = R extends Reducer<
29
- infer S,
30
- any
31
- >
32
- ? S
33
- : never;
34
- export type ReducerAction<R extends Reducer<any, any>> = R extends Reducer<
35
- any,
36
- infer A
37
- >
38
- ? A
39
- : never;
28
+ export type ReducerState<R extends Reducer<any, any>> =
29
+ R extends Reducer<infer S, any> ? S : never;
30
+ export type ReducerAction<R extends Reducer<any, any>> =
31
+ R extends Reducer<any, infer A> ? A : never;
package/src/state/RIC.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  const RIC: (cb: (...args: any[]) => void, options: any) => void = (
2
- typeof requestIdleCallback === 'function'
3
- ? requestIdleCallback
4
- : (cb: any) => setTimeout(cb, 0)
5
- ) as any;
2
+ typeof requestIdleCallback === 'function' ? requestIdleCallback : (
3
+ (cb: any) => setTimeout(cb, 0)
4
+ )) as any;
6
5
  export default RIC;
@@ -159,17 +159,15 @@ describe('reducer', () => {
159
159
  existing: any,
160
160
  incoming: any,
161
161
  ) {
162
- return this.shouldReorder(
163
- existingMeta,
164
- incomingMeta,
165
- existing,
166
- incoming,
167
- )
168
- ? existingMeta
162
+ return (
163
+ this.shouldReorder(existingMeta, incomingMeta, existing, incoming)
164
+ ) ?
165
+ existingMeta
169
166
  : {
170
167
  ...incomingMeta,
171
- expiresAt: incoming.content
172
- ? incomingMeta.expiresAt
168
+ expiresAt:
169
+ incoming.content ?
170
+ incomingMeta.expiresAt
173
171
  : existingMeta.expiresAt,
174
172
  };
175
173
  }
@@ -138,8 +138,8 @@ function filterOptimistic(
138
138
  return state.optimistic.filter(
139
139
  optimisticAction =>
140
140
  optimisticAction.meta.key !== resolvingAction.meta.key ||
141
- (optimisticAction.type === OPTIMISTIC_TYPE
142
- ? optimisticAction.meta.fetchedAt !== resolvingAction.meta.fetchedAt
143
- : optimisticAction.meta.date > resolvingAction.meta.date),
141
+ (optimisticAction.type === OPTIMISTIC_TYPE ?
142
+ optimisticAction.meta.fetchedAt !== resolvingAction.meta.fetchedAt
143
+ : optimisticAction.meta.date > resolvingAction.meta.date),
144
144
  );
145
145
  }
@@ -18,6 +18,7 @@ export default class DevToolsManager implements Manager {
18
18
  State<unknown>
19
19
  ][];
20
20
  protected controller: Controller;
21
+ maxBufferLength: number;
21
22
  constructor(config?: DevToolsConfig, skipLogging?: (action: ActionTypes) => boolean);
22
23
  handleAction(action: any, state: any): void;
23
24
  /** Called when initial state is ready */