Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rename environment/_helpers.js -> environment/_messaging.js * environment: move duplicated background listeners to environment/_common.js * Cache subclasses Map
- Loading branch information
1 parent
d5022c1
commit 6d8fadc
Showing
11 changed files
with
71 additions
and
148 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
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,38 @@ | ||
import Cache from '../utils/Cache'; | ||
|
||
export function addCommonBackgroundListeners(addListener) { | ||
const session = new Map(); | ||
|
||
addListener('session', ([operation, key, value]) => { | ||
switch (operation) { | ||
case 'get': | ||
return session.get(key); | ||
case 'set': | ||
session.set(key, value); | ||
break; | ||
case 'delete': | ||
return session.delete(key); | ||
case 'clear': | ||
return session.clear(); | ||
default: | ||
throw new Error(`Invalid session operation: ${operation}`); | ||
} | ||
}); | ||
|
||
const cache = new Cache(); | ||
|
||
addListener('XHRCache', ([operation, key, value]) => { | ||
switch (operation) { | ||
case 'set': | ||
return cache.set(key, value); | ||
case 'check': | ||
return cache.get(key, value); | ||
case 'delete': | ||
return cache.delete(key); | ||
case 'clear': | ||
return cache.clear(); | ||
default: | ||
throw new Error(`Invalid XHRCache operation: ${operation}`); | ||
} | ||
}); | ||
} |
File renamed without changes.
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 |
---|---|---|
@@ -1,43 +1,27 @@ | ||
export default class Cache { | ||
entries = new Map(); | ||
|
||
constructor(capacity = 250) { | ||
export default class Cache extends Map { | ||
constructor(capacity = 500) { | ||
super(); | ||
this.capacity = capacity; | ||
} | ||
|
||
check(key, maxAge = Infinity) { | ||
const entry = this.entries.get(key); | ||
get(key, maxAge = Infinity) { | ||
const entry = super.get(key); | ||
const now = Date.now(); | ||
if (entry && (now - entry.timestamp < maxAge)) { | ||
entry.timestamp = now; | ||
return entry.data; | ||
return entry.value; | ||
} | ||
} | ||
|
||
set(key, value) { | ||
this.entries.set(key, { | ||
data: value, | ||
timestamp: Date.now(), | ||
}); | ||
super.set(key, { value, timestamp: Date.now() }); | ||
|
||
if (this.entries.size > this.capacity) { | ||
this._prune(); | ||
if (this.size > this.capacity) { | ||
// evict least-recently used | ||
Array.from(this.entries()) | ||
.sort(([, a], [, b]) => b.timestamp - a.timestamp) | ||
.slice((this.capacity / 2) | 0) | ||
.forEach(([key]) => this.delete(key)); | ||
} | ||
} | ||
|
||
delete(key) { | ||
this.entries.delete(key); | ||
} | ||
|
||
clear() { | ||
this.entries.clear(); | ||
} | ||
|
||
_prune() { | ||
// evict least-recently used | ||
Array.from(this.entries.entries()) | ||
.sort(([, a], [, b]) => b.timestamp - a.timestamp) | ||
.slice((this.capacity / 2) | 0) | ||
.forEach(([key]) => this.entries.delete(key)); | ||
} | ||
} |
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