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
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public addMessage(messageArg, attachedPayloadArg) {
|
2018-03-20 07:16:54 +00:00
|
|
|
this.messageStore.add(
|
|
|
|
new UniverseMessage(this, messageArg, attachedPayloadArg, this.destructionTime)
|
|
|
|
);
|
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;
|
|
|
|
}
|
|
|
|
}
|