All files / src event-stream.ts

88.46% Statements 23/26
75% Branches 3/4
87.5% Functions 7/8
95.24% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          4x         4x         23x 23x             4x 2x           4x 4x                 4x 2x                 4x                   4x 22x 40x 4x       4x         22x     4x 24x   4x  
import { EventStore, EventStream } from './event-store';
import { Event } from './model/event';
import { Stream } from './model/stream';
import { PersistenceProvider } from './provider/provider';
import { Publisher } from './publisher/publisher';
import { eventMerge } from './model/event-util';
 
/**
 * An Event Stream
 */
export class EventStreamImpl implements EventStream {
    private stream: Stream;
    private eventStore: EventStore;
 
    public constructor(eventStore: EventStore, stream: Stream) {
        this.eventStore = eventStore;
        this.stream = stream;
    }
 
    /**
     * The event stream identifier
     * The event stream
     */
    public get streamId(): string {
        return this.stream.id;
    }
 
    /**
     * The parent aggregation for this event stream
     */
    public get aggregation(): string {
        return this.stream.aggregation;
    }
 
    /**
     * Rertieve a list containing all the events in the stream in order.
     * @param offset The start position in the stream list
     * @param limit The desired quantity events
     * @return All the events
     */
    public getEvents(offset?: number, limit?: number): Promise<Array<Event>> {
        return this.getProvider().getEvents(this.stream, offset, limit);
    }
 
    /**
     * Rertieve only one object containing all the events's data in the stream in order.
     * @param offset The start position in the stream list
     * @param limit The desired quantity events
     * @return All the events
     */
    public async loadFromHistory(offset?: number, limit?: number): Promise<Event> {
        return eventMerge(await this.getEvents(offset, limit));
    }
 
    /**
     * Add a new event to the end of the event stream.
     * @param data The event data
     * @param type The Event type
     * @return The event, updated with informations like its sequence order and commitTimestamp
     */
    public async addEvent(data: any, type?: string) {
        let addedEvent: Event = await this.getProvider().addEvent(this.stream, data, type);
        if (this.eventStore.publisher) {
            const eventId = await (this.eventStore.publisher as Publisher).publish({
                event: addedEvent,
                stream: this.stream
            });
            addedEvent = {
                ...addedEvent,
                id: eventId,
            };
        }
        return addedEvent;
    }
 
    private getProvider(): PersistenceProvider {
        return this.eventStore.provider;
    }
}