-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ts
More file actions
39 lines (33 loc) · 1.76 KB
/
run.ts
File metadata and controls
39 lines (33 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
The true entrypoint of the background script. We use dependency injection in receiving global
objects from index.ts so that we may more easily test how the background script functions.
The general pattern of the background script is to create a global store that, at any point in
time, has the entire application state. Responders, exactly one for each action, return the
updates to be made to the store state. Listeners have various behaviors which take effect after
an action has been processed by the store and may call external APIs such as fetch or
chrome.tabs.create to make API requests, create tabs, or start taking screenshots. The results
of these functions are then dispatched back to the store. Finally, the background script listens
for various events including changes to tabs/windows and dispatches them to the store.
*/
import { AppStore, create } from './store'
import { listeners } from './listeners'
import * as apiHandlers from './api-handlers'
import { monitorChrome } from './monitorChrome'
declare global {
interface Window {
store: AppStore
}
}
export function run(backgroundWindow: Window): void {
// Attach the store to the window so the popup can access it see src/popup/mount.tsx
const store = (backgroundWindow.store = create())
// Add a subscription for each listener, passing dependencies to each
for (const listener of Object.keys(listeners) as ReadonlyArray<Action['type']>) {
store.on(listener, listeners[listener]!)
}
// Monitor various events managed by the chrome API, dispatching relevant information to the store
// see https://developer.chrome.com/docs/extensions/reference/
monitorChrome()
// When a chrome window is created, detect whether the user is already logged in
apiHandlers.detectLogin()
}