2018-03-13 06:15:40 +01:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
|
|
|
|
2018-05-26 13:44:32 +02:00
|
|
|
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
2018-03-13 06:15:40 +01:00
|
|
|
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
|
|
|
|
2018-03-20 08:16:54 +01:00
|
|
|
import { Objectmap } from 'lik';
|
2018-03-13 06:15:40 +01:00
|
|
|
|
|
|
|
import { Observable } from 'rxjs';
|
2018-03-20 08:16:54 +01:00
|
|
|
import { rxjs } from 'smartrx';
|
2018-03-13 06:15:40 +01:00
|
|
|
|
2018-04-29 16:38:23 +02:00
|
|
|
/**
|
|
|
|
* universe store handles the creation, storage and retrieval of messages.
|
|
|
|
*/
|
2018-05-26 13:44:32 +02:00
|
|
|
export class UniverseCache {
|
2018-05-28 12:07:25 +02:00
|
|
|
// ========
|
|
|
|
// INSTANCE
|
|
|
|
// ========
|
2018-03-13 06:15:40 +01:00
|
|
|
public standardMessageExpiry: number;
|
|
|
|
public destructionTime: number = 60000;
|
2018-05-26 13:44:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* stores messages for this instance
|
|
|
|
*/
|
2018-05-28 12:07:25 +02:00
|
|
|
public messageMap = new Objectmap<UniverseMessage>();
|
2018-05-26 13:44:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* stores the channels that are available within the universe
|
|
|
|
*/
|
2018-05-28 12:07:25 +02:00
|
|
|
public channelMap = new Objectmap<UniverseChannel>();
|
2018-05-26 13:44:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* allows messages to be processed in a blacklist mode for further analysis
|
|
|
|
*/
|
|
|
|
public blackListChannel = new UniverseChannel(this, 'blacklist', 'nada');
|
|
|
|
|
2018-03-13 06:15:40 +01:00
|
|
|
constructor(standardMessageExpiryArg: number) {
|
|
|
|
this.standardMessageExpiry = standardMessageExpiryArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add a message to the UniverseStore
|
|
|
|
* @param messageArg
|
|
|
|
* @param attachedPayloadArg
|
|
|
|
*/
|
2018-05-26 13:44:32 +02:00
|
|
|
public async addMessage(messageArg: UniverseMessage) {
|
|
|
|
messageArg.setUniverseCache(this);
|
|
|
|
UniverseChannel.authorizeAMessageForAChannel(this, messageArg);
|
2018-05-28 12:07:25 +02:00
|
|
|
this.messageMap.add(messageArg);
|
2018-03-13 06:15:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a message from the UniverseStore
|
|
|
|
*/
|
|
|
|
public readMessagesYoungerThan(unixTimeArg?: number): Observable<UniverseMessage> {
|
2018-05-28 12:07:25 +02:00
|
|
|
const messageObservable = rxjs.Observable.from(this.messageMap.getArray()).filter(
|
2018-03-20 08:16:54 +01:00
|
|
|
messageArg => {
|
2018-03-13 06:15:40 +01:00
|
|
|
return messageArg.timestamp.isYoungerThanMilliSeconds(this.destructionTime);
|
2018-03-20 08:16:54 +01:00
|
|
|
}
|
|
|
|
);
|
2018-03-13 06:15:40 +01:00
|
|
|
return messageObservable;
|
|
|
|
}
|
|
|
|
}
|