Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): support instantiateModalStack #4516

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions etc/runtime.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ export interface ImagesFactory {
get(name: string): string;
}

// @public (undocumented)
export function instantiateModalStack(initialIndex?: number): ModalStack;

// @public @deprecated (undocumented)
export function isLoggedIn(): boolean | undefined;

Expand Down Expand Up @@ -276,6 +279,14 @@ export interface MatchOptions {
// @public
export function matchPath(pathname: string, options: MatchPathOptions): MatchResult | null;

// @public (undocumented)
export interface ModalStack {
// (undocumented)
pull: () => void;
// (undocumented)
push: () => number;
}

// @public (undocumented)
function mountUseBrick({ renderRoot, rendererContext, scopedStores }: RenderUseBrickResult, element: HTMLElement): MountUseBrickResult;

Expand Down
47 changes: 47 additions & 0 deletions packages/runtime/src/ModalStack.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { instantiateModalStack as _instantiateModalStack } from "./ModalStack.js";

const defaultInitialIndex = 1000;

describe("instantiateModalStack", () => {
let instantiateModalStack: typeof _instantiateModalStack;

beforeEach(() => {
jest.isolateModules(() => {
({ instantiateModalStack } = require("./ModalStack.js"));
});
});

test("should push a new index onto the stack and return the correct value", () => {
const stack = instantiateModalStack();
const result = stack.push();
expect(result).toBe(defaultInitialIndex);
});

test("should increment the index correctly on multiple stack pushes", () => {
const stack1 = instantiateModalStack();
const stack2 = instantiateModalStack();
const stack3 = instantiateModalStack();

const result1 = stack1.push();
const result2 = stack2.push();
expect(result1).toBe(defaultInitialIndex);
expect(result2).toBe(defaultInitialIndex + 1);

stack1.pull();
const result3 = stack3.push();
expect(result3).toBe(defaultInitialIndex + 2);
});

test("should handle pushes without pull", () => {
const stack = instantiateModalStack();
const result1 = stack.push();
const result2 = stack.push();
expect(result1).toBe(defaultInitialIndex);
expect(result2).toBe(defaultInitialIndex);
});

test("should respect param of initialIndex", () => {
const stack = instantiateModalStack(10);
expect(stack.push()).toBe(10);
});
});
29 changes: 29 additions & 0 deletions packages/runtime/src/ModalStack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const stack: number[] = [];
weareoutman marked this conversation as resolved.
Show resolved Hide resolved

export interface ModalStack {
push: () => number;
pull: () => void;
}

export function instantiateModalStack(initialIndex = 1000): ModalStack {
let index = -1;
const pull = (): void => {
if (index > -1) {
const found = stack.indexOf(index);
// Assert: found should always be greater than -1
// istanbul ignore else
if (found > -1) {
stack.splice(found, 1);
}
}
};
const push = (): number => {
// Handle pushes without pull
pull();
// Find the next available index
index = (stack[stack.length - 1] ?? -1) + 1;
stack.push(index);
return index + initialIndex;
};
return { push, pull };
}
weareoutman marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ export { Notification, type NotificationOptions } from "./Notification.js";
export { Dialog, type DialogOptions } from "./Dialog.js";
export * from "./getV2RuntimeFromDll.js";
export { setUIVersion } from "./setUIVersion.js";
export * from "./ModalStack.js";
Loading