2018-03-13 05:15:40 +00:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
|
|
|
|
2018-05-26 11:44:32 +00:00
|
|
|
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
2018-03-13 05:15:40 +00:00
|
|
|
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
|
|
|
|
2019-01-31 01:52:18 +00:00
|
|
|
import { Objectmap } from '@pushrocks/lik';
|
2018-03-13 05:15:40 +00:00
|
|
|
|
2019-01-31 01:52:18 +00:00
|
|
|
import { Observable, from } from 'rxjs';
|
|
|
|
import { filter } from 'rxjs/operators';
|
|
|
|
import { rxjs } from '@pushrocks/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-05-26 11:44:32 +00:00
|
|
|
export class UniverseCache {
|
2018-05-28 10:07:25 +00:00
|
|
|
// ========
|
|
|
|
// INSTANCE
|
|
|
|
// ========
|
2018-03-13 05:15:40 +00:00
|
|
|
public standardMessageExpiry: number;
|
|
|
|
public destructionTime: number = 60000;
|
2018-05-26 11:44:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* stores messages for this instance
|
|
|
|
*/
|
2018-05-28 10:07:25 +00:00
|
|
|
public messageMap = new Objectmap<UniverseMessage>();
|
2018-05-26 11:44:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* stores the channels that are available within the universe
|
|
|
|
*/
|
2018-05-28 10:07:25 +00:00
|
|
|
public channelMap = new Objectmap<UniverseChannel>();
|
2018-05-26 11:44:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* allows messages to be processed in a blacklist mode for further analysis
|
|
|
|
*/
|
|
|
|
public blackListChannel = new UniverseChannel(this, 'blacklist', 'nada');
|
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
constructor(standardMessageExpiryArg: number) {
|
|
|
|
this.standardMessageExpiry = standardMessageExpiryArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-11 15:52:01 +00:00
|
|
|
* add a message to the UniverseCache
|
2018-03-13 05:15:40 +00:00
|
|
|
* @param messageArg
|
|
|
|
* @param attachedPayloadArg
|
|
|
|
*/
|
2018-05-26 11:44:32 +00:00
|
|
|
public async addMessage(messageArg: UniverseMessage) {
|
|
|
|
messageArg.setUniverseCache(this);
|
|
|
|
UniverseChannel.authorizeAMessageForAChannel(this, messageArg);
|
2018-05-28 10:07:25 +00:00
|
|
|
this.messageMap.add(messageArg);
|
2018-03-13 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-11 15:52:01 +00:00
|
|
|
* Read a message from the UniverseCache
|
2018-03-13 05:15:40 +00:00
|
|
|
*/
|
2019-04-24 16:20:31 +00:00
|
|
|
public readMessagesYoungerThan(unixTimeArg?: number, channelName?: string): Observable<UniverseMessage> {
|
2019-01-31 01:52:18 +00:00
|
|
|
const messageObservable = from(this.messageMap.getArray()).pipe(
|
|
|
|
filter(messageArg => {
|
2019-04-22 07:58:36 +00:00
|
|
|
return messageArg.smartTimestamp.isYoungerThanMilliSeconds(this.destructionTime);
|
2019-01-31 01:52:18 +00:00
|
|
|
})
|
2018-03-20 07:16:54 +00:00
|
|
|
);
|
2018-03-13 05:15:40 +00:00
|
|
|
return messageObservable;
|
|
|
|
}
|
|
|
|
}
|