-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat(schematics): add support for create effect to schematics #1725
feat(schematics): add support for create effect to schematics #1725
Conversation
@@ -35,4 +35,22 @@ export class <%= classify(name) %>Effects { | |||
<% } else { %> | |||
constructor(private actions$: Actions) {} | |||
<% } %> | |||
|
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.
If there was an option to extend a template that could be a nice solution here @brandonroberts @timdeschryver do you know if it's possible?
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.
Not currently
Preview docs changes for 69b6251 at https://previews.ngrx.io/pr1725-69b6251/ |
We'll also need to account for using action creators with the new effect creators. Instead of using |
Hmm I see, I will try to find a proper solution that will consider the action creator option possibility, |
69b6251
to
ac45157
Compare
@brandonroberts I have updated the solution what do you think? |
Preview docs changes for ac45157 at https://previews.ngrx.io/pr1725-ac45157/ |
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.
The CI build seems to be failing (as an fyi in case you missed it)
@itayod it would be easier to generate parts of the template in the factory code and pass a variable to the template for output. That's probably as close as we'll get to partial templates. |
ac45157
to
079517c
Compare
I agree that's what I just did 😄 |
b1cecf6
to
80ece49
Compare
We can save some line breaks in the template by moving the if cases up. It was too much to type separately.
|
Preview docs changes for 80ece49 at https://previews.ngrx.io/pr1725-80ece49/ |
80ece49
to
137eb71
Compare
Preview docs changes for 137eb71 at https://previews.ngrx.io/pr1725-137eb71/ |
137eb71
to
c0f85cd
Compare
I have updated the code. |
Preview docs changes for c0f85cd at https://previews.ngrx.io/pr1725-c0f85cd/ |
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.
LGTM
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.
I was a tidbit too fast 😅
Should we also add a test to check createEffect
isn't used when we don't use the -c
flag?
const effectName = stringUtils.classify(name); | ||
return effectCreator | ||
? `load${effectName}s$ = createEffect(() => this.actions$.pipe(` | ||
: '@Effect()\n' + `\tload${effectName}s$ = this.actions$.pipe(`; |
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.
The tab here isn't needed, currently it will output:
@Effect()
loadFoos$ = this.actions$.pipe(
ofType(FooActionTypes.LoadFoos),
/** An EMPTY observable only emits completion. Replace with your own observable API request */
concatMap(() => EMPTY)
);
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.
hmm weird when I test it I am getting different result 🤔
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.
apparently because I use tab as 2 spaces in my pc it looked good...
thanks for your notice!! :)
Not sure what you mean, how would a ts compiler test look like? |
Well, I mean use ts compiler to test if the template has the expected result. function getImportSpecifiersFor(file: ts.Node, importName: string) {
let imports;
file.forEachChild((node) => {
if (ts.isImportDeclaration(node)
&& node.moduleSpecifier.text === importName
&& node.importClause) {
imports = (node.importClause.namedBindings as NamedImports).elements
.map((importElem) => importElem.name.text)
}
})
} and then the test looks: const importsFromNgrx = getImportSpecifiersFor(file, '@ngrx/effects');
expect(importsFromNgrx).toEqual([ 'Actions', 'createEffect', 'ofType' ]); |
c0f85cd
to
b44e3cc
Compare
Preview docs changes for b44e3cc at https://previews.ngrx.io/pr1725-b44e3cc/ |
@@ -316,4 +317,42 @@ describe('Effect Schematic', () => { | |||
/constructor\(private actions\$: Actions<FooActions>\) {}/ | |||
); | |||
}); | |||
|
|||
it('should create an effect using creator function', () => { | |||
const options = { ...defaultOptions, effectCreator: true, feature: true }; |
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.
const options = { ...defaultOptions, effectCreator: true, feature: true }; | |
const options = { ...defaultOptions, effectCreators: true, feature: true }; |
it('should create an api effect using creator function', () => { | ||
const options = { | ||
...defaultOptions, | ||
effectCreator: true, |
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.
effectCreator: true, | |
effectCreators: true, |
/** | ||
* Specifies if the effect creation uses 'createEffect' | ||
*/ | ||
effectCreator?: boolean; |
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.
effectCreator?: boolean; | |
effectCreators?: boolean; |
b44e3cc
to
518f6e9
Compare
Preview docs changes for 518f6e9 at https://previews.ngrx.io/pr1725-518f6e9/ |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Closes #1682
What is the new behavior?
add support for
createEffect
function in effect schematicsDoes this PR introduce a breaking change?