Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflex return payloads for events and callbacks #477

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions javascript/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const beforeDOMUpdate = event => {
const reflexElement = XPathToElement(xpathElement)
const reflex = reflexes[reflexId]
const promise = reflex.promise
const payload = event.detail.payload

reflex.pendingOperations--

Expand All @@ -23,7 +24,7 @@ export const beforeDOMUpdate = event => {
element: reflexElement,
event,
data: promise.data,
payload: event.detail.payload
payload
})
)

Expand All @@ -32,7 +33,8 @@ export const beforeDOMUpdate = event => {
'success',
reflexElement,
controllerElement,
reflexId
reflexId,
payload
)
)
}
Expand All @@ -45,6 +47,7 @@ export const afterDOMUpdate = event => {
const reflexElement = XPathToElement(xpathElement)
const reflex = reflexes[reflexId]
const promise = reflex.promise
const payload = event.detail.payload

reflex.completedOperations++

Expand All @@ -58,7 +61,7 @@ export const afterDOMUpdate = event => {
element: reflexElement,
event,
data: promise.data,
payload: event.detail.payload
payload
})
)

Expand All @@ -67,7 +70,8 @@ export const afterDOMUpdate = event => {
'finalize',
reflexElement,
controllerElement,
reflexId
reflexId,
payload
)
)
}
Expand All @@ -80,6 +84,7 @@ export const serverMessage = event => {
const reflexElement = XPathToElement(xpathElement)
const promise = reflexes[reflexId].promise
const subjects = { error: true, halted: true, nothing: true, success: true }
const payload = event.detail.payload

controllerElement.reflexError = controllerElement.reflexError || {}

Expand All @@ -91,13 +96,19 @@ export const serverMessage = event => {
element: reflexElement,
event,
toString: () => body,
payload: event.detail.payload
payload
})

reflexes[reflexId].finalStage = subject === 'halted' ? 'halted' : 'after'

if (Debug.enabled) Log[subject === 'error' ? 'error' : 'success'](event)

if (subjects[subject])
dispatchLifecycleEvent(subject, reflexElement, controllerElement, reflexId)
dispatchLifecycleEvent(
subject,
reflexElement,
controllerElement,
reflexId,
payload
)
}
45 changes: 30 additions & 15 deletions javascript/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const invokeLifecycleMethod = (
stage,
reflexElement,
controllerElement,
reflexId
reflexId,
payload
) => {
if (!controllerElement || !controllerElement.reflexData[reflexId]) return

Expand Down Expand Up @@ -50,7 +51,8 @@ const invokeLifecycleMethod = (
reflexElement,
reflex,
controllerElement.reflexError[reflexId],
reflexId
reflexId,
payload
)
}

Expand All @@ -60,7 +62,8 @@ const invokeLifecycleMethod = (
reflexElement,
reflex,
controllerElement.reflexError[reflexId],
reflexId
reflexId,
payload
)
}

Expand All @@ -79,7 +82,8 @@ document.addEventListener(
'before',
event.detail.element,
event.detail.controller.element,
event.detail.reflexId
event.detail.reflexId,
event.detail.payload
),
true
)
Expand All @@ -91,13 +95,15 @@ document.addEventListener(
'success',
event.detail.element,
event.detail.controller.element,
event.detail.reflexId
event.detail.reflexId,
event.detail.payload
)
dispatchLifecycleEvent(
'after',
event.detail.element,
event.detail.controller.element,
event.detail.reflexId
event.detail.reflexId,
event.detail.payload
)
},
true
Expand All @@ -110,13 +116,15 @@ document.addEventListener(
'success',
event.detail.element,
event.detail.controller.element,
event.detail.reflexId
event.detail.reflexId,
event.detail.payload
)
dispatchLifecycleEvent(
'after',
event.detail.element,
event.detail.controller.element,
event.detail.reflexId
event.detail.reflexId,
event.detail.payload
)
},
true
Expand All @@ -129,13 +137,15 @@ document.addEventListener(
'error',
event.detail.element,
event.detail.controller.element,
event.detail.reflexId
event.detail.reflexId,
event.detail.payload
)
dispatchLifecycleEvent(
'after',
event.detail.element,
event.detail.controller.element,
event.detail.reflexId
event.detail.reflexId,
event.detail.payload
)
},
true
Expand All @@ -148,7 +158,8 @@ document.addEventListener(
'halted',
event.detail.element,
event.detail.controller.element,
event.detail.reflexId
event.detail.reflexId,
event.detail.payload
),
true
)
Expand All @@ -160,7 +171,8 @@ document.addEventListener(
'after',
event.detail.element,
event.detail.controller.element,
event.detail.reflexId
event.detail.reflexId,
event.detail.payload
),
true
)
Expand All @@ -172,7 +184,8 @@ document.addEventListener(
'finalize',
event.detail.element,
event.detail.controller.element,
event.detail.reflexId
event.detail.reflexId,
event.detail.payload
),
true
)
Expand All @@ -197,7 +210,8 @@ export const dispatchLifecycleEvent = (
stage,
reflexElement,
controllerElement,
reflexId
reflexId,
payload
) => {
if (!controllerElement) {
if (Debug.enabled && !reflexes[reflexId].warned) {
Expand Down Expand Up @@ -230,7 +244,8 @@ export const dispatchLifecycleEvent = (
reflex: target,
controller,
reflexId,
element: reflexElement
element: reflexElement,
payload
}

controllerElement.dispatchEvent(
Expand Down
8 changes: 6 additions & 2 deletions javascript/reflexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ export const performOperations = data => {
reflexData = operation[0].detail
? operation[0].detail.stimulusReflex
: operation[0].stimulusReflex
reflexData.payload = operation[0].detail
? operation[0].detail.payload
: operation[0].payload
}
}
})

if (reflexData) {
const { reflexId } = reflexData
const { reflexId, payload } = reflexData

if (!reflexes[reflexId] && isolationMode.disabled) {
const controllerElement = XPathToElement(reflexData.xpathController)
Expand All @@ -84,7 +87,8 @@ export const performOperations = data => {
'before',
reflexElement,
controllerElement,
reflexId
reflexId,
payload
)
registerReflex(reflexData)
}
Expand Down