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

dependable-react

v0.3.0

Published

## Getting started

Downloads

9

Readme

Dependable React

Getting started

Note: dependable-react has a peer dependency to react@^16.8.0, so don't forget to install the latest React version:

npm install --save dependable-react react react-dom

Usage

DefineModule

Use this to setup your services/tokens/factories/... that will later be injected. Take care that you can't inject anything before defining it. You can define:

  • A class (that will be a singleton when injected)
  • A value (e.g. window)
  • A factory (method that will be called)

You can also define a parent scope to enable scope nesting.

function DefineModule(providers: Array<TProvider>, scope?: ScopeToken | string, parentScope?: ScopeToken): ScopeToken;
class DataService {
  public load() {}
}

DefineModule([
  DataService, // Short hand
  {
    provider: DataService,
    initClass: DataService,
  },
]);

InjectionProvider

A provider that creates a new scope and passes it to the useInject automatically. This enables structural scoping without worying about the scope tokens.

function InjectionProvider(options: {
  providers: Array<TProvider>;
  scope?: ScopeToken | string;
  parentScope?: ScopeToken;
});
const App = () => (
  <InjectionProvider provider={[DataService]}>
    <SomeComponent />
  </InjectionProvider>
);

GenerateTestBed

When testing your code that depends on something (e.g. DataService) you can't use DefineModule since it's a global thing. Use GenerateTestBead in a beforeEach hook.

function GenerateTestBed(
  providers: Array<TProvider>,
  scope?: ScopeToken | string,
  parentScope?: ScopeToken,
): ScopeToken;
import { DataService } from '../';

const STORAGE_TOKEN = new InjectionToken<Storage>();

beforeEach(() => {
  class FakeDataService {
    public load() {}
  }

  GenerateTestBed([
    {
      provider: DataService,
      initClass: FakeDataService,
    },
    {
      provider: STORAGE_TOKEN,
      initFactory: () => {
        if (window.require && window.require('electron')) {
          return new ElectronStoreService(window);
        }

        return new LocalStorageService();
      },
    },
  ]);
});

InjectionToken

If for some reason you don't want to use the exact dependency for storing the value (e.g. window) you can use InjectionToken instead.

new InjectionToken<T>(key: string);
const WINDOW_TOKEN = new InjectionToken<Window>();

DefineModule([
  {
    provider: WINDOW_TOKEN,
    initValue: window,
  },
]);

inject

Use this for injecting stuff in non-react code.

function inject<T>(cls: IConstructor<T> | InjectionToken<T>, scope?: ScopeToken): T;
class SomeService {}

class DataService {
  private someService = inject(SomeService);
}

⚠️Note: ⚠️ Take care when defining modules to define them in the order of injection. The previous exapmle would only work as this:

DefineModule([SomeService, DataService]);

useInject

function useInject<T>(cls: IConstructor<T> | InjectionToken<T>, scope?: ScopeToken): T;

Scope & SSR support

In order to support Server-side rendering and other use cases that require multiple scopes, there is a possibility to define a specific scope.

The scope will be passed to the class as the constructor argument or to a factory as a function argument. That way the class or factory can pass it to inject if they need it.

useContextInject

function useContextInject<T>(cls: TKey<T>, contextKey: Context<ScopeToken>): T;

To make things simpler with React, the most likely case is to pass the scope through the React context. To avoid two operations (useContext to get the scope, and then useInject), there is one more react hook called useContextInject.

TODO

Polyfilling

The lib makes use of the following features that are not yet available everywhere. Based on your browser support, you might want to polyfill them:

Build Status Greenkeeper badge npm version Dependency Status DevDependency Status

License

The MIT License

Credits

dependable-react is maintained and sponsored by Infinum.