Skip to content

Commit

Permalink
Add required: true option to Switch.currentProfile(), and cache r…
Browse files Browse the repository at this point in the history
…esult for future calls
  • Loading branch information
TooTallNate committed Jan 13, 2024
1 parent ba6b9da commit 0f84ee1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-weeks-unite.md
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Add `required: true` option to `Switch.currentProfile()`, and cache result for future calls
21 changes: 18 additions & 3 deletions packages/runtime/src/switch/profile.ts
Expand Up @@ -9,14 +9,16 @@ function _init() {
init = true;
}

export type ProfileUid = [bigint, bigint];

/**
* Represents a user profile that exists on the system.
*/
export class Profile {
/**
* The unique ID of the profile, represented as an array of two `bigint` values.
*/
declare readonly uid: [bigint, bigint];
declare readonly uid: ProfileUid;

/**
* The human readable nickname of the profile.
Expand All @@ -37,14 +39,27 @@ export class Profile {
}
$.accountProfileInit(Profile);

let p: Profile | null = null;

export interface CurrentProfileOptions {
required?: boolean;
}

/**
* Return a {@link Profile} instance if there was a preselected user
* when launching the application, or `null` if there was none.
*
* If `required: true` is set and there was no preselected user, then
* the user selection interface will be shown to allow the user to
* select a profile. Subsequent calls to `currentProfile()` will
* return the selected profile without user interaction.
*/
export function currentProfile() {
export function currentProfile({ required }: CurrentProfileOptions = {}) {
_init();
const p = $.accountCurrentProfile();
if (p) return p;
p = $.accountCurrentProfile();
if (p) Object.setPrototypeOf(p, Profile.prototype);
while (!p && required) p = selectProfile();
return p;
}

Expand Down

0 comments on commit 0f84ee1

Please sign in to comment.