Skip to content

Commit

Permalink
refactor: move observable to rxjs file
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksteamdev committed Sep 12, 2022
1 parent 7071074 commit 5632883
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
8 changes: 2 additions & 6 deletions packages/vite-plugin/src/node/fileWriter-hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import {
PrunePayload,
UpdatePayload,
} from 'vite'
import {
allFilesReady$,
getFileName,
getViteUrl,
prefix,
} from './fileWriter-utilities'
import { allFilesReady$ } from './fileWriter-rxjs'
import { getFileName, getViteUrl, prefix } from './fileWriter-utilities'
import { _debug } from './helpers'
import { CrxHMRPayload } from './types'

Expand Down
10 changes: 10 additions & 0 deletions packages/vite-plugin/src/node/fileWriter-rxjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { readFile } from 'fs-extra'
import MagicString from 'magic-string'
import {
filter,
map,
mergeMap,
Observable,
of,
ReplaySubject,
retry,
startWith,
switchMap,
} from 'rxjs'
import { ViteDevServer } from 'vite'
import { outputFiles } from './fileWriter-filesMap'
import { getFileName, getOutputPath, getViteUrl } from './fileWriter-utilities'
import { join } from './path'
import { CrxDevAssetId, CrxDevScriptId, CrxPlugin } from './types'
Expand Down Expand Up @@ -60,6 +63,13 @@ export const buildStart$ = fileWriterEvent$.pipe(
switchMap((e) => of(e)),
)

/** Emit when all script files are written */
export const allFilesReady$ = buildEnd$.pipe(
switchMap(() => outputFiles.change$.pipe(startWith({ type: 'start' }))),
map(() => [...outputFiles.values()]),
switchMap((files) => Promise.all(files.map(({ file }) => file))),
)

/* ------------------- WRITE OPS ------------------- */

interface OperatorFileData {
Expand Down
20 changes: 2 additions & 18 deletions packages/vite-plugin/src/node/fileWriter-utilities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { firstValueFrom, map, startWith, switchMap, tap } from 'rxjs'
import { firstValueFrom } from 'rxjs'
import { ViteDevServer } from 'vite'
import { outputFiles } from './fileWriter-filesMap'
import { buildEnd$ } from './fileWriter-rxjs'
import { allFilesReady$ } from './fileWriter-rxjs'
import { _debug } from './helpers'
import { isAbsolute, join } from './path'
import { CrxDevAssetId, CrxDevScriptId } from './types'
Expand Down Expand Up @@ -131,22 +131,6 @@ export async function fileReady(script: FileWriterId): Promise<void> {
await Promise.all(deps.map(fileReady))
}

// only emit when all script files are written
export const allFilesReady$ = buildEnd$.pipe(
tap((e) => {
return debug('buildEnd %o', e)
}),
switchMap(() => outputFiles.change$.pipe(startWith({ type: 'start' }))),
tap((e) => {
return debug('change$ %o', e)
}),
map(() => [...outputFiles.values()]),
switchMap((files) => Promise.all(files.map(({ file }) => file))),
tap((e) => {
return debug('buildEnd %o', e)
}),
)

/** Resolves when all existing files in scriptFiles are written. */
export async function allFilesReady(): Promise<void> {
await firstValueFrom(allFilesReady$)
Expand Down

0 comments on commit 5632883

Please sign in to comment.