0.1.28 • Published 7 years ago
services-pod v0.1.28
Provide singleton services
Links
Install
yarn add services-pod
or npm install services-pod
Has support CommonJS/ES6
Overview
getServicesPodInstance and createServicesPodInstance
import { getServicesPodInstance, createServicesPodInstance, ServicesPod } import from 'services-pod';
// pod1 === pod2
const pod1 = getServicesPodInstance();
const pod2 = getServicesPodInstance();
// pod1 !== pod2
const pod1 = createServicesPodInstance(); // or new ServicesPod();
const pod2 = createServicesPodInstance(); // or new ServicesPod();
ServicesPod instance methods
.get | Returns single service(s) |
.set | Setup service(s) |
.purge | Will reset resolved values for all services, if service (IServiceTarget) implements .purge method it will be invoked also |
.unset | Will invoke .purge for all resolvers and will reset resolvers (ServiceResolver) in pod (ServicesPod) |
pod.get(target: ServiceTargetName | ServiceTargetName[] | IServiceTarget | IServiceTarget[]): any | any[];
pod.set(resolverData: ServiceResolverData | ServiceResolverData[]): void;
pod.purge(): void;
pod.unset(): void;
// where
type ServiceTargetName = string;
interface IServiceTarget extends IPurgable {
serviceTargetName: ServiceTargetName;
new(...args: any[]): any;
}
interface IServiceResolverData {
serviceTarget: IServiceTarget;
serviceFactory?: ServiceFactory;
dependencies?: IServiceTarget[];
}
type ServiceResolverData = IServiceResolverData;
type ServiceFactory = (...args: any[]) => any;
Example
import { getServicesPodInstance } from 'services-pod';
const pod = getServicesPodInstance();
const resolverData = [
{
serviceTarget: EnvService,
serviceFactory() {
return new EnvService(process.env);
},
},
{
serviceTarget: ApiService,
serviceFactory(envService) {
const conf = { /* smth */ };
const http = HttpClient();
return new ApiService(envService, http, conf)
},
dependencies: [EnvService],
},
{
serviceTarget: ProfileApiService,
dependencies: [ApiService],
},
];
pod.set(resolverData);
// where
class EnvService {
env;
constructor(env = {}) {
this.env = env;
}
// required
static get serviceTargetName() {
return 'envService';
}
}
class ApiService {
envService;
http;
conf;
constructor(envService, http, conf) {
this.envService = envService;
this.http = http;
this.conf = conf;
}
// required
static get serviceTargetName() {
return 'apiService';
}
}
class ProfileApiService {
apiService;
constructor(apiService) {
this.apiService = apiService;
}
// required
static get serviceTargetName() {
return 'profileApiService';
}
}
// get service(s) anywhere
import { getServicesPodInstance } from 'services-pod';
import { ProfileApiService, ApiService } from '...';
const pod = getServicesPodInstance();
// returns {object} ApiService instance
pod.get(ApiService)
pod.get(ApiService.serviceTargetName)
pod.get([ApiService])
// returns {array} [ApiService, ProfileApiService] instances
pod.get([ApiService, ProfileApiService])
pod.get([ApiService, ProfileApiService.serviceTargetName])
Vue compatibility
import { getServicesPodInstance } from 'services-pod';
const pod = getServicesPodInstance();
// global mixin
export default function installer(Vue) {
Vue.mixin({
beforeCreate() {
const { inject } = this.$options;
if (!isEmpty(inject)) {
each(inject, (_, injectionName) => {
const service = servicesPod.get(injectionName);
if (service && !isEmpty(service)) {
inject[injectionName].default = service;
}
});
}
},
});
}
// in vue component
import { ApiService } from '...';
export default {
name: 'vue-component-name',
inject: [ApiService.serviceTargetName]
}
// warning: you hasn't need to use "provide" property for this services
License
Released under the MIT license