Create SketchupBridge for tryHoistBinding

This commit is contained in:
oguzhankoral
2023-07-14 12:49:26 +03:00
parent 09a0e91214
commit bf84060f5e
3 changed files with 15 additions and 4 deletions
+2
View File
@@ -18,6 +18,8 @@ export class BaseBridge {
// NOTE: this could be private - as it should be only used by the host application.
emit(eventName: string, payload: string) {
console.log(payload)
const parsedPayload = payload ? (JSON.parse(payload) as unknown) : null
this.emitter.emit(eventName, parsedPayload)
}
+3
View File
@@ -117,6 +117,9 @@ export class SketchupBridge extends BaseBridge {
}
private receiveResponse(requestId: string, data: string) {
console.log(requestId)
console.log(data)
if (!this.requests[requestId])
throw new Error(
`Sketchup Bridge found no request to resolve with the id of ${requestId}. Something is weird!`
+10 -4
View File
@@ -5,6 +5,7 @@ import {
IBaseBinding,
IRhinoRandomBinding
} from '~/lib/bindings/definitions/baseBindings'
import { SketchupBridge } from '~/lib/bridge/sketchup'
// Makes TS happy
declare let globalThis: Record<string, unknown> & {
@@ -25,10 +26,13 @@ export default defineNuxtPlugin(async () => {
'rhinoRandomBinding'
)
const sketchupRandomBinding = await tryHoistBinding<unknown>('sketchupRandomBinding')
return {
provide: {
baseBinding,
rhinoRandomBinding
rhinoRandomBinding,
sketchupRandomBinding
}
}
})
@@ -40,7 +44,7 @@ export default defineNuxtPlugin(async () => {
* @returns null if the binding was not found, or the binding.
*/
const tryHoistBinding = async <T>(name: string) => {
let bridge: GenericBridge | null = null
let bridge: GenericBridge | SketchupBridge | null = null
if (globalThis.CefSharp) {
await globalThis.CefSharp.BindObjectAsync(name)
@@ -48,13 +52,15 @@ const tryHoistBinding = async <T>(name: string) => {
await bridge.create()
}
if (globalThis.chrome && !bridge) {
if (globalThis.chrome && globalThis.chrome.webview && !bridge) {
bridge = new GenericBridge(globalThis.chrome.webview.hostObjects[name])
await bridge.create()
}
if (globalThis.sketchup && !bridge) {
// TODO
bridge = new SketchupBridge(name)
const res = await bridge.isInitalized
if (!res) bridge = null
}
if (!bridge) console.warn(`Failed to bind ${name} binding.`)