javascript - YUI3 custom async events not working on Y.Global -
i trying implement async event leveraging yui3 library. application had been notified event passed late subscription, simular load or ready events do.
here is have far, no luck around.
yui().use('event', 'event-custom', function(y){ function oncustomevent () { y.global.on('custom:event', function(){ alert('custom fired'); }); } window.settimeout(oncustomevent, 2000); }); yui().use('event', 'event-custom', function(y){ y.publish('custom:event', { emitfacade: true, broadcast: 2, fireonce: true, async: true }); function firecustomevent () { y.global.fire('custom:event'); } window.settimeout(firecustomevent, 1000); });
if give hint what's wrong code? thank you.
upd:
after bit investigations turns out async events work fine inside 1 use()
instance , when not using global
broadcasting. that's either bug or limitation. still discovering
okay, @ high level inconsistency global events (how understood it) lays in sandbox nature of y object. @ point fire sync events globally cause async parameters subscribe
custom event made on y instance , not passed further (and yui uses defaults or whatever). possibly makes sense why such kind of events should fireable globally? either miss substantial part of yui , candidate bug report.
anyway not have time dive deeper in yui , what practically need wrapped in 40 lines of code:
yui.add('async-pubsub', function(y) { var subscribers = {}; if ( !yui.asyncpubsub ) { yui.namespace('asyncpubsub'); yui.asyncpubsub = (function(){ var eventsfired = {}; function dopublishfor(name) { var subscriber; ( subscriber in subscribers ) { if ( subscriber === name ) { (subscribers[name]).call(); delete ( subscribers[name] ); // keep planet clean } } } return { 'publish': function(name, options) { eventsfired[name] = options || {}; dopublishfor(name); }, 'subscribe': function(name, callback) { if ( subscribers[name] ) { y.log('more 1 async subscriber per instance, overriding it.', 'warning', 'async-pubsub'); } subscribers[name] = callback || function() {}; if ( eventsfired[name] ) { window.settimeout( function () { dopublishfor(name); },0 ); } } }; })(); } y.asyncpubsub = yui.asyncpubsub; }, '1.0', {requires: []});
there limitation , room optimization here, ability subscribe 1 action 1 event per use
instance, not need more. try debug , enhance snippet in future if there interest.
still curious yui behavior, bug or something?
Comments
Post a Comment