Skip to content

Commit ab972dd

Browse files
authored
PosthogAnalytics unwatch settings on logout (matrix-org#11207)
* PosthogAnalytics unwatch settings on logout * Tidy
1 parent 411a63f commit ab972dd

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/Lifecycle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,11 +748,12 @@ let _isLoggingOut = false;
748748
* Logs the current session out and transitions to the logged-out state
749749
*/
750750
export function logout(): void {
751-
if (!MatrixClientPeg.get()) return;
751+
const client = MatrixClientPeg.get();
752+
if (!client) return;
752753

753754
PosthogAnalytics.instance.logout();
754755

755-
if (MatrixClientPeg.get()!.isGuest()) {
756+
if (client.isGuest()) {
756757
// logout doesn't work for guest sessions
757758
// Also we sometimes want to re-log in a guest session if we abort the login.
758759
// defer until next tick because it calls a synchronous dispatch, and we are likely here from a dispatch.
@@ -761,7 +762,6 @@ export function logout(): void {
761762
}
762763

763764
_isLoggingOut = true;
764-
const client = MatrixClientPeg.get()!;
765765
PlatformPeg.get()?.destroyPickleKey(client.getSafeUserId(), client.getDeviceId() ?? "");
766766
client.logout(true).then(onLoggedOut, (err) => {
767767
// Just throwing an error here is going to be very unhelpful

src/PosthogAnalytics.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export class PosthogAnalytics {
138138
private propertiesForNextEvent: Partial<Record<"$set" | "$set_once", UserProperties>> = {};
139139
private userPropertyCache: UserProperties = {};
140140
private authenticationType: Signup["authenticationType"] = "Other";
141+
private watchSettingRef?: string;
141142

142143
public static get instance(): PosthogAnalytics {
143144
if (!this._instance) {
@@ -337,6 +338,7 @@ export class PosthogAnalytics {
337338
if (this.enabled) {
338339
this.posthog.reset();
339340
}
341+
if (this.watchSettingRef) SettingsStore.unwatchSetting(this.watchSettingRef);
340342
this.setAnonymity(Anonymity.Disabled);
341343
}
342344

@@ -400,7 +402,7 @@ export class PosthogAnalytics {
400402
// * When the user changes their preferences on this device
401403
// Note that for new accounts, pseudonymousAnalyticsOptIn won't be set, so updateAnonymityFromSettings
402404
// won't be called (i.e. this.anonymity will be left as the default, until the setting changes)
403-
SettingsStore.watchSetting(
405+
this.watchSettingRef = SettingsStore.watchSetting(
404406
"pseudonymousAnalyticsOptIn",
405407
null,
406408
(originalSettingName, changedInRoomId, atLevel, newValueAtLevel, newValue) => {

0 commit comments

Comments
 (0)