Outfire Posted October 12, 2017 Share Posted October 12, 2017 RemoveListener is works, but only in one way: let spine // our spine animation let log = () => console.log('nonsense'); let listener = {complete: log}; spine.state.addListener(listener); And removes it like spine.state.removeListener(listener); It does not work if i do it like this: let spine //our spine animation let log = () => console.log('nonsense'); spine.state.addListener({complete: log}); // trying to remove spine.state.addListener(log); // or just like this spine.state.addListener({complete: log}); Works fine if add and remove only the same object, but its long way. Is there another way to do this? ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted October 12, 2017 Share Posted October 12, 2017 I like the idea. This is the part in pixi-spine: https://github.com/pixijs/pixi-spine/blob/master/src/core/AnimationState.ts#L561 This is upstream, spine-ts: https://github.com/EsotericSoftware/spine-runtimes/blob/3.6/spine-ts/core/src/AnimationState.ts#L561 You can hack pixi-spine: PIXI.spine.core.AnimationState.prototype.removeListener = function (...) { } Please make PR or at least give me the code with the behaviour you want. I'm all hands for enhancing pixi-spine, we already have some things that distinguish us from spine-ts. Quote Link to comment Share on other sites More sharing options...
Outfire Posted October 12, 2017 Author Share Posted October 12, 2017 Need some time to think about it. I think about "spine.removeListener({complete: log})" But we can add "log" twice or more in one "complete", and then our removeListener must remove all "log"s in "complete" or just the last one. Maybe it is possible to do like in DOM addEventListener, when you add new listener on the same event with the same listener name it removes old one and push new one instead of it . Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted October 12, 2017 Share Posted October 12, 2017 "removes old one" is bad idea because that'll break spine-ts compatibility. You can add your own version with add/remove under different name. Outfire 1 Quote Link to comment Share on other sites More sharing options...
Outfire Posted October 12, 2017 Author Share Posted October 12, 2017 Yes, it is my problem if I forget that I already have listener with the same name. I think variant - "spine.removeListener({complete: 'log'})" (event name and listener name) - is the best way and pretty obvious one. And "spine.removeListener({complete: ['log', 'update'], start: 'playSound'})" to remove multiple events. Perhaps is it possible to upgrade clearListeners function , so it takes event name as an argument and as result removes all listeners subscribed to this particular event? Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted October 12, 2017 Share Posted October 12, 2017 OK, try to hack pixi-spine, and if it works for you and satisfies me, i'll add it to the lib I remind you that canonical spine runtimes does not have this feature, so it'll be sugar for pixi-spine. Outfire 1 Quote Link to comment Share on other sites More sharing options...
Outfire Posted October 30, 2017 Author Share Posted October 30, 2017 I considered on how to implement function to remove listeners, and here is the result. The following code can be used to remove function from particular listener. PIXI.spine.core.AnimationState.prototype.removeListenersByName = function (findListener) { this.listeners.forEach( listener => { for(let currentListenerName in listener) { for(let findListenerName in findListener) { if (currentListenerName === findListenerName && listener[currentListenerName].name === findListener[findListenerName]) { if(Object.keys(listener).length === 1) { this.removeListener(listener); } else { delete listener[currentListenerName] } } } } }) } spine.addListener({ complete: foo, end: bar }) removeListenersByName({ complete: 'foo' }) removeListenersByName({ complete: 'foo', end: 'bar' }) But this solution has one problem. spine.addListener({ complete: foo.bind(this)}) Then listeners[0].complete.name will be 'bound foo', not 'foo'.This problem can be solved in the following way. PIXI.spine.core.AnimationState.prototype.isBounded = function (listenerName, findName) { let splited = listenerName.split(' '); if (splited.length === 1) { return false; } return splited[0] === 'bound' && splited[1] === findName } PIXI.spine.core.AnimationState.prototype.removeListenersByName = function (findListener) { this.listeners.forEach( listener => { for(let currentListenerName in listener) { for(let findListenerName in findListener) { if (currentListenerName === findListenerName) { let currentName = listener[currentListenerName].name; let findName = findListener[findListenerName]; if (this.isBounded(currentName, findName)) { findName = 'bound ' + findName; } if (currentName === findName) { if(Object.keys(listener).length === 1) { this.removeListener(listener); } else { delete listener[currentListenerName] } } } } } }) }; Some other issue can appear. spine.addListener({complete: () => console.log('something') }); Here listeners[0].complete.name will be 'complete', as the name of the listener, to which function it was bound. But it's just information. To remove all functions from particular listener. PIXI.spine.core.AnimationState.prototype.clearListenersByName = function (listenerName) { this.listeners.forEach( listener => { for(let key in listener) { if (key === listenerName) { if(Object.keys(listener).length === 1) { this.removeListener(listener); } else { delete listener[key] } } } }) } spine.addListener({ complete: foo, end: bar }) spine.addListener({ complete: foo}) clearListenersByName('complete') That is the result of my small expierence in js. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.