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

feat(core): Improve legacy hooks integration #2483

Merged
merged 3 commits into from
Nov 17, 2021

Conversation

vonagam
Copy link
Member

@vonagam vonagam commented Oct 27, 2021

Main thing is that now legacy hooks wrappers are cached much more. Currently collectLegacyHooks creates new array every time it is called with afterHooks being spread/reversed, errorHook being produced again and then all legacy combined into one array. Those things can be done during .hooks setup call.

Also:

  • Added fromBeforeHooks, fromAfterHooks and fromErrorHook to complete the set.
  • Typed better those helpers.
  • Removed types argument from enableLegacyHooks, it lacked a meaning.
  • Added all to list of protectedMethods for services.

@vonagam vonagam force-pushed the feat-improve-legacy branch 2 times, most recently from 39e4de2 to 6f5b9da Compare October 27, 2021 15:15
@vonagam vonagam force-pushed the feat-improve-legacy branch from 45a8a98 to 408aff4 Compare November 7, 2021 18:29
@vonagam
Copy link
Member Author

vonagam commented Nov 8, 2021

Had to break down enableLegacyHooks into pieces because of high Cognitive Complexity. But then one part, that it was split into, was still not passing the check... Then i found out that if you use _.each() or array.forEach() it does not directly increase the score (only nesting), while for-of loop does, which penalizes it. for-of is better than alternatives (performance, integration with async/generators and no dependencies), so it is strange that its usage ends up being punished... Last commit about that and should not be merged.

Real problem here though is that the passing level is set too low (maybe because of replacements for for-of that are used in code and lower the score). Here the author of the indicator says that 15 is a recommended maximum. If you want lower, maybe some middle ground will do - between 11 and 13.

@daffl
Copy link
Member

daffl commented Nov 10, 2021

This looks great, thank you!

I've been looking with @marshallswain into a way to make the transition less scary (early adopters with larger codebases had concerns about the old hooks becoming "legacy") and we were thinking of using a naming of "Regular" (before, after, error) and "Async" hooks which sparked the idea of adding the regular hooks into @feathersjs/hooks. The PR for that is at feathersjs/hooks#92 which basically allows you to do something like:

import { hooks } from '@feathersjs/hooks';

class DummyClass {
    async create(data: any) {
        return data;
    }
}

hooks(DummyClass, {
    create: middleware([
        collect({
            before: [],
            after: [],
            error: []
        })
    ])
})

So I'm thinking of getting your PR in, porting it into the @feathersjs/hook PR and then integrating the new version here.

@vonagam
Copy link
Member Author

vonagam commented Nov 10, 2021

Sounds good.

Small note - it might make sense to add finally as an allowed hook type. It was supported before unofficially and is trivial to add - it is like before hook but uses Promise.finally instead of Promise.then and should go first in order.

@marshallswain marshallswain merged commit 7dfe592 into feathersjs:dove Nov 17, 2021
marshallswain added a commit to feathersjs/hooks that referenced this pull request Nov 18, 2021
This pulls over the code for Regular hooks from this PR: feathersjs/feathers#2483
@marshallswain
Copy link
Member

I've copied the utility functions into the hooks repo in the deno-port branch. I simplified the typings since @feathersjs/hooks doesn't know about the Application and Service context.

https://github.com/feathersjs/hooks/blob/deno-port/packages/hooks/src/regular.ts

marshallswain added a commit to feathersjs/hooks that referenced this pull request Nov 18, 2021
This pulls over the code for Regular hooks from this PR: feathersjs/feathers#2483
marshallswain added a commit to feathersjs/hooks that referenced this pull request Nov 18, 2021
This pulls over the code for Regular hooks from this PR: feathersjs/feathers#2483
marshallswain added a commit to feathersjs/hooks that referenced this pull request Nov 18, 2021
* `collect` hooks util. Not hitting error hooks

* Fix error hook handling and update tests

* docs: complete refactor

* fix: cleaner utilities for regular hooks & collect

This pulls over the code for Regular hooks from this PR: feathersjs/feathers#2483

Co-authored-by: marshall_thompson2 <[email protected]>
Co-authored-by: daffl <[email protected]>
marshallswain added a commit to feathersjs/hooks that referenced this pull request Nov 19, 2021
* `collect` hooks util. Not hitting error hooks

* Fix error hook handling and update tests

* docs: complete refactor

* feat: Port package and tests to Deno first

* feat: cleaner regular hooks

This pulls over the code for Regular hooks from this PR: feathersjs/feathers#2483

* Add makefile and build infrastructure

* Update build file and dependencies

* Remove NodeJS workflow

* test: collect hooks

* fix: imports

* feat: run coverage reports in CI

This also adds the `cov_profile` folder to the .gitignore.  Not sure if we need it checked in.

* chore: setup deno lint

* chore: configure deno lint and deno fmt in makefile

* chore: format and lint as part of test script

* chore: simplify test script

* Some additional tweaks

Co-authored-by: marshall_thompson2 <[email protected]>
Co-authored-by: daffl <[email protected]>
daffl added a commit to feathersjs/hooks that referenced this pull request Nov 19, 2021
This pulls over the code for Regular hooks from this PR: feathersjs/feathers#2483

Co-authored-by: marshall_thompson2 <[email protected]>
Co-authored-by: daffl <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants