npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-invoke

v0.2.2

Published

Invoke any native code directly from Javascript in React Native

Downloads

33

Readme

React Native Invoke

NPM Version NPM Downloads Build Status

Invoke any native code directly from Javascript in React Native (without wrapping it first with a native manager). Gives you full access from JS to all native API of iOS and Android.

Why

The story behind this library and why it might be useful: https://medium.com/@talkol/invoke-any-native-api-directly-from-pure-javascript-in-react-native-1fb6afcdf57d

Install

####Both Platforms

  • In your project root, npm install react-native-invoke --save or add it in your package.json:
  "dependencies": {
	...
    "react-native-invoke": "^0.2.0"
 }

iOS

  • In your project root, npm install react-native-invoke --save

  • In Xcode, in Project Navigator (left pane), right-click on the Libraries > Add files to [project name] Add ./node_modules/react-native-invoke/ios/RNInvoke.xcodeproj

  • In Xcode, in Project Navigator (left pane), click on your project (top) and select the Build Phases tab (right pane) In the Link Binary With Libraries section add libRNInvoke.a

  • In Xcode, in Project Navigator (left pane), click on your project (top) and select the Build Settings tab (right pane) In the Header Search Paths section add $(SRCROOT)/../node_modules/react-native-invoke/ios Make sure on the right to mark this new path recursive

Android

  • Add react-native-invoke from node_modules to your settings.gradle:

    include ':app'
    include ':react-native-invoke'
    project(':react-native-invoke').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-invoke/android/invoke')
  • In app/build.gradle add react-native-invoke as a dependency

    dependencies {
    	...
        compile project(':react-native-invoke')
    }

- In `YourApplication.java` register `InvokeReactPackage` in your pacakges:

	```java
	@Override
	protected List<ReactPackage> getPackages() {
	    return Arrays.<ReactPackage>asList(
	            new MainReactPackage(),
	            ...
	            new InvokeReactPackage()
	    );
	 }
	```

<br><br>
## Executing calls to native

> Notice that this is regular Javascript code. It has full access to all native API in iOS and there's no RN native manager involved wrapping each individual API call.

```js
import Invoke from 'react-native-invoke';

// execute a single call
const _getContentOffset = Invoke.call(_scrollView, 'contentOffset');
const {x, y} = await Invoke.execute(_getContentOffset);

Invoke.execute returns a promise. The native code doesn't actually execute until Invoke.execute runs.

// execute multiple calls
const _getScrollView = Invoke.call(_scrollParent, 'scrollView');
const _getContentOffset = Invoke.call(_getScrollView, 'contentOffset');
const {x, y} = await Invoke.execute(_getContentOffset);

Only simple serializable objects can pass between native and JS. Since many methods take a complex object as argument, we support making multiple calls in one execution so the result of one call can be passed to the next one without going through JS.

Example invocations

###iOS

1. from Objective-C
CGPoint offset = [componentView.scrollView contentOffset];
    to Javascript
const _getScrollView = Invoke.call(_componentView, 'scrollView');
const _getOffset = Invoke.call(_getScrollView, 'contentOffset');
const {x, y} = await Invoke.execute(_getOffset);
CGRect frame = componentView.frame;
    to Javascript
const _getFrame = Invoke.call(_componentView, 'frame');
let {x, y, width, height} = await Invoke.execute(_getFrame);
[componentView setFrame:frame];
    to Javascript
const _setFrame = Invoke.call(_componentView, 'setFrame:', Invoke.IOS.CGRect({x, y, width, height}));
await Invoke.execute(_setFrame);
id textView = [componentView valueForKey:@'_textView'];
CGRect pos = [textView caretRectForPosition:textView.selectedTextRange.start];
    to Javascript
const _getTextView = Invoke.call(_componentView, 'valueForKey:', '_textView');
const _getSelectedTextRange = Invoke.call(_getTextView, 'selectedTextRange');
const _getStartPosition = Invoke.call(_getSelectedTextRange, 'start');
const _getCaretRect = Invoke.call(_getTextView, 'caretRectForPosition:', _getStartPosition);
const {x, y, width, height} = await Invoke.execute(_getCaretRect);

###Android

1. from java
reactSwipeRefreshLayout.setRefreshing(false);
    to Javascript
const swipeRefreshLayout = Invoke.React.view(this.refs['refresh']);
const setRefreshing = Invoke.call(swipeRefreshLayout, 'setRefreshing', {type: "Boolean", value: false});
await Invoke.execute(setRefreshing);
2. from java
scrollView.getScrollY()
    to Javascript
const scrollView = Invoke.React.view(this.refs['scroll']);
const getScrollY = Invoke.call(scrollView, 'getScrollY');
const y = await Invoke.execute(getScrollY);
3. from java
textView.getSelectionEnd()
    to Javascript
const textView = Invoke.React.view(this.refs['input']);
const getSelectionEnd = Invoke.call(textView, 'getSelectionEnd');
const selectionEnd = await Invoke.execute(getSelectionEnd);

####iOS

cd example
npm install
react-native run-ios

or open ios/example.xcodeproj to open in xcode.

####Android

cd example
npm install
react-native run-android

####javascript

API

> Invoke.execute(invocation)

Send the entire invocation to native and execute it. Code runs in native only when we reach this command. Returns a promise with the final return value (make sure it's serializable).

> Invoke.call(target, methodSignature, arg1, arg2, ...)

Prepare a call for later execution.

> Invoke.React.view(componentRef)

Returns (in later execution) the native view backing the React component ref.Example:

<ScrollView refreshControl={<RefreshControl refreshing={true} ref='myRefreshControl'/>} />
const _componentView = Invoke.React.view(this.refs['myRefreshControl']);

Returns (in later execution) an iOS point.

> Invoke.IOS.CGRect({x, y, width, height})

Returns (in later execution) an iOS rect.

Notes

  • The final return value from native arrives as the promise result of Invoke.execute. It has to be serializable! If you have return values that aren't serializable (like complex objects), you probably need to have several Invoke.calls and pass them between eachother.

  • All native code is executed on the main thread.

MIT