djinn-state
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -42,2 +42,3 @@ import { DjinnService } from './DjinnService'; | ||
start: () => void; | ||
private __isValidService; | ||
} | ||
@@ -44,0 +45,0 @@ interface IDjinnServiceRegister { |
@@ -38,3 +38,3 @@ "use strict"; | ||
var serviceName = anyService.name; | ||
if (service.__proto__ !== DjinnService_1.DjinnService) { | ||
if (!_this.__isValidService(service)) { | ||
console.error("Tried to register an invalid Service Class: \"" + serviceName + "\".\nDjinn services must extends the class \"DjinnService\"."); | ||
@@ -111,4 +111,13 @@ return; | ||
} | ||
Djinn.prototype.__isValidService = function (service) { | ||
if (!service.__proto__) { | ||
return false; | ||
} | ||
if (service.__proto__ === DjinnService_1.DjinnService) { | ||
return true; | ||
} | ||
return this.__isValidService(service.__proto__); | ||
}; | ||
return Djinn; | ||
}()); | ||
exports.Djinn = Djinn; |
{ | ||
"name": "djinn-state", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Powerful yet simple state machine", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -15,3 +15,6 @@ import { Djinn } from '../Djinn'; | ||
class InheritedService extends FakeService { | ||
} | ||
describe('Djinn', () => { | ||
@@ -52,2 +55,11 @@ it('should be creating a new Djinn instance', () => { | ||
it('should register an inherited service', () => { | ||
const djinn = new Djinn(); | ||
djinn.register(InheritedService); | ||
djinn.start(); | ||
const service = djinn.getService(InheritedService); | ||
expect(service).toBeDefined(); | ||
}); | ||
it('should return an instance of a registered Service', () => { | ||
@@ -54,0 +66,0 @@ const djinn = new Djinn(); |
import { DjinnService } from './DjinnService'; | ||
import { forEachToken } from 'tslint'; | ||
@@ -30,3 +29,3 @@ | ||
if ((<any>service).__proto__ !== DjinnService) { | ||
if (!this.__isValidService(service)) { | ||
console.error(`Tried to register an invalid Service Class: "${serviceName}".\nDjinn services must extends the class "DjinnService".`); | ||
@@ -59,3 +58,3 @@ return; | ||
getService = <T>(service: new () => T): T => { | ||
const serviceName = (<any>service).name; | ||
const serviceName = (service as any).name; | ||
@@ -69,3 +68,3 @@ const registeredService = this.__services[serviceName]; | ||
if (registeredService.scoped) { | ||
const ServiceClass = <any>registeredService.service; | ||
const ServiceClass = (registeredService as any).service; | ||
const serviceInstance = new ServiceClass(); | ||
@@ -84,3 +83,3 @@ serviceInstance.initService(); | ||
remove = (service: any) => { | ||
const serviceName = (<any>service).name; | ||
const serviceName = (service as any).name; | ||
delete this.__services[serviceName]; | ||
@@ -107,2 +106,14 @@ delete this.__servicesSingletons[serviceName]; | ||
}; | ||
private __isValidService(service: any): boolean { | ||
if (!service.__proto__) { | ||
return false; | ||
} | ||
if (service.__proto__ === DjinnService) { | ||
return true; | ||
} | ||
return this.__isValidService(service.__proto__); | ||
} | ||
} | ||
@@ -109,0 +120,0 @@ |
Sorry, the diff of this file is not supported yet
24288
525