• Stars
    star
    110
  • Rank 316,770 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Through a simple way to deal with the event flow.

Flow Hub

NPM Size

Why

flowhub is more simpler and lightweight (gzip 2kb). It satisfies most of the situation event-driven situation, suitable for dealing with a variety of event streams.

For frameworks of component systems, such as React, Vue.js, etc., communication between non parent and child components is a bothering thing, but it can be made easy by using flowhub.

简体中文

Installing

npm i flowhub --save

or

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

Simple Usage

import $hub from 'flowhub';

// register an event listener
$hub.on('test', data => console.log('test', data))

setInterval(() => {
  // send the 'test' event
  $hub.emit('test', { code: 1 })
}, 1000)

More

Remove listener

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

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

listener.off()
// or
// $hub.off('test', listener)

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

once

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

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

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

Multiple

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


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

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

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

Note that, the listener receives each time the adapted event source occurs. For example, the above example will produce three logs.

Proxy Store

// set store value
$hub.store.code = 1

// listen store  value
// if this value already has a "current value," the listener immediately returns the "current value," just as Rx.BehaviorSubject
$hub.on('@store/code', data => console.log('store code', data)

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

DOM Element

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>

Chain Pipe

$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

Chaining & Converter

// register converter
$hub.converter.DOMEventFormat1 = function (event) {
  return [e.type, e.target]
}
$hub.converter.DOMEventFormat2 = function (event) {
  return [e.target, e.type]
}

// you can control the convection by free combination of chaining, so as to get the effect you want.
const dispatcher = $hub.DOM('button')
                        .from('click')
                        .convert('DOMEventFormat1')
                        .emit('dom-click-event')
                        .from('mousedown')
                        .convert('DOMEventFormat1')
                        .emit('dom-mousedown-event')
// or
// $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')

// other
// $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)

License

MIT

More Repositories

1

TwitterPaggingViewer

A twitter like navigation bar, page viewer.
Objective-C
356
star
2

SVG-Skeleton

Create the skeleton screen through SVG element
JavaScript
167
star
3

XCode-Color-Fixer

StoryBoard / XIB 颜色偏差很严重,怎么破?XCode-Color-Fixer帮你忙!
Objective-C
125
star
4

fe-guide

Joyy UED 前端规范指南
92
star
5

MagicDrag

MagicDrag is an application introducing page components build for Interactive Animations.
Swift
45
star
6

CRChecker

CRChecker is a debug tool, helps you find out circular reference problem.
Objective-C
39
star
7

cherry

一款高效、好用的图片处理工具(https://yyued.github.io/cherry/)
HTML
34
star
8

generator-lego

F2E workflow, base on yeoman & gulp
CSS
22
star
9

legolib

面向移动端的轻量级前端 Components,提供易用、可靠的解决方案
CSS
20
star
10

LEGO-Mobile

LEGO移动前端开发框架,提供简单、快速的 Web 开发体验
JavaScript
18
star
11

H5Live

JavaScript
15
star
12

grunt-workflow

这是一个帮助前端开发工程师简化工作的工具
CSS
13
star
13

lottery

一个浏览器端抽奖动画js组件
JavaScript
12
star
14

ENMetadataCleaner

Decrease PSD file size.
JavaScript
11
star
15

LegoUI-pc

LegoUI, 一套基于BEM的轻量级前端UI库
CSS
8
star
16

LegoUI-mobi

CSS
6
star
17

ENAutoBackUpTool

JavaScript
6
star
18

UIKit-Android

Kotlin
6
star
19

nice-commit

规范 Git Commit
JavaScript
6
star
20

share-sheet

share sheet
Vue
5
star
21

fekit.crx

f2e toolkit chrome-extension dev env
JavaScript
4
star
22

YFT

create Image and Label from Character-Bitmap.
Objective-C
3
star
23

value-animator

TypeScript
3
star
24

Lego-Snippets

LegoUI Snippets for Sublime Text 2/3
2
star
25

fe-solution

前端解决方案
JavaScript
2
star
26

PSD-Etiquette

PSD图层管理指南
2
star
27

fekit

f2e dev toolkit
JavaScript
1
star
28

document-template

The project document template
1
star
29

LEGO-Pack

Just pack a folder and generate md5 hash.
JavaScript
1
star
30

CodeX

CodeX helps you build User Interface from PRD to design, and generate code via Sketch.
JavaScript
1
star