Giter Site home page Giter Site logo

di-service's People

Contributors

dancecoder avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

di-service's Issues

Improve undefined dependency error message

Currently message looks like this

Incorrect service requirements: undefined requirement

Usually undefined in dependencies happens in case some circular imports/require. So need to add information about requiring class and all dependences in list to help debugging.

Implement support for declare requiring class as a dependency

I would like to implement context based logger service, this logger need context name. Logger constructo may take context name from a class which will use this logger. Tipically loggers are initialized in constructor of a class. With DIService it may be initialized before class instance created.

To be implemented

SERVICE_REQUIRING - symbol, should be substituted by the class-object (not an instance) which caused current class initialization by requirement, other words a class-object which declare current class as a dependency by SERVICE_REQUIRE. See also example below.

NOTE: SERVICE_MULTIPLE must be required if SERVICE_REQUIRING appears in dependency list

Examples

Custom service which requiring for the logger service

class CustomService {
   
   static [SERVICE_REQUIRE] = [ServiceLogger];
   static [LOGGER_CONTEXT] = 'CUSTOM';

   #logger;

   constructor(logger) {
       this.#logger = logger;
   }

   action() {
       this.#logger.log('Action performed'); // => 'CUSTOM Action performed' in console
   }
}

Logger service

const LOGGER_CONTEXT = Symbol();

class ServiceLogger {

   static [SERVICE_REQUIRE] = [SERVICE_REQUIRING];
   static [SERVICE_MULTIPLE] = true;

   #contextName;
   constructor(requiringClz) {
      this.#contextName = requiringClz[LOGGER_CONTEXT] ?? requiringClz.name;
      console.log(requiringClz === CustomService)  // => true
   }
   log(message) {
      console.log(this.#contextName, message);
   }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.