-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: Simplify action shapes (#3143)
- Loading branch information
Showing
67 changed files
with
670 additions
and
605 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@data-client/core': minor | ||
'@data-client/react': patch | ||
--- | ||
|
||
action.meta.args -> action.args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
'@data-client/core': minor | ||
--- | ||
|
||
Add `actions` export | ||
|
||
`actions` is a namespace for all action creators. It is typically | ||
preferred to use [Controller's](https://dataclient.io/docs/api/Controller) type-safe dispatch methods, as | ||
members of this namespace could have breaking changes in a minor release. | ||
|
||
```ts | ||
import { actions, type Manager, type Middleware } from '@data-client/core'; | ||
|
||
export default class MyManager implements Manager { | ||
getMiddleware = (): Middleware => controller => next => { | ||
const todo = { id: '5', title: 'my first todo' }; | ||
|
||
// These do the same thing | ||
controller.dispatch( | ||
actions.createSet(Todo, { args: [{ id: todo.id }], value: todo }), | ||
); | ||
// This is simpler; type-enforced; and will only change in major versions | ||
controller.set(Todo, { id: todo.id }, todo); | ||
|
||
return async action => next(action); | ||
}; | ||
|
||
cleanup() {} | ||
} | ||
``` | ||
|
||
BREAKING CHANGE: Removed `createFetch`, `createSet`, `createSetResponse` from export. Use action.createFetch instead. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@data-client/core': minor | ||
'@data-client/react': patch | ||
--- | ||
|
||
action.meta.key -> action.key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ class BaseEntity extends Entity { | |
id = 0; | ||
|
||
pk() { | ||
return `${this.id}`; | ||
return this.id; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { EXPIREALL_TYPE } from '../../actionTypes.js'; | ||
import type { ExpireAllAction } from '../../types.js'; | ||
|
||
export function createExpireAll( | ||
testKey: (key: string) => boolean, | ||
): ExpireAllAction { | ||
return { | ||
type: EXPIREALL_TYPE, | ||
testKey, | ||
}; | ||
} |
19 changes: 9 additions & 10 deletions
19
packages/core/src/controller/createFetch.ts → ...ore/src/controller/actions/createFetch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,35 @@ | ||
import type { EndpointInterface, NI } from '@data-client/normalizr'; | ||
|
||
import { EndpointUpdateFunction } from './types.js'; | ||
import { FETCH_TYPE } from '../actionTypes.js'; | ||
import type { FetchAction, FetchMeta } from '../types.js'; | ||
import { FETCH_TYPE } from '../../actionTypes.js'; | ||
import type { FetchAction, FetchMeta } from '../../types.js'; | ||
import { EndpointUpdateFunction } from '../types.js'; | ||
|
||
/** | ||
* Requesting a fetch to begin | ||
*/ | ||
export default function createFetch< | ||
export function createFetch< | ||
E extends EndpointInterface & { update?: EndpointUpdateFunction<E> }, | ||
>( | ||
endpoint: E, | ||
{ args }: { args: readonly [...Parameters<E>] }, | ||
): FetchAction<E> { | ||
const key = endpoint.key(...args); | ||
let resolve: (value?: any | PromiseLike<any>) => void = 0 as any; | ||
let reject: (reason?: any) => void = 0 as any; | ||
const promise = new Promise<any>((a, b) => { | ||
[resolve, reject] = [a, b]; | ||
}); | ||
const meta: FetchMeta<typeof args> = { | ||
args, | ||
key, | ||
const meta: FetchMeta = { | ||
fetchedAt: Date.now(), | ||
resolve, | ||
reject, | ||
promise, | ||
fetchedAt: Date.now(), | ||
}; | ||
|
||
return { | ||
type: FETCH_TYPE, | ||
meta, | ||
endpoint, | ||
args, | ||
key: endpoint.key(...args), | ||
meta, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { EndpointInterface } from '@data-client/normalizr'; | ||
|
||
import { INVALIDATE_TYPE } from '../../actionTypes.js'; | ||
import type { InvalidateAction } from '../../types.js'; | ||
|
||
export function createInvalidate<E extends EndpointInterface>( | ||
endpoint: E, | ||
{ args }: { args: readonly [...Parameters<E>] }, | ||
): InvalidateAction { | ||
return { | ||
type: INVALIDATE_TYPE, | ||
key: endpoint.key(...args), | ||
}; | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/core/src/controller/actions/createInvalidateAll.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { INVALIDATEALL_TYPE } from '../../actionTypes.js'; | ||
import type { InvalidateAllAction } from '../../types.js'; | ||
|
||
export function createInvalidateAll( | ||
testKey: (key: string) => boolean, | ||
): InvalidateAllAction { | ||
return { | ||
type: INVALIDATEALL_TYPE, | ||
testKey, | ||
}; | ||
} |
Oops, something went wrong.