chore: improve event flow (#14266)

This commit is contained in:
DarkSky
2026-01-16 16:07:27 +08:00
committed by GitHub
parent d4581b839a
commit 924d58603f
43 changed files with 2306 additions and 567 deletions
@@ -187,3 +187,32 @@ export const BUILD_IN_SERVERS: (ServerMetadata & { config: ServerConfig })[] =
},
]
: [];
export type TelemetryChannel =
| 'stable'
| 'beta'
| 'internal'
| 'canary'
| 'local';
const OFFICIAL_TELEMETRY_ENDPOINTS: Record<TelemetryChannel, string> = {
stable: 'https://app.affine.pro',
beta: 'https://insider.affine.pro',
internal: 'https://insider.affine.pro',
canary: 'https://affine.fail',
local: 'http://localhost:8080',
};
export function getOfficialTelemetryEndpoint(
channel = BUILD_CONFIG.appBuildType
): string {
if (BUILD_CONFIG.debug) {
return BUILD_CONFIG.isNative
? OFFICIAL_TELEMETRY_ENDPOINTS.local
: location.origin;
} else if (['beta', 'internal', 'canary', 'stable'].includes(channel)) {
return OFFICIAL_TELEMETRY_ENDPOINTS[channel];
}
return OFFICIAL_TELEMETRY_ENDPOINTS.stable;
}
@@ -1,12 +1,16 @@
import { shallowEqual } from '@affine/component';
import { DebugLogger } from '@affine/debug';
import { ServerDeploymentType } from '@affine/graphql';
import { tracker } from '@affine/track';
import { flushTelemetry, setTelemetryContext, tracker } from '@affine/track';
import { LiveData, OnEvent, Service } from '@toeverything/infra';
import type { AuthAccountInfo, Server, ServersService } from '../../cloud';
import { getOfficialTelemetryEndpoint } from '../../cloud/constant';
import type { GlobalContextService } from '../../global-context';
import { ApplicationStarted } from '../../lifecycle';
const logger = new DebugLogger('telemetry-service');
@OnEvent(ApplicationStarted, e => e.onApplicationStart)
export class TelemetryService extends Service {
private readonly disposableFns: (() => void)[] = [];
@@ -46,6 +50,21 @@ export class TelemetryService extends Service {
let prevSelfHosted: boolean | undefined = undefined;
const unsubscribe = this.currentAccount$.subscribe(
({ account, selfHosted }) => {
const channel =
BUILD_CONFIG.appBuildType === 'beta' ||
BUILD_CONFIG.appBuildType === 'internal' ||
BUILD_CONFIG.appBuildType === 'canary' ||
BUILD_CONFIG.appBuildType === 'stable'
? BUILD_CONFIG.appBuildType
: 'stable';
setTelemetryContext({
isAuthed: !!account,
isSelfHosted: !!selfHosted,
channel,
officialEndpoint: getOfficialTelemetryEndpoint(channel),
});
if (prevAccount) {
tracker.reset();
}
@@ -64,6 +83,13 @@ export class TelemetryService extends Service {
$name: account.label,
$avatar: account.avatar,
});
void flushTelemetry().catch(error => {
logger.error('failed to flush telemetry after login', error);
});
} else if (prevAccount) {
void flushTelemetry().catch(error => {
logger.error('failed to flush telemetry after logout', error);
});
}
}
);