-
-
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.
- Loading branch information
Showing
55 changed files
with
600 additions
and
556 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
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, | ||
}; | ||
} |
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,13 @@ | ||
import type { ActionMeta } from '../../actions.js'; | ||
|
||
export function createMeta( | ||
expiryLength: number, | ||
fetchedAt?: number, | ||
): ActionMeta { | ||
const now = Date.now(); | ||
return { | ||
fetchedAt: fetchedAt ?? now, | ||
date: now, | ||
expiresAt: now + expiryLength, | ||
}; | ||
} |
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 @@ | ||
import type { EndpointInterface } from '@data-client/normalizr'; | ||
|
||
import { createMeta } from './createMeta.js'; | ||
import { OPTIMISTIC_TYPE } from '../../actionTypes.js'; | ||
import type { OptimisticAction } from '../../types.js'; | ||
import type { EndpointUpdateFunction } from '../types.js'; | ||
|
||
export function createOptimistic< | ||
E extends EndpointInterface & { | ||
update?: EndpointUpdateFunction<E>; | ||
}, | ||
>( | ||
endpoint: E, | ||
args: readonly [...Parameters<E>], | ||
fetchedAt: number, | ||
): OptimisticAction<E> { | ||
/* istanbul ignore next */ | ||
if ( | ||
process.env.NODE_ENV === 'development' && | ||
(endpoint.dataExpiryLength ?? 0) < 0 | ||
) { | ||
throw new Error('Negative expiry length are not allowed.'); | ||
} | ||
|
||
return { | ||
type: OPTIMISTIC_TYPE, | ||
endpoint, | ||
args, | ||
key: endpoint.key(...args), | ||
meta: createMeta(endpoint.dataExpiryLength ?? 60000, fetchedAt), | ||
}; | ||
} |
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,9 @@ | ||
import { RESET_TYPE } from '../../actionTypes.js'; | ||
import type { ResetAction } from '../../types.js'; | ||
|
||
export function createReset(): ResetAction { | ||
return { | ||
type: RESET_TYPE, | ||
date: Date.now(), | ||
}; | ||
} |
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 @@ | ||
import type { | ||
Denormalize, | ||
Queryable, | ||
SchemaArgs, | ||
} from '@data-client/normalizr'; | ||
|
||
import { createMeta } from './createMeta.js'; | ||
import { SET_TYPE } from '../../actionTypes.js'; | ||
import type { SetAction } from '../../types.js'; | ||
import ensurePojo from '../ensurePojo.js'; | ||
|
||
export function createSet<S extends Queryable>( | ||
schema: S, | ||
{ | ||
args, | ||
fetchedAt, | ||
value, | ||
}: { | ||
args: readonly [...SchemaArgs<S>]; | ||
value: {} | ((previousValue: Denormalize<S>) => {}); | ||
fetchedAt?: number; | ||
}, | ||
): SetAction<S> { | ||
const action: SetAction<S> = { | ||
type: SET_TYPE, | ||
schema, | ||
value, | ||
args: args.map(ensurePojo) as SchemaArgs<S>, | ||
meta: createMeta(60000, fetchedAt), | ||
}; | ||
return action; | ||
} |
Oops, something went wrong.