before -> operate -> after#110
Conversation
|
Late thought: think of |
|
I like where this is going, and want to float an idea to see if we can push the concept further. What if we introduce a promise to facilitate the const operate = (element, operation, callee, callback) => {
before(element, callee, operation)
if (!operation.cancel) {
if (operation.delay) {
new Promise((resolve, reject) => {
setTimeout(() => resolve(callback()), operation.delay)
}).then(() => after(element, callee, operation))
} else {
callback()
after(element, callee, operation)
}
}
}The operations could then further simplify to something along these lines. append: (operation, callee) => {
processElements(operation, element => {
operate(element, operation, callee, () => {
const { html, focusSelector } = operation
element.insertAdjacentHTML('beforeend', html || '')
assignFocus(focusSelector)
})
})
}I also wonder if passing append: (operation) => {
processElements(operation, element => {
operate('append', element, operation, () => { ... |
|
Hey, my head hasn't been in this for a few months but:
|
That's a very good reason to keep I think we may be able to avoid passing |
|
I'm excited to make this even cleaner. Passing in the |
|
Just to think for a moment about your proposal to move I'm also in the process of moving That would mean that we'd add events to operations that really don't make sense to have events, most specifically |
hopsoft
left a comment
There was a problem hiding this comment.
I tried a few things that eliminated the need to pass callee around but haven't been satisfied with any of them. I think we can merge and perhaps continue exploring ways to make this cleaner in the future.
Building on #109, this PR aims to standardize the way operations are defined by introducing a light DSL that can be used in a consistent pattern.
Previously, we call dispatch directly to emit before and after events, which clutters the code, is non-DRY and leaves open the possibility of typos. Instead, I would like to switch to
beforeandafterfunctions which are intended to immediately preceed and immediately follow theoperateclosure.What is the
operateclosure!?I noticed that a number of features being added (or proposed) was adding code to every one of the operations. Again, this is a smell. Instead, I created
operateto function as a pipeline for modifying all of the operations. This allowed me to move out thecancel: truecheck from each operation, and introduce a new option,delay, which allows developers to stagger execution of individual operations. This can be useful for UI/UX reasons, as well as giving 3rd-party components a few ms to "catch up" if necessary.operatecould someday be pluggable, as well, although I don't attempt it in this PR.The cool thing about the
processElementsandoperatefunctions is that developers building custom events can import them and use them, or not, as they wish. I already document usingprocessElementsin today's documentation... this just picks up where it left off.