Skip to content

Commit

Permalink
Improve emit API
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed May 11, 2024
1 parent 87bfb35 commit 3496584
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 58 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ module.exports = {
import React, { useState } from "react";
import {
webViewRender,
emit,
emitToNative,
useNativeMessage,
} from "react-native-react-bridge/lib/web";
// Importing css is supported
Expand All @@ -189,7 +189,7 @@ const Root = () => {
// emit sends message to React Native
// type: event name
// data: some data which will be serialized by JSON.stringify
emit({ type: "hello", data: 123 });
emitToNative({ type: "hello", data: 123 });
}}
/>
</div>
Expand All @@ -208,7 +208,7 @@ export default webViewRender(<Root />);

import React from "react";
import WebView from "react-native-webview";
import { emit, useWebViewMessage } from "react-native-react-bridge";
import { emitToWebView, useWebViewMessage } from "react-native-react-bridge";
import webApp from "./WebApp";

const App = () => {
Expand All @@ -220,7 +220,7 @@ const App = () => {
// type: event name
// data: some data which will be serialized by JSON.stringify
if (message.type === "hello" && message.data === 123) {
emit(ref, { type: "success", data: "succeeded!" });
emitToWebView(ref, { type: "success", data: "succeeded!" });
}
});

Expand Down
4 changes: 2 additions & 2 deletions examples/DemoApp/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
TextInput,
} from 'react-native';
import WebView from 'react-native-webview';
import {emit, useWebViewMessage} from 'react-native-react-bridge';
import {emitToWebView, useWebViewMessage} from 'react-native-react-bridge';
import webApp from './WebApp';

const App = () => {
Expand Down Expand Up @@ -45,7 +45,7 @@ const App = () => {
value={data}
/>
<Pressable
onPress={() => emit(ref, {type: 'hello', data: data})}
onPress={() => emitToWebView(ref, {type: 'hello', data: data})}
style={styles.button}>
<Text>send to Web</Text>
</Pressable>
Expand Down
4 changes: 2 additions & 2 deletions examples/DemoApp/WebApp/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useState} from 'react';
import {
webViewRender,
emit,
emitToNative,
useNativeMessage,
} from 'react-native-react-bridge/lib/web';
import './example.css';
Expand Down Expand Up @@ -29,7 +29,7 @@ const Root = () => {
</div>
<textarea value={data} onChange={e => setData(e.target.value)} />
<div>
<Button onClick={() => emit({type: 'hi', data: data})}>
<Button onClick={() => emitToNative({type: 'hi', data: data})}>
send to React Native
</Button>
</div>
Expand Down
4 changes: 2 additions & 2 deletions examples/DemoAppExpo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
TextInput,
} from "react-native";
import WebView from "react-native-webview";
import { emit, useWebViewMessage } from "react-native-react-bridge";
import { emitToWebView, useWebViewMessage } from "react-native-react-bridge";
import webApp from "./WebApp";

const App = () => {
Expand Down Expand Up @@ -45,7 +45,7 @@ const App = () => {
value={data}
/>
<Pressable
onPress={() => emit(ref, { type: "hello", data: data })}
onPress={() => emitToWebView(ref, { type: "hello", data: data })}
style={styles.button}
>
<Text>send to Web</Text>
Expand Down
32 changes: 16 additions & 16 deletions examples/DemoAppExpo/WebApp/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import React, {useState} from 'react';
import React, { useState } from "react";
import {
webViewRender,
emit,
emitToNative,
useNativeMessage,
} from 'react-native-react-bridge/lib/web';
import './example.css';
import png from './Octocat.png';
import {Button} from './button';
} from "react-native-react-bridge/lib/web";
import "./example.css";
import png from "./Octocat.png";
import { Button } from "./button";

const style = {
width: '100vw',
height: '100vh',
margin: 'auto',
backgroundColor: 'lightblue',
width: "100vw",
height: "100vh",
margin: "auto",
backgroundColor: "lightblue",
};

const Root = () => {
const [data, setData] = useState('This is Web');
useNativeMessage(message => {
if (message.type === 'hello') {
const [data, setData] = useState("This is Web");
useNativeMessage((message) => {
if (message.type === "hello") {
setData(message.data);
}
});
return (
<div style={style}>
<div>
<img src={png} width={100} height={'auto'} />
<img src={png} width={100} height={"auto"} />
</div>
<textarea value={data} onChange={e => setData(e.target.value)} />
<textarea value={data} onChange={(e) => setData(e.target.value)} />
<div>
<Button onClick={() => emit({type: 'hi', data: data})}>
<Button onClick={() => emitToNative({ type: "hi", data: data })}>
send to React Native
</Button>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* @module
*/
export type { ReactNativeMessage, WebViewMessage } from "./types";
export { useWebViewMessage } from "./native";
export { useWebViewMessage, emitToWebView } from "./native";
2 changes: 1 addition & 1 deletion src/native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const useWebViewMessage = <T>(
/**
* A function to send a message to WebView.
*/
export const emit = <T>(
export const emitToWebView = <T>(
ref: RefObject<WebView>,
message: ReactNativeMessage<T>
) => {
Expand Down
48 changes: 24 additions & 24 deletions src/plugin/__snapshots__/bundler.spec.ts.snap

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/web/preact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
*/
import { useEffect } from "preact/compat";
import { render, ComponentChild } from "preact";
import { listenNativeMessage, emitToNative, getWebViewRootElement } from "./core";
import {
listenNativeMessage,
emitToNative,
getWebViewRootElement,
} from "./core";
import type { ReactNativeMessage } from "../types";

/**
Expand All @@ -28,4 +32,4 @@ export const useNativeMessage = <T>(
useEffect(() => listenNativeMessage(onSubscribe), [onSubscribe]);
};

export { emitToNative as emit };
export { emitToNative };
4 changes: 2 additions & 2 deletions src/web/react.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment jsdom
import { afterEach, describe, expect, it } from "vitest";
import { cleanup, render, screen, waitFor } from "@testing-library/react";
import { useNativeMessage, emit } from "./react";
import { useNativeMessage, emitToNative } from "./react";
import { useWebViewMessage, buildEmitToWebView } from "../native";
import { useEffect, useState } from "react";
import { ReactNativeMessage, WebViewMessage } from "../types";
Expand Down Expand Up @@ -88,7 +88,7 @@ describe("send message to native", () => {
return (
<button
onClick={() => {
emit(message);
emitToNative(message);
}}
>
send
Expand Down
8 changes: 6 additions & 2 deletions src/web/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import { ReactElement, useEffect } from "react";
import { render } from "react-dom";
import { createRoot } from "react-dom/client";
import { emitToNative, getWebViewRootElement, listenNativeMessage } from "./core";
import {
emitToNative,
getWebViewRootElement,
listenNativeMessage,
} from "./core";
import type { ReactNativeMessage } from "../types";

/**
Expand Down Expand Up @@ -37,4 +41,4 @@ export const useNativeMessage = <T>(
useEffect(() => listenNativeMessage(onSubscribe), [onSubscribe]);
};

export { emitToNative as emit };
export { emitToNative };

0 comments on commit 3496584

Please sign in to comment.