A tiny Vue.js mix-in for event handler management and memory leaks prevention. It also provides a simple API for easier debugging and diagnostics.
Processing DOM events or similar in OO code implies correct set-up and releasing of event handlers. Also, cross-referenced class instances and other objects need special care to prevent the dreaded memory leaks.
npm install -S vue-tidy
or yarn add vue-tidy
<template>
<div :id="unique"></div>
</template>
<script>
import tidyOwner from 'vue-tidy'
export default {
data: () => ({
unique: undefined // Can be used as element ID if we have many instances.
}),
name: 'Funny',
mixins: [tidyOwner],
created() {
this.debugOn(true) // In DEV environment, this enables following .debug() calls.
this.unique = this.ownTag
// Everything we do next, will be auto-undone before we vanish.
this.own.fiend1 = reference_to_some_other_thing
this.ownOn('resize', 'onResize', window)
this.debugOn(true) // If we need this...
},
methods: {
onResize() {
this.debug('onResize') // --> 'Funny#1 onResize +0ms' on developer console.
/* do something smarter */
}
}
}
</script>
The mix-in is the default export.
The mix-in adds the following features to a vue instance:
created
- sets up the machinery;beforeDestroy
- calls dispose() method (see below).
debug
(...)
outputs coloured and timestamped console messages, when enabled;.debugOn
([*]): *
enables or disablesdebug
method and returnsthis
for chaining, if argument supplied; otherwise returns boolean showing if debugging is enabled.dispose
()
frees up all bound resources, called automatically bybeforeDestroy
hook. It cleans theown
container, firingdispose
method of every object instance having it. Then it un-registers all handlers set byownOn
method.ownOff
(event= : string, emitter= : Object) : this
un-registers handlers registered for matching (event, emitter) pairs. It is called by dispose(), so in most cases you don't need to call it explicitly.ownOn
(event : string, handler, emitter, api=) : this
registersevent
handler
withemitter
object. If emitter API differs fromaddEventListener/removeEventListener
or$on/$off
oron/off
, then you need explicitly define the API, like['listenTo', 'ignore']
. Thehandler
parameter can be instance method name or a function.
own
: Object
a keyed container for private data, that should be gracefully cleaned up.ownClass
: string
class name (read-only).ownId
: number
globally unique class instance number(read-only).ownTag
: string
is set toownClass
+ '#' +
ownId
(read-only).
Assigning a value to any instance property will throw TypeError
.
To do something special during beforeDestroy hook, you may define ownDispose()
method.
If defined, this method will run first and if it returns boolean false
,
the default dispose()
will not be called.
There is a helper function used internally for initializing the ownClass
property.
guess(vm, [tryHarder : boolean]) : {string | undefined}
This function can work without the mix-in as well. It must be imported separately:
import guess from 'vue-tidy/guess'