4 Star 1 Fork 0

Gitee 极速下载/hubjs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/yyued/hub.js
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Flow Hub

NPM Size

为什么使用

满足绝大部分情况事件驱动的情况,适合用于处理各种事件流,简单、轻量 (gzip 2kb) 的 JS 库。

对于各种组件系统的框架,如:React、Vue.js 等等,非父子组件之间的通讯是一件麻烦的事情,但通过使用 flowhub 会变得轻松简单。

安装

npm i flowhub --save

或者

<script src="https://cdn.jsdelivr.net/npm/flowhub/dist/flowhub.min.js"></script>

简单使用

import $hub from 'flowhub';

// 注册监听者
$hub.on('test', data => console.log('test', data))

setInterval(() => {
  // 发送 'test' 事件
  $hub.emit('test', { code: 1 })
}, 1000)

更多

移除监听

const listener = $hub.on('test', data => console.log(data))

$hub.emit('test', { code: 1 })

listener.off()
// 或者
// $hub.off('test', listener)

$hub.emit('test', { code: 2 })

单次

const listener = $hub.once('test', data => console.log(data))

$hub.emit('test', { code: 1 })

$hub.emit('test', { code: 2 })

复数

const listener = $hub.on(['test', 'test-1', 'test-2'], data => console.log(data))

$hub.emit(['test', 'test-1', 'test-2'], { code: 1 })

listener.off()
// 或者
// $hub.off(['test', 'test-1' ], listener)

$hub.emit(['test', 'test-1', 'test-2'], { code: 2 })

注意,监听者会接收到每次适配到的事件源发生。例如上述例子,则会产生三条日志。

Proxy Store

// 设置 store 值
$hub.store.code = 1

// 监听 store 里具体 某个数值
// 若 这个数值已存在 “当前值”,则监听成功后,立即返回 “当前值”,就像 Rx.BehaviorSubject
$hub.on('@store/code', data => console.log('store code', data))

setInterval(() => {
    ++$hub.store.code
    // 或者
    // $hub.emit('@store/code', 1)
}, 1000)

DOM 元素

const dispatcher = $hub.DOM('button')
                        .from('click')
                        .emit('dom-click-event')
                        .from('mousedown')
                        .emit('dom-mousedown-event')

$hub.on('dom-click-event', event => console.log('button click', event))

$hub.on('dom-mousedown-event', event => console.log('button mousedown', event))

setTimeout(dispatcher.off, 10000)

Fetch

const dispatcher = $hub.Fetch('https://xxxxx')
                        .emit('fetch-event1')
                        .emit('fetch-event2')

setTimeout(dispatcher.reload, 2000)

$hub.on('fetch-event1', result => console.log('fetch1', result))

$hub.on('fetch-event2', result => console.log('fetch2', result))

WebSocket

const dispatcher = $hub.WS('ws://xxxxx')
                        .emit('ws-event1')
                        .emit('ws-event2')

$hub.on('ws-event1', result => console.log('ws1: ', result))
$hub.on('ws-event2', result => console.log('ws2: ', result))

setTimeout(dispatcher.off, 3000)

socket.io

<script src="./lib/socket.io.min.js"></script>

<script>
const dispatcher = $hub.IO('http://xxxxx')
                        .from('mock')
                        .emit('io-event')

dispatcher.socket.emit('mock', { key: 'xxxxx' })

$hub.on('io-event', result => console.log('io:', result))

setTimeout(dispatcher.off, 3000)
</script>

通道链式

$hub.chain('test')
        .pipe(
          d => new Promise(resolve => setTimeout(() => resolve(d + 1), 2000)),
          d => d + 2,
          d => d + 3
        )
        .pipe(d => d + 3)

$hub.on('@chain/test', d => console.log(d))

$hub.emit('@chain/test', 1) // 10

链式 & 转换器

// 注册转换器
$hub.converter.DOMEventFormat1 = function (event) {
  return [event.type, event.target]
}
$hub.converter.DOMEventFormat2 = function (event) {
  return [e.target, e.type]
}

// 可以通过自由组合 链式 衔接的顺序,进行对流的控制,从而达到你想要的效果
const dispatcher = $hub.DOM('button')
                        .from('click')
                        .convert('DOMEventFormat1')
                        .emit('dom-click-event')
                        .from('mousedown')
                        .convert('DOMEventFormat1')
                        .emit('dom-mousedown-event')
// 或者
// $hub.DOM('button').from('click').convert('DOMEventFormat1' ).emit('dom-click-event1').emit('dom-click-event2' )
// $hub.DOM('button').from('click').convert('DOMEventFormat1' ).convert('DOMEventFormat2').emit('dom-click-event1' )

// 其他
// $hub.Fetch('https://xxx' ).emit('e1').convert('converter' ).emit('e2')
// $hub.WS('ws://xxx' ).emit('e1').convert('converter' ).emit('e2')
// $hub.IO('https://xxx').from('x1' ).convert('converter').emit('e1' ).from('x2').emit('e1' )

$hub.on('dom-click-event', event => console.log('button click', event))

$hub.on('dom-mousedown-event', event => console.log('button mousedown', event))

setTimeout(dispatcher.off, 10000)

许可

MIT

MIT License Copyright (c) 2017 YY UED Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

通过简单的方式去处理来自 自定义发布者 / DOM 元素 / Fetch 请求 / WebSocket / socket io 事件流 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者 (2)

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mirrors/hubjs.git
git@gitee.com:mirrors/hubjs.git
mirrors
hubjs
hubjs
master

搜索帮助