Skip to content

Commit

Permalink
refactor: switch uic live execution declaratively with observable
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwu1105 committed Mar 6, 2023
1 parent 9e2571d commit 81960ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
16 changes: 7 additions & 9 deletions src/rcc/rcc-live-execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ import { compileResource } from './compile-resource'
export function registerRccLiveExecution$({
extensionUri,
}: RegisterRccLiveExecutionArgs) {
const enabled$ = getLiveExecutionEnabledFromConfig$({
tool: 'rcc',
resource: undefined,
})

const glob$ = getLiveExecutionGlobFromConfig$({
tool: 'rcc',
resource: undefined,
Expand All @@ -43,21 +38,24 @@ export function registerRccLiveExecution$({

const watcher$ = glob$.pipe(switchMap(glob => getWatcher$(glob)))

const onQrcFileChange$ = merge(qrcFiles$, watcher$).pipe(
const onQrcFileUpdated$ = merge(qrcFiles$, watcher$).pipe(
mergeMap(qrcUri =>
registerResourcesLiveExecution$({ extensionUri, qrcUri }),
),
)

return enabled$.pipe(
return getLiveExecutionEnabledFromConfig$({
tool: 'rcc',
resource: undefined,
}).pipe(
switchMap(enabled => {
if (!enabled)
return of({
kind: 'Success',
value: 'Live execution disabled',
value: 'rcc live execution disabled',
} as const)

return onQrcFileChange$
return onQrcFileUpdated$
}),
)
}
Expand Down
37 changes: 17 additions & 20 deletions src/uic/uic-live-execution.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
import { concatMap, firstValueFrom, switchMap } from 'rxjs'
import { concatMap, of, switchMap } from 'rxjs'
import type { URI } from 'vscode-uri'
import {
getLiveExecutionEnabledFromConfig$,
getLiveExecutionGlobFromConfig$,
} from '../configurations'
import type { SupportedTool } from '../types'
import { getWatcher$ } from '../watcher'
import { compileUi } from './compile-ui'

export function registerUicLiveExecution$({
extensionUri,
}: GetUicLiveExecutionArgs) {
return getLiveExecutionGlobFromConfig$({
const onUiFileUpdated$ = getLiveExecutionGlobFromConfig$({
tool: 'uic',
resource: undefined,
defaultValue: '**/*.ui',
}).pipe(
switchMap(glob => getWatcher$(glob)),
concatMap(uri => onUiFileUpdated({ uri, extensionUri })),
switchMap(global => getWatcher$(global)),
concatMap(uri => compileUi({ extensionUri }, uri)),
)
}

type GetUicLiveExecutionArgs = { readonly extensionUri: URI }
return getLiveExecutionEnabledFromConfig$({
tool: 'uic',
resource: undefined,
}).pipe(
switchMap(enabled => {
if (!enabled)
return of({
kind: 'Success',
value: 'uic live execution disabled',
} as const)

async function onUiFileUpdated({ uri, extensionUri }: OnUiFileUpdatedArgs) {
const tool: SupportedTool = 'uic'
const enabled = await firstValueFrom(
getLiveExecutionEnabledFromConfig$({ tool, resource: uri }),
return onUiFileUpdated$
}),
)

if (!enabled)
return { kind: 'Success', value: 'Live execution disabled' } as const

return compileUi({ extensionUri }, uri)
}

type OnUiFileUpdatedArgs = {
readonly extensionUri: URI
readonly uri: URI
}
type GetUicLiveExecutionArgs = { readonly extensionUri: URI }

0 comments on commit 81960ec

Please sign in to comment.