-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(webextension): cache worker x script
improve worker cache
- Loading branch information
Showing
7 changed files
with
83 additions
and
48 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
36 changes: 36 additions & 0 deletions
36
packages/webextension/app/scripts/background/scriptWorkerSet.ts
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,36 @@ | ||
import { TextlintWorker } from "./textlint"; | ||
import { keyOfScript, Script } from "./database"; | ||
|
||
// worker: Set<url> | ||
const _workerRunningUrlMap = new Map<TextlintWorker, Set<string>>(); | ||
const _scriptRunningWorkerMap = new Map<string, TextlintWorker>(); | ||
export const scriptWorkerSet = { | ||
get(script: Script) { | ||
return _scriptRunningWorkerMap.get(keyOfScript(script)); | ||
}, | ||
has(script: Script) { | ||
return _scriptRunningWorkerMap.has(keyOfScript(script)); | ||
}, | ||
add({ script, worker, url }: { script: Script; worker: TextlintWorker; url: string }) { | ||
const set = _workerRunningUrlMap.get(worker) ?? new Set<string>(); | ||
set.add(url); | ||
_workerRunningUrlMap.set(worker, set); | ||
return _scriptRunningWorkerMap.set(keyOfScript(script), worker); | ||
}, | ||
delete({ script, url }: { script: Script; url: string }) { | ||
for (const [worker, urlSet] of _workerRunningUrlMap.entries()) { | ||
if (urlSet.has(url)) { | ||
urlSet.delete(url); | ||
} | ||
if (urlSet.size === 0) { | ||
worker.dispose(); | ||
_workerRunningUrlMap.delete(worker); | ||
} | ||
} | ||
return _scriptRunningWorkerMap.delete(keyOfScript(script)); | ||
}, | ||
dump() { | ||
console.log("Running Scripts: ", _scriptRunningWorkerMap.keys()); | ||
console.log("Running Workers x URLs: ", _workerRunningUrlMap.entries()); | ||
} | ||
}; |
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