@data-client/core 0.10.0 → 0.11.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 (114) hide show
  1. package/README.md +2 -1
  2. package/dist/index.js +122 -89
  3. package/dist/index.umd.min.js +1 -1
  4. package/legacy/actionTypes.js +1 -1
  5. package/legacy/actions.js +1 -1
  6. package/legacy/controller/AbortOptimistic.js +2 -0
  7. package/legacy/controller/Controller.js +108 -76
  8. package/legacy/controller/createExpireAll.js +1 -1
  9. package/legacy/controller/createFetch.js +1 -1
  10. package/legacy/controller/createInvalidate.js +1 -1
  11. package/legacy/controller/createInvalidateAll.js +1 -1
  12. package/legacy/controller/createOptimistic.js +1 -1
  13. package/legacy/controller/createReset.js +1 -1
  14. package/legacy/controller/createSet.js +1 -1
  15. package/legacy/controller/createSubscription.js +1 -1
  16. package/legacy/controller/ensurePojo.js +1 -1
  17. package/legacy/controller/types.js +1 -1
  18. package/legacy/index.js +1 -1
  19. package/legacy/internal.js +2 -2
  20. package/legacy/manager/ConnectionListener.js +1 -1
  21. package/legacy/manager/DefaultConnectionListener.js +1 -1
  22. package/legacy/manager/DevtoolsManager.js +1 -1
  23. package/legacy/manager/LogoutManager.js +1 -1
  24. package/legacy/manager/NetworkManager.js +1 -1
  25. package/legacy/manager/PollingSubscription.js +1 -1
  26. package/legacy/manager/SubscriptionManager.js +1 -1
  27. package/legacy/manager/applyManager.js +1 -1
  28. package/legacy/manager/devtoolsTypes.js +1 -1
  29. package/legacy/manager/index.js +1 -1
  30. package/legacy/middlewareTypes.js +1 -1
  31. package/legacy/next/index.js +1 -1
  32. package/legacy/state/RIC.js +1 -1
  33. package/legacy/state/reducer/createReducer.js +4 -4
  34. package/legacy/state/reducer/expireReducer.js +1 -1
  35. package/legacy/state/reducer/fetchReducer.js +1 -1
  36. package/legacy/state/reducer/invalidateReducer.js +5 -5
  37. package/legacy/state/reducer/setReducer.js +7 -7
  38. package/legacy/state/selectMeta.js +1 -1
  39. package/legacy/types.js +1 -1
  40. package/lib/actionTypes.js +1 -1
  41. package/lib/actions.d.ts +1 -1
  42. package/lib/actions.d.ts.map +1 -1
  43. package/lib/actions.js +1 -1
  44. package/lib/controller/AbortOptimistic.d.ts +3 -0
  45. package/lib/controller/AbortOptimistic.d.ts.map +1 -0
  46. package/lib/controller/AbortOptimistic.js +2 -0
  47. package/lib/controller/Controller.d.ts +36 -11
  48. package/lib/controller/Controller.d.ts.map +1 -1
  49. package/lib/controller/Controller.js +108 -76
  50. package/lib/controller/createExpireAll.js +1 -1
  51. package/lib/controller/createFetch.js +1 -1
  52. package/lib/controller/createInvalidate.js +1 -1
  53. package/lib/controller/createInvalidateAll.js +1 -1
  54. package/lib/controller/createOptimistic.js +1 -1
  55. package/lib/controller/createReset.js +1 -1
  56. package/lib/controller/createSet.js +1 -1
  57. package/lib/controller/createSubscription.js +1 -1
  58. package/lib/controller/ensurePojo.js +1 -1
  59. package/lib/controller/types.js +1 -1
  60. package/lib/index.d.ts +1 -1
  61. package/lib/index.d.ts.map +1 -1
  62. package/lib/index.js +1 -1
  63. package/lib/internal.d.ts +1 -1
  64. package/lib/internal.d.ts.map +1 -1
  65. package/lib/internal.js +2 -2
  66. package/lib/manager/ConnectionListener.js +1 -1
  67. package/lib/manager/DefaultConnectionListener.js +1 -1
  68. package/lib/manager/DevtoolsManager.js +1 -1
  69. package/lib/manager/LogoutManager.js +1 -1
  70. package/lib/manager/NetworkManager.js +1 -1
  71. package/lib/manager/PollingSubscription.js +1 -1
  72. package/lib/manager/SubscriptionManager.js +1 -1
  73. package/lib/manager/applyManager.js +1 -1
  74. package/lib/manager/devtoolsTypes.js +1 -1
  75. package/lib/manager/index.js +1 -1
  76. package/lib/middlewareTypes.js +1 -1
  77. package/lib/next/index.js +1 -1
  78. package/lib/state/RIC.js +1 -1
  79. package/lib/state/reducer/createReducer.js +4 -4
  80. package/lib/state/reducer/expireReducer.d.ts +1 -1
  81. package/lib/state/reducer/expireReducer.js +1 -1
  82. package/lib/state/reducer/fetchReducer.js +1 -1
  83. package/lib/state/reducer/invalidateReducer.d.ts +1 -1
  84. package/lib/state/reducer/invalidateReducer.js +6 -6
  85. package/lib/state/reducer/setReducer.d.ts.map +1 -1
  86. package/lib/state/reducer/setReducer.js +8 -8
  87. package/lib/state/selectMeta.js +1 -1
  88. package/lib/types.d.ts +1 -1
  89. package/lib/types.d.ts.map +1 -1
  90. package/lib/types.js +1 -1
  91. package/package.json +3 -3
  92. package/src/actions.ts +1 -1
  93. package/src/controller/AbortOptimistic.ts +1 -0
  94. package/src/controller/Controller.ts +185 -63
  95. package/src/controller/__tests__/Controller.ts +2 -4
  96. package/src/controller/__tests__/__snapshots__/get.ts.snap +120 -0
  97. package/src/controller/__tests__/get.ts +285 -0
  98. package/src/controller/__tests__/getResponse.ts +2 -2
  99. package/src/index.ts +2 -1
  100. package/src/internal.ts +1 -1
  101. package/src/state/__tests__/__snapshots__/reducer.ts.snap +4 -4
  102. package/src/state/__tests__/reducer.ts +25 -25
  103. package/src/state/reducer/createReducer.ts +3 -3
  104. package/src/state/reducer/invalidateReducer.ts +4 -4
  105. package/src/state/reducer/setReducer.ts +7 -6
  106. package/src/types.ts +3 -1
  107. package/ts3.4/actions.d.ts +1 -1
  108. package/ts3.4/controller/AbortOptimistic.d.ts +3 -0
  109. package/ts3.4/controller/Controller.d.ts +47 -19
  110. package/ts3.4/index.d.ts +1 -1
  111. package/ts3.4/internal.d.ts +1 -1
  112. package/ts3.4/state/reducer/expireReducer.d.ts +1 -1
  113. package/ts3.4/state/reducer/invalidateReducer.d.ts +1 -1
  114. package/ts3.4/types.d.ts +1 -1
@@ -28,8 +28,8 @@ export default function createReducer(controller: Controller): ReducerType {
28
28
  delete (state as any).entities[key]?.[pk];
29
29
  delete (state as any).entityMeta[key]?.[pk];
30
30
  });
31
- action.results.forEach(fetchKey => {
32
- delete (state as any).results[fetchKey];
31
+ action.endpoints.forEach(fetchKey => {
32
+ delete (state as any).endpoints[fetchKey];
33
33
  delete (state as any).meta[fetchKey];
34
34
  });
35
35
  return state;
@@ -62,7 +62,7 @@ export default function createReducer(controller: Controller): ReducerType {
62
62
  export const initialState: State<unknown> = {
63
63
  entities: {},
64
64
  indexes: {},
65
- results: {},
65
+ endpoints: {},
66
66
  meta: {},
67
67
  entityMeta: {},
68
68
  optimistic: [],
@@ -9,10 +9,10 @@ export function invalidateReducer(
9
9
  state: State<unknown>,
10
10
  action: InvalidateAction | InvalidateAllAction,
11
11
  ) {
12
- const results = { ...state.results };
12
+ const endpoints = { ...state.endpoints };
13
13
  const meta = { ...state.meta };
14
14
  const invalidateKey = (key: string) => {
15
- delete results[key];
15
+ delete endpoints[key];
16
16
  const itemMeta = {
17
17
  ...meta[key],
18
18
  expiresAt: 0,
@@ -24,7 +24,7 @@ export function invalidateReducer(
24
24
  if (action.type === INVALIDATE_TYPE) {
25
25
  invalidateKey(action.meta.key);
26
26
  } else {
27
- Object.keys(results).forEach(key => {
27
+ Object.keys(endpoints).forEach(key => {
28
28
  if (action.testKey(key)) {
29
29
  invalidateKey(key);
30
30
  }
@@ -33,7 +33,7 @@ export function invalidateReducer(
33
33
 
34
34
  return {
35
35
  ...state,
36
- results,
36
+ endpoints,
37
37
  meta,
38
38
  };
39
39
  }
@@ -1,6 +1,7 @@
1
1
  import { normalize } from '@data-client/normalizr';
2
2
 
3
3
  import { OPTIMISTIC_TYPE } from '../../actionTypes.js';
4
+ import AbortOptimistic from '../../controller/AbortOptimistic.js';
4
5
  import type Controller from '../../controller/Controller.js';
5
6
  import type { State, SetAction, OptimisticAction } from '../../types.js';
6
7
 
@@ -27,7 +28,7 @@ export function setReducer(
27
28
  );
28
29
  } catch (e: any) {
29
30
  // AbortOptimistic means 'do nothing', otherwise we count the exception as endpoint failure
30
- if (e.constructor?.name === 'AbortOptimistic') {
31
+ if (e.constructor === AbortOptimistic) {
31
32
  return state;
32
33
  }
33
34
  throw e;
@@ -44,15 +45,15 @@ export function setReducer(
44
45
  state.entityMeta,
45
46
  action.meta,
46
47
  );
47
- const results = {
48
- ...state.results,
48
+ const endpoints = {
49
+ ...state.endpoints,
49
50
  [action.meta.key]: result,
50
51
  };
51
52
  try {
52
53
  if (action.endpoint.update) {
53
54
  const updaters = action.endpoint.update(result, ...action.meta.args);
54
55
  Object.keys(updaters).forEach(key => {
55
- results[key] = updaters[key](results[key]);
56
+ endpoints[key] = updaters[key](endpoints[key]);
56
57
  });
57
58
  }
58
59
  // no reason to completely fail because of user-code error
@@ -66,7 +67,7 @@ export function setReducer(
66
67
  return {
67
68
  entities,
68
69
  indexes,
69
- results,
70
+ endpoints,
70
71
  entityMeta,
71
72
  meta: {
72
73
  ...state.meta,
@@ -79,7 +80,7 @@ export function setReducer(
79
80
  optimistic: filterOptimistic(state, action),
80
81
  lastReset: state.lastReset,
81
82
  };
82
- // reducer must update the state, so in case of processing errors we simply compute the results inline
83
+ // reducer must update the state, so in case of processing errors we simply compute the endpoints inline
83
84
  } catch (error: any) {
84
85
  if (typeof error === 'object') {
85
86
  error.message = `Error processing ${
package/src/types.ts CHANGED
@@ -20,7 +20,9 @@ export interface State<T> {
20
20
  readonly [entityKey: string]: { readonly [pk: string]: T } | undefined;
21
21
  };
22
22
  readonly indexes: NormalizedIndex;
23
- readonly results: { readonly [key: string]: unknown | PK[] | PK | undefined };
23
+ readonly endpoints: {
24
+ readonly [key: string]: unknown | PK[] | PK | undefined;
25
+ };
24
26
  readonly meta: {
25
27
  readonly [key: string]: {
26
28
  readonly date: number;
@@ -91,7 +91,7 @@ export interface GCAction {
91
91
  string,
92
92
  string
93
93
  ][];
94
- results: string[];
94
+ endpoints: string[];
95
95
  }
96
96
  export type ActionTypes = FetchAction | OptimisticAction | SetAction | SubscribeAction | UnsubscribeAction | InvalidateAction | InvalidateAllAction | ExpireAllAction | ResetAction | GCAction;
97
97
  export {};
@@ -0,0 +1,3 @@
1
+ export default class AbortOptimistic extends Error {
2
+ }
3
+ //# sourceMappingURL=AbortOptimistic.d.ts.map
@@ -1,5 +1,5 @@
1
- import { ErrorTypes, SnapshotInterface, DenormalizeCache, Schema, Denormalize } from '@data-client/normalizr';
2
- import { ExpiryStatus, EndpointInterface, FetchFunction, ResolveType, DenormalizeNullable } from '@data-client/normalizr';
1
+ import { ErrorTypes, SnapshotInterface, Schema, Denormalize, Queryable, SchemaArgs } from '@data-client/normalizr';
2
+ import { ExpiryStatus, EndpointInterface, FetchFunction, ResolveType, DenormalizeNullable, MemoCache } from '@data-client/normalizr';
3
3
  import { EndpointUpdateFunction } from './types.js';
4
4
  import { ActionTypes, State } from '../types.js';
5
5
  export type GenericDispatch = (value: any) => Promise<void>;
@@ -7,7 +7,7 @@ export type DataClientDispatch = (value: ActionTypes) => Promise<void>;
7
7
  interface ConstructorProps<D extends GenericDispatch = DataClientDispatch> {
8
8
  dispatch?: D;
9
9
  getState?: () => State<unknown>;
10
- globalCache?: DenormalizeCache;
10
+ memo?: Pick<MemoCache, 'denormalize' | 'query' | 'buildQueryKey'>;
11
11
  }
12
12
  /**
13
13
  * Imperative control of Reactive Data Client store
@@ -28,8 +28,11 @@ export default class Controller<D extends GenericDispatch = DataClientDispatch>
28
28
  * @see https://dataclient.io/docs/api/Controller#getState
29
29
  */
30
30
  readonly getState: () => State<unknown>;
31
- readonly globalCache: DenormalizeCache;
32
- constructor({ dispatch, getState, globalCache, }?: ConstructorProps<D>);
31
+ /**
32
+ * Singleton to maintain referential equality between calls
33
+ */
34
+ readonly memo: Pick<MemoCache, 'denormalize' | 'query' | 'buildQueryKey'>;
35
+ constructor({ dispatch, getState, memo, }?: ConstructorProps<D>);
33
36
  /*************** Action Dispatchers ***************/
34
37
  /**
35
38
  * Fetches the endpoint with given args, updating the Reactive Data Client cache with the response or error upon completion.
@@ -37,14 +40,14 @@ export default class Controller<D extends GenericDispatch = DataClientDispatch>
37
40
  */
38
41
  fetch: <E extends EndpointInterface<FetchFunction, Schema | undefined, boolean | undefined> & {
39
42
  update?: EndpointUpdateFunction<E> | undefined;
40
- }>(endpoint: E, ...args_0: Parameters<E>) => E["schema"] extends null | undefined ? ReturnType<E> : Promise<Denormalize<E["schema"]>>;
43
+ }>(endpoint: E, ...args_0: Parameters<E>) => E['schema'] extends undefined | null ? ReturnType<E> : Promise<Denormalize<E['schema']>>;
41
44
  /**
42
45
  * Fetches only if endpoint is considered 'stale'; otherwise returns undefined
43
46
  * @see https://dataclient.io/docs/api/Controller#fetchIfStale
44
47
  */
45
48
  fetchIfStale: <E extends EndpointInterface<FetchFunction, Schema | undefined, boolean | undefined> & {
46
49
  update?: EndpointUpdateFunction<E> | undefined;
47
- }>(endpoint: E, ...args_0: Parameters<E>) => E["schema"] extends null | undefined ? ReturnType<E> | ResolveType<E> : Denormalize<E["schema"]> | Promise<Denormalize<E["schema"]>>;
50
+ }>(endpoint: E, ...args_0: Parameters<E>) => E['schema'] extends undefined | null ? ReturnType<E> | ResolveType<E> : Promise<Denormalize<E['schema']>> | Denormalize<E['schema']>;
48
51
  /**
49
52
  * Forces refetching and suspense on useSuspense with the same Endpoint and parameters.
50
53
  * @see https://dataclient.io/docs/api/Controller#invalidate
@@ -121,18 +124,18 @@ export default class Controller<D extends GenericDispatch = DataClientDispatch>
121
124
  * @see https://dataclient.io/docs/api/Controller#subscribe
122
125
  */
123
126
  subscribe: <E extends EndpointInterface<FetchFunction, Schema | undefined, false | undefined>>(endpoint: E, ...args: readonly [
124
- null
125
- ] | readonly [
126
127
  ...Parameters<E>
128
+ ] | readonly [
129
+ null
127
130
  ]) => Promise<void>;
128
131
  /**
129
132
  * Marks completion of subscription to a given Endpoint.
130
133
  * @see https://dataclient.io/docs/api/Controller#unsubscribe
131
134
  */
132
135
  unsubscribe: <E extends EndpointInterface<FetchFunction, Schema | undefined, false | undefined>>(endpoint: E, ...args: readonly [
133
- null
134
- ] | readonly [
135
136
  ...Parameters<E>
137
+ ] | readonly [
138
+ null
136
139
  ]) => Promise<void>;
137
140
  /*************** More ***************/
138
141
  /**
@@ -156,18 +159,43 @@ export default class Controller<D extends GenericDispatch = DataClientDispatch>
156
159
  * Gets the (globally referentially stable) response for a given endpoint/args pair from state given.
157
160
  * @see https://dataclient.io/docs/api/Controller#getResponse
158
161
  */
159
- getResponse: <E extends Pick<EndpointInterface<FetchFunction, Schema | undefined, boolean | undefined>, "schema" | "key" | "invalidIfStale">, Args extends readonly [
160
- null
161
- ] | readonly [
162
- ...Parameters<E["key"]>
163
- ]>(endpoint: E, ...rest: [
164
- ...Args,
162
+ getResponse<E extends EndpointInterface>(endpoint: E, ...rest: readonly [
163
+ null,
164
+ State<unknown>
165
+ ]): {
166
+ data: DenormalizeNullable<E['schema']>;
167
+ expiryStatus: ExpiryStatus;
168
+ expiresAt: number;
169
+ };
170
+ getResponse<E extends EndpointInterface>(endpoint: E, ...rest: readonly [
171
+ ...Parameters<E>,
165
172
  State<unknown>
166
- ]) => {
167
- data: DenormalizeNullable<E["schema"]>;
173
+ ]): {
174
+ data: DenormalizeNullable<E['schema']>;
168
175
  expiryStatus: ExpiryStatus;
169
176
  expiresAt: number;
170
177
  };
178
+ getResponse<E extends Pick<EndpointInterface, 'key' | 'schema' | 'invalidIfStale'>>(endpoint: E, ...rest: readonly [
179
+ ...(readonly [
180
+ ...Parameters<E['key']>
181
+ ] | readonly [
182
+ null
183
+ ]),
184
+ State<unknown>
185
+ ]): {
186
+ data: DenormalizeNullable<E['schema']>;
187
+ expiryStatus: ExpiryStatus;
188
+ expiresAt: number;
189
+ };
190
+ /**
191
+ * Queries the store for a Querable schema
192
+ * @see https://dataclient.io/docs/api/Controller#get
193
+ */
194
+ get<S extends Queryable>(schema: S, ...rest: readonly [
195
+ ...SchemaArgs<S>,
196
+ Pick<State<unknown>, 'entities' | 'entityMeta'>
197
+ ]): DenormalizeNullable<S> | undefined;
198
+ private getSchemaResponse;
171
199
  }
172
200
  export { ErrorTypes };
173
201
  //# sourceMappingURL=Controller.d.ts.map
package/ts3.4/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as __INTERNAL__1 from './internal.js';
2
2
  export { __INTERNAL__1 as __INTERNAL__ };
3
- export { NetworkError, UnknownError, ErrorTypes, Schema, EndpointInterface, EntityInterface, ResolveType, DenormalizeCache, DenormalizeNullable, Denormalize, Normalize, NormalizeNullable, FetchFunction, EndpointExtraOptions, } from '@data-client/normalizr';
3
+ export { NetworkError, UnknownError, ErrorTypes, Schema, EndpointInterface, EntityInterface, ResolveType, DenormalizeNullable, Denormalize, Normalize, NormalizeNullable, FetchFunction, EndpointExtraOptions, Queryable, SchemaArgs, } from '@data-client/normalizr';
4
4
  export { ExpiryStatus } from '@data-client/normalizr';
5
5
  export { default as NetworkManager, ResetError, } from './manager/NetworkManager.js';
6
6
  export { default as createReducer, initialState, } from './state/reducer/createReducer.js';
@@ -1,4 +1,4 @@
1
- export { inferResults, INVALID } from '@data-client/normalizr';
1
+ export { MemoCache, INVALID } from '@data-client/normalizr';
2
2
  export { default as RIC } from './state/RIC.js';
3
3
  export { initialState } from './state/reducer/createReducer.js';
4
4
  //# sourceMappingURL=internal.d.ts.map
@@ -16,7 +16,7 @@ export declare function expireReducer(state: State<unknown>, action: ExpireAllAc
16
16
  } | undefined;
17
17
  };
18
18
  indexes: import("packages/normalizr/lib/interface.js").NormalizedIndex;
19
- results: {
19
+ endpoints: {
20
20
  readonly [key: string]: unknown;
21
21
  };
22
22
  entityMeta: {
@@ -1,6 +1,6 @@
1
1
  import { State, InvalidateAllAction, InvalidateAction } from '../../types.js';
2
2
  export declare function invalidateReducer(state: State<unknown>, action: InvalidateAction | InvalidateAllAction): {
3
- results: {
3
+ endpoints: {
4
4
  [x: string]: unknown;
5
5
  };
6
6
  meta: {
package/ts3.4/types.d.ts CHANGED
@@ -14,7 +14,7 @@ export interface State<T> {
14
14
  } | undefined;
15
15
  };
16
16
  readonly indexes: NormalizedIndex;
17
- readonly results: {
17
+ readonly endpoints: {
18
18
  readonly [key: string]: unknown | PK[] | PK | undefined;
19
19
  };
20
20
  readonly meta: {