Skip to content

Commit

Permalink
feat(masonry): Create Brick Warehouse module with functions to add, r…
Browse files Browse the repository at this point in the history
…etrieve and delete instances
  • Loading branch information
Karan-Palan committed Sep 2, 2024
1 parent 1a72c64 commit 4fba69d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
6 changes: 6 additions & 0 deletions modules/masonry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
"playground": "vite playground --host --port 5601"
},
"dependencies": {
"uuid": "^10.0.0"
},
"devDependencies": {
"@types/uuid": "^10.0.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
createBrickData,
createBrickExpression,
createBrickStatement,
} from './BrickFactory';
} from './brickFactory';
import BrickBlock from './BrickBlock';
import BrickData from './BrickData';
import BrickExpression from './BrickExpression';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import BrickStatement from './BrickStatement';
import BrickExpression from './BrickExpression';
import BrickData from './BrickData';
import BrickBlock from './BrickBlock';
import { addBrickToWarehouse } from './brickWarehouse';

/**
* Factory function to create a new BrickBlock instance.
Expand All @@ -30,10 +31,12 @@ export function createBrickBlock(params: {
connectBelow: boolean;
nestLengthY: number;
}): BrickBlock {
return new BrickBlock({
const brick = new BrickBlock({
id: uuidv4(),
...params,
});
addBrickToWarehouse(brick);
return brick;
}

/**
Expand All @@ -54,10 +57,12 @@ export function createBrickData(params: {
outline: TBrickColor;
scale: number;
}): BrickData {
return new BrickData({
const brick = new BrickData({
id: uuidv4(),
...params,
});
addBrickToWarehouse(brick);
return brick;
}

/**
Expand All @@ -83,10 +88,12 @@ export function createBrickExpression(params: {
outline: TBrickColor;
scale: number;
}): BrickExpression {
return new BrickExpression({
const brick = new BrickExpression({
id: uuidv4(),
...params,
});
addBrickToWarehouse(brick);
return brick;
}

/**
Expand All @@ -113,8 +120,10 @@ export function createBrickStatement(params: {
connectAbove: boolean;
connectBelow: boolean;
}): BrickStatement {
return new BrickStatement({
const brick = new BrickStatement({
id: uuidv4(),
...params,
});
addBrickToWarehouse(brick);
return brick;
}
38 changes: 38 additions & 0 deletions modules/masonry/src/brick/design0/brickWarehouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import BrickStatement from './BrickStatement';
import BrickExpression from './BrickExpression';
import BrickData from './BrickData';
import BrickBlock from './BrickBlock';

// Warehouse to manage brick instances
const brickWarehouse: Map<string, BrickStatement | BrickExpression | BrickData | BrickBlock> =
new Map();

/**
* Adds a brick instance to the warehouse.
* @param brick - The brick instance to add.
*/
export function addBrickToWarehouse(
brick: BrickStatement | BrickExpression | BrickData | BrickBlock,
): void {
brickWarehouse.set(brick.id, brick);
}

/**
* Retrieves a brick instance from the warehouse by its ID.
* @param id - The ID of the brick to retrieve.
* @returns The brick instance if found, otherwise undefined.
*/
export function getBrickFromWarehouse(
id: string,
): BrickStatement | BrickExpression | BrickData | BrickBlock | undefined {
return brickWarehouse.get(id);
}

/**
* Deletes a brick instance from the warehouse by its ID.
* @param id - The ID of the brick to delete.
* @returns True if the brick was deleted, false if it was not found.
*/
export function deleteBrickFromWarehouse(id: string): boolean {
return brickWarehouse.delete(id);
}

0 comments on commit 4fba69d

Please sign in to comment.