Skip to content

optimize file import process and migrate server persistence to Bun - #23

Merged
xellanix merged 8 commits into
mainfrom
vite-bun
May 21, 2026
Merged

xellanix merged 8 commits into
mainfrom
vite-bun

Conversation

@xellanix

Copy link
Copy Markdown
Owner

Overview

This pull request optimizes the file import workflow and upgrades the server persistence layer to leverage asynchronous operations. It improves user feedback with a loading toast, offloads ZIP decompression to prevent UI blocking, and refactors filesystem interactions for better efficiency.

Key Changes

🚀 Features

  • Add loading toast for file import: Introduces a persistent loading toast notification at the start of the file import process and dismisses it upon completion to provide visual feedback.
    • Create a persistent loading toast before iterating through files.
    • Dismiss the toast notification immediately prior to calling the success callback.

🐛 Bug Fixes

  • Align array indices with visual group order: Resolves an issue where items grouped visually in the queue had disjointed, non-sequential underlying indices.
    • Sort the master's contents chronologically during the import phase by the first appearance of each group to ensure accurate rendering sequence.
    • Centralize group name fallback parsing using the new groupName utility and implement sortMasterContents.
    • Standardize formatting and indentation for errors in the import processor.

🧹 Chore/Refactoring

  • Migrate server persistence to async Bun file APIs: Transitions server persistence from blocking, synchronous Node.js fs methods to asynchronous, native Bun file operations to improve concurrency and prevent event loop blockages.
    • Save data to a temporary file before overwriting the target destination to ensure atomic file writes and prevent corruption.
    • Update the build configuration, server socket listeners, and tsconfig to support ESM, top-level await, and Bun types.
  • Remove redundant directory creation: Refactors file importing by removing explicit directory checks and manual creation logic.
    • Rely on Bun.write to automatically create missing parent directories recursively, simplifying the pipeline and reducing synchronous filesystem checks.
  • Offload zip decompression to worker thread using async unzip: Optimizes UI performance during .zip file processing by transitioning from the blocking unzipSync function to the asynchronous, non-blocking unzip API provided by fflate.
    • Streamline the iteration model into a single pass over the unzipped object entries instead of running two separate full-array loops sequentially.
  • Centralize mime type mapping and optimize unzipped entries loop: Consolidates MIME type resolution during the asset import process to eliminate duplicate execution paths and brittle extension checks.
    • Introduce a dedicated MIME_MAP dictionary and a getMime helper function to safely evaluate file extensions.
    • Cache zip file entry extraction to avoid repeated descriptor processing and unnecessary structural recalculations.
    • Simplify error handling via ternary variable declarations and remove redundant response constraint checks.
  • Centralize error logging: Extracts duplicate toast notification and console error logging routines into a unified local helper function to improve maintainability.
  • Deduplicate projection json parsing: Extracts the JSON parsing and projection store hydration logic into a reusable helper function to eliminate code duplication across file and ZIP workflows.

Version Bump

0.1.0

xellanix added 8 commits May 21, 2026 15:06
Extract the JSON parsing and projection store hydration logic into a
reusable helper function to eliminate code duplication across the
file and ZIP import workflows.

Previously, the logic for evaluating `settings.json` files and
iterating through projection payloads was fully duplicated between the
standalone JSON file processor and the ZIP archive extraction loop.

- Extract the core parsing logic to `tryAddProjectionFromJson`.
- Unify the application store additions and socket event emissions.
- Reduce maintenance overhead by centralizing import logic.
Extract the duplicate toast notification and console error logging
routines into a unified local helper function. This simplifies the
error-handling blocks within the file import loop and improves code
maintainability.

Previously, both failed network responses and catch blocks explicitly
duplicated calls to `toast.error` and `console.error` with localized
string variables.

- Create a local `showToastError` utility function.
- Replace inline toast and console error sequences with the helper.
…entries loop

Consolidate MIME type resolution during the asset import process to
eliminate duplicate execution paths and inline boilerplate. Previously,
the file extension checks relied on manual, brittle `endsWith` structures
and unsafe regular expressions that fallback to generating invalid image
MIME types for unknown extensions.

This change introduces a dedicated `MIME_MAP` dictionary and a `getMime`
helper function to safely evaluate file extensions in a clean, maintainable
manner. Additionally, cached `Object.entries(unzipped)` iterations avoid
unnecessary structural recalculations on the unzipped object across multiple
loops.

- Add `MIME_MAP` lookups supporting video, svg, and common image formats.
- Implement fallback defaults to `application/octet-stream` safely.
- Cache zip file entry extraction to prevent repeated descriptor processing.
- Remove redundant `response.ok` constraint checks when scanning headers.
- Simplify error handling via ternary variable declarations.
…ync unzip

Optimize the UI performance during .zip file processing by transitioning
from the blocking, synchronous `unzipSync` function to the asynchronous,
non-blocking `unzip` API provided by `fflate`.

Previously, extracting large projection archives containing multiple
image or video assets completely locked up the browser's main execution
thread, preventing the UI from rendering updates or maintaining smooth
frame rates. Moving to an async Promise wrapper allows decompression
work to happen off-thread. Additionally, the iteration model has been
streamlined into a single pass over the unzipped object entries instead
of running two separate full-array loops sequentially for assets and
JSON manifests.

- Replace synchronous browser-blocking `unzipSync` with asynchronous
  `unzip`.
- Consolidate dual asset/JSON processing passes into a unified
  single-loop architecture.
- Skip processing early for empty entries to reduce unneeded object
  allocations.
Fixes an issue where items grouped visually in the queue had
disjointed, non-sequential underlying indices (e.g., jumping from 1 to
X). This misalignment caused unexpected downstream behavior because the
physical array sequence did not correspond to the rendered neighbor
positions.

Sorting the master's contents chronologically during the import phase
by the first appearance of each group ensures that the underlying array
indices accurately reflect their visual sequence in the queue control.

- Centralize group name fallback parsing using the new `groupName`
  utility.
- Implement `sortMasterContents` to order imported items by group
  timeline.
- Standardize formatting and indentation for errors in the import
  processor.
Refactor file importing by removing explicit directory checks and
creation logic. Since `Bun.write` automatically creates missing parent
directories recursively when writing files, handling this manually is
redundant.

This optimization simplifies the file-writing pipeline and reduces
unnecessary synchronous filesystem checks before writing incoming
assets.

- Remove unused `mkdirSync` and `dirname` imports from `fs` and `path`.
- Delete synchronous directory checks and manual directory creation.
Transition server persistence from blocking, synchronous Node.js `fs`
methods to asynchronous, native Bun file operations. This prevents
event loop blockages during I/O and improves concurrency.

File writes are now atomic; data is saved to a temporary file before
overwriting the target destination, preventing file corruption. The
build configuration and `tsconfig` are updated to support ESM and
top-level await.

- Replace `fs.readFileSync` and `fs.writeFileSync` with
  `Bun.file`/`Bun.write`.
- Implement atomic file updates using `fs/promises` `rename`.
- Update server socket listeners to await persistence utilities.
- Target `ESNext` and enable Bun types to leverage top-level await.
Introduce a loading toast notification during the file import process
to improve user experience. Previously, importing files provided no
visual feedback, leaving users unaware of the background execution
progress and making the application appear unresponsive.

By initializing a loading toast at the start of processing and
dismissing it upon completion, users now receive clear indicators of
the ongoing activity.

- Create a persistent loading toast before iterating through files.
- Dismiss the toast notification immediately prior to calling the
  success callback.
@xellanix
xellanix merged commit 2d5a01c into main May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant