File size: 7,798 Bytes
4fea3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
interface DatabaseRecord {
    id: number; name: string; metadata: { [key: string]: any };
}interface QueryOptions { limit?: number; offset?: number; orderBy?: string; filters?: { [field: string]: any }; }enum ConnectionStatus {
    DISCONNECTED = 'disconnected', CONNECTING = 'connecting', CONNECTED = 'connected',
    ERROR = 'error'
} type EventHandler<T> = ((data: T) => void) | ((data: T) => Promise<void>);
class DatabaseConnection {
    private connectionString: string;
    private status: ConnectionStatus = ConnectionStatus.DISCONNECTED; private retryCount: number = 0; private maxRetries: number = 3; constructor(connectionString: string) { this.connectionString = connectionString; } async connect(): Promise<void> {
        this.status = ConnectionStatus.CONNECTING; try {
            await this.simulateConnection();
            this.status = ConnectionStatus.CONNECTED;
            this.retryCount = 0;
        } catch (error) {
            this.status = ConnectionStatus.ERROR;
            this.retryCount++;
            if (this.retryCount < this.maxRetries) {
                await this.delay(1000 * this.retryCount);
                return this.connect();
            } throw new Error(`Failed to connect after ${this.maxRetries} attempts`);
        }
    } private async simulateConnection(): Promise<void> {
        await this.delay(Math.random() * 1000);
        if (Math.random() < 0.2) {
            throw new Error('Connection failed');
        }
    } private delay(ms: number): Promise<void> {
        return new Promise(resolve => setTimeout(resolve, ms));
    } getStatus(): ConnectionStatus {
        return this.status;
    }
} class AsyncDataProcessor<T> {
    private processors: Array<(data: T) => Promise<T>> = [];
    private errorHandlers: Array<(error: Error, data: T) => void> = [];
    addProcessor(processor: (data: T) => Promise<T>): this {
        this.processors.push(processor); return this;
    } onError(handler: (error: Error, data: T) => void): this { this.errorHandlers.push(handler); return this; } async process(data: T): Promise<T> {
        let result = data; for (const processor of this.processors) {
            try { result = await processor(result); } catch (error) {
                this.errorHandlers.forEach(handler => handler(error as Error, result));
                throw error;
            }
        } return result;
    }
} class GenericRepository<T extends { id: number }> {
    private data: Map<number, T> = new Map();
    private eventHandlers: Map<string, EventHandler<T>[]> = new Map();
    async create(item: Omit<T, 'id'>): Promise<T> {
        const id = this.generateId();
        const newItem = { ...item, id } as T;
        this.data.set(id, newItem);
        this.emit('created', newItem); return newItem;
    } async findById(id: number): Promise<T | null> {
        const item = this.data.get(id) || null; if (item) {
            this.emit('read', item);
        } return item;
    } async findAll(options: QueryOptions = {}): Promise<T[]> {
        let items = Array.from(this.data.values());
        if (options.filters) {
            items = items.filter(item => this.matchesFilters(item, options.filters!));
        } if (options.orderBy) {
            items = this.sortItems(items, options.orderBy);
        } if (options.limit) {
            const start = options.offset || 0;
            items = items.slice(start, start + options.limit);
        } return items;
    } async update(id: number, updates: Partial<Omit<T, 'id'>>): Promise<T | null> {
        const existing = this.data.get(id);
        if (!existing) return null;
        const updated = { ...existing, ...updates } as T;
        this.data.set(id, updated);
        this.emit('updated', updated);
        return updated;
    } async delete(id: number): Promise<boolean> {
        const item = this.data.get(id);
        if (!item) return false;
        this.data.delete(id);
        this.emit('deleted', item);
        return true;
    } on<K extends string>(event: K, handler: EventHandler<T>): void {
        if (!this.eventHandlers.has(event)) {
            this.eventHandlers.set(event, []);
        } this.eventHandlers.get(event)!.push(handler);
    } private emit(event: string, data: T): void {
        const handlers = this.eventHandlers.get(event) || [];
        handlers.forEach(handler => handler(data));
    } private generateId(): number {
        return Math.floor(Math.random() * 1000000) + Date.now();
    }
    private matchesFilters(item: T, filters: { [field: string]: any }): boolean {
        return Object.entries(filters).every(([field, value]) => {
            const itemValue = (item as any)[field];
            if (Array.isArray(value)) {
                return value.includes(itemValue);
            }
            return itemValue === value;
        });
    }
    private sortItems(items: T[], orderBy: string): T[] {
        const [field, direction = 'asc'] = orderBy.split(':');
        return items.sort((a, b) => {
            const aValue = (a as any)[field];
            const bValue = (b as any)[field];
            const comparison = aValue < bValue ? -1 : aValue > bValue ? 1 : 0;
            return direction === 'desc' ? -comparison : comparison;
        });
    }
} function createAsyncIterator<T>(items: T[], delay: number = 100) {
    return { async*[Symbol.asyncIterator]() { for (const item of items) { await new Promise(resolve => setTimeout(resolve, delay)); yield item; } } };
} async function processInBatches<T, R>(
    items: T[], batchSize: number, processor: (batch: T[]) => Promise<R[]>): Promise<R[]> {
    const results: R[] = [];
    for (let i = 0; i < items.length; i += batchSize) {
        const batch = items.slice(i, i + batchSize); const batchResults = await processor(batch);
        results.push(...batchResults);
    } return results;
} const randomData: DatabaseRecord[] = [
    { id: 1, name: 'First Record', metadata: { type: 'test', active: true } }, { id: 2, name: 'Second Record', metadata: { type: 'prod', active: false } },
    { id: 3, name: 'Third Record', metadata: { type: 'dev', active: true } }]; async function demonstrateAsyncOperations() {
        console.log('Starting async operations demo...'); const connection = new DatabaseConnection('mock://localhost:5432/testdb');
        await connection.connect(); console.log('Connection status:', connection.getStatus());
        const repository = new GenericRepository<DatabaseRecord>(); repository.on('created', record => console.log('Record created:', record.name)); repository.on('updated', record => console.log('Record updated:', record.name));
        for (const record of randomData) { await repository.create(record); } const processor = new AsyncDataProcessor<DatabaseRecord>()
            .addProcessor(async record => {
                await new Promise(resolve => setTimeout(resolve, 50));
                return { ...record, metadata: { ...record.metadata, processed: true } };
            }).onError((error, data) => console.error('Processing error:', error.message, data.name));
        const allRecords = await repository.findAll({ limit: 10 }); for (const record of allRecords) {
            await processor.process(record);
        } const asyncIterable = createAsyncIterator(allRecords, 200);
        console.log('Iterating through records:'); for await (const record of asyncIterable) {
            console.log(`- ${record.name} (${record.metadata.type})`);
        } console.log('Demo completed!');
    } export { DatabaseConnection, AsyncDataProcessor, GenericRepository, ConnectionStatus, createAsyncIterator, processInBatches, demonstrateAsyncOperations };