Skip to content

Commit

Permalink
feat(): support instantiateModalStack
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Oct 16, 2024
1 parent c36e5d7 commit 75c4ab4
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions dll/editor-bricks-helper/manifest.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@
"httpErrorToString",
"i18nText",
"initI18n",
"instantiateModalStack",
"isLoggedIn",
"logout",
"looseCheckIf",
Expand Down
11 changes: 11 additions & 0 deletions etc/brick-kit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,9 @@ export interface IfContainer {
// @internal (undocumented)
export const initI18n: () => void;

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

// @public
export function isLoggedIn(): boolean;

Expand Down Expand Up @@ -471,6 +474,14 @@ export abstract class ModalElement extends UpdatingElement {
openModal: (e?: CustomEvent) => void;
}

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

// Warning: (ae-internal-missing-underscore) The name "PartialMicroApp" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal (undocumented)
Expand Down
1 change: 1 addition & 0 deletions packages/brick-dll/manifest.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -18675,6 +18675,7 @@
"httpErrorToString",
"i18nText",
"initI18n",
"instantiateModalStack",
"isLoggedIn",
"logout",
"looseCheckIf",
Expand Down
47 changes: 47 additions & 0 deletions packages/brick-kit/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";

const defaultInitialIndex = 1000;

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

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

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/brick-kit/src/ModalStack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const stack: number[] = [];

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 };
}
1 change: 1 addition & 0 deletions packages/brick-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ export * from "./abortController";
export * from "./getRealValue";
export * from "./constructEventListener";
export * from "./websocket";
export * from "./ModalStack";

0 comments on commit 75c4ab4

Please sign in to comment.