-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New MessageController as the single place for in-memory messages
- Loading branch information
1 parent
274949b
commit 74cb808
Showing
11 changed files
with
169 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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, | ||
}; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.