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

Setup mutation aware declarative reflexes #197

Merged
merged 4 commits into from
May 19, 2020
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
28 changes: 14 additions & 14 deletions javascript/stimulus_reflex.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defaultSchema } from './schema'
import { getConsumer } from './consumer'
import { dispatchLifecycleEvent } from './lifecycle'
import { allReflexControllers } from './controllers'
import { uuidv4 } from './utils'
import { uuidv4, debounce } from './utils'
import Log from './log'
import {
attributeValue,
Expand Down Expand Up @@ -188,7 +188,7 @@ class StimulusReflexController extends Controller {
// Sets up declarative reflex behavior.
// Any elements that define data-reflex will automatically be wired up with the default StimulusReflexController.
//
const setupDeclarativeReflexes = () => {
const setupDeclarativeReflexes = debounce(() => {
document
.querySelectorAll(`[${stimulusApplication.schema.reflexAttribute}]`)
.forEach(element => {
Expand Down Expand Up @@ -231,7 +231,7 @@ const setupDeclarativeReflexes = () => {
actionValue
)
})
}
}, 20)

// compute the DOM element(s) which will be the morph root
// use the data-reflex-root attribute on the reflex or the controller
Expand Down Expand Up @@ -284,16 +284,17 @@ const initialize = (application, initializeOptions = {}) => {

if (!document.stimulusReflexInitialized) {
document.stimulusReflexInitialized = true
window.addEventListener('load', () => setTimeout(setupDeclarativeReflexes, 1))
document.addEventListener('turbolinks:load', () =>
setTimeout(setupDeclarativeReflexes, 1)
)
document.addEventListener('cable-ready:after-morph', () =>
setTimeout(setupDeclarativeReflexes, 1)
)
document.addEventListener('ajax:complete', () =>
setTimeout(setupDeclarativeReflexes, 1)
)

window.addEventListener('load', () => {
setupDeclarativeReflexes()
const observer = new MutationObserver(setupDeclarativeReflexes)
observer.observe(document.documentElement, {
attributes: true,
childList: true,
subtree: true
})
})

// Trigger success and after lifecycle methods from before-morph to ensure we can find a reference
// to the source element in case it gets removed from the DOM via morph.
// This is safe because the server side reflex completed successfully.
Expand Down Expand Up @@ -374,7 +375,6 @@ if (!document.stimulusReflexInitialized) {
export default {
initialize,
register,
setupDeclarativeReflexes,
get debug () {
return debugging
},
Expand Down
11 changes: 11 additions & 0 deletions javascript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ export const camelize = (value, uppercaseFirstLetter = true) => {

return value
}

export const debounce = (callback, delay = 250) => {
let timeoutId
return (...args) => {
clearTimeout(timeoutId)
timeoutId = setTimeout(() => {
timeoutId = null
callback(...args)
}, delay)
}
}