experiments(dui3): cleanup

This commit is contained in:
Dimitrie Stefanescu
2023-07-14 11:13:21 +01:00
parent bf84060f5e
commit 520a65c73c
4 changed files with 30 additions and 14 deletions
@@ -54,8 +54,6 @@ export async function useAccountsSetup(): Promise<DUIAccountsState> {
accounts.value = newAccs
}
// Call this one first to initialize the account state
// QUESTION: could be flopped in a iife so as not to block and drop the asyncness of this setup function?
await refreshAccounts()
const defaultAccount = computed(() =>
+10 -2
View File
@@ -11,9 +11,15 @@ export class GenericBridge extends BaseBridge {
this.bridge = object
}
public async create() {
public async create(): Promise<boolean> {
// NOTE: GetMethods is a call to the .NET side.
const availableMethodNames = await this.bridge.GetBindingsMethodNames()
let availableMethodNames = [] as string[]
try {
availableMethodNames = await this.bridge.GetBindingsMethodNames()
} catch {
return false
}
// NOTE: hoisting original calls as lowerCasedMethodNames, but using the UpperCasedName for the .NET call
// This allows us to follow js convetions and keep .NET ones too (eg. bindings.sayHi('') => public string SayHi(string name) {}
@@ -23,6 +29,8 @@ export class GenericBridge extends BaseBridge {
hoistTarget[lowercasedMethodName] = (...args: unknown[]) =>
this.runMethod(methodName, args)
}
return true
}
private async runMethod(methodName: string, args: unknown[]): Promise<unknown> {
+9 -1
View File
@@ -47,7 +47,9 @@ export class SketchupBridge extends BaseBridge {
// NOTE: we need to hoist the bindings in global scope BEFORE we call sketchup exec get comands below.
;(globalThis as Record<string, unknown>).bindings = this
}
public async create(): Promise<boolean> {
// Initialization continues in the receiveCommandsAndInitializeBridge function,
// where we expect sketchup to return to us the command names for related bindings/views.
// NOTE: as we want to have multiple sketchup bindings in the future, we will
@@ -55,6 +57,13 @@ export class SketchupBridge extends BaseBridge {
// eslint-disable-next-line camelcase
// sketchup.exec({ name: 'getCommands', view_id: this.bindingsName })
sketchup.getCommands(this.bindingsName)
try {
await this.isInitalized
return true
} catch {
return false
}
}
/**
@@ -79,7 +88,6 @@ export class SketchupBridge extends BaseBridge {
* @param message
*/
private rejectBindings(message: string) {
this.resolveIsInitializedPromise(false)
this.rejectIsInitializedPromise(message)
}
+11 -9
View File
@@ -22,6 +22,8 @@ declare let globalThis: Record<string, unknown> & {
export default defineNuxtPlugin(async () => {
const baseBinding = await tryHoistBinding<IBaseBinding>('baseBinding')
const nonExistantBindings = await tryHoistBinding<IBaseBinding>('nonExistantBindings')
const rhinoRandomBinding = await tryHoistBinding<IRhinoRandomBinding>(
'rhinoRandomBinding'
)
@@ -45,24 +47,24 @@ export default defineNuxtPlugin(async () => {
*/
const tryHoistBinding = async <T>(name: string) => {
let bridge: GenericBridge | SketchupBridge | null = null
let tempBridge: GenericBridge | SketchupBridge | null = null
if (globalThis.CefSharp) {
await globalThis.CefSharp.BindObjectAsync(name)
bridge = new GenericBridge(globalThis[name] as unknown as IRawBridge)
await bridge.create()
tempBridge = new GenericBridge(globalThis[name] as unknown as IRawBridge)
}
if (globalThis.chrome && globalThis.chrome.webview && !bridge) {
bridge = new GenericBridge(globalThis.chrome.webview.hostObjects[name])
await bridge.create()
if (globalThis.chrome && globalThis.chrome.webview && !tempBridge) {
tempBridge = new GenericBridge(globalThis.chrome.webview.hostObjects[name])
}
if (globalThis.sketchup && !bridge) {
bridge = new SketchupBridge(name)
const res = await bridge.isInitalized
if (!res) bridge = null
if (globalThis.sketchup && !tempBridge) {
tempBridge = new SketchupBridge(name)
}
const res = await tempBridge?.create()
if (res) bridge = tempBridge
if (!bridge) console.warn(`Failed to bind ${name} binding.`)
globalThis[name] = bridge