koin.js 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -5640,10 +5640,59 @@ function getCore2(system) {
5640
5640
  return getCore(system);
5641
5641
  }
5642
5642
 
5643
+ // src/lib/rom-cache.ts
5644
+ var CACHE_NAME = "koin-rom-cache-v1";
5645
+ async function getCachedRom(romId) {
5646
+ if (typeof caches === "undefined") return null;
5647
+ try {
5648
+ const cache = await caches.open(CACHE_NAME);
5649
+ const response = await cache.match(romId);
5650
+ if (response) {
5651
+ console.log(`[Cache] Hit for ROM ${romId}`);
5652
+ return await response.blob();
5653
+ }
5654
+ } catch (e) {
5655
+ console.warn("[Cache] Read failed:", e);
5656
+ }
5657
+ return null;
5658
+ }
5659
+ var pendingFetches = /* @__PURE__ */ new Map();
5660
+ async function fetchAndCacheRom(romId, url) {
5661
+ if (pendingFetches.has(romId)) {
5662
+ console.log(`[Cache] Joining in-flight fetch for ${romId}`);
5663
+ return pendingFetches.get(romId);
5664
+ }
5665
+ const fetchPromise = (async () => {
5666
+ try {
5667
+ const cached = await getCachedRom(romId);
5668
+ if (cached) return cached;
5669
+ console.log(`[Cache] Fetching ROM ${romId}...`);
5670
+ const response = await fetch(url);
5671
+ if (!response.ok) throw new Error(`Failed to fetch ROM: ${response.statusText}`);
5672
+ const blob = await response.blob();
5673
+ if (typeof caches !== "undefined") {
5674
+ try {
5675
+ const cache = await caches.open(CACHE_NAME);
5676
+ await cache.put(romId, new Response(blob));
5677
+ console.log(`[Cache] Cached ROM ${romId}`);
5678
+ } catch (e) {
5679
+ console.warn("[Cache] Write failed:", e);
5680
+ }
5681
+ }
5682
+ return blob;
5683
+ } finally {
5684
+ pendingFetches.delete(romId);
5685
+ }
5686
+ })();
5687
+ pendingFetches.set(romId, fetchPromise);
5688
+ return fetchPromise;
5689
+ }
5690
+
5643
5691
  // src/hooks/emulator/useEmulatorCore.ts
5644
5692
  function useEmulatorCore({
5645
5693
  system,
5646
5694
  romUrl,
5695
+ romId,
5647
5696
  core: coreOverride,
5648
5697
  biosUrl,
5649
5698
  initialState,
@@ -5725,7 +5774,25 @@ function useEmulatorCore({
5725
5774
  setError(null);
5726
5775
  const core = coreOverride || getCore2(system);
5727
5776
  let romOption = romUrl;
5728
- if (romFileName) {
5777
+ if (romId) {
5778
+ try {
5779
+ let blob = await getCachedRom(romId);
5780
+ if (!blob) {
5781
+ console.log(`[Nostalgist] Fetching and caching ROM ${romId}`);
5782
+ blob = await fetchAndCacheRom(romId, romUrl);
5783
+ } else {
5784
+ console.log(`[Nostalgist] Loaded ROM ${romId} from cache`);
5785
+ }
5786
+ if (blob) {
5787
+ romOption = {
5788
+ fileName: romFileName || "rom.bin",
5789
+ fileContent: blob
5790
+ };
5791
+ }
5792
+ } catch (err) {
5793
+ console.error("[Nostalgist] Cache/Fetch error, falling back to direct URL:", err);
5794
+ }
5795
+ } else if (romFileName) {
5729
5796
  romOption = { fileName: romFileName, fileContent: romUrl };
5730
5797
  }
5731
5798
  const inputConfig = buildRetroArchConfig({
@@ -6392,7 +6459,8 @@ var useNostalgist = ({
6392
6459
  onError,
6393
6460
  initialVolume = 100,
6394
6461
  romFileName,
6395
- shader
6462
+ shader,
6463
+ romId
6396
6464
  }) => {
6397
6465
  const isHeavySystem = useMemo(() => {
6398
6466
  return PERFORMANCE_TIER_2_SYSTEMS.has(system.toUpperCase());
@@ -6420,6 +6488,7 @@ var useNostalgist = ({
6420
6488
  } = useEmulatorCore({
6421
6489
  system,
6422
6490
  romUrl,
6491
+ romId,
6423
6492
  core,
6424
6493
  biosUrl,
6425
6494
  initialState,
@@ -6613,6 +6682,7 @@ function useControls(system, onNotify) {
6613
6682
  function useGameSession(props) {
6614
6683
  const {
6615
6684
  romUrl,
6685
+ romId,
6616
6686
  romFileName,
6617
6687
  system,
6618
6688
  core,
@@ -6661,6 +6731,7 @@ function useGameSession(props) {
6661
6731
  const nostalgist = useNostalgist({
6662
6732
  system,
6663
6733
  romUrl,
6734
+ romId,
6664
6735
  romFileName,
6665
6736
  core,
6666
6737
  biosUrl,
@@ -8124,6 +8195,6 @@ var ShortcutsReference = memo(function ShortcutsReference2({
8124
8195
  });
8125
8196
  var ShortcutsReference_default = ShortcutsReference;
8126
8197
 
8127
- export { ALL_BUTTONS, AchievementPopup, BUTTON_GROUPS, BUTTON_LABELS, CONSOLE_CAPABILITIES, CONSOLE_KEYBOARD_OVERRIDES, DEFAULT_CONTROLS, DEFAULT_GAMEPAD, DEFAULT_KEYBOARD, DPAD_BUTTONS, FACE_BUTTONS, GamePlayer_default as GamePlayer, PERFORMANCE_TIER_1_SYSTEMS, PERFORMANCE_TIER_2_SYSTEMS, RASidebar, RA_MEDIA_BASE, SHADER_PRESETS, SHOULDER_BUTTONS, STANDARD_AXIS_MAP, STANDARD_GAMEPAD_BUTTONS, STICK_BUTTONS, SUPPORTED_EXTENSIONS, SYSTEMS, SYSTEM_BUTTONS, ShaderSelector_default as ShaderSelector, ShortcutsReference_default as ShortcutsReference, TRIGGER_BUTTONS, ToastContainer, buildRetroArchConfig, clearAllControls, consoleHasButton, detectControllerBrand, detectSystem, formatGamepadButton, formatKeyCode, gamepadToRetroArchConfig, getAchievementBadgeUrl, getConsoleButtons, getConsoleCapabilities, getConsoleKeyboardDefaults, getCore, getDBSystemNames, getFullGamepadMapping, getFullKeyboardMapping, getSupportedExtensions, getSystem, getSystemByDbName, getSystemByKey, getSystemFromExtension, getSystemsList, getUserAvatarUrl, isSystemSupported, keyboardToRetroArchConfig, loadAllGamepadMappings, loadGamepadMapping, loadKeyboardMapping, normalizeSystemKey, saveGamepadMapping, saveKeyboardMapping, systemsMatch, useGameRecording, useGamepad, useNostalgist, useToast };
8198
+ export { ALL_BUTTONS, AchievementPopup, BUTTON_GROUPS, BUTTON_LABELS, CONSOLE_CAPABILITIES, CONSOLE_KEYBOARD_OVERRIDES, DEFAULT_CONTROLS, DEFAULT_GAMEPAD, DEFAULT_KEYBOARD, DPAD_BUTTONS, FACE_BUTTONS, GamePlayer_default as GamePlayer, PERFORMANCE_TIER_1_SYSTEMS, PERFORMANCE_TIER_2_SYSTEMS, RASidebar, RA_MEDIA_BASE, SHADER_PRESETS, SHOULDER_BUTTONS, STANDARD_AXIS_MAP, STANDARD_GAMEPAD_BUTTONS, STICK_BUTTONS, SUPPORTED_EXTENSIONS, SYSTEMS, SYSTEM_BUTTONS, ShaderSelector_default as ShaderSelector, ShortcutsReference_default as ShortcutsReference, TRIGGER_BUTTONS, ToastContainer, buildRetroArchConfig, clearAllControls, consoleHasButton, detectControllerBrand, detectSystem, fetchAndCacheRom, formatGamepadButton, formatKeyCode, gamepadToRetroArchConfig, getAchievementBadgeUrl, getCachedRom, getConsoleButtons, getConsoleCapabilities, getConsoleKeyboardDefaults, getCore, getDBSystemNames, getFullGamepadMapping, getFullKeyboardMapping, getSupportedExtensions, getSystem, getSystemByDbName, getSystemByKey, getSystemFromExtension, getSystemsList, getUserAvatarUrl, isSystemSupported, keyboardToRetroArchConfig, loadAllGamepadMappings, loadGamepadMapping, loadKeyboardMapping, normalizeSystemKey, saveGamepadMapping, saveKeyboardMapping, systemsMatch, useGameRecording, useGamepad, useNostalgist, useToast };
8128
8199
  //# sourceMappingURL=index.mjs.map
8129
8200
  //# sourceMappingURL=index.mjs.map