@ -1,9 +1,17 @@
// You can see MessageController for in memory registered messages.
// You can see MessageController for in memory registered messages.
// Ee register messages to it everytime we send one, so that when an event happens we can find which message it was based on this id.
// Ee register messages to it everytime we send one, so that when an event happens we can find which message it was based on this id.
import { MessageModel } from '../../../js/models/messages' ;
type MessageControllerEntry = {
message : MessageModel ;
timestamp : number ;
} ;
// It's not only data from the db which is stored on the MessageController entries, we could fetch this again. What we cannot fetch from the db and which is stored here is all listeners a particular messages is linked to for instance. We will be able to get rid of this once we don't use backbone models at all
// It's not only data from the db which is stored on the MessageController entries, we could fetch this again. What we cannot fetch from the db and which is stored here is all listeners a particular messages is linked to for instance. We will be able to get rid of this once we don't use backbone models at all
export class MessageController {
export class MessageController {
private static instance : MessageController | null ;
private static instance : MessageController | null ;
private readonly messageLookup : Map < string , any > ;
private readonly messageLookup : Map < string , Mess ageCo ntrollerEntr y> ;
private constructor ( ) {
private constructor ( ) {
this . messageLookup = new Map ( ) ;
this . messageLookup = new Map ( ) ;
@ -19,7 +27,7 @@ export class MessageController {
return MessageController . instance ;
return MessageController . instance ;
}
}
public register ( id : string , message : any ) {
public register ( id : string , message : MessageModel ) {
const existing = this . messageLookup . get ( id ) ;
const existing = this . messageLookup . get ( id ) ;
if ( existing ) {
if ( existing ) {
this . messageLookup . set ( id , {
this . messageLookup . set ( id , {
@ -43,12 +51,10 @@ export class MessageController {
public cleanup() {
public cleanup() {
window . log . warn ( 'Cleaning up getMessageController() oldest messages...' ) ;
window . log . warn ( 'Cleaning up getMessageController() oldest messages...' ) ;
const messages = Object . values ( this . messageLookup ) ;
const now = Date . now ( ) ;
const now = Date . now ( ) ;
// tslint:disable-next-line: one-variable-per-declaration
this . messageLookup . forEach ( messageEntry = > {
for ( let i = 0 , max = messages . length ; i < max ; i += 1 ) {
const { message , timestamp } = messageEntry ;
const { message , timestamp } = messages [ i ] ;
const conversation = message . getConversation ( ) ;
const conversation = message . getConversation ( ) ;
if (
if (
@ -57,7 +63,7 @@ export class MessageController {
) {
) {
this . unregister ( message . id ) ;
this . unregister ( message . id ) ;
}
}
}
} ) ;
}
}
// tslint:disable-next-line: function-name
// tslint:disable-next-line: function-name