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

@itrocks/plugin

v0.0.8

Published

A structure that allows classes to be extended with new behaviors via plugin

Downloads

399

Readme

npm version npm downloads GitHub issues discord

plugin

A structure that allows classes to be extended with new behaviors via plugin.

Installation

npm i @itrocks/plugin

Usage

Creating your Feature Class

Example

class Feature extends HasPlugins<Feature>
{
	
	constructor(options: Partial<Options<Feature>> = {})
	{
		super(options)
		this.constructPlugins()
		this.initPlugins()
	}

	do()
	{
		console.log('your feature does something')
	}

}

Creating a plugin

Example

class FeatureExtension extends Plugin<Feature>
{

	constructor(of: Feature)
	{
		super(of)
		console.log('Your feature has been extended')
	}

	init()
	{
		console.log('FeatureExtension.init()')
	}

}

Overloading functions

A common way to add new features to your orignal feature is by overriding some of its functions during the constructor() phase.

This common AOP design pattern can be implemented very simply:

Example

class FeatureExtension extends Plugin<Feature>
{

	constructor(of: Feature)
	{
		super(of)
		console.log('Your feature has been extended')

		const superDo = this.of.do
		this.of.do    = function() {
			console.log('Your plugin does something before your feature does')
			return superDo.call(this)
		}
	}

}

Be sure to follow the SOLID principles: your override can add features but should not alter the main feature's original behavior.

Instantiating your feature

Choose the options and plugins for each of your feature class instances.

Example

new Feature({ plugins: [FeatureExtension] }).do()

API

HasPlugins

The class your feature class should inherit from.

options

The options property receives the instance options, including the plugin list.

plugins

The plugins property is an object where each property name corresponds to a plugin type name, and the value is the plugin instance associated with your feature.

HasPlugins constructor()

Stores the options passed to your feature's constructor in the options property.

constructPlugins()

Instantiates each plugin listed in options.plugins. The instances are stored in the plugins property.

initPlugins()

Calls init() for each plugin instance stored in the plugins property if it is instantiated.

initPlugins(initFunction = 'init')

Your feature class constructor can call this method with different values for initFunction. The matching functions in your plugins will be called, if they are instantiated.

Options

The options for your feature link an option name to its value.

One specific option is the plugins applied to your feature:

plugins

The plugins property consists of an array of Plugin types you want to apply to your feature.

These plugins will be instantiated during the constructPlugins() phase, and their instances will be stored in the feature's plugins property.

Plugin

The class your plugin classes must extend.

of

The of property stores the main feature object, which extends HasPlugins.

You can access other plugins you depend on through of.plugins once the constructorPlugins() phase is complete, for example, during the init() phase or when executing overloaded functions.

Plugin constructor()

Your plugin can execute code during its constructor phase.

At this stage, not all feature plugins are instantiated, so avoid accessing plugins you may depend on.

init()

Your plugin can execute code during its init phase.

At this stage, all feature plugins are instantiated, so you can access dependencies.