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

@ebay/oja-context

v2.0.4

Published

Dependency injection via actions

Downloads

19

Readme

oja-context

Downloads

Defines a basic Oja dependency injection API.

This module is a subset of eBay Oja framework.

Idea

The dependency injection approach allows the developer to slice the business logic into small, isolated business actions that are injected via context.

That encourages developers to use the same action interface across application code and makes the code more predictable, easy to define, and test with mock data. And yes - it makes it boring.

Dependency injection diagram

context diagram

Install

npm install @ebay/oja-context --save

API

The context based approach provides a more explicit way of composing actions in the application while in publisher/subscriber pattern the consumers of the events are completely disconnected from the publishers.

ojaContext(createBaseContext: Function): Function

Create a factory function, that if called, will return a context object extended from an object returned by createBaseContext.

ojaContext(options: Map): Function

Create a context with actions and properties injected as part of options. It returns a context reference which provides access to all other actions. It can also extend a Flow class if requested, so that one can mix flow with context API.

Options:
{
    functions: Map <
            namespace: String,
            Map <actoinName: String, action: Function>
        >,

    properties: <Map <string, value:Any> >,

    resolve: Function,

    selectors: Map <string, value:Any>
}

Where:

  • functions: allows to inject action implementation upon the context creation, it is useful for unit testing
  • properties: allows to inject any parameter upon the context creation
  • resolve: allows to inject custom resolution logic for action discovery, and this is a primary way to inject actions.
  • selectors: is a set of key:value pairs that allows to enforce specific selectors for all actions in the given context; the selectors can be dropped if they are prefixed with '~' trying to find a match.

Calling action

const result = await context.action('action-name-space', arg1, arg2, ...);

Or more specific in some rare case with the help of selectors

const result = await context.action({
    name: 'action-name-space',
    'selector1': 'foo', // this selector will not be dropped
    '~selector2': /^bar/, // <<< you can use regexp, and this selector can be dropped
    '~selector3': (value) => true, // <<< or match function, and this selector can be dropped
    ...
}, arg1, arg2, ...);

It will make the most relevant match. If no exact match found for the selectors, it will drop selectors prefixed with '~' one by one trying to match the rest starting from the last and moving up while removing. This provide a fallback logic.

Usage

Context creation

These are the main properties used for context creation:

  • properties are translated into context.<property name> access pattern
  • functions are translated into `context..action:function access pattern
  • resolve allows to inject resolution of the actions
const createContext = require('@ebay/oja-context');

// inject/configure context
const context = createContext({
    // injecting properteis
    properties: {
        parameters: {
            foo: 'foov',
            bar: 'barv'
        }
    },
    // injecting actions
    functions: {
        'domainName1/actionName1': context => {},
        'domainName1/actionName2': context => {},
        'domainName2/actionName3': context => {},
        'domainName2/actionName4': context => {}
    }
});

// use it
console.log(context.foo); // >> foov
console.log(context.bar); // >> barv

// call action
const actionResult1 = await context.action('domainName1/actionName1');
const actionResult3 = await context.action('domainName2/actionName3', 'foo', 'bar);

Context can extend any base object, for example, you can extend Flow class, and use a pub/sub with context based approach.


const createContextFactory = require('@ebay/oja-context');
const Flow = require('@ebay/oja-pipe').Flow;
const createContext = createContextFactory(() => new Flow());

// inject/configure context
const context = createContext({
    ...
});

modules.exports = async context => {
    const searchResults = await context.action('actions/search', 'foo'); // passing some parameters
    // publish it
    context.define('searchResults', searchResults);
};
// consume it in some other action
modules.exports = async context => {
    const searchResults = await context.consume('searchResults');
    return {
        searchResults
    };
};

General advice

When you migrate your application to oja dependency injection and to make sure you enjoy using it, your code MUST avoid:

  • Use any callback style. If you have callback function, wrap it into promise right away.
  • Use promise then or catch. Outlaw them! Always use async/await style.
  • Use of event emitters. They should be wrapped into streams of events or promise for catching a single event.