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

need to add getter for private "this._hook" #103

Open
1 task done
krivalex opened this issue Sep 10, 2024 · 0 comments
Open
1 task done

need to add getter for private "this._hook" #103

krivalex opened this issue Sep 10, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@krivalex
Copy link

Describe the feature

on hookable.ts - just need a getter like this

 getHooksState() {
    return this._hooks;
  }

I use hookable on bussiness project, and made a communication beetween big modules with callHook.

in my project a make a class extender for Hookables.js. Code as below.
make this, because have a problem with 30-40 not-unique hooks, with function - it was a big problem.

my "on" method:

  1. makes a unique check in hookables private this._hooks
  2. makes a dev log event

I real need this in this project, because have a factory-generation code with no-unique symbols and name of the functions.

And I suggest making a public and secure getter, because now i just use "eslint ignore dot-notation" for use private variable.

/* eslint-disable dot-notation */
import { Logger } from '@/utils/logger/logger'
import { createHooks } from 'hookable'

type ExpandFunc = (() => Promise<void>) | (() => void)

export class AppHooksSetup<T> {
  hooks = createHooks<Record<string, any>>()

  on<K = T>(names: keyof (K & string)[] | (keyof K & any), callback: ExpandFunc = async () => {}) {
    if (Array.isArray(names)) {
      names.forEach((name: string) => {
        if (this.isUnique(name, callback)) {
          this.hook(name, callback)
          Logger.info('HOOKS', `Выполнен EVENT ${name}`)
        }
      })
      return
    }
    if (this.isUnique(names, callback)) {
      this.hook(names, callback)
      Logger.info('HOOKS', `Выполнен EVENT ${names}`)
    }
  }

  event<K = T>(name: keyof K & string, args?: any[]) {
    Logger.info('HOOKS', `Зарегистирирован EVENT ${name}`)
    return this.hooks.callHook(name, args)
  }

  off() {
    return this.hooks.removeAllHooks()
  }

  private hook(name: string, callback: ExpandFunc = async () => {}) {
    return this.hooks.hook(name, callback)
  }

  private isUnique(name: string, callback: ExpandFunc = async () => {}) {
    const hooks = this.hooks['_hooks']
    const internal = hooks[name]?.toString()
    const external = callback?.toString()

    return (hooks[name] && !internal.includes(external)) || !hooks[name]
  }
}

Additional information

  • Would you be willing to help implement this feature?
@krivalex krivalex added the enhancement New feature or request label Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant