{"id":15834955,"url":"https://github.com/kaelzhang/progress-hooks","last_synced_at":"2025-04-01T12:28:27.856Z","repository":{"id":57331042,"uuid":"180096841","full_name":"kaelzhang/progress-hooks","owner":"kaelzhang","description":"The manager of sequential hooks to work with tapable","archived":false,"fork":false,"pushed_at":"2019-04-11T08:14:03.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T13:16:18.720Z","etag":null,"topics":["tapable"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaelzhang.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-08T07:46:29.000Z","updated_at":"2019-04-11T08:14:04.000Z","dependencies_parsed_at":"2022-09-16T17:21:30.802Z","dependency_job_id":null,"html_url":"https://github.com/kaelzhang/progress-hooks","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fprogress-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fprogress-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fprogress-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fprogress-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaelzhang","download_url":"https://codeload.github.com/kaelzhang/progress-hooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246424194,"owners_count":20774918,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["tapable"],"created_at":"2024-10-05T14:02:58.572Z","updated_at":"2025-04-01T12:28:27.838Z","avatar_url":"https://github.com/kaelzhang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/kaelzhang/progress-hooks.svg?branch=master)](https://travis-ci.org/kaelzhang/progress-hooks)\n[![Coverage](https://codecov.io/gh/kaelzhang/progress-hooks/branch/master/graph/badge.svg)](https://codecov.io/gh/kaelzhang/progress-hooks)\n\u003c!-- optional appveyor tst\n[![Windows Build Status](https://ci.appveyor.com/api/projects/status/github/kaelzhang/progress-hooks?branch=master\u0026svg=true)](https://ci.appveyor.com/project/kaelzhang/progress-hooks)\n--\u003e\n\u003c!-- optional npm version\n[![NPM version](https://badge.fury.io/js/progress-hooks.svg)](http://badge.fury.io/js/progress-hooks)\n--\u003e\n\u003c!-- optional npm downloads\n[![npm module downloads per month](http://img.shields.io/npm/dm/progress-hooks.svg)](https://www.npmjs.org/package/progress-hooks)\n--\u003e\n\u003c!-- optional dependency status\n[![Dependency Status](https://david-dm.org/kaelzhang/progress-hooks.svg)](https://david-dm.org/kaelzhang/progress-hooks)\n--\u003e\n\n# progress-hooks\n\nThe manager of sequential hooks to work with [`tapable`](https://www.npmjs.com/package/tapable).\n\n`progress-hooks` applies the taps(plugins) only if the previous hook has been called.\n\nUsually, it used to replace the code slice `this.hooks = {}` of the `tapable` example\n\n## Install\n\n```sh\n$ npm i progress-hooks\n```\n\n## Usage\n\n```js\nconst {\n  SyncHook,\n  AsyncParallelHook\n} = require('tapable')\nconst {\n  Hooks,\n  ADD,\n  CLEAN,\n  COUNT\n} = require('progress-hooks')\n\nclass Car {\n  constructor () {\n    // this.hooks = {\n    //   accelerate: new SyncHook(['newSpeed']),\n    //   brake: new SyncHook()\n    // }\n\n    // Instead of the code above, we create hooks by `new Hooks()`\n\n    this.hooks = new Hooks({\n      accelerate: new SyncHook(['newSpeed']),\n      brake: {\n        hook: new SyncHook(),\n        // The car needs to brake twice, then stop\n        plan: 2\n      },\n      stop: new SyncHook()\n    })\n  }\n}\n\nconst car = new Car()\nlet speed = 0\n\n// The `LoggerPlugin` method is not actually tapped into the `car.hooks.brake`,\n// but instead, it is held by `progress-hooks`\ncar.hooks.brake.tap('LoggerPlugin', () =\u003e {\n  if (speed === 0) {\n    throw new Error('can not brake')\n  }\n\n  console.log('brake')\n})\n\ncar.hooks.stop.tap('StopEnginePlugin', () =\u003e {\n  console.log('stopped')\n})\n\n// And it will not be called\ncar.hooks.brake.call()\n\ncar.hooks.accelerate.tap('SetterPlugin', newSpeed =\u003e {\n  speed = newSpeed\n})\n\ncar.hook.accelerate.call(120)\n// And after `car.hook.accelerate.call()` is invoked,\n// The `LoggerPlugin` will be applied\n\ncar.hooks.brake.call()\n// prints: 'brake'\n\ncar.hooks.stop.call()\n// nothing `console.log`ged, because of `plan: 2`\n\ncar.hooks.brake.call()\ncar.hooks.stop.call()\n// prints: stopped\n```\n\n## new Hooks(rawHooks, options)\n\n- **rawHooks** `{[string]: tapable.Hook | PlannedHook}`\n- **options?** `Object`\n  - **disableAfterCalled?** `boolean=true` If `true`(the default value) the hook will be disabled after called.\n\n```ts\ninterface PlannedHook {\n  hook: tapable.Hook\n  // If plan is `2`, then the next hook will not be activated\n  // until the current hook has been called twice\n  plan: number\n}\n```\n\nReturns `hooks`\n\nThe returned `hooks` is a relatively clean object that we can get all hook names just with `Object.keys(hooks)`:\n\n```js\nObject.keys(car.hooks)\n// ['accelerate', 'brake']\n```\n\n\u003c!--\nAnd we can access the real tapable hook by access:\n\n```js\ncar.hooks.accelerate.hook  // -\u003e The tapable hook\n```\n--\u003e\n\n### `hooks[ADD](name, hook): void`\n\nAdds a new hook.\n\n```js\nconst hooks = new Hooks()\n\nhooks[ADD]('accelerate', new SyncHook(['newSpeed']))\n```\n\n### `hooks[CLEAN](): void`\n\nCleans hook taps if the hook is not enabled, so that we could reload plugins.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaelzhang%2Fprogress-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaelzhang%2Fprogress-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaelzhang%2Fprogress-hooks/lists"}