core: upgrade to context tracing

This commit is contained in:
undefined committed 2024-10-05 20:38:20 +08:00
1 parent e79e978b15
commit 28b3abf441
9 files changed
+42 -38

No files matched your search

+4 -2
View File
@@ -50,8 +50,10 @@ export abstract class Service<T = any, C extends Context = Context> extends cord
const T = <F extends (...args: any[]) => any>(origFunc: F, disposeFunc?) =>
function method(this: cordis.Service, ...args: Parameters<F>) {
const res = origFunc(...args);
this[Context.current]?.on('dispose', () => (disposeFunc ? disposeFunc(res) : res()));
this.ctx.effect(() => {
const res = origFunc(...args);
return () => (disposeFunc ? disposeFunc(res) : res());
});
};
export class ApiMixin extends Service {
+4 -7
View File
@@ -179,7 +179,6 @@ export class HomeHandler extends Handler {
}
}
let geoip: Context['geoip'] = null;
class HomeSecurityHandler extends Handler {
@requireSudo
async get() {
@@ -189,7 +188,7 @@ class HomeSecurityHandler extends Handler {
session.isCurrent = session._id === this.session._id;
session._id = md5(session._id);
session.updateUa = useragent.parse(session.updateUa || session.createUa || '');
session.updateGeoip = geoip?.lookup?.(
session.updateGeoip = this.ctx.geoip?.lookup?.(
session.updateIp || session.createIp,
this.translate('geoip_locale'),
);
@@ -202,7 +201,7 @@ class HomeSecurityHandler extends Handler {
'credentialID', 'name', 'credentialType', 'credentialDeviceType',
'authenticatorAttachment', 'regat', 'fmt',
])),
geoipProvider: geoip?.provider,
geoipProvider: this.ctx?.geoip?.provider,
icon: (str = '') => str.split(' ')[0].toLowerCase(),
};
}
@@ -591,10 +590,8 @@ class HomeMessagesConnectionHandler extends ConnectionHandler {
}
}
export async function apply(ctx: Context) {
ctx.inject(['geoip'], (g) => {
geoip = g.geoip;
});
export const inject = { geoip: { required: false } };
export function apply(ctx: Context) {
ctx.Route('homepage', '/', HomeHandler);
ctx.Route('home_security', '/home/security', HomeSecurityHandler, PRIV.PRIV_USER_PROFILE);
ctx.Route('user_changemail_with_code', '/home/changeMail/:code', UserChangemailWithCodeHandler, PRIV.PRIV_USER_PROFILE);
+2 -1
View File
@@ -2,6 +2,7 @@
import { statSync } from 'fs';
import { pick } from 'lodash';
import { lookup } from 'mime-types';
import { Context } from '../context';
import {
AccessDeniedError, FileExistsError, FileLimitExceededError, FileUploadError, NotFoundError,
ValidationError,
@@ -132,7 +133,7 @@ export class SwitchAccountHandler extends Handler {
}
}
export async function apply(ctx) {
export async function apply(ctx: Context) {
ctx.Route('switch_language', '/language/:lang', SwitchLanguageHandler);
ctx.Route('home_files', '/file', FilesHandler);
ctx.Route('fs_download', '/file/:uid/:filename', FSDownloadHandler);
+6 -4
View File
@@ -19,10 +19,12 @@ class I18nService extends Service {
}
load(lang: string, content: Record<string, string>) {
translations[lang] ||= [];
translations[lang].unshift(content);
this[Context.current]?.on('dispose', () => {
translations[lang] = translations[lang].filter((i) => i !== content);
this.ctx.effect(() => {
translations[lang] ||= [];
translations[lang].unshift(content);
return () => {
translations[lang] = translations[lang].filter((i) => i !== content);
};
});
}
+5 -3
View File
@@ -301,8 +301,10 @@ declare module 'cordis' {
const T = <F extends (...args: any[]) => any>(origFunc: F, disposeFunc?) =>
function method(this: cordis.Service, ...args: Parameters<F>) {
const res = origFunc(...args);
this[Context.current]?.on('dispose', () => (disposeFunc ? disposeFunc(res) : res()));
this.ctx.effect(() => {
const res = origFunc(...args);
return () => (disposeFunc ? disposeFunc(res) : res());
});
};
export class SettingService extends Service {
@@ -316,7 +318,7 @@ export class SettingService extends Service {
}
get(key: string) {
return (this[Context.current] ? this[Context.current].domain?.config?.[key.replace(/\./g, '$')] : null) ?? global.Hydro.model.system.get(key);
return (this.ctx ? this.ctx.domain?.config?.[key.replace(/\./g, '$')] : null) ?? global.Hydro.model.system.get(key);
}
}
+5 -3
View File
@@ -13,9 +13,11 @@ export class CheckService extends Service {
checkers: Dictionary<CheckItem> = {};
c = {};
addChecker(type: string, checkFunc: CheckItem) {
this.checkers[`check${type}`] = checkFunc;
this[Context.current]?.on('dispose', () => {
delete this.checkers[`check${type}`];
this.ctx.effect(() => {
this.checkers[`check${type}`] = checkFunc;
return () => {
delete this.checkers[`check${type}`];
};
});
}
+5 -3
View File
@@ -72,9 +72,11 @@ export class WorkerService extends Service {
}
public addHandler(type: string, handler: Function) {
this.handlers[type] = handler;
this[Context.current]?.on('dispose', () => {
delete this.handlers[type];
this.ctx.effect(() => {
this.handlers[type] = handler;
return () => {
delete this.handlers[type];
};
});
}