-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#7784: skip waiting for managed storage after initial wait #7790
Changes from 7 commits
c48d40b
8dcf1dc
f2d1f7c
ea5e847
7196816
d23a4a0
dc1a0e0
f9dec79
185ca73
067213f
1884d36
671709f
009cdbf
5453e13
700b1fc
517a06e
9787f65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,19 +29,26 @@ import { expectContext } from "@/utils/expectContext"; | |
import { | ||
readManagedStorage, | ||
isInitialized as isManagedStorageInitialized, | ||
resetInitializationTimestamp as resetManagedStorageInitializationState, | ||
initManagedStorage, | ||
} from "@/store/enterprise/managedStorage"; | ||
import { Events } from "@/telemetry/events"; | ||
|
||
import { DEFAULT_SERVICE_URL, UNINSTALL_URL } from "@/urlConstants"; | ||
|
||
import { CONTROL_ROOM_TOKEN_INTEGRATION_ID } from "@/integrations/constants"; | ||
import { getExtensionConsoleUrl } from "@/utils/extensionUtils"; | ||
import { SessionValue } from "@/mv3/SessionStorage"; | ||
import { allSettled } from "@/utils/promiseUtils"; | ||
|
||
/** | ||
* The latest version of PixieBrix available in the Chrome Web Store, or null if the version hasn't been fetched. | ||
*/ | ||
let _availableVersion: string | null = null; | ||
|
||
const initialized = new SessionValue<boolean>( | ||
"installerInitialized", | ||
import.meta.url, | ||
); | ||
|
||
/** | ||
* Returns true if this appears to be a Chrome Web Store install and/or the user has an app URL where they're | ||
* authenticated so the extension can be linked. | ||
|
@@ -213,7 +220,7 @@ export async function requirePartnerAuth(): Promise<void> { | |
} | ||
|
||
// Exported for testing | ||
export async function handleInstall({ | ||
export async function showInstallPage({ | ||
reason, | ||
previousVersion, | ||
}: Runtime.OnInstalledDetailsType): Promise<void> { | ||
|
@@ -291,7 +298,9 @@ export async function handleInstall({ | |
} | ||
} | ||
|
||
function onUpdateAvailable({ version }: Runtime.OnUpdateAvailableDetailsType) { | ||
function setAvailableVersion({ | ||
version, | ||
}: Runtime.OnUpdateAvailableDetailsType): void { | ||
_availableVersion = version; | ||
} | ||
|
||
|
@@ -320,10 +329,45 @@ async function setUninstallURL(): Promise<void> { | |
await browser.runtime.setUninstallURL(url.href); | ||
} | ||
|
||
function initInstaller() { | ||
browser.runtime.onUpdateAvailable.addListener(onUpdateAvailable); | ||
browser.runtime.onInstalled.addListener(handleInstall); | ||
browser.runtime.onStartup.addListener(initTelemetry); | ||
/** | ||
* Run initialization that should occur once per extension session. | ||
*/ | ||
async function initSessionOnce(): Promise<void> { | ||
// Using our own session value vs. webext-events because onExtensionStart has a 100ms delay | ||
// https://github.com/fregante/webext-events/blob/main/source/on-extension-start.ts#L56 | ||
|
||
// Can't use onStartup because it doesn't mean "on extension start", but "on browser profile start". | ||
// Events registered onStartup won't run when the user first installs or when they re-enable the extension. | ||
// See: https://developer.chrome.com/docs/extensions/reference/api/runtime#event-onStartup | ||
|
||
if (await initialized.get()) { | ||
// Session already initialized | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm generally against inlining logic that is reused, e.g. logic like I think this call could be: const initSessionOnce = pMemoize(async () => {
//
}, {
// initSessionOnce has no arguments, so only one value is stored
cache: new SessionMap('managedstorageinit', import.meta.url)
}) or const oncePerSession = (key, url, fn) => pMemoize(fn, {
cache: new SessionMap(key, url)
})
const initSessionOnce = oncePerSession('managedstorageinit', import.meta.url, async () => {
//
}); |
||
|
||
console.debug("Running extension session initialization"); | ||
|
||
const storagePromise = (async () => { | ||
await resetManagedStorageInitializationState(); | ||
await initManagedStorage(); | ||
})(); | ||
|
||
const telemetryPromise = (async () => { | ||
if (await isLinked()) { | ||
// Init telemetry is a no-op if not linked. So calling without being linked just delays the throttle | ||
await initTelemetry(); | ||
} | ||
})(); | ||
|
||
await allSettled([storagePromise, telemetryPromise], { catch: "ignore" }); | ||
} | ||
|
||
function initInstaller(): void { | ||
void initSessionOnce(); | ||
|
||
browser.runtime.onInstalled.addListener(showInstallPage); | ||
browser.runtime.onUpdateAvailable.addListener(setAvailableVersion); | ||
|
||
dntConfig.onChanged(() => { | ||
void setUninstallURL(); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
|
||
import { type JsonObject } from "type-fest"; | ||
import { compact, debounce, throttle, uniq } from "lodash"; | ||
import { isLinked } from "@/auth/authStorage"; | ||
import { getModComponentState } from "@/store/extensionsStorage"; | ||
import { | ||
getLinkedApiClient, | ||
|
@@ -305,8 +304,9 @@ async function collectUserSummary(): Promise<UserSummary> { | |
} | ||
|
||
async function init(): Promise<void> { | ||
if ((await isLinked()) && (await allowsTrack())) { | ||
const client = await getLinkedApiClient(); | ||
const client = await maybeGetLinkedApiClient(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix race condition |
||
|
||
if (client && (await allowsTrack())) { | ||
await client.post("/api/identify/", { | ||
uid: await getUUID(), | ||
data: await collectUserSummary(), | ||
|
@@ -321,7 +321,7 @@ export const initTelemetry = throttle(init, 30 * 60 * 1000, { | |
}); | ||
|
||
/** | ||
* @deprecated Only allowed in @/background files. Otherwise use: `import reportEvent from "@/telemetry/reportEvent"` | ||
* @deprecated Only allowed in @/background files. Otherwise, use: `import reportEvent from "@/telemetry/reportEvent"` | ||
*/ | ||
export async function recordEvent({ | ||
event, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accidentally left this in the other PR