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

refactor: extract event-emitter to specific file #1872

Merged
merged 2 commits into from
Aug 18, 2022
Merged
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
49 changes: 49 additions & 0 deletions src/eventEmitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {EmitterSubscription, NativeEventEmitter} from 'react-native';

import {getAndroidModule, getIosModule, getNativeModule} from './iap';
import {isAndroid, isIos} from './internal';
import type {PurchaseError} from './purchaseError';
import type {Purchase} from './types';

const eventEmitter = new NativeEventEmitter(getNativeModule());

/**
* Add IAP purchase event
*/
export const purchaseUpdatedListener = (
listener: (event: Purchase) => void,
) => {
const emitterSubscription = eventEmitter.addListener(
'purchase-updated',
listener,
);

if (isAndroid) {
getAndroidModule().startListening();
}

return emitterSubscription;
};

/**
* Add IAP purchase error event
*/
export const purchaseErrorListener = (
listener: (error: PurchaseError) => void,
): EmitterSubscription => eventEmitter.addListener('purchase-error', listener);

/**
* Add IAP promoted subscription event
*
* @platform iOS
*/
export const promotedProductListener = (listener: () => void) => {
if (isIos) {
return new NativeEventEmitter(getIosModule()).addListener(
'iap-promoted-product',
listener,
);
}

return null;
};
5 changes: 2 additions & 3 deletions src/hooks/withIAPContext.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React, {useContext, useEffect, useMemo, useState} from 'react';

import {
getPromotedProductIOS,
initConnection,
promotedProductListener,
purchaseErrorListener,
purchaseUpdatedListener,
} from '../iap';
} from '../eventEmitter';
import {getPromotedProductIOS, initConnection} from '../iap';
import type {PurchaseError} from '../purchaseError';
import type {
InAppPurchase,
Expand Down
64 changes: 6 additions & 58 deletions src/iap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
EmitterSubscription,
Linking,
NativeEventEmitter,
NativeModules,
Platform,
} from 'react-native';
import {Linking, NativeModules, Platform} from 'react-native';

import type * as Amazon from './types/amazon';
import type * as Android from './types/android';
Expand All @@ -15,9 +9,7 @@ import {
fillProductsWithAdditionalData,
isAmazon,
isAndroid,
isIos,
} from './internal';
import type {PurchaseError} from './purchaseError';
import type {
InAppPurchase,
Product,
Expand Down Expand Up @@ -55,7 +47,9 @@ const checkNativeAndroidAvailable = (): void => {
}
};

const getAndroidModule = (): typeof RNIapModule | typeof RNIapAmazonModule => {
export const getAndroidModule = ():
| typeof RNIapModule
| typeof RNIapAmazonModule => {
checkNativeAndroidAvailable();

return androidNativeModule
Expand All @@ -71,13 +65,13 @@ const checkNativeIOSAvailable = (): void => {
}
};

const getIosModule = (): typeof RNIapIos => {
export const getIosModule = (): typeof RNIapIos => {
checkNativeIOSAvailable();

return RNIapIos;
};

const getNativeModule = ():
export const getNativeModule = ():
| typeof RNIapModule
| typeof RNIapAmazonModule
| typeof RNIapIos => {
Expand Down Expand Up @@ -575,52 +569,6 @@ export const validateReceiptAmazon = async (
return await enhancedFetch<Amazon.ReceiptType>(url);
};

/**
* Add IAP purchase event
* @returns {callback(e: InAppPurchase | ProductPurchase)}
*/
export const purchaseUpdatedListener = (
listener: (event: InAppPurchase | SubscriptionPurchase) => void,
): EmitterSubscription => {
const emitterSubscription = new NativeEventEmitter(
getNativeModule(),
).addListener('purchase-updated', listener);

if (isAndroid) {
getAndroidModule().startListening();
}

return emitterSubscription;
};

/**
* Add IAP purchase error event
* @returns {callback(e: PurchaseError)}
*/
export const purchaseErrorListener = (
listener: (errorEvent: PurchaseError) => void,
): EmitterSubscription =>
new NativeEventEmitter(getNativeModule()).addListener(
'purchase-error',
listener,
);

/**
* Add IAP promoted subscription event
* Only available on iOS
*/
export const promotedProductListener = (
listener: (productId?: string) => void,
): EmitterSubscription | null => {
if (isIos) {
return new NativeEventEmitter(getIosModule()).addListener(
'iap-promoted-product',
listener,
);
}
return null;
};

/**
* Get the current receipt base64 encoded in IOS.
* @param {forceRefresh?:boolean}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './iap';
export * from './types';
export * from './eventEmitter';
export * from './hooks/useIAP';
export * from './hooks/withIAPContext';
export * from './purchaseError';