@push.rocks/smartbucket
Version:
A TypeScript library providing a cloud-agnostic interface for managing object storage with functionalities like bucket management, file and directory operations, and advanced features such as metadata handling and file locking.
80 lines (79 loc) • 2.58 kB
TypeScript
import * as plugins from './plugins.js';
import * as interfaces from './interfaces.js';
import type { Bucket } from './classes.bucket.js';
import { EventEmitter } from 'node:events';
/**
* BucketWatcher monitors a storage bucket for changes (add/modify/delete)
* using a polling-based approach. Designed to follow the SmartdataDbWatcher pattern.
*
* @example
* ```typescript
* const watcher = bucket.createWatcher({ prefix: 'uploads/', pollIntervalMs: 3000 });
*
* // RxJS Observable pattern
* watcher.changeSubject.subscribe((change) => {
* console.log('Change:', change);
* });
*
* // EventEmitter pattern
* watcher.on('change', (change) => console.log(change));
* watcher.on('error', (err) => console.error(err));
*
* await watcher.start();
* await watcher.readyDeferred.promise; // Wait for initial state
*
* // Later...
* await watcher.stop();
* ```
*/
export declare class BucketWatcher extends EventEmitter {
/** Deferred that resolves when initial state is built and watcher is ready */
readyDeferred: plugins.smartpromise.Deferred<unknown>;
/** Observable for receiving change events (supports RxJS operators) */
changeSubject: plugins.smartrx.rxjs.Observable<interfaces.IStorageChangeEvent | interfaces.IStorageChangeEvent[]>;
private rawSubject;
private previousState;
private pollIntervalId;
private isPolling;
private isStopped;
private readonly bucketRef;
private readonly prefix;
private readonly pollIntervalMs;
private readonly bufferTimeMs?;
private readonly includeInitial;
private readonly pageSize;
constructor(bucketRef: Bucket, options?: interfaces.IBucketWatcherOptions);
/**
* Start watching the bucket for changes
*/
start(): Promise<void>;
/**
* Stop watching the bucket
*/
stop(): Promise<void>;
/**
* Alias for stop() - for consistency with other APIs
*/
close(): Promise<void>;
/**
* Build the initial state by listing all objects with metadata
*/
private buildInitialState;
/**
* Poll for changes by comparing current state against previous state
*/
private poll;
/**
* Detect changes between current and previous state
*/
private detectChanges;
/**
* Emit a change event via both RxJS Subject and EventEmitter
*/
private emitChange;
/**
* List objects with full metadata (ETag, Size, LastModified)
* This is a private method that yields full _Object data, not just keys
*/
private listObjectsWithMetadata;
}