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

proxy-service-container

v0.2.0

Published

Services container which provides proxy for different services with dependency injection.

Downloads

1

Readme

proxy-service-container

About

In proxy-service-container you can use different services (i.e. storage services).

Each Proxy Service has defined methods which are callable from outside with unified parameters (unified - when method is called) and each Proxy Service includes service related logic.

Description of Proxy Pattern

With proxy-service-container you can handle multiple services and you can choose between them which service and which method should be called.

Installation

Example usage

import ProxyServiceContainer from "../dist/proxy-service-container.js";

const sc1 = new ProxyServiceContainer();
 
sc1.addService('storage1', {
    save: function (a, b, c) {
        // dependency injection here - dependency1 and dependency2
        // rest parameters are from sc1.call('save', 1, 2, 3); - so a === 1, b === 2, c === 3
        console.log('storage1.save called', a, b, c);
        if (!this.test) {
            this.test = 1;
        } else {
            console.log(this);          // { test: 1 }
                                        // this === plain object which remember set values
        }
        return 'storage 1 result';
    }
});
 
sc1.addService('storage2', {
    save: function (a, b, c) {
        console.log('storage2.save called', a, b, c);
        return 'storage2 result';
    }
}, true); // last parameter is true - set proxy service as default
 
let result;
 
result = sc1.call('save', 1, 2, 3);                         // storage2.save called 1 2 3
console.log('result =', result);                            // result = storage2 result
 
sc1.setDefaultService('storage1');
 
result = sc1.call('save', 4, 5, 6);                         // storage1.save called dependency2 value 4 dependency1 value 5 6
console.log('result =', result);                            // result = storage 1 result
 
result = sc1.callAll('save', 7, 8, 9);                      // storage1.save called dependency2 value 7 dependency1 value 8 9
                                                            // storage2.save called 7 8 9
console.log('result =', result);                            // result = { storage1: 'storage 1 result', storage2: 'storage2 result' }
 
result = sc1.call('not_existing_function', 'error');
console.log('result =', result);                            // result = -1
 
result = sc1.callAll('not_existing_function', 10, 11, 12);
console.log('result =', result);                            // result = { storage1: -1, storage2: -1 }
 
result = sc1.setDefaultService('not_existing_service');
console.log('result =', result);                            // result = -2
 
result = sc1.callOn(['storage2'], 'save', 20, 21, 22);      // storage2.save called 20 21 22
console.log('result =', result);                            // result = { storage2: 'storage2 result' }

Gives output:

storage2.save called 1 2 3
result = storage2 result
storage1.save called 4 5 6
result = storage 1 result
storage1.save called 7 8 9
{ test: 1 }
storage2.save called 7 8 9
result = { storage1: 'storage 1 result', storage2: 'storage2 result' }
result = -1
result = { storage1: -1, storage2: -1 }
result = -2
storage2.save called 20 21 22
result = { storage2: 'storage2 result' }

Methods

ProxyServiceContainer::addService(String name, Object methods, Object dependencies = {}, Boolean isDefault = false)

Adds Proxy Service.

Methods are plain object which contains keys as methods names and values methods itself.

Each value of methods (which is a function) has passed parameters. If parameter is named the same as dependency key - a dependency will be injected as parameter.

Otherwise the rest parameters has values in order as in call, callOn and callAll methods.

ProxyServiceContainer::call(String methodName[, Mixed param1, Mixed param2, ...])

Calls method methodName with parameters param1, param2, ... on default Proxy Service.

Default Proxy service is first added or currently added with ProxyServiceContainer::addService with 4th parameter equals true.

ProxyServiceContainer::callAll(String methodName[, Mixed param1, Mixed param2, ...])

Calls all Proxy Services methods methodName with parameters param1, param2, ...

ProxyServiceContainer::callOn(Array serviceNames, String methodName[, Mixed param1, Mixed param2, ...])

Calls method defined in methodName on Proxy Services passed as string names in serviceNames.