-
Notifications
You must be signed in to change notification settings - Fork 0
[Refactor/#9] 웹뷰 메시지 수신을 브리지 인터페이스로 정리 #10
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,71 @@ | ||
| /** | ||
| * 웹 → 앱 postMessage 브리지. | ||
| * | ||
| * 웹은 `window.ReactNativeWebView.postMessage(JSON.stringify({ type, payload }))`로 보낸다. | ||
| * `type`은 메시지 표식, `payload`는 메시지별 본문이다. 화면은 JSON 파싱·`type` 분기를 모르고 | ||
| * `type`별 핸들러만 넘긴다. | ||
| * | ||
| * 메시지 추가 절차: | ||
| * 1. `messages/<이름>.ts`에 type 상수 · payload 인터페이스 · `(payload: unknown) => T | null` 파서 | ||
| * 2. 아래 `parsers`에 한 줄 등록 — 나머지 타입은 여기서 파생된다 | ||
| * 3. 화면의 `dispatchBridgeMessage` 핸들러에 항목 추가 | ||
| * 4. stream-client-web에 같은 type 상수·같은 payload 필드로 송신부 작성 (PR을 서로 링크) | ||
| */ | ||
|
|
||
| import { | ||
| parseSafeAreaColorsPayload, | ||
| SAFE_AREA_COLORS_MESSAGE_TYPE, | ||
| } from "@/features/webview/bridge/messages/safeAreaColors"; | ||
|
|
||
| // 파서는 형식이 안 맞으면 `null`을 돌려준다. 던지지 않는다 — onMessage는 WebView 안의 어떤 | ||
| // 스크립트든 부를 수 있어, 이상한 값은 오류가 아니라 "우리 메시지가 아님"으로 다룬다. | ||
| const parsers = { | ||
| [SAFE_AREA_COLORS_MESSAGE_TYPE]: parseSafeAreaColorsPayload, | ||
| } satisfies Record<string, (payload: unknown) => unknown>; | ||
|
|
||
| type BridgeMessageType = keyof typeof parsers; | ||
| type BridgePayload<K extends BridgeMessageType> = NonNullable<ReturnType<(typeof parsers)[K]>>; | ||
|
|
||
| export type BridgeHandlers = Partial<{ | ||
| [K in BridgeMessageType]: (payload: BridgePayload<K>) => void; | ||
| }>; | ||
|
|
||
| function isBridgeMessageType(type: unknown): type is BridgeMessageType { | ||
| return typeof type === "string" && Object.hasOwn(parsers, type); | ||
| } | ||
|
|
||
| /** | ||
| * WebView `onMessage` 원문을 받아, 아는 메시지이고 핸들러가 있으면 부른다. | ||
| * 모르는 메시지·형식 불일치는 조용히 버린다 — 웹이 다른 용도로 postMessage를 쓸 수 있다. | ||
| */ | ||
| export function dispatchBridgeMessage(data: string, handlers: BridgeHandlers): void { | ||
| let message: unknown; | ||
|
|
||
| try { | ||
| message = JSON.parse(data); | ||
| } catch { | ||
| // JSON이 아니면 우리 메시지가 아니다. | ||
| return; | ||
| } | ||
|
|
||
| if (typeof message !== "object" || message === null) { | ||
| return; | ||
| } | ||
|
|
||
| const { type, payload } = message as Record<string, unknown>; | ||
|
|
||
| if (!isBridgeMessageType(type)) { | ||
| return; | ||
| } | ||
|
|
||
| const parsed = parsers[type](payload); | ||
|
|
||
| if (parsed === null) { | ||
| return; | ||
| } | ||
|
|
||
| // 레지스트리 키로 좁혀진 type과 그 파서 결과는 짝이 맞지만, TS는 인덱스 접근에서 이 상관관계를 | ||
| // 추적하지 못한다. 핸들러 쪽 시그니처는 BridgeHandlers로 보장되므로 여기서만 넓힌다. | ||
| const handler = handlers[type] as ((payload: unknown) => void) | undefined; | ||
| handler?.(parsed); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🌐 Web query:
https://github.com/billilge/stream-client-web/pull/57 safeAreaColors useNativeSafeAreaColors payload deployment💡 Result:
<source_evidence>
Citations:
🏁 Script executed:
Repository: billilge/stream-client-app
Length of output: 26164
🏁 Script executed:
Repository: billilge/stream-client-app
Length of output: 2466
🏁 Script executed:
Repository: billilge/stream-client-app
Length of output: 1021
🏁 Script executed:
Repository: billilge/stream-client-app
Length of output: 1103
Wrap safe-area colors in
payloadin the web sender.If PR
#57’s sender is served to this app, it posts{ type, bottom, top }.dispatchBridgeMessagepasses onlypayloadtoparseSafeAreaColorsPayload, which rejects the missing value. The handler then does not update the safe-area colors, soWebViewScreenkeeps its defaults. Send the colors insidepayloadbefore deploying the sender with this receiver.Suggested fix
🤖 Prompt for AI Agents