Skip to content

Commit

Permalink
feat: access payload in operation callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Aug 28, 2022
1 parent 5a14b27 commit a830b2c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-cougars-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hydrofoil/shell-operations": patch
---

Pass the original payload to operation completion callback
3 changes: 3 additions & 0 deletions packages/operations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createModel } from '@captaincodeman/rdx'
import { Model } from '@hydrofoil/shell-core/store'
import { Term } from '@rdfjs/types'
import TermMap from '@rdf-esm/term-map'
import { GraphPointer } from 'clownface'
import { effects } from './lib/effects.js'
import reducers from './lib/reducers.js'

Expand All @@ -26,6 +27,8 @@ export interface OperationsState {
operations: Map<Term, UndefinedResult | SuccessResult | FailureResult>
}

export type Payload = GraphPointer

export const operation = createModel({
state: <OperationsState>{
operations: new TermMap(),
Expand Down
7 changes: 5 additions & 2 deletions packages/operations/lib/completed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import type { ResponseWrapper } from 'alcaeus/ResponseWrapper'
import type { ResourceRepresentation } from 'alcaeus/ResourceRepresentation'
import { hydra } from '@tpluscode/rdf-ns-builders'
import type { Error } from '@rdfine/hydra/lib/Error'
import type { Payload } from '../index'

export interface OperationCompleted {
payload: Payload
operation: RuntimeOperation
response?: ResponseWrapper
representation?: ResourceRepresentation
Expand All @@ -14,14 +16,15 @@ export interface OperationCompleted {
export function completed(store: Store) {
const dispatch = store.getDispatch()

return function ({ operation, response, representation }: OperationCompleted) {
return function ({ payload, operation, response, representation }: OperationCompleted) {
if (!response?.xhr.ok) {
const error = representation?.ofType<Error>(hydra.Error).shift()
dispatch.operation.failed({ operation, response, error })
dispatch.operation.failed({ payload, operation, response, error })
return
}

dispatch.operation.succeeded({
payload,
operation,
response,
representation,
Expand Down
6 changes: 3 additions & 3 deletions packages/operations/lib/invoke.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { RuntimeOperation } from 'alcaeus'
import { GraphPointer } from 'clownface'
import { turtle } from '@tpluscode/rdf-string'
import type { Store } from '@hydrofoil/shell'
import type { Payload } from '../index'

export interface InvokeOperation {
operation: RuntimeOperation
payload: GraphPointer
payload: Payload
}

export function invoke(store: Store) {
Expand All @@ -18,6 +18,6 @@ export function invoke(store: Store) {
'content-type': 'text/turtle',
})

dispatch.operation.completed({ operation, ...response })
dispatch.operation.completed({ payload, operation, ...response })
}
}
3 changes: 2 additions & 1 deletion packages/operations/lib/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import type { RuntimeOperation } from 'alcaeus'
import type { Error } from '@rdfine/hydra/lib/Error'
import type { ResponseWrapper } from 'alcaeus/ResponseWrapper'
import type { ResourceRepresentation } from 'alcaeus/ResourceRepresentation'
import type { FailureResult, OperationsState, SuccessResult } from '..'
import type { FailureResult, OperationsState, Payload, SuccessResult } from '..'

export interface OperationCompleted {
payload: Payload
operation: RuntimeOperation
}

Expand Down

0 comments on commit a830b2c

Please sign in to comment.