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';
|
|
|
|
|
2020-09-24 18:13:48 +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';
|
2019-08-12 12:59:37 +00:00
|
|
|
import { UniverseConnection } from './smartuniverse.classes.universeconnection';
|
2019-08-13 13:48:20 +00:00
|
|
|
import { Universe } from './smartuniverse.classes.universe';
|
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;
|
2019-09-17 13:40:54 +00:00
|
|
|
public destructionTime: number = 10000;
|
2018-05-26 11:44:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* stores messages for this instance
|
|
|
|
*/
|
2020-09-24 18:13:48 +00:00
|
|
|
public messageMap = new ObjectMap<UniverseMessage<any>>();
|
2018-05-26 11:44:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* stores the channels that are available within the universe
|
|
|
|
*/
|
2020-09-24 18:13:48 +00:00
|
|
|
public channelMap = new ObjectMap<UniverseChannel>();
|
2018-05-26 11:44:32 +00:00
|
|
|
|
2019-08-13 13:48:20 +00:00
|
|
|
/**
|
|
|
|
* stores all connections
|
|
|
|
*/
|
2020-09-24 18:13:48 +00:00
|
|
|
public connectionMap = new plugins.lik.ObjectMap<UniverseConnection>();
|
2019-08-12 12:59:37 +00:00
|
|
|
|
2018-05-26 11:44:32 +00:00
|
|
|
/**
|
|
|
|
* allows messages to be processed in a blacklist mode for further analysis
|
|
|
|
*/
|
2019-08-13 13:48:20 +00:00
|
|
|
public blackListChannel: UniverseChannel;
|
|
|
|
|
|
|
|
public universeRef: Universe;
|
2018-05-26 11:44:32 +00:00
|
|
|
|
2019-08-13 13:48:20 +00:00
|
|
|
constructor(universeArg: Universe, standardMessageExpiryArg: number) {
|
|
|
|
this.universeRef = universeArg;
|
2018-03-13 05:15:40 +00:00
|
|
|
this.standardMessageExpiry = standardMessageExpiryArg;
|
2019-08-13 13:48:20 +00:00
|
|
|
this.blackListChannel = new UniverseChannel(this.universeRef, 'blacklist', 'nada');
|
2018-03-13 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
2019-09-10 21:55:20 +00:00
|
|
|
public async addMessage(messageArg: UniverseMessage<any>) {
|
2018-05-26 11:44:32 +00:00
|
|
|
messageArg.setUniverseCache(this);
|
|
|
|
UniverseChannel.authorizeAMessageForAChannel(this, messageArg);
|
2018-05-28 10:07:25 +00:00
|
|
|
this.messageMap.add(messageArg);
|
2020-09-24 18:17:52 +00:00
|
|
|
messageArg.universeChannelList.forEach((universeChannel) => {
|
2019-09-10 07:56:32 +00:00
|
|
|
universeChannel.push(messageArg);
|
2019-08-13 13:48:20 +00:00
|
|
|
});
|
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-06-06 21:23:37 +00:00
|
|
|
public readMessagesYoungerThan(
|
|
|
|
unixTimeArg?: number,
|
|
|
|
channelName?: string
|
2019-09-10 21:55:20 +00:00
|
|
|
): Observable<UniverseMessage<any>> {
|
2019-01-31 01:52:18 +00:00
|
|
|
const messageObservable = from(this.messageMap.getArray()).pipe(
|
2020-09-24 18:17:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|