Skip to content

Commit

Permalink
refactor: extract event-emitter to specific file (hyochan#1872)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremybarbet committed Aug 20, 2022
1 parent c968f64 commit a38aa5d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 61 deletions.
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 {
Product,
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 {
Product,
ProductPurchase,
Expand Down Expand Up @@ -54,7 +46,9 @@ const checkNativeAndroidAvailable = (): void => {
}
};

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

return androidNativeModule
Expand All @@ -70,13 +64,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 @@ -634,52 +628,6 @@ export const validateReceiptAmazon = async ({
return await enhancedFetch<Amazon.ReceiptType>(url);
};

/**
* Add IAP purchase event
* @returns {callback(e: ProductPurchase | ProductPurchase)}
*/
export const purchaseUpdatedListener = (
listener: (event: ProductPurchase | 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';

0 comments on commit a38aa5d

Please sign in to comment.