Skip to content

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

@krivalex

Description

@krivalex

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions