Skip to content

Commit

Permalink
remove unnecessary gate (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
leastbad authored Feb 4, 2021
1 parent 29ba27c commit ba5a9ca
Showing 1 changed file with 72 additions and 82 deletions.
154 changes: 72 additions & 82 deletions javascript/stimulus_reflex.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,108 +497,98 @@ const initialize = (application, initializeOptions = {}) => {
})
}

if (!document.stimulusReflexInitialized) {
document.stimulusReflexInitialized = true
const beforeDOMUpdate = event => {
const { stimulusReflex } = event.detail || {}
if (!stimulusReflex) return
const { reflexId, xpathElement, xpathController } = stimulusReflex
const controllerElement = XPathToElement(xpathController)
const reflexElement = XPathToElement(xpathElement)
const reflex = reflexes[reflexId]
const promise = reflex.promise

const beforeDOMUpdate = event => {
const { stimulusReflex } = event.detail || {}
if (!stimulusReflex) return
const { reflexId, xpathElement, xpathController } = stimulusReflex
const controllerElement = XPathToElement(xpathController)
const reflexElement = XPathToElement(xpathElement)
const reflex = reflexes[reflexId]
const promise = reflex.promise
reflex.pendingOperations--

reflex.pendingOperations--

if (reflex.pendingOperations > 0) return

if (!stimulusReflex.resolveLate)
setTimeout(() =>
promise.resolve({ element: reflexElement, event, data: promise.data })
)
if (reflex.pendingOperations > 0) return

if (!stimulusReflex.resolveLate)
setTimeout(() =>
dispatchLifecycleEvent(
'success',
reflexElement,
controllerElement,
reflexId
)
promise.resolve({ element: reflexElement, event, data: promise.data })
)
}

document.addEventListener('cable-ready:before-inner-html', beforeDOMUpdate)
document.addEventListener('cable-ready:before-morph', beforeDOMUpdate)

const afterDOMUpdate = event => {
const { stimulusReflex } = event.detail || {}
if (!stimulusReflex) return
const { reflexId, xpathElement, xpathController } = stimulusReflex
const controllerElement = XPathToElement(xpathController)
const reflexElement = XPathToElement(xpathElement)
const reflex = reflexes[reflexId]
const promise = reflex.promise
setTimeout(() =>
dispatchLifecycleEvent(
'success',
reflexElement,
controllerElement,
reflexId
)
)
}

reflex.completedOperations++
const afterDOMUpdate = event => {
const { stimulusReflex } = event.detail || {}
if (!stimulusReflex) return
const { reflexId, xpathElement, xpathController } = stimulusReflex
const controllerElement = XPathToElement(xpathController)
const reflexElement = XPathToElement(xpathElement)
const reflex = reflexes[reflexId]
const promise = reflex.promise

if (Debug.enabled) Log.success(event)
reflex.completedOperations++

if (reflex.completedOperations < reflex.totalOperations) return
if (Debug.enabled) Log.success(event)

if (stimulusReflex.resolveLate)
setTimeout(() =>
promise.resolve({ element: reflexElement, event, data: promise.data })
)
if (reflex.completedOperations < reflex.totalOperations) return

if (stimulusReflex.resolveLate)
setTimeout(() =>
dispatchLifecycleEvent(
'finalize',
reflexElement,
controllerElement,
reflexId
)
promise.resolve({ element: reflexElement, event, data: promise.data })
)
}

document.addEventListener('cable-ready:after-inner-html', afterDOMUpdate)
document.addEventListener('cable-ready:after-morph', afterDOMUpdate)

document.addEventListener('stimulus-reflex:server-message', event => {
const { reflexId, serverMessage, xpathController, xpathElement } =
event.detail.stimulusReflex || {}
const { subject, body } = serverMessage
const controllerElement = XPathToElement(xpathController)
const reflexElement = XPathToElement(xpathElement)
const promise = reflexes[reflexId].promise
const subjects = { error: true, halted: true, nothing: true, success: true }

controllerElement.reflexError = controllerElement.reflexError || {}

if (controllerElement && subject === 'error')
controllerElement.reflexError[reflexId] = body
setTimeout(() =>
dispatchLifecycleEvent(
'finalize',
reflexElement,
controllerElement,
reflexId
)
)
}

promise[subject === 'error' ? 'reject' : 'resolve']({
data: promise.data,
element: reflexElement,
event,
toString: () => body
})
const serverMessage = event => {
const { reflexId, serverMessage, xpathController, xpathElement } =
event.detail.stimulusReflex || {}
const { subject, body } = serverMessage
const controllerElement = XPathToElement(xpathController)
const reflexElement = XPathToElement(xpathElement)
const promise = reflexes[reflexId].promise
const subjects = { error: true, halted: true, nothing: true, success: true }

controllerElement.reflexError = controllerElement.reflexError || {}

if (controllerElement && subject === 'error')
controllerElement.reflexError[reflexId] = body

promise[subject === 'error' ? 'reject' : 'resolve']({
data: promise.data,
element: reflexElement,
event,
toString: () => body
})

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

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

if (subjects[subject])
dispatchLifecycleEvent(
subject,
reflexElement,
controllerElement,
reflexId
)
})
if (subjects[subject])
dispatchLifecycleEvent(subject, reflexElement, controllerElement, reflexId)
}

document.addEventListener('stimulus-reflex:server-message', serverMessage)
document.addEventListener('cable-ready:before-inner-html', beforeDOMUpdate)
document.addEventListener('cable-ready:before-morph', beforeDOMUpdate)
document.addEventListener('cable-ready:after-inner-html', afterDOMUpdate)
document.addEventListener('cable-ready:after-morph', afterDOMUpdate)
window.addEventListener('load', setupDeclarativeReflexes)

export default {
Expand Down

0 comments on commit ba5a9ca

Please sign in to comment.