unjs / unjs/hookable

need to add getter for private "this._hook"

Open
#103 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
960
Forks
43
Avg merge
19m
Merged PRs (30d)
1

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in hookable.ts and inspect how createHooks stores its private _hooks state. Confirm the requested public getter exposes that state without requiring private-property access, then run the repository’s existing checks to verify the TypeScript API change.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.