Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
paramaggarwal committed Aug 27, 2015
1 parent d535312 commit f975989
Show file tree
Hide file tree
Showing 21 changed files with 1,621 additions and 25 deletions.
46 changes: 46 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[ignore]

# We fork some components by platform.
.*/*.web.js
.*/*.android.js

# Some modules have their own node_modules with overlap
.*/node_modules/node-haste/.*

# Ignore react-tools where there are overlaps, but don't ignore anything that
# react-native relies on
.*/node_modules/react-tools/src/React.js
.*/node_modules/react-tools/src/renderers/shared/event/EventPropagators.js
.*/node_modules/react-tools/src/renderers/shared/event/eventPlugins/ResponderEventPlugin.js
.*/node_modules/react-tools/src/shared/vendor/core/ExecutionEnvironment.js


# Ignore commoner tests
.*/node_modules/commoner/test/.*

# See https://github.com/facebook/flow/issues/442
.*/react-tools/node_modules/commoner/lib/reader.js

# Ignore jest
.*/react-native/node_modules/jest-cli/.*

[include]

[libs]
node_modules/react-native/Libraries/react-native/react-native-interface.js

[options]
module.system=haste

munge_underscores=true

suppress_type=$FlowIssue
suppress_type=$FlowFixMe
suppress_type=$FixMe

suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(>=0\\.\\(1[0-4]\\|[0-9]\\).[0-9]\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(>=0\\.\\(1[0-4]\\|[0-9]\\).[0-9]\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)? #[0-9]+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy

[version]
0.14.0
51 changes: 26 additions & 25 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
# Logs
logs
*.log
# OSX
#
.DS_Store

# Runtime data
pids
*.pid
*.seed
# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
# node.js
#
node_modules/
npm-debug.log
27 changes: 27 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate

# node.js
#
node_modules/
npm-debug.log
61 changes: 61 additions & 0 deletions GlobalEventEmitter.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

var React = require('react-native');
var {
NativeModules,
DeviceEventEmitter,
} = React;

var RNTGlobalEventEmitter = NativeModules.RNTGlobalEventEmitter;

var listeners = {};
DeviceEventEmitter.addListener('onNotification', (data) => {
var notifName = data.name;
var notifData = data.userInfo;

for (var i=0; i<listeners[notifName].length; i++) {
var listener = listeners[notifName][i];
listener(notifData);
};
});

function addListener(eventName, callback) {
listeners[eventName] = listeners[eventName] ? listeners[eventName].push(callback) : [callback];
RNTGlobalEventEmitter.addObserver(eventName);
};

function emit(eventName, data) {
RNTGlobalEventEmitter.postNotification(eventName, data);
};

function removeListener(eventName, callbackRef) {
var i = listeners[eventName].indexOf(callbackRef);
if (i != -1) {
listeners[eventName].splice(i, 1);
}

if (!listeners[eventName].length) {
removeAllListeners(eventName);
};
}

function removeAllListeners(eventName) {
RNTGlobalEventEmitter.removeObserver(eventName);
delete listeners[eventName];
if (!listeners.length) {
DeviceEventEmitter.removeAllListeners('onNotification');
};
}

var DeviceMotion = {
addListener,
emit,
removeListener,
removeAllListeners,
};

DeviceMotion.UIApplicationNotifications = RNTGlobalEventEmitter.UIApplicationNotifications;
DeviceMotion.UIWindowNotifications = RNTGlobalEventEmitter.UIWindowNotifications;
DeviceMotion.UIKeyboardNotifications = RNTGlobalEventEmitter.UIKeyboardNotifications;

module.exports = DeviceMotion;
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
# react-native-global-event-emitter
Shared event emitter between native and JS for React Native.

Uses iOS `NotificationCenter` and RN `DeviceEventEmitter` to provide a seamless global event bus between native and React Native.

## Screenshot

![Screenshot of the example app](https://github.com/paramaggarwal/react-native-global-event-emitter/raw/master/Screenshot.png)

## Usage

```javascript
// 60fps updates
DeviceMotion.startDeviceMotionUpdates(1000/60, (data) => {
console.log('Raw motion data: ', data);
});

// when done,
DeviceMotion.stopDeviceMotionUpdates();
```

## Properties

* `startDeviceMotionUpdates`: Pass an interval in `ms` and a callback to call with data.
* `stopDeviceMotionUpdates`: Call to stop listening for events.

## Installation

Use your preferred method of including the library in your app.

## Example
Try the included example:

```sh
git clone git@github.com:paramaggarwal/react-native-device-motion.git
npm install
open iOS/RNTDeviceMotion.xcodeproj
```

Then `Cmd+R` to start the React Packager, build and run the project in the simulator.

## Author
Param Aggarwal (paramaggarwal@gmail.com)

## License
MIT License
14 changes: 14 additions & 0 deletions RNTGlobalEventEmitter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// RNTGlobalEventEmitter.h
// RNTGlobalEventEmitter
//
// Created by Param Aggarwal on 27/08/15.
// Copyright (c) 2015 Facebook. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "RCTBridge.h"

@interface RNTGlobalEventEmitter : NSObject <RCTBridgeModule>

@end
101 changes: 101 additions & 0 deletions RNTGlobalEventEmitter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// RNTGlobalEventEmitter.m
// RNTGlobalEventEmitter
//
// Created by Param Aggarwal on 27/08/15.
// Copyright (c) 2015 Facebook. All rights reserved.
//

#import "RNTGlobalEventEmitter.h"
#import "RCTEventDispatcher.h"

@interface RNTGlobalEventEmitter ()

- (void)bridgeNotification:(NSNotification *)notification;

@end

@implementation RNTGlobalEventEmitter

@synthesize bridge = _bridge;

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(addObserver:(NSString *)notificationName)
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(bridgeNotification:)
name:notificationName
object:nil];
}

RCT_EXPORT_METHOD(postNotification:(NSString *)notificationName userInfo:(NSDictionary *)userInfo)
{
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName
object:nil
userInfo:userInfo];
}

RCT_EXPORT_METHOD(removeObserver:(NSString *)notificationName)
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:notificationName
object:nil];
}

- (void)bridgeNotification:(NSNotification *)notification
{
[self.bridge.eventDispatcher sendDeviceEventWithName:@"onNotification"
body:@{
@"name": notification.name,
@"userInfo": notification.userInfo ?: [NSNull null],
}];

}

- (NSDictionary *)constantsToExport
{
return @{
@"UIApplicationNotifications": @{
@"UIApplicationDidEnterBackgroundNotification": UIApplicationDidEnterBackgroundNotification,
@"UIApplicationWillEnterForegroundNotification": UIApplicationWillEnterForegroundNotification,
@"UIApplicationDidFinishLaunchingNotification": UIApplicationDidFinishLaunchingNotification,
@"UIApplicationDidBecomeActiveNotification": UIApplicationDidBecomeActiveNotification,
@"UIApplicationWillResignActiveNotification": UIApplicationWillResignActiveNotification,
@"UIApplicationDidReceiveMemoryWarningNotification": UIApplicationDidReceiveMemoryWarningNotification,
@"UIApplicationWillTerminateNotification": UIApplicationWillTerminateNotification,
@"UIApplicationSignificantTimeChangeNotification": UIApplicationSignificantTimeChangeNotification,
@"UIApplicationWillChangeStatusBarOrientationNotification": UIApplicationWillChangeStatusBarOrientationNotification,
@"UIApplicationDidChangeStatusBarOrientationNotification": UIApplicationDidChangeStatusBarOrientationNotification,
@"UIApplicationStatusBarOrientationUserInfoKey": UIApplicationStatusBarOrientationUserInfoKey,
@"UIApplicationWillChangeStatusBarFrameNotification": UIApplicationWillChangeStatusBarFrameNotification,
@"UIApplicationDidChangeStatusBarFrameNotification": UIApplicationDidChangeStatusBarFrameNotification,
},
@"UIWindowNotifications": @{
@"UIWindowDidBecomeVisibleNotification": UIWindowDidBecomeVisibleNotification,
@"UIWindowDidBecomeHiddenNotification": UIWindowDidBecomeHiddenNotification,
@"UIWindowDidBecomeKeyNotification": UIWindowDidBecomeKeyNotification,
@"UIWindowDidResignKeyNotification": UIWindowDidResignKeyNotification,
},
@"UIKeyboardNotifications": @{
@"UIKeyboardWillShowNotification": UIKeyboardWillShowNotification,
@"UIKeyboardDidShowNotification": UIKeyboardDidShowNotification,
@"UIKeyboardWillHideNotification": UIKeyboardWillHideNotification,
@"UIKeyboardDidHideNotification": UIKeyboardDidHideNotification,
@"UIKeyboardWillChangeFrameNotification": UIKeyboardWillChangeFrameNotification,
@"UIKeyboardDidChangeFrameNotification": UIKeyboardDidChangeFrameNotification,
},
};
}

- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
Binary file added Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f975989

Please sign in to comment.