Skip to content

Commit

Permalink
Make name parameter of Application#mountSaveData() optional
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Mar 23, 2024
1 parent d4d0ccf commit e87cae9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-gifts-hear.md
@@ -0,0 +1,5 @@
---
"nxjs-runtime": patch
---

Make `name` parameter of `Application#mountSaveData()` optional
8 changes: 4 additions & 4 deletions packages/runtime/src/storage.ts
Expand Up @@ -120,20 +120,20 @@ Object.defineProperty(globalThis, 'localStorage', {
get() {
const { self } = Application;
const profile = currentProfile({ required: true });
const name = 'localstorage';
const base = `${name}:/localStorage/`;

let dev: FsDev;
try {
dev = self.mountSaveData(name, profile);
dev = self.mountSaveData(profile);
} catch (err: any) {
// rethrow if not `ResultTargetNotFound` (meaning save data has not been created)
if (err.description !== 1002) throw err;

self.createSaveData(profile);
dev = self.mountSaveData(name, profile);
dev = self.mountSaveData(profile);
}

const base = new URL('localStorage/', dev.url);

const impl: StorageImpl = {
clear() {
removeSync(base);
Expand Down
18 changes: 11 additions & 7 deletions packages/runtime/src/switch/ns.ts
@@ -1,5 +1,6 @@
import { $ } from '../$';
import { FsDev } from './fsdev';
import { crypto } from '../crypto';
import { readFileSync } from '../fs';
import { proto, stub } from '../utils';
import type { Profile } from './profile';
Expand All @@ -13,6 +14,8 @@ function _init() {
init = true;
}

const genName = () => `s${crypto.randomUUID().replace(/-/g, '').slice(0, 16)}`;

/**
* Represents an installed application (game) on the console,
* or a homebrew application (`.nro` file).
Expand Down Expand Up @@ -124,20 +127,21 @@ export class Application {
*
* ```typescript
* const profile = Switch.currentProfile({ required: true });
* const device = app.mountSaveData('save', profile);
* const saveData = app.mountSaveData(profile);
*
* // Use the filesystem functions to do operations on the save mount
* console.log(Switch.readDirSync('save:/'));
* console.log(Switch.readDirSync(saveData.url));
*
* // Make sure to use `device.commit()` after any write operations
* Switch.writeFileSync('save:/state', 'your app state…');
* device.commit();
* // Make sure to use `saveData.commit()` after any write operations
* const saveStateUrl = new URL('state', saveData.url)
* Switch.writeFileSync(saveStateUrl, 'your app state…');
* saveData.commit();
* ```
*
* @param name The name of the device mount for filesystem paths.
* @param profile The {@link Profile} which the save data belongs to.
* @param name The name of the device mount for filesystem paths. If not provided, a random name is generated.
*/
mountSaveData(name: string, profile: Profile) {
mountSaveData(profile: Profile, name = genName()): FsDev {
$.fsdevMountSaveData(name, this.nacp, profile.uid);
return new FsDev(name);
}
Expand Down

0 comments on commit e87cae9

Please sign in to comment.