Skip to content

Commit

Permalink
Merge pull request #11 from sevenwestmedia/feat/ExposeTheCacheKeyToUser
Browse files Browse the repository at this point in the history
feat: Expose paramsCacheKey to consumers
  • Loading branch information
JakeGinnivan authored Sep 3, 2019
2 parents 5234f16 + 5e653a6 commit 8c68b2e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/data-loader-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { createTypedDataLoader, DataLoaderActions } from './data-loader'
import { LoaderStatus } from './data-loader-state'

export type LoadResource<TData, TResourceParameters, TInternalState, TGlobalParameters> = (
params: { resourceType: string } & TResourceParameters & TInternalState & TGlobalParameters,
params: {
resourceType: string
/** This is the key the data loader uses for looking up the data */
paramsCacheKey: string
} & TResourceParameters &
TInternalState &
TGlobalParameters,
) => Promise<TData> | TData

interface Resources {
Expand Down
6 changes: 5 additions & 1 deletion src/data-loader-store-and-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export interface ActionResult<TInternalState> {

export interface LoadParams {
resourceType: string

/** This is the key the data loader uses for looking up the data */
paramsCacheKey: string

[param: string]: any
}

Expand Down Expand Up @@ -168,7 +172,7 @@ export class DataLoaderStoreAndLoader {

let result: any
try {
result = this.performLoad(paramsObject)
result = this.performLoad({ ...paramsObject, paramsCacheKey: paramsObjectHash })
} catch (err) {
this.updateParamsHashState(paramsObjectHash, {
status: LoaderStatus.Idle,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/data-loader.params.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Object {
"passedParams": Object {
"bar": 1,
"id": "testKey",
"paramsCacheKey": "7a4496e0",
"resourceType": "testDataType",
},
"renderActions": Object {
Expand Down
2 changes: 1 addition & 1 deletion tests/data-loader.params.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('data-loader', () => {

await sut.testDataPromise.resolve({ result: 'Test' })

expect(sut.passedParams).toEqual(args)
expect(sut.passedParams).toEqual({ ...args, paramsCacheKey: '7a4496e0' })
sut.assertState()
})

Expand Down

0 comments on commit 8c68b2e

Please sign in to comment.