Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove onNativeEvent #180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,34 +634,6 @@ Report a given feature status
reportStatus: ({feature: 'ACCOUNT', status: 'CRITICAL' | 'GOOD' | 'BAD', reason: string}) => Promise<void>;
```

### onNativeEvent

<kbd>App version >=11.3</kbd>

Listens to native app events

```ts
type NativeEventHandler = ({ event }: {event: string}) => {action: 'default'};

onNativeEvent: (handler: NativeEventHandler) => () => void;
```

#### Example

```ts
onNativeEvent(({event}) => {
if (event === 'tappedNavigationBarBackButton') {
// do something
}
return {action: 'default'};
});
```

#### Available events

- `tappedNavigationBarBackButton`: fired when the user taps on the back button
of the native Navigation Bar. Allowed response actions: `default`

### checkPermissionStatus

<kbd>App version >=11.4</kbd>
Expand Down
1 change: 0 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export {
isWebViewBridgeAvailable,
onNativeEvent,
NativeEventHandler,
setLogger,
} from './src/post-message';
Expand Down
40 changes: 0 additions & 40 deletions src/__tests__/post-message-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import '../post-message';
import {
isWebViewBridgeAvailable,
postMessageToNativeApp,
onNativeEvent,
NativeEventHandler,
} from '../post-message';
import {
createFakeAndroidPostMessage,
Expand Down Expand Up @@ -136,41 +134,3 @@ test('malformed json throws', () => {
window.__tuenti_webview_bridge!.postMessage('{bad;json}');
}).toThrow();
});

test('onNativeEvent subscription', () => {
const ANY_ID = '123';
const ANY_EVENT_NAME = 'any-event-name';

const request = {
type: 'NATIVE_EVENT',
id: ANY_ID,
payload: {event: ANY_EVENT_NAME},
};

const expectedResponse = {
type: 'NATIVE_EVENT',
id: ANY_ID,
payload: {action: 'default'},
};

createFakeWebKitPostMessage({
checkMessage: (msg) => {
expect(msg).toMatchObject(expectedResponse);
},
});

const handler = jest.fn(({event}) => {
expect(event).toBe(ANY_EVENT_NAME);
return {action: 'default'};
}) as NativeEventHandler;

const unsubscribe = onNativeEvent(handler);

window.__tuenti_webview_bridge!.postMessage(JSON.stringify(request));

unsubscribe();

window.__tuenti_webview_bridge!.postMessage(JSON.stringify(request));

expect(handler).toHaveBeenCalledTimes(1);
});
21 changes: 0 additions & 21 deletions src/post-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import {getId} from './message-id';
* - ResponsesFromNativeApp: native app responds to a request initiated by the web
*/
type RequestsFromNativeApp = {
NATIVE_EVENT: {
type: 'NATIVE_EVENT';
id: string;
payload: {event: string};
};
SESSION_RENEWED: {
type: 'SESSION_RENEWED';
id: string;
Expand Down Expand Up @@ -598,22 +593,6 @@ export const listenToNativeMessage = <T extends keyof RequestsFromNativeApp>(
};
};

export const onNativeEvent = (
eventHandler: NativeEventHandler,
): (() => void) => {
const handler = (payload: NativeAppRequestPayload<'NATIVE_EVENT'>) => {
const response = eventHandler({
event: payload.event,
});

return {
action: response.action || 'default',
};
};

return listenToNativeMessage('NATIVE_EVENT', handler);
};

export const onSessionRenewal = (
handler: (payload: {accessToken: string}) => void,
): (() => void) => listenToNativeMessage('SESSION_RENEWED', handler);