npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-tidy

v1.0.0

Published

A tiny Vue.js mix-in for event handler management and memory leaks prevention.

Downloads

14

Readme

vue-tidy

Build Status Code coverage

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.

Installation

npm install -S vue-tidyor yarn add vue-tidy

Usage

<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>

API

The mix-in is the default export.

The mix-in adds the following features to a vue instance:

Lifecycle hooks

  • created - sets up the machinery;
  • beforeDestroy - calls dispose() method (see below).

Instance methods

  • debug(...)outputs coloured and timestamped console messages, when enabled;.
  • debugOn([*]): *enables or disables debug method and returns this for chaining, if argument supplied; otherwise returns boolean showing if debugging is enabled.
  • dispose()frees up all bound resources, called automatically by beforeDestroy hook. It cleans the own container, firing dispose method of every object instance having it. Then it un-registers all handlers set by ownOn 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 registers event handler with emitter object. If emitter API differs from addEventListener/removeEventListener or $on/$off or on/off, then you need explicitly define the API, like ['listenTo', 'ignore']. The handler parameter can be instance method name or a function.

Instance properties

  • own: Objecta keyed container for private data, that should be gracefully cleaned up.
  • ownClass: stringclass name (read-only).
  • ownId: numberglobally unique class instance number(read-only).
  • ownTag: stringis set to ownClass+ '#' +ownId (read-only).

Assigning a value to any instance property will throw TypeError.

Overriding dispose() method

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.

Name guess helper

There is a helper function used internally for initializing the ownClass property.

guess(vm, [tryHarder : boolean]) : {atring | undefined}

This function can work without the mix-in as well. It must be imported separately: import guess from 'vue-tidy/guess'