-
Notifications
You must be signed in to change notification settings - Fork 126
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
set-then and execute #363
Open
betocantu93
wants to merge
4
commits into
DockYard:master
Choose a base branch
from
betocantu93:add-mut-then-helper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
set-then and execute #363
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import isPromise from '../utils/is-promise'; | ||
|
||
export function execute(actions = []) { | ||
return function() { | ||
let invoke = function(acc, curr) { | ||
if (isPromise(acc)) { | ||
return acc.then(() => curr()); | ||
} | ||
|
||
return curr(); | ||
}; | ||
|
||
return actions.reduce((acc, curr, idx) => { | ||
if (idx === 0) { | ||
return curr(); | ||
} | ||
|
||
return invoke(acc, curr); | ||
}, undefined); | ||
}; | ||
} | ||
|
||
export default helper(execute); | ||
|
||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import isPromise from '../utils/is-promise'; | ||
|
||
export function mutThen(actions = []) { | ||
return function(...args) { | ||
let invoke = function(acc, curr) { | ||
if (isPromise(acc)) { | ||
return acc.then(() => curr()); | ||
} | ||
|
||
return curr(); | ||
}; | ||
|
||
return actions.reduce((acc, curr, idx) => { | ||
if (idx === 0) { | ||
return curr(...args); | ||
} | ||
|
||
return invoke(acc, curr); | ||
}, undefined); | ||
}; | ||
} | ||
|
||
export default helper(mutThen); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, execute } from 'ember-composable-helpers/helpers/execute'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, mutThen } from 'ember-composable-helpers/helpers/mut-then'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render } from '@ember/test-helpers'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
module('Integration | Helper | execute', function(hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
// Replace this with your real tests. | ||
test('it renders', async function(assert) { | ||
this.set('inputValue', '1234'); | ||
|
||
await render(hbs`{{execute inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '1234'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render } from '@ember/test-helpers'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
module('Integration | Helper | mut-then', function(hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
// Replace this with your real tests. | ||
test('it renders', async function(assert) { | ||
this.set('inputValue', '1234'); | ||
|
||
await render(hbs`{{mut-then inputValue}}`); | ||
|
||
assert.equal(this.element.textContent.trim(), '1234'); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is a copy of
queue
without args? What is the pain of ignoring the args in eachaction
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that's correct, a copy but without args, mainly to give the template ergonomics, Currently there's no way to simple chain events giving the template full control of partial application without leaks, queue and pipe 'leaks' into the chain of fns, for example using
on
with queue helper leaks the Dom event to the chain, using pipe leaks the return value, which might actually cause troubles if any fn in the chain returns void and any of the next function args expects maybe params to be defined or something, you can see execute as thecall
helper but with a promise aware chain of fnsTo chain perhaps unrelated stuff like closing a dialog, creating a contact, displaying a notification and also triggering
@onClick
and logging to Google Analytics via a service action, all of these fns may receive args in certain way or order so leaking may have undesirable effectsThe event will be leaked, and so isValid would be true... I can try to give more examples if that doesn't make sense yet?
In our use case we have a serious amount of fns down on the render tree which need to hook say to the onBlur of an input and it wouldn't be posible if we had to worry about how they are actually physically placed, we see them as kind of a series of reactions to mainly user UI interaction event which any of them is entirely responsible of partially apply the needed params
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call helper may receive an array of fns? Would that make more sense? 🤔🤔
Opting out from leaks makes sense when dealing with deeply nested trees of components
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok that makes sense. Thx for the explanation! I think we will need to come up with a really clear name b/c when one steps back and evaluates how to call a chain of functions, I'm not sure an obvious answer stands out (and may be more confusing with two options)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, totally agree, what do you think about sequence?
🤔🤔🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it sounds like the functions you are using might be called in multiple places? Otherwise, you could always align the arguments with the arity of arguments if only used in one spot?
I'm also thinking about this from a typescript point of view. You have a list of expected arguments. However, this will pass nothing and could show an error. Typically, in this case you would compose multiple functions together while perhaps sharing some logic.
What are your thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, pretty much. I use them sparingly across the app/engines in different ways, from JS and helpers, so having full control on the template is a must, to avoid foot guns.
Honestly I haven't (probably should) used a lot of functional programming or composition of fns, in
.js
land in Ember. I often use the templates for that, but I agree, you compose multiple functions together taking the arity, order and such into account when needed to share logic or something in common, in this particular case, the only thing these fns have in common is that they were triggered by something... sometimes an event, sometimes a callback, basically like observer pattern, pretty much, these are subscribers fns. in a Template only Component land, pipe, queue, (sequence|execute) come pretty handyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another good approach could be to just add a
noargs
helper...