# new FileSystemCache()
Example
import { getFileSystemCache } from 'muziertcclient';
const cache = getFileSystemCache({
userId: 'user123',
roomName: 'meeting-room',
isRoomOwner: true,
client: muzieClient
});
await cache.initialize();
const tree = await cache.getFileTree();
Classes
Methods
# _handleVisibilityChange()
Handles visibility change events to trigger synchronization when the tab becomes visible.
# async addFolder(folderData) → {Promise.<boolean>}
Adds a folder to the local cache and updates the tree structure.
Parameters:
| Name | Type | Description |
|---|---|---|
folderData |
FileMetadata
|
Metadata of the folder to add |
If adding the folder fails
Error
True when the folder is added successfully
Promise.<boolean>
Example
await cache.addFolder({
fileId: 'folder-123',
location: '/documents/new-folder',
fileName: 'new-folder',
timestamp: new Date().toISOString()
});
# addToTree(tree, nodeMap, item)
Adds a file or folder to the tree structure.
Parameters:
| Name | Type | Description |
|---|---|---|
tree |
TreeNode
|
Root node of the tree |
nodeMap |
Object.<string, TreeNode>
|
Map of existing nodes by path |
item |
FileMetadata
|
File metadata of the item to add |
# buildFileTree(items) → {TreeNode}
Constructs a hierarchical tree structure from a flat list of file metadata. Creates parent-child relationships and ensures all paths in the tree are valid.
Parameters:
| Name | Type | Description |
|---|---|---|
items |
Array.<FileMetadata>
|
Array of file metadata objects |
Root node of the constructed file tree
Example
const files = [
{ fileId: '1', fileName: 'doc.pdf', location: '/docs', isFolder: false },
{ fileId: '2', location: '/docs', isFolder: true }
];
const tree = cache.buildFileTree(files);
# async deleteFile(fileId, locationopt) → {Promise.<boolean>}
Deletes a file from the local cache and updates the tree structure.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
fileId |
string
|
Unique identifier of the file to delete |
|
location |
string
|
<optional> |
Location path of the file |
If deleting the file fails
Error
True when the file is deleted successfully
Promise.<boolean>
Example
await cache.deleteFile('file-123', '/documents');
# dispose()
Disposes of the file system cache by clearing intervals and removing event listeners.
Example
// Clean up when leaving the room
cache.dispose();
# ensurePathExists(fullPath, nodeMap, excludePathopt, isFileopt, sourceItemopt)
Ensures that all parent folders in the given path exist in the node map. Creates synthetic folder nodes if necessary to fill gaps in the path.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fullPath |
string
|
Full path to the file or folder |
||
nodeMap |
Object.<string, TreeNode>
|
Map of existing nodes by path |
||
excludePath |
string
|
<optional> |
null | Path to exclude from creation |
isFile |
boolean
|
<optional> |
false | Whether this is a file path (vs folder path) |
sourceItem |
Object
|
<optional> |
null | Source item for inheriting sharing info |
# findFileParent(fileId, nodeMap) → {string|null}
Finds the parent path of a file in the tree structure.
Parameters:
| Name | Type | Description |
|---|---|---|
fileId |
string
|
Unique identifier of the file |
nodeMap |
Object.<string, TreeNode>
|
Map of existing nodes by path |
Parent path of the file, or null if not found
string
|
null
# flattenTree(tree) → {Object.<string, TreeNode>}
Flattens the hierarchical tree structure into a map of nodes by path.
Parameters:
| Name | Type | Description |
|---|---|---|
tree |
TreeNode
|
Root node of the tree |
Map of nodes by path
Object.<string, TreeNode>
# flattenTreeToFiles(tree) → {Array}
Flattens a tree structure to get all files
Parameters:
| Name | Type | Description |
|---|---|---|
tree |
Object
|
Tree node |
Array of file objects
Array
# async getFileTree(waitForSyncopt) → {Promise.<TreeNode>}
Retrieves the file tree from the local cache. Optionally waits for synchronization to complete before returning the tree.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
waitForSync |
boolean
|
<optional> |
false | Whether to wait for synchronization to complete |
If retrieving the file tree fails
Error
The file tree
Promise.<TreeNode>
Examples
const tree = await cache.getFileTree();
console.log('Root children:', Object.keys(tree.children));
// Wait for any pending sync to complete
const tree = await cache.getFileTree(true);
# async getFileTreeByRoom(roomName) → {Promise.<TreeNode>}
Gets file tree filtered by room name (for room owners only)
Parameters:
| Name | Type | Description |
|---|---|---|
roomName |
string
|
Room name to filter by |
Tree structure for the specified room
Promise.<TreeNode>
Example
// As room owner, get file tree for a specific room
const tree = await cache.getFileTreeByRoom('team-meeting');
# async getFilesByRoom(roomName) → {Promise.<Array.<FileMetadata>>}
Gets files filtered by room name (for room owners only)
Parameters:
| Name | Type | Description |
|---|---|---|
roomName |
string
|
Room name to filter by |
Array of file metadata objects for the specified room
Promise.<Array.<FileMetadata>>
Example
// As room owner, get files for a specific room
const files = await cache.getFilesByRoom('team-meeting');
# async getFilesByType(resourceType) → {Promise.<Array.<FileMetadata>>}
Retrieves files of the specified resource type from the local cache. Files are already filtered by room (cache is room-specific).
Parameters:
| Name | Type | Description |
|---|---|---|
resourceType |
string
|
Resource type to filter files by |
If retrieving files by resource type fails
Error
Array of file metadata objects
Promise.<Array.<FileMetadata>>
Example
const recordings = await cache.getFilesByType('recording');
recordings.forEach(r => console.log(r.fileName));
# getNameFromPath(path) → {string}
Extracts the name from the given path.
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string
|
Path to extract the name from |
Name extracted from the path
string
# getParentPath(path) → {string}
Retrieves the parent path of the given path.
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string
|
Path to get the parent of |
Parent path
string
# async initialize() → {Promise.<boolean>}
Initializes the file system cache by setting up the IndexedDB database, loading all files, and starting periodic synchronization.
If database setup fails
Error
True when initialization completes successfully
Promise.<boolean>
Example
const cache = getFileSystemCache({ userId: 'user123', roomName: 'room1', client });
await cache.initialize();
console.log('Cache ready');
# async loadAllFiles() → {Promise.<{tree: TreeNode, items: Array.<FileMetadata>}>}
Fetches all files and folders from the server and rebuilds the local cache. Clears existing cache data and stores the fresh data in IndexedDB.
If loading files from the server fails
Error
The file tree and all file metadata
Promise.<{tree: TreeNode, items: Array.<FileMetadata>}>
Example
const { tree, items } = await cache.loadAllFiles();
console.log(`Loaded ${items.length} files`);
# async moveFile(fileId, newLocation) → {Promise.<{tree: TreeNode}>}
Moves a file to a new location in the tree structure. Updates the file's path and parent node in the tree.
Parameters:
| Name | Type | Description |
|---|---|---|
fileId |
string
|
Unique identifier of the file to move |
newLocation |
string
|
New location path for the file |
If moving the file fails
Error
The updated file tree
Promise.<{tree: TreeNode}>
Example
// Move file to a different folder
const { tree } = await cache.moveFile('file123', '/documents/archive');
# removeFromTree(tree, nodeMap, item)
Removes a file or folder from the tree structure.
Parameters:
| Name | Type | Description |
|---|---|---|
tree |
TreeNode
|
Root node of the tree |
nodeMap |
Object.<string, TreeNode>
|
Map of existing nodes by path |
item |
FileMetadata
|
File metadata of the item to remove |
# resumeSync()
Resumes periodic sync after it was paused due to consecutive failures. Resets the failure counter and sync paused flag.
Example
// After fixing network issues, resume sync
cache.resumeSync();
# startPeriodicSync()
Starts periodic synchronization with the server. Sets up intervals for incremental and full synchronization.
# async syncWithServer(optionsopt) → {Promise.<{tree: TreeNode}>}
Synchronizes the local cache with the server. Performs incremental or full synchronization based on the provided options.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
options |
Object
|
<optional> |
Synchronization options |
|
forceFullSync |
boolean
|
<optional> |
false | Whether to force a full synchronization |
If synchronization fails
Error
The updated file tree
Promise.<{tree: TreeNode}>
Examples
// Incremental sync
const { tree } = await cache.syncWithServer();
// Force full resync
const { tree } = await cache.syncWithServer({ forceFullSync: true });