2018-03-13 05:15:40 +00:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
|
|
|
|
|
|
|
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
|
|
|
|
2018-03-20 07:16:54 +00:00
|
|
|
import { Objectmap } from 'lik';
|
2018-03-13 05:15:40 +00:00
|
|
|
|
|
|
|
import { Observable } from 'rxjs';
|
2018-03-20 07:16:54 +00:00
|
|
|
import { rxjs } from 'smartrx';
|
2018-03-13 05:15:40 +00:00
|
|
|
|
2018-04-29 14:38:23 +00:00
|
|
|
/**
|
|
|
|
* universe store handles the creation, storage and retrieval of messages.
|
|
|
|
*/
|
2018-03-13 05:15:40 +00:00
|
|
|
export class UniverseStore {
|
|
|
|
public standardMessageExpiry: number;
|
|
|
|
public destructionTime: number = 60000;
|
|
|
|
public messageStore = new Objectmap<UniverseMessage>();
|
|
|
|
private lastId: number = 0; // stores the last id
|
|
|
|
|
|
|
|
constructor(standardMessageExpiryArg: number) {
|
|
|
|
this.standardMessageExpiry = standardMessageExpiryArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add a message to the UniverseStore
|
|
|
|
* @param messageArg
|
|
|
|
* @param attachedPayloadArg
|
|
|
|
*/
|
2018-05-19 22:41:59 +00:00
|
|
|
public addMessage(messageArg: UniverseMessage) {
|
|
|
|
this.messageStore.add(messageArg);
|
2018-03-13 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a message from the UniverseStore
|
|
|
|
*/
|
|
|
|
public readMessagesYoungerThan(unixTimeArg?: number): Observable<UniverseMessage> {
|
2018-03-20 07:16:54 +00:00
|
|
|
const messageObservable = rxjs.Observable.from(this.messageStore.getArray()).filter(
|
|
|
|
messageArg => {
|
2018-03-13 05:15:40 +00:00
|
|
|
return messageArg.timestamp.isYoungerThanMilliSeconds(this.destructionTime);
|
2018-03-20 07:16:54 +00:00
|
|
|
}
|
|
|
|
);
|
2018-03-13 05:15:40 +00:00
|
|
|
return messageObservable;
|
|
|
|
}
|
|
|
|
}
|