-
Notifications
You must be signed in to change notification settings - Fork 393
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
Can we remove tap from hook? #109
Comments
Why? I can't undestand, also it is internal module for webpack, what is use case using this without webpack? |
Because I using it in my Vue Component, But I can't find anything as good as tapable for handling the flow of events. I wanted to submit PR, but the 'lib/ hookcodefactory.js' file seemed too hard to understand. |
How are you using this with Vue components? |
const hook = new SyncHook()
// After a while
hook.call()
// dialog-a.vue
export default {
methdos: {
someMethod(){}
}
created() {
hook.tap('If the dialog appears', this.someMethod)
}
destroyed() {
// I want to remove it...
}
} |
Due to internal optimizations done which depends on the taps called, it would be very inefficient to remove plugins. Therefore it it's not in the API. Avoid removing plugins. Note that tapable is optimized for a call flow like: 1. attach to hooks, 2. call hooks at lot. You could still handle your use case this way: const hook = new SyncHook()
// After a while
hook.call()
const hookMethods = new Set();
hook.tap("dialog-a", () => { for(const m of hookMethods) m(); });
// dialog-a.vue
export default {
methdos: {
someMethod(){}
}
created() {
hookMethods.add(this.someMethod);
}
destroyed() {
hookMethods.delete(this.someMethod);
}
} but other solutions might be better for this |
@sokra Actually this is a real shame. tapable has a great API that's useful outside webpack. Lacking the ability to remove a tap really diminishes it's value. |
why don't you use event hub? const eventHub = new Vue()
export default {
created() {
eventHub.$on('some-action', this.addTodo)
},
destroyed() {
eventHub.$off('some-action', this.addTodo)
},
methods: {
addTodo() {},
},
} |
因为当应用变得复杂的时候,tapable覆盖的场景更全面 |
need this feature |
@wangzongxu I find a method to remove tap: var hook = new SyncHook(['param'])
var fn = param => console.log(param)
hook.tap('example', fn)
hook.call('frist')
// frist
setTimeout(() => {
removeTapFromHook(hook, 'example');
}, 3000)
// remove
function removeTapFromHook(hook, tapName) {
const index = hooks.taps.findIndex(tap => tap.name === tapName);
if (index > -1) {
hooks.taps.splice(index, 1);
}
} We only need remove the tap from the private variable |
That's private. But anyway if you do that call |
The text was updated successfully, but these errors were encountered: