Skip to content

Commit 7db997d

Browse files
authored
Move plugin listeners to the plugin files (#131)
* Move plugin listeners to the plugin files * Add type of listener response * Bump version
1 parent cbcabb0 commit 7db997d

62 files changed

Lines changed: 220 additions & 133 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "median-js-bridge",
3-
"version": "2.13.5",
3+
"version": "2.13.6",
44
"description": "Median bridge and utilities for JS web frameworks",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/commands/android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addCommand } from '../utils.js';
1+
import { addCommand } from '../utils/index.js';
22

33
const android = {
44
geoLocation: {

src/commands/general.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AnyData, CallbackParams } from '../types/index.js';
2-
import { addCommand, addCommandCallback } from '../utils.js';
2+
import { addCommand, addCommandCallback } from '../utils/index.js';
3+
import { createListener } from '../utils/listener.js';
34

45
const camera = {
56
setCaptureQuality: function (quality: 'high' | 'low') {
@@ -89,6 +90,10 @@ const internalExternal = {
8990
},
9091
};
9192

93+
const jsNavigation = {
94+
url: createListener<{ url: string }>('_median_url_changed'),
95+
};
96+
9297
export type KeyboardInfo = {
9398
visible: boolean;
9499
keyboardWindowSize: {
@@ -337,6 +342,7 @@ const general = {
337342
contextMenu,
338343
deviceInfo,
339344
internalExternal,
345+
jsNavigation,
340346
keyboard,
341347
nativebridge,
342348
navigationLevels,

src/commands/ios.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CallbackParams, PermissionStatusData } from '../types/index.js';
2-
import { addCommand, addCommandCallback } from '../utils.js';
2+
import { addCommand, addCommandCallback } from '../utils/index.js';
33
import { NewWindowMode } from './general.js';
44

55
const ios = {

src/index.ts

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,11 @@ import * as commands from './commands/index.js';
22
import * as plugins from './plugins/index.js';
33
import { BranchInitializedData } from './plugins/branch.js';
44
import { InAppPurchaseInfoReadyData } from './plugins/iap.js';
5-
import { ShareToAppData } from './plugins/share.js';
6-
import { createTempFunctionName, setMedianCallback, setSubscription } from './utils.js';
5+
import { ShareToAppData } from './plugins/shareIntoApp.js';
6+
import { createListener } from './utils/listener.js';
7+
import { AppsFlyer } from './types/appsflyer.js';
78

89
namespace Median {
9-
///////////////////////////////
10-
// Internal //
11-
///////////////////////////////
12-
const listeners: Record<string, Record<string, (...args: any[]) => void>> = {};
13-
14-
const addListener = <T>(functionName: string, callback: (data: T) => void) => {
15-
const functionId = createTempFunctionName(functionName);
16-
17-
if (typeof callback !== 'function') {
18-
return functionId;
19-
}
20-
21-
listeners[functionName] = listeners[functionName] || {};
22-
listeners[functionName][functionId] = callback;
23-
24-
setMedianCallback(functionName, listeners[functionName]);
25-
setSubscription(functionName, true);
26-
27-
return functionId;
28-
};
29-
30-
const removeListener = (functionName: string, functionId: string) => {
31-
if (!functionName || !functionId) return;
32-
33-
listeners[functionName] = listeners[functionName] || {};
34-
delete listeners[functionName][functionId];
35-
36-
setMedianCallback(functionName, listeners[functionName]);
37-
if (Object.keys(listeners[functionName]).length === 0) {
38-
setSubscription(functionName, false);
39-
}
40-
};
41-
42-
const createListenerProp = <T = void>(functionName: string) => ({
43-
addListener: (callback: (data: T) => void) => addListener<T>(functionName, callback),
44-
removeListener: (functionId: string) => removeListener(functionName, functionId),
45-
});
46-
4710
///////////////////////////////
4811
// General Commands //
4912
///////////////////////////////
@@ -55,6 +18,7 @@ namespace Median {
5518
export const connectivity = commands.general.connectivity;
5619
export const deviceInfo = commands.general.deviceInfo;
5720
export const internalExternal = commands.general.internalExternal;
21+
export const jsNavigation = commands.general.jsNavigation;
5822
export const keyboard = commands.general.keyboard;
5923
export const nativebridge = commands.general.nativebridge;
6024
export const navigationLevels = commands.general.navigationLevels;
@@ -119,8 +83,8 @@ namespace Median {
11983
export const opentok = plugins.opentok;
12084
export const permissions = plugins.permissions;
12185
export const plaid = plugins.plaid;
122-
export const purchase = plugins.iap.purchase;
12386
export const revenueCat = plugins.revenueCat;
87+
export const shareIntoApp = plugins.shareIntoApp;
12488
export const socialLogin = plugins.socialLogin;
12589
export const socialShare = plugins.socialShare;
12690
export const storage = {
@@ -168,25 +132,48 @@ namespace Median {
168132
///////////////////////////////
169133
// Events //
170134
///////////////////////////////
171-
export const appsFlyerConversionData = createListenerProp<any>('_median_appsflyer_cd');
172-
export const appsFlyerDeeplinkResult = createListenerProp<any>('_median_appsflyer_deeplink_result');
173-
export const appsFlyerSdkStart = createListenerProp<any>('_median_appsflyer_sdk_start');
174-
export const appResumed = createListenerProp('_median_app_resumed');
175-
export const branchInitialized = createListenerProp<BranchInitializedData>('_median_branch_initialized');
176-
export const deviceShake = createListenerProp('_median_device_shake');
177-
export const iapInfoReady = createListenerProp<InAppPurchaseInfoReadyData>('_median_info_ready');
178-
export const iapPurchases = createListenerProp<any>('_median_iap_purchases');
179-
export const jsNavigation = {
180-
url: createListenerProp<{ url: string }>('_median_url_changed'),
181-
};
182-
export const oneSignalPushOpened = createListenerProp<any>('_median_onesignal_push_opened');
183-
export const shareToApp = createListenerProp<ShareToAppData>('_median_share_to_app');
135+
export const appResumed = createListener('_median_app_resumed');
136+
export const deviceShake = createListener('_median_device_shake');
137+
138+
/**
139+
* @deprecated Use `Median.appsflyer.conversionData` instead.
140+
*/
141+
export const appsFlyerConversionData = createListener<AppsFlyer.ConversionData>('_median_appsflyer_cd');
142+
/**
143+
* @deprecated Use `Median.appsflyer.deeplinkResult` instead.
144+
*/
145+
export const appsFlyerDeeplinkResult = createListener<AppsFlyer.DeeplinkResult>('_median_appsflyer_deeplink_result');
146+
/**
147+
* @deprecated Use `Median.appsflyer.sdkStart` instead.
148+
*/
149+
export const appsFlyerSdkStart = createListener<AppsFlyer.SdkStartResponse>('_median_appsflyer_sdk_start');
150+
/**
151+
* @deprecated Use `Median.branch.initialized` instead.
152+
*/
153+
export const branchInitialized = createListener<BranchInitializedData>('_median_branch_initialized');
154+
/**
155+
* @deprecated Use `Median.iap.infoReady` instead.
156+
*/
157+
export const iapInfoReady = createListener<InAppPurchaseInfoReadyData>('_median_info_ready');
158+
/**
159+
* @deprecated Use `Median.iap.purchaseResult` instead.
160+
*/
161+
export const iapPurchases = createListener<Record<string, any>>('_median_iap_purchases');
162+
/**
163+
* @deprecated Use `Median.oneSignal.pushOpened` instead.
164+
*/
165+
export const oneSignalPushOpened = createListener<Record<string, any>>('_median_onesignal_push_opened');
166+
/**
167+
* @deprecated Use `Median.shareIntoApp.shareResult` instead.
168+
*/
169+
export const shareToApp = createListener<ShareToAppData>('_median_share_to_app');
184170
}
185171

186172
export default Median;
187173

188174
///////////////////////////////
189175
// Types //
190176
///////////////////////////////
177+
export { AppsFlyer } from './types/appsflyer.js';
191178
export { HealthBridge } from './types/healthBridge.js';
192179
export { MasterLock } from './types/masterlock.js';

src/plugins/adjust.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CallbackData } from '../types/index.js';
2-
import { addCommand, addCommandCallback } from '../utils.js';
2+
import { addCommand, addCommandCallback } from '../utils/index.js';
33

44
export class AdjustEvent {
55
token: string;

src/plugins/admob.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CallbackParams } from '../types/index.js';
2-
import { addCommand, addCommandCallback } from '../utils.js';
2+
import { addCommand, addCommandCallback } from '../utils/index.js';
33

44
type AdmobRequestTrackingData = { status: 'authorized' | 'denied' | 'restricted' };
55

src/plugins/ageSafety.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AnyData } from '../types/index.js';
2-
import { addCallbackFunction, addCommand } from '../utils.js';
2+
import { addCallbackFunction, addCommand } from '../utils/index.js';
33

44
type AgeSignalsOptions = {
55
/**

src/plugins/appreview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CallbackParams } from '../types/index.js';
2-
import { addCommand, addCommandCallback } from '../utils.js';
2+
import { addCommand, addCommandCallback } from '../utils/index.js';
33

44
const appreview = {
55
prompt: function (params?: CallbackParams) {

src/plugins/appsflyer.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { addCommand } from '../utils.js';
1+
import { AppsFlyer } from '../types/appsflyer.js';
2+
import { addCommand } from '../utils/index.js';
3+
import { createListener } from '../utils/listener.js';
24

35
/**
46
* Appsflyer plugin for Median.
@@ -24,6 +26,21 @@ const appsflyer = {
2426
setCustomerUserId: function (userId: string) {
2527
addCommand('median://appsflyer/setCustomerUserId', { userId });
2628
},
29+
30+
/**
31+
* Listens for conversion data events from the AppsFlyer SDK.
32+
*/
33+
conversionData: createListener<AppsFlyer.ConversionData>('_median_appsflyer_cd'),
34+
35+
/**
36+
* Listens for deep link results from the AppsFlyer SDK.
37+
*/
38+
deeplinkResult: createListener<AppsFlyer.DeeplinkResult>('_median_appsflyer_deeplink_result'),
39+
40+
/**
41+
* Listens for AppsFlyer SDK start events.
42+
*/
43+
sdkStart: createListener<AppsFlyer.SdkStartResponse>('_median_appsflyer_sdk_start'),
2744
};
2845

2946
export default appsflyer;

0 commit comments

Comments
 (0)