dancecoder / di-service Goto Github PK
View Code? Open in Web Editor NEWReally simple dependency injection solution for JavaScript
License: MIT License
Really simple dependency injection solution for JavaScript
License: MIT License
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.
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.
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
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);
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.