Js observer mode publishing/subscription

Source: Internet
Author: User

Js observer mode publishing/subscription
In the past, when writing pages, there were never so many asynchronous operations. I only learned about asynchronous programming when using 'setinterval' and 'xmlhttprequest. In the course of learning node this time, it is clear that asynchronous failback is used. But when coding and writing code, it falls into the callback hell ). Es6Promise is used to solve some problems. We also see the event-driven solution of the EventProxy module. In the process of using EventProxy, there is a feeling of familiarity. It is a bit like publishing/subscribing in observer mode. First, the array is used to cache the subscriber's subscribed messages. When the subscriber subscribes to a message, it pushes the subscribed message to the queue of the specified message. When the publisher publishes the message, we traverse and execute the push to the callback event in the specified message queue. The subscriber does not need to care when the publisher publishes messages. The publisher does not need to care about the subscriber subscription status.

Var observer = new Observe (); var callback = function (num) {console. log ("event:" + num); // output event: 2}; // subscribe to the Message observer. listen ("event1", callback); observer. listen ("event2", callback); // remove the subscribe message 1observer. remove ('event1', callback); // The publisher publishes a message to the observer. trigger ("event1", 1); observer. trigger ("event2", 2); the Code is as follows: function Observe () {// cache the subscriber's message queue this. _ list = [];}/*** subscriber subscribes to a message * @ param {string} key message name * @ par Am {Function} fn callback event * @ return {Null} */Observe. prototype. listen = function (key, fn) {if (! This. _ list [key]) {this. _ list [key] = [];} // subscribe to a message and add it to the cache list. this. _ list [key]. push (fn );}; /***** remove the subscribed message * @ param {string} key message name * @ param {Function} fn return event * @ return {Null} */Observe. prototype. remove = function (key, fn) {// obtain the message record var arrFn = this under the current key. _ list [key]; if (! ArrFn) return false; // if (! Fn) {arrFn. length = 0;} else {for (var I = 0; I <arrFn. length; I ++) {if (fn = arrFn [I]) {arrFn. splice (I, 1 );}}}}; /*** publish a message by the publisher * @ param {string} key message name * @ param {string | Object} message data * @ return {Null} */Observe. prototype. trigger = function (key, aa) {// get the key. The second and above parameters var key = Array. prototype. shift. call (arguments); var args = arguments; var arrFn = this. _ list [key]; if (! ArrFn | arrFn. length = 0) {return;} // traverses and executes all messages under the current key for (var I = 0; I <arrFn. length; I ++) {arrFn [I]. apply (this, args );}};

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.