-
-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: mobile web bridge concept (#1228)
- Loading branch information
Showing
5 changed files
with
177 additions
and
18 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
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,15 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
class WebProcessDeviceInterface { | ||
constructor(messageSender) { | ||
this.appVersion = '1.2.3' | ||
this.environment = 1 | ||
this.databases = [] | ||
this.messageSender = messageSender | ||
} | ||
|
||
setApplication() {} | ||
|
||
sendMessage(functionName, args) { | ||
return this.messageSender.sendMessage(functionName, args) | ||
} | ||
} |
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,31 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
class WebProcessMessageSender { | ||
constructor() { | ||
this.pendingMessages = [] | ||
window.addEventListener('message', this.handleMessageFromReactNative.bind(this)) | ||
} | ||
|
||
handleMessageFromReactNative(event) { | ||
const message = event.data | ||
try { | ||
const parsed = JSON.parse(message) | ||
const { messageId, returnValue } = parsed | ||
const pendingMessage = this.pendingMessages.find((m) => m.messageId === messageId) | ||
pendingMessage.resolve(returnValue) | ||
this.pendingMessages.splice(this.pendingMessages.indexOf(pendingMessage), 1) | ||
} catch (error) { | ||
console.log('Error parsing message from React Native', message, error) | ||
} | ||
} | ||
|
||
sendMessage(functionName, args) { | ||
const messageId = Math.random() | ||
window.ReactNativeWebView.postMessage(JSON.stringify({ functionName: functionName, args: args, messageId })) | ||
return new Promise((resolve) => { | ||
this.pendingMessages.push({ | ||
messageId, | ||
resolve, | ||
}) | ||
}) | ||
} | ||
} |
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