Skip to content

Commit ee9056d

Browse files
author
jph
committed
v1
0 parents  commit ee9056d

File tree

6 files changed

+266
-0
lines changed

6 files changed

+266
-0
lines changed

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Compiled binary addons (http://nodejs.org/api/addons.html)
27+
build/Release
28+
29+
# Dependency directories
30+
node_modules
31+
jspm_packages
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history
38+
39+
40+
.idea

.npmignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.idea
2+
Screenshots
3+
.gif
4+
5+
# OSX
6+
#
7+
.DS_Store
8+
9+
# Xcode
10+
#
11+
build/
12+
*.pbxuser
13+
!default.pbxuser
14+
*.mode1v3
15+
!default.mode1v3
16+
*.mode2v3
17+
!default.mode2v3
18+
*.perspectivev3
19+
!default.perspectivev3
20+
xcuserdata
21+
*.xccheckout
22+
*.moved-aside
23+
DerivedData
24+
*.hmap
25+
*.ipa
26+
*.xcuserstate
27+
28+
# node.js
29+
#
30+
node_modules/
31+
npm-debug.log
32+
examples/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Jia PengHui
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# react-native-event-bus
2+
3+
[ ![release](https://img.shields.io/github/release/crazycodeboy/react-native-event-bus.svg?maxAge=2592000?style=flat-square)](https://github.com/crazycodeboy/react-native-event-bus/releases)
4+
[ ![PRs Welcome](https://img.shields.io/badge/PRs-Welcome-brightgreen.svg)](https://github.com/crazycodeboy/react-native-event-bus/pulls)
5+
[ ![NPM version](http://img.shields.io/npm/v/react-native-event-bus.svg?style=flat)](https://www.npmjs.com/package/react-native-event-bus)
6+
[![License MIT](http://img.shields.io/badge/license-MIT-orange.svg?style=flat)](https://raw.githubusercontent.com/crazycodeboy/react-native-event-bus/master/LICENSE)
7+
8+
Event bus for react native, cross-interface communication solution, it works on iOS and Android.
9+
10+
## Content
11+
12+
- [Installation](#installation)
13+
- [Getting started](#getting-started)
14+
- [API](#api)
15+
- [Contribution](#contribution)
16+
17+
18+
## Installation
19+
20+
* 1.Run `npm i react-native-event-bus --save`
21+
* 2.`import EventBus from 'react-native-event-bus'`
22+
23+
## Getting started
24+
25+
Add `react-native-event-bus` to your js file.
26+
27+
`import EventBus from 'react-native-event-bus'`
28+
29+
Inside your component's render method, use CheckBox:
30+
31+
### fireEvent
32+
33+
```javascript
34+
EventBus.getInstance().fireEvent("your event name", {
35+
...params
36+
})
37+
38+
//
39+
```
40+
41+
### addListener
42+
43+
```
44+
componentDidMount() {
45+
EventBus.getInstance().addListener("your event name", this.listener = data => {
46+
// handle the event
47+
})
48+
}
49+
50+
componentWillUnmount() {
51+
EventBus.getInstance().removeListener(this.listener);
52+
}
53+
```
54+
55+
## Contribution
56+
57+
Issues are welcome. Please add a screenshot of bug and code snippet. Quickest way to solve issue is to reproduce it on one of the examples.
58+
59+
Pull requests are welcome. If you want to change API or making something big better to create issue and discuss it first.
60+
61+
---
62+
63+
**MIT Licensed**

index.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* 事件总线,跨界面通信解决方案
3+
* {eventName1:[listener1,listener2],eventName2:[listener3,listener4]}
4+
*/
5+
export default class EventBus {
6+
static getInstance() {
7+
if (typeof EventBus.instance === 'object') {
8+
return EventBus.instance;
9+
}
10+
return new EventBus();
11+
}
12+
13+
constructor() {
14+
if (typeof EventBus.instance === 'object') {
15+
return EventBus.instance;
16+
}
17+
EventBus.instance = this;
18+
this.eventListeners = {};
19+
}
20+
21+
/**
22+
* 发送事件
23+
* @param eventName 事件名 string
24+
* @param data 要发送的数据
25+
*/
26+
fireEvent(eventName, data) {
27+
let listeners = this.eventListeners[eventName];
28+
if (Array.isArray(listeners)) {
29+
listeners.map(listener => {
30+
if (typeof listener === 'function') {
31+
listener(data);
32+
}
33+
})
34+
}
35+
}
36+
37+
/**
38+
* 注册事件监听器
39+
* @param eventName 事件名 string
40+
* @param listener 监听回调 function
41+
*/
42+
addListener(eventName, listener) {
43+
let listeners = this.eventListeners[eventName];
44+
if (Array.isArray(listeners)) {
45+
listeners.push(listener);
46+
} else {
47+
this.eventListeners[eventName] = [listener];
48+
}
49+
}
50+
51+
/**
52+
* 移除监听器
53+
* @param listener 监听回调 function
54+
*/
55+
removeListener(listener) {
56+
Object.keys(this.eventListeners).map(eventName => {
57+
let listeners = this.eventListeners[eventName];
58+
this._remove(listeners, listener);
59+
if (listeners.length === 0) {
60+
delete this.eventListeners[eventName];
61+
}
62+
})
63+
}
64+
65+
/**
66+
* 将数组中指定元素移除
67+
* **/
68+
_remove(array, item) {
69+
if (!array) return;
70+
for (let i = 0, l = array.length; i < l; i++) {
71+
if (item === array[i]) array.splice(i, 1);
72+
}
73+
}
74+
}

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "react-native-event-bus",
3+
"version": "2.1.6",
4+
"description": "Checkbox component for react native, it works on iOS and Android.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/crazycodeboy/react-native-check-box.git"
12+
},
13+
"keywords": [
14+
"react-native",
15+
"react-native-component",
16+
"react-native-event-bus",
17+
"event-bus",
18+
"check",
19+
"box",
20+
"react-component",
21+
"ios",
22+
"android"
23+
],
24+
"author": "crazycodeboy",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/crazycodeboy/react-native-event-bus/issues"
28+
},
29+
"dependencies": {
30+
"prop-types": "^15.5.7"
31+
},
32+
"peerDependencies": {
33+
"react-native": ">=0.20.0"
34+
},
35+
"homepage": "https://github.com/crazycodeboy/react-native-event-bus#readme"
36+
}

0 commit comments

Comments
 (0)