move MessageController to typescript
parent
727261b36a
commit
228e4ed662
@ -1,65 +0,0 @@
|
||||
// eslint-disable-next-line func-names
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
window.Whisper = window.Whisper || {};
|
||||
|
||||
const messageLookup = Object.create(null);
|
||||
|
||||
const SECOND = 1000;
|
||||
const MINUTE = SECOND * 60;
|
||||
const FIVE_MINUTES = MINUTE * 5;
|
||||
const HOUR = MINUTE * 60;
|
||||
|
||||
function register(id, message) {
|
||||
const existing = messageLookup[id];
|
||||
if (existing) {
|
||||
messageLookup[id] = {
|
||||
message: existing.message,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
return existing.message;
|
||||
}
|
||||
|
||||
messageLookup[id] = {
|
||||
message,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
function unregister(id) {
|
||||
delete messageLookup[id];
|
||||
}
|
||||
|
||||
function cleanup() {
|
||||
const messages = Object.values(messageLookup);
|
||||
const now = Date.now();
|
||||
|
||||
for (let i = 0, max = messages.length; i < max; i += 1) {
|
||||
const { message, timestamp } = messages[i];
|
||||
const conversation = message.getConversation();
|
||||
|
||||
if (
|
||||
now - timestamp > FIVE_MINUTES &&
|
||||
(!conversation || !conversation.messageCollection.length)
|
||||
) {
|
||||
delete messageLookup[message.id];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _get() {
|
||||
return messageLookup;
|
||||
}
|
||||
|
||||
setInterval(cleanup, HOUR);
|
||||
|
||||
window.MessageController = {
|
||||
register,
|
||||
unregister,
|
||||
cleanup,
|
||||
_get,
|
||||
};
|
||||
})();
|
@ -0,0 +1,67 @@
|
||||
// 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.
|
||||
// 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 {
|
||||
private static instance: MessageController | null;
|
||||
private readonly messageLookup: Map<string, any>;
|
||||
|
||||
private constructor() {
|
||||
this.messageLookup = new Map();
|
||||
// cleanup every hour the cache
|
||||
setInterval(this.cleanup, 3600 * 1000);
|
||||
}
|
||||
|
||||
public static getInstance() {
|
||||
if (MessageController.instance) {
|
||||
return MessageController.instance;
|
||||
}
|
||||
MessageController.instance = new MessageController();
|
||||
return MessageController.instance;
|
||||
}
|
||||
|
||||
public register(id: string, message: any) {
|
||||
const existing = this.messageLookup.get(id);
|
||||
if (existing) {
|
||||
this.messageLookup.set(id, {
|
||||
message: existing.message,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
return existing.message;
|
||||
}
|
||||
|
||||
this.messageLookup.set(id, {
|
||||
message,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public unregister(id: string) {
|
||||
this.messageLookup.delete(id);
|
||||
}
|
||||
|
||||
public cleanup() {
|
||||
window.log.warn('Cleaning up getMessageController() oldest messages...');
|
||||
const messages = Object.values(this.messageLookup);
|
||||
const now = Date.now();
|
||||
|
||||
// tslint:disable-next-line: one-variable-per-declaration
|
||||
for (let i = 0, max = messages.length; i < max; i += 1) {
|
||||
const { message, timestamp } = messages[i];
|
||||
const conversation = message.getConversation();
|
||||
|
||||
if (
|
||||
now - timestamp > 5 * 60 * 1000 &&
|
||||
(!conversation || !conversation.messageCollection.length)
|
||||
) {
|
||||
this.unregister(message.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// tslint:disable-next-line: function-name
|
||||
public get(identifier: string) {
|
||||
return this.messageLookup.get(identifier);
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
import * as Outgoing from './outgoing';
|
||||
import { MessageController } from './MessageController';
|
||||
|
||||
export { Outgoing };
|
||||
export { Outgoing, MessageController };
|
||||
|
Loading…
Reference in New Issue