smartuniverse/ts/smartuniverse.classes.universecache.ts

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-03-13 05:15:40 +00:00
import * as plugins from './smartuniverse.plugins';
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.
*/
export class UniverseCache {
// ========
// INSTANCE
// ========
2018-03-13 05:15:40 +00:00
public standardMessageExpiry: number;
public destructionTime: number = 60000;
/**
* stores messages for this instance
*/
public messageMap = new Objectmap<UniverseMessage>();
/**
* stores the channels that are available within the universe
*/
public channelMap = new Objectmap<UniverseChannel>();
/**
* 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
*/
public async addMessage(messageArg: UniverseMessage) {
messageArg.setUniverseCache(this);
UniverseChannel.authorizeAMessageForAChannel(this, messageArg);
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-06-06 21:23:37 +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;
}
}