scopeEvent service
parent
4ea48add11
commit
792d2ae22c
|
@ -0,0 +1,35 @@
|
|||
class ScopeEvent
|
||||
scopes: {},
|
||||
_searchDuplicatedScopes: (id) ->
|
||||
return _.find Object.keys(@scopes), (key) =>
|
||||
return @scopes[key].$id == id
|
||||
|
||||
_create: (name, scope) ->
|
||||
duplicatedScopeName = @._searchDuplicatedScopes(scope.$id)
|
||||
|
||||
if duplicatedScopeName
|
||||
throw new Error("scopeEvent: this scope is already
|
||||
register with the name \"" + duplicatedScopeName + "\"")
|
||||
|
||||
if @scopes[name]
|
||||
throw new Error("scopeEvent: \"" + name + "\" already in use")
|
||||
else
|
||||
scope._tgEmitter = new EventEmitter2()
|
||||
|
||||
scope.$on "$destroy", () =>
|
||||
scope._tgEmitter.removeAllListeners()
|
||||
delete @scopes[name]
|
||||
|
||||
@scopes[name] = scope
|
||||
|
||||
emitter: (name, scope) ->
|
||||
if scope
|
||||
scope = @._create(name, scope)
|
||||
else if @scopes[name]
|
||||
scope = @scopes[name]
|
||||
else
|
||||
throw new Error("scopeEvent: \"" + name + "\" scope doesn't exist'")
|
||||
|
||||
return scope._tgEmitter
|
||||
|
||||
angular.module("taigaCommon").service("tgScopeEvent", ScopeEvent)
|
|
@ -0,0 +1,83 @@
|
|||
angular.module("taigaCommon").provider("$exceptionHandler", angular.mock.$ExceptionHandlerProvider)
|
||||
|
||||
describe.only "tgScopeEvent", ->
|
||||
scopeEvent = null
|
||||
$rootScope = null
|
||||
|
||||
_inject = ->
|
||||
inject (_tgScopeEvent_, _$rootScope_) ->
|
||||
scopeEvent = _tgScopeEvent_
|
||||
scopeEvent.scopes = {}
|
||||
|
||||
$rootScope = _$rootScope_
|
||||
|
||||
_setup = ->
|
||||
_inject()
|
||||
|
||||
beforeEach ->
|
||||
module "taigaCommon"
|
||||
_setup()
|
||||
|
||||
it "get non-existent emitter", () ->
|
||||
fn = () -> scopeEvent.emitter("test")
|
||||
|
||||
expect(fn).to.throw(Error, "scopeEvent: \"test\" scope doesn't exist'")
|
||||
|
||||
it "create emitter", () ->
|
||||
scope = $rootScope.$new()
|
||||
|
||||
emitter = scopeEvent.emitter("test", scope)
|
||||
|
||||
expect(emitter).to.be.an.instanceof(EventEmitter2)
|
||||
|
||||
it "get emitter", () ->
|
||||
scope = $rootScope.$new()
|
||||
|
||||
scopeEvent.emitter("test", scope)
|
||||
|
||||
emitter = scopeEvent.emitter("test")
|
||||
|
||||
expect(emitter).to.be.an.instanceof(EventEmitter2)
|
||||
|
||||
it "duplicate emitter name", () ->
|
||||
scope = $rootScope.$new()
|
||||
scope2 = $rootScope.$new()
|
||||
|
||||
scopeEvent.emitter("test", scope)
|
||||
|
||||
fn = () -> scopeEvent.emitter("test", scope2)
|
||||
|
||||
expect(fn).to.throw(Error, "scopeEvent: \"test\" already in use")
|
||||
|
||||
it "duplicate scope", () ->
|
||||
scope = $rootScope.$new()
|
||||
|
||||
scopeEvent.emitter("test", scope)
|
||||
|
||||
fn = () -> scopeEvent.emitter("test2", scope)
|
||||
|
||||
expect(fn).to.throw(Error, "scopeEvent: this scope is already register with the name \"test\"")
|
||||
|
||||
|
||||
it "create listener", () ->
|
||||
scope = $rootScope.$new()
|
||||
|
||||
emitter = scopeEvent.emitter("test", scope)
|
||||
emitter.on "test_listener", () -> return
|
||||
|
||||
expect(scope._tgEmitter.listeners("test_listener")[0]).to.be.ok
|
||||
|
||||
it "remove emitter and listeners after scope destroy", () ->
|
||||
scope = $rootScope.$new()
|
||||
|
||||
emitter = scopeEvent.emitter("test", scope)
|
||||
|
||||
emitter.on "test_listener", () -> return
|
||||
|
||||
expect(scope._tgEmitter.listeners("test_listener")).to.have.length(1)
|
||||
|
||||
expect(scopeEvent.scopes['test']).to.be.ok
|
||||
scope.$destroy()
|
||||
|
||||
expect(scopeEvent.scopes['test']).to.be.undefined
|
||||
expect(scope._tgEmitter.listeners("test_listener")).to.have.length(0)
|
Loading…
Reference in New Issue