訂閱者/發布者模式,自定義消息類型的工具庫:Msg

jopen 10年前發布 | 13K 次閱讀 Msg 消息系統

關于Msg

Msg是基于訂閱者/發布者模式的事件庫,事件被抽象為消息訂閱/消息傳播模式。Msg 為 Message 的縮寫。

API介紹

Msg 的API 比較簡單,其介紹都濃縮在test.html頁面中,將js部分粘貼如下

    (function() {

        var test = new Msg()

        //test on

        //普通綁定
        test.on('msg1', function() {
            console.log(arguments)
        })

        //多次綁定
        test.on('msg1', function() {
            console.log(arguments)
            console.log('msg1 二次綁定')
        })

        //批量綁定
        test.on(['msg2', 'msg3', 'msg4', 'msg5', 'msg6'], function() {
            console.log(arguments)
        })

        console.log(test)

        //test spread

        //觸發全部
        test.spread()

        //觸發全部并攜帶數據
        test.spread(null, '觸發全部并攜帶數據 * 1', '觸發全部并攜帶數據l * 2')

        //觸發一類消息
        test.spread('msg1', '觸發一類消息 msg1 * 1')

        //觸發一組消息類型
        test.spread(['msg2', 'msg3', 'msg4', 'msg1'], '觸發一組消息類型')


        //test once

        test.once('test_once', function(msg) {
            console.log('觸發一次后,接觸綁定 ' + msg)
        })

        test.on('test_once', function(msg) {
            console.log('雖然也是test_once,但用on 綁定,所以還在綁定中 \n' + msg)
        })


        test.spread('test_once', 'test_once done!')

        setTimeout(function() {
            test.spread('test_once', '測試是否解除了 once 的綁定,由于是異步刪除,所以也得異步測試')
        }, 10)


        // test hold

        test.hold('test_hold', 5, function(times) {
            console.log('hold 方法可以掛起事件,在觸發次數達到指定次數之后, 才作出反應:' + times)
        })


        test.spread('test_hold', 1)
        test.spread('test_hold', 2)
        test.spread('test_hold', 3)
        test.spread('test_hold', 4)
        test.spread('test_hold', 5)
        test.spread('test_hold', '達到次數后,就可以自如啟動反應了')

        //test tie

        //設置命名空間,可以方便的用 test.off('.nameSpace') 一次性解除
        test.tie(['tie1.nameSpace', 'tie2.nameSpace', 'tie3.nameSpace', 'tie4.nameSpace'], function(tie1, tie2, tie3, tie4) {
            console.log('tie 方法綁定所有事件,在它們至少都被觸發過一次之后,才產生反應')
            console.log(arguments)
        })

        test.spread('tie2', 'tie2 data', 'tie2 data', 'tie2 data')
        test.spread('tie4', 'tie4 data')
        test.spread('tie1', 'tie1 data')
        test.spread('tie3', 'tie3 data do')
        test.spread('tie3', 'tie3 data done')

        //test tick

        test.tick('msg2', 'tick 是異步啟動')
        test.spread('msg2', 'spread 是同步啟動,所以這條消息應該在tick之前')

        //test delay

        test.delay(1000, 'msg2', 'delay 是延遲啟動,這條消息在1000后才發出來')

        //test off

        //取消一類消息
        test.off('msg3')
        test.spread('msg3', 'msg2已經被取消,這條消息不會出現')

        //根據命名空間取消消息反應
        test.off('.nameSpace')

        //取消一組消息類型
        test.off(['msg1', 'msg4', 'msg5'])
        test.spread(['msg1', 'msg4', 'msg5', 'msg6'], '這組消息隊列大多被取消了,只剩msg6')

        //取消所有消息反應
        setTimeout(function() {
            test.off()
            test.spread(null, '前面有測試延遲啟動,時常為1秒,所以取消所有消息反應的測試,也應在1秒后進行,這條消息不會出現在控制臺,因為所有消息反應都被取消')
        }, 1000)


    }());

項目主頁:http://www.baiduhome.net/lib/view/home/1416496691070

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!