Warehouse.js
Overview
The Warehouse.js file defines the Warehouse class, which serves as an abstraction layer over the browser's localStorage API for managing persistent meeting data storage. Its primary purpose is to facilitate saving, loading, removing, and clearing meeting records while gracefully handling the absence of localStorage support. The class enforces a limit of retaining only the 10 most recent meeting records to prevent excessive storage consumption.
This file is a critical component of the Persistent Meeting History functionality, enabling durable storage of past meetings for later review or management. It isolates direct storage manipulation from the rest of the application, promoting modularity and maintainability.
Class: Warehouse
Properties
local: boolean
Flag indicating whether the browser supportslocalStorage. Initialized in the constructor by checking ifStorageis defined.defaultKey: string
The default key ("meetings") under which meeting data arrays are stored inlocalStorage.
Constructor
constructor()
Detects browser support for
localStorage.Sets the
localflag totrueif supported, otherwisefalse.
Usage:
Instantiating the class initializes the support flag:
const warehouse = new Warehouse();
console.log(warehouse.local); // true if localStorage supported, else false
Methods
save
save(payload, key = this.defaultKey)
Purpose:
Saves a new meeting record (payload) tolocalStorageunder the specified key. If no key is provided, defaults to"meetings".Parameters:
payload(any): The meeting data to save, typically an array of increments or meeting objects.key(string, optional): The storage key; defaults todefaultKey.
Returns:
The return value ofwindow.localStorage.setItem(usuallyundefined), ornulliflocalStorageis unsupported.Behavior:
Checks if
localStorageis supported (localflag).Loads existing stored meetings by calling
load().Parses the stored string into an array or initializes an empty array.
Appends the new
payloadto the array.Maintains a maximum of 10 meeting records by removing the oldest entry if exceeded.
Serializes and saves the updated array back to
localStorage.
Example:
const meetingData = [{ attendeeCount: 3, averageWage: 25, startTime: "..." }];
warehouse.save(meetingData);
load
load(key = this.defaultKey)
Purpose:
Retrieves the stored meeting data string fromlocalStoragefor the given key.Parameters:
key(string, optional): The storage key to load from. Defaults to"meetings".
Returns:
The stringified JSON stored under the key, ornullif no data exists orlocalStorageis unsupported.Behavior:
ValidateslocalStorageavailability and key type, then returns the raw stored string.
Usage:
const storedMeetingsJSON = warehouse.load();
const storedMeetings = storedMeetingsJSON ? JSON.parse(storedMeetingsJSON) : [];
remove
remove(key = this.defaultKey)
Purpose:
Removes the stored item under the specified key fromlocalStorage.Parameters:
key(string, optional): The key to remove; defaults to"meetings".
Returns:
The return value ofwindow.localStorage.removeItem, ornullif unsupported or invalid key.Behavior:
ValidateslocalStorageavailability and key type before removal.
clear
clear()
Purpose:
Clears the entirelocalStoragefor the domain.Returns:
Return value ofwindow.localStorage.clear(), ornullif unsupported.Behavior:
Does not accept parameters; clears all keys inlocalStorage.
key
key(index)
Purpose:
Returns the key name at the specified index inlocalStorage.Parameters:
index(number): The position of the key to retrieve.
Returns:
The key name at the given index, ornullif unsupported.Behavior:
Provides direct access to keys inlocalStoragefor enumeration or inspection.
Implementation Details and Algorithms
LocalStorage Support Detection:
The constructor verifies if theStorageinterface is available in the global environment. This check enables graceful fallback where storage is not supported (e.g., some browsers or privacy modes).Data Structure and Serialization:
Meeting data is stored as a JSON stringified array under a single key ("meetings"by default). Each element in the array corresponds to a saved meeting record (commonly an array of increments).Data Retention Limit:
Thesavemethod maintains at most 10 stored meeting records. When adding a new record exceeds this limit, the oldest record is removed usingshift(). This prevents uncontrolled storage growth and possible quota exceed errors.Input Validation:
Methods involving keys (load,remove) check that the key is a string to prevent runtime errors.
Interaction with Other Parts of the System
The
Warehouseclass is primarily used by modules responsible for meeting lifecycle and history management. When a meeting concludes via the Meeting Lifecycle Management module, the final meeting increments are passed to theWarehouse.save()method to persist the data.The stored meeting arrays are loaded by the historical display components within Persistent Meeting History and Historical Meeting Display to render past meeting summaries in tabular form.
User interface actions (from User Interaction and Interface) such as clearing storage invoke the
Warehouse.remove()orWarehouse.clear()methods.The abstraction provided by
Warehousedecouples persistent storage concerns from UI and business logic, enabling easier maintenance and potential future switching of storage backends.
Usage Example
Typical workflow involving Warehouse:
import Warehouse from './Warehouse.js';
const warehouse = new Warehouse();
// Saving a new meeting record
const newMeeting = [{ attendeeCount: 5, averageWage: 30, startTime: Date.now() }];
warehouse.save(newMeeting);
// Loading all stored meetings
const savedData = warehouse.load();
const meetings = savedData ? JSON.parse(savedData) : [];
// Removing stored meetings
warehouse.remove(); // removes under default "meetings" key
// Clearing all localStorage data (use with caution)
warehouse.clear();
Visual Diagram: Warehouse Class Structure
classDiagram
class Warehouse {
+local: boolean
+defaultKey: string
+constructor()
+save()
+load()
+remove()
+clear()
+key()
}
This diagram highlights the Warehouse class with its key properties and methods, illustrating its role as a self-contained storage abstraction.
Related Topics
Persistent Meeting History — Explains how the
Warehouseintegrates into the system to provide meeting data persistence and retrieval.LocalStorage Data Management — Details the internal workings and guarantees of the
Warehouseclass methods andlocalStoragehandling.Historical Meeting Display — Demonstrates how meetings loaded via
Warehouse.load()are rendered for user review.User Interaction and Interface — Describes controls that trigger storage operations such as saving on meeting stop or clearing storage on user request.