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

jasc

v1.5.1

Published

Jasc Another Service Container

Downloads

258

Readme

Jasc Another Service Container

NPM Version CircleCI

Features

  • Circular Dependency detection
  • Lazy loading of services
    • the Service callback function is called once when the service is resolved
  • Services are served as properties on the container

Installation

npm i jasc

Usage

Basic usage

import Container from 'jasc'

const container = new Container()
container.serve('greet', () => name => {
    console.log(`Hello ${name}!`)
})

container.greet('Michael Jackson') // Hello Michael Jackson!

Dependencies

const container = new Container()

container
    .serve('store', () => createStore(rootReducer)) // the function will be invoked when the service is resolved, deferring the factory
    .serve('actionTypes', {      // when the service does not have any dependencies, the function is optional.
        INCREMENT: 'INCREMENT',
        DECREMENT: 'DECREMENT',
    })
    .serve('Actions', ({store, actionTypes}) => ({
        increment: () => store.dispatch({type: actionTypes.INCREMENT}),
        decrement: () => store.dispatch({type: actionTypes.DECREMENT}),
    }))
    .serve('Counter', c => Counter(c.Actions)) // Counter is a higher order component, eg: export default (actions) => class Counter extends React.Component {....

TypeScript

With typescript, it is recommended that you define an interface for all the services the container should serve, and use this when creating the container. In this example, the following is in a file (e.g. 'configure.ts'). In here, everything should be configured and set up, and the container as the Services interface should be returned:

import { Store, createStore, applyMiddleware } from 'redux'
import Container from Jasc

import { createApp } from '../app.tsx'
import { Actions, IActions, reducer, IState } from '../actions.ts'

interface Services {
    store: Store<IState>
    actions: IActions
    App: Redux.ComponentType
}

export default (): Services => {
    const container = new Container<Services>() // The container now expectes you to serve 'store', 'actions', and 'app' with correct types. 
    return container
        .serve('store', () => createStore(reducer))
        .serve('actions', ({store}) => new Actions(store.dispatch))
        .serve('App', ({actions}) => createApp(actions))   // the return-value of this last serve call is the fully configured container. if you fail to serve all services defined in the Services interface, TS will complain. 
}

One gotcha with TS

interface Services {
    a: number
    b: number
}
const container = new Container<Services>()
const configured = container.serve('a', () => 1).serve('b', ioc => ioc.a + 1)

console.log(container as any === configured) // true, it's the same instance.
container.a    // TS error, the type of container is just Container<Services>, it does not inherit the props from Services
configured.a   // TS ok, the type of configured is the Container<Services> unioned with each service served. 

The 'Container' is not a union with the 'Services', but the return-type of a 'serve' call is a union of the 'Container' and a property for the service being served. A chain of serve-calls is therefore needed to build the complete type containing all the services.

This is intentional, and you should not circumvent this by casting the container as the container unioned with the services! If you do, you will loose the typescript checks verifying that you've served all the services that you have defined and that the services' types are correct.