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

honk-di

v1.0.0

Published

`honk-di` is a [Guice](https://code.google.com/p/google-guice/)-like dependency injector for (Java|Coffee)Script.

Downloads

66

Readme

Honk! DI!

honk-di is a Guice-like dependency injector for (Java|Coffee)Script.

The Injector

The injector creates classes and resolves their dependencies. Take the following.

class User
  say: (name) -> console.log "hi, #{name}"

injector = new inject.Injector()
user1 = injector.getInstance(User)
user2 = injector.getInstance(User)

assert user1 !== user2

But what if we want only one User? We scope it!

class User
  @scope: 'singleton'

  say: (name) -> console.log "hi, #{name}"

injector = new inject.Injector()
user1 = injector.getInstance(User)
user2 = injector.getInstance(User)

assert user1 === user2

Dependencies

Making a single class isn't going to save you much. What you want is for it to resolve your dependencies.

inject = require 'honk-di'

class Sayer
  @scope: 'singleton'

  say: (name) -> console.log "hi, #{name}"

class User
  sayer: inject(Sayer)

  say: (name) -> @sayer.say(name)

injector = new inject.Injector()
user     = inject.getInstance(User)
sayer    = inject.getInstance(Sayer)

user.say('Jimmy')
> Hi, jimmy
user.sayer === sayer
> True

Note how DI's managed the singleton scope of the sayer for us. If we pulled out another User from the injector, it would be a new instance with the same instance to Sayer

Binders

A binder is analogous to a Guice Module. But, seeing how "module" means something in the ol' Node.js world, they're called binders here.

Sometimes your dependencies get more complicated. Like, you may have a MapView that's just an in-memory mock for your test, but uses Google Maps in the UI, and you're working on porting them to Leaflet. You can control this kind of malarkey with DI pretty well.

inject = require 'honk-di'

class ProductionEnv extends inject.Binder
  configure: ->
    @bind(MapView).to(GoogleMapView)
    @bind(UserStore).to(MemUserStore).inScope('singleton')
    @bindConstant('api.root').to('/api/v1')

Now, the injector may consult some number of Binders before creating an instance. Say we have a controller as follows:

class Sayer
  @scope: 'singleton'

  say: (name) -> console.log "hi, #{name}"

class MapController
  view:     inject(MapView)
  users:    inject(UserStore)
  apiRoot:  inject('api.root')

  render: ->
    $.get("#{apiRoot}/users")
      .then((users) -> @users.reset(user))
      .done((users) -> @view.render(users))

What's missing

Providers, complex scopes. I'm sure plenty of other small weird things.

Good Ideas

The best DI apps have one call to injector.getInstance. Passing the injector around or pulling many instances out of it is likely a bad sign. Try to express hairy sections as classes that have some number of dependencies and let DI figure it out for you.