1- export type CommandType = 'SHARE_MEME' ; // 밈 공유하기
2-
3- // 각 커맨드별 데이터 타입 정의
4- interface ShareMemeData {
5- title : string ;
6- image : string ;
7- }
8-
9- // 커맨드별 데이터 타입 매핑
10- type CommandDataMap = {
11- SHARE_MEME : ShareMemeData ;
12- } ;
13-
14- // 브릿지 커맨드 인터페이스
15- interface BridgeCommand < T extends CommandType > {
16- command : T ;
17- data : CommandDataMap [ T ] ;
18- }
1+ import {
2+ BridgeCommand ,
3+ COMMAND_TYPE ,
4+ CommandDataMap ,
5+ CommandType ,
6+ ShareMemeData ,
7+ } from '../types/bridge' ;
198
209// 개발 환경용 모의 브릿지
2110const mockNativeBridge = {
@@ -27,8 +16,8 @@ const mockNativeBridge = {
2716const mockWebKit = {
2817 messageHandlers : {
2918 wikiHandler : {
30- postMessage ( data : unknown ) {
31- console . log ( 'Mock iOS Bridge called with:' , data ) ;
19+ postMessage ( data : string ) {
20+ console . log ( 'Mock iOS Bridge called with:' , JSON . parse ( data ) ) ;
3221 } ,
3322 } ,
3423 } ,
@@ -75,23 +64,21 @@ class NativeBridge {
7564 }
7665
7766 private sendCommand < T extends CommandType > (
78- command : T ,
67+ type : T ,
7968 data : CommandDataMap [ T ] ,
8069 ) : void {
8170 try {
8271 const bridgeCommand : BridgeCommand < T > = {
83- command ,
72+ type ,
8473 data,
8574 } ;
8675
87- if ( this . isAndroid ( ) && window . wiki ) {
88- // Android 브릿지 호출
89- window . wiki . postMessage ( JSON . stringify ( bridgeCommand , null , 2 ) ) ;
90- } else if ( this . isIOS ( ) && window . webkit ?. messageHandlers ?. wikiHandler ) {
91- // iOS 브릿지 호출
92- window . webkit . messageHandlers . wikiHandler . postMessage (
93- JSON . stringify ( bridgeCommand , null , 2 ) ,
94- ) ;
76+ const message = JSON . stringify ( bridgeCommand , null , 2 ) ;
77+
78+ if ( this . isAndroid ( ) ) {
79+ window . wiki ?. postMessage ( message ) ;
80+ } else if ( this . isIOS ( ) ) {
81+ window . webkit ?. messageHandlers ?. wikiHandler ?. postMessage ( message ) ;
9582 } else {
9683 console . warn ( 'Native bridge is not available' ) ;
9784 }
@@ -102,23 +89,8 @@ class NativeBridge {
10289
10390 // 밈 공유하기
10491 shareMeme ( data : ShareMemeData ) : void {
105- this . sendCommand ( ' SHARE_MEME' , data ) ;
92+ this . sendCommand ( COMMAND_TYPE . SHARE_MEME , data ) ;
10693 }
10794}
10895
10996export const nativeBridge = NativeBridge . getInstance ( ) ;
110-
111- declare global {
112- interface Window {
113- wiki ?: {
114- postMessage ( data : string ) : void ;
115- } ;
116- webkit ?: {
117- messageHandlers : {
118- wikiHandler : {
119- postMessage ( data : string ) : void ;
120- } ;
121- } ;
122- } ;
123- }
124- }
0 commit comments