-
-
Notifications
You must be signed in to change notification settings - Fork 647
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract event-emitter to specific file (#1872)
- Loading branch information
1 parent
e0fb3ad
commit 94de093
Showing
4 changed files
with
58 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |