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

remove unnecessary gate #439

Merged
merged 2 commits into from
Feb 4, 2021
Merged
Changes from 1 commit
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
165 changes: 78 additions & 87 deletions javascript/stimulus_reflex.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,117 +491,108 @@ const initialize = (application, initializeOptions = {}) => {
Debug.set(!!debug)
}

if (!document.stimulusReflexInitialized) {
document.stimulusReflexInitialized = true

window.addEventListener('load', () => {
setupDeclarativeReflexes()
const observer = new MutationObserver(setupDeclarativeReflexes)
observer.observe(document.documentElement, {
attributes: true,
childList: true,
subtree: true
})
window.addEventListener('load', () => {
setupDeclarativeReflexes()
const observer = new MutationObserver(setupDeclarativeReflexes)
observer.observe(document.documentElement, {
attributes: true,
childList: true,
subtree: 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

reflex.pendingOperations--
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

if (reflex.pendingOperations > 0) return
reflex.pendingOperations--

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)
setTimeout(() =>
dispatchLifecycleEvent(
'success',
reflexElement,
controllerElement,
reflexId
)
)
}

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
document.addEventListener('cable-ready:before-inner-html', beforeDOMUpdate)
document.addEventListener('cable-ready:before-morph', beforeDOMUpdate)

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)
setTimeout(() =>
dispatchLifecycleEvent(
'finalize',
reflexElement,
controllerElement,
reflexId
)
)
}

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 }
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 || {}
controllerElement.reflexError = controllerElement.reflexError || {}

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

promise[subject === 'error' ? 'reject' : 'resolve']({
data: promise.data,
element: reflexElement,
event,
toString: () => 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)
})

export default {
initialize,
Expand Down