Skip to content

Commit

Permalink
feat(vue2): scan apps button
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Feb 11, 2022
1 parent 9022113 commit 824f54d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
21 changes: 13 additions & 8 deletions packages/app-backend-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,25 @@ export async function removeApp (app: App, ctx: BackendContext) {
}
}

let scanTimeout: any

// eslint-disable-next-line camelcase
export function _legacy_getAndRegisterApps (ctx: BackendContext) {
export function _legacy_getAndRegisterApps (ctx: BackendContext, clear = false) {
setTimeout(() => {
// Remove apps that are legacy
ctx.appRecords.forEach(appRecord => {
if (appRecord.meta.Vue) {
removeAppRecord(appRecord, ctx)
}
})
if (clear) {
// Remove apps that are legacy
ctx.appRecords.forEach(appRecord => {
if (appRecord.meta.Vue) {
removeAppRecord(appRecord, ctx)
}
})
}

const apps = scan()

clearTimeout(scanTimeout)
if (!apps.length) {
setTimeout(() => _legacy_getAndRegisterApps(ctx), 1000)
scanTimeout = setTimeout(() => _legacy_getAndRegisterApps(ctx), 1000)
}

apps.forEach(app => {
Expand Down
13 changes: 11 additions & 2 deletions packages/app-backend-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ export async function initBackend (bridge: Bridge) {
hook,
})

SharedData.legacyApps = false
if (hook.Vue) {
connect()
_legacy_getAndRegisterApps(ctx)
_legacy_getAndRegisterApps(ctx, true)
SharedData.legacyApps = true
}
hook.on(HookEvents.INIT, () => {
_legacy_getAndRegisterApps(ctx)
_legacy_getAndRegisterApps(ctx, true)
SharedData.legacyApps = true
})

hook.on(HookEvents.APP_ADD, async app => {
Expand Down Expand Up @@ -356,6 +359,12 @@ function connectBridge () {
}
})

ctx.bridge.on(BridgeEvents.TO_BACK_SCAN_LEGACY_APPS, () => {
if (hook.Vue) {
_legacy_getAndRegisterApps(ctx)
}
})

// Components

ctx.bridge.on(BridgeEvents.TO_BACK_COMPONENT_TREE, ({ instanceId, filter }) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/app-frontend/src/features/apps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export function waitForAppSelect (): Promise<void> {
}
}

export function scanLegacyApps () {
getBridge().send(BridgeEvents.TO_BACK_SCAN_LEGACY_APPS, {})
}

export function setupAppsBridgeEvents (bridge: Bridge) {
bridge.on(BridgeEvents.TO_FRONT_APP_ADD, ({ appRecord }) => {
addApp(appRecord)
Expand Down
2 changes: 2 additions & 0 deletions packages/app-frontend/src/features/header/AppHeaderSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ export default defineComponent({
@mouseleave="queueClose()"
@wheel="onMouseWheel"
>
<slot name="before" />

<VueDropdownButton
v-for="(item, index) of items"
:key="index"
Expand Down
14 changes: 13 additions & 1 deletion packages/app-frontend/src/features/header/AppSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppSelectItem from './AppSelectItem.vue'
import { watch, defineComponent, computed } from '@vue/composition-api'
import { BridgeEvents, SharedData } from '@vue-devtools/shared-utils'
import { useApps, pendingSelectAppId } from '@front/features/apps'
import { useApps, pendingSelectAppId, scanLegacyApps } from '@front/features/apps'
import { useOrientation } from '@front/features/layout/orientation'
import { useRouter } from '@front/util/router'
import { useBridge } from '../bridge'
Expand Down Expand Up @@ -72,6 +72,7 @@ export default defineComponent({
selectApp,
orientation,
hasNewVueVersion,
scanLegacyApps,
}
},
})
Expand Down Expand Up @@ -119,6 +120,17 @@ export default defineComponent({
:app="item"
/>
</template>

<template #before>
<VueButton
v-if="$shared.legacyApps"
class="flat m-1"
icon-left="cached"
@click="scanLegacyApps()"
>
Scan apps
</VueButton>
</template>
</AppHeaderSelect>
</template>

Expand Down
1 change: 1 addition & 0 deletions packages/shared-utils/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export enum BridgeEvents {
TO_FRONT_APP_REMOVE = 'f:app:remove',
TO_BACK_APP_SELECT = 'b:app:select',
TO_FRONT_APP_SELECTED = 'f:app:selected',
TO_BACK_SCAN_LEGACY_APPS = 'b:app:scan-legacy',

// Components
TO_BACK_COMPONENT_TREE = 'b:component:tree',
Expand Down
1 change: 1 addition & 0 deletions packages/shared-utils/src/shared-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const internalSharedData = {
pluginPermissions: {} as any,
pluginSettings: {} as any,
pageConfig: {} as any,
legacyApps: false,
debugInfo: false,
}

Expand Down

0 comments on commit 824f54d

Please sign in to comment.