Skip to content

Commit

Permalink
feat: some changes about class static
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Oct 22, 2023
1 parent d698df1 commit a440959
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
10 changes: 3 additions & 7 deletions packages/kotori/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { none, obj } from '@kotori-bot/tools';
import { langType } from '@kotori-bot/i18n';
import Api from './api';
import Events from './events';
import Mixed from './mixed';
import Core from './core';

interface Istatus {
value: 'online' | 'offline';
Expand Down Expand Up @@ -33,16 +33,12 @@ export interface AdapterConfig extends obj {
export type sendFunc = (action: string, params?: object) => void;
export type AdapterType = new (config: AdapterConfig, identity: string) => Adapter;

export abstract class Adapter<T extends Api = Api> extends Events implements IAdapter {
public static get adapterStack() {
return Object.create(this.AdapterStack);
}

export abstract class Adapter<T extends Api = Api> extends Core implements IAdapter {
public constructor(config: AdapterConfig, identity: string) {
super();
this.config = config;
this.identity = identity;
this.apis = Adapter.apiStack[this.platform] as T[];
this.apis = Core.apiStack[this.platform] as T[];
}

protected readonly apis: T[];
Expand Down
2 changes: 1 addition & 1 deletion packages/kotori/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Core {

public static readonly baseDir = CONST;

public static config: GlobalConfig = CONFIG;
public static configs: GlobalConfig = CONFIG;

public static options: GlobalOptions = { node_env: process.env.node_env === 'dev' ? 'dev' : 'build' };
}
Expand Down
12 changes: 6 additions & 6 deletions packages/kotori/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,6 @@ export class Events extends Core {
group_request: [],
};

protected static readonly emit = <T extends keyof eventType>(eventData: eventType[T]) => {
this.eventStack[eventData.type].forEach(callback => {
(callback as eventCallback<T>)(eventData);
});
};

protected static readonly addListener: eventListenerFunc = (type, callback) => {
const eventStack = this.eventStack[type] as unknown[];
if (eventStack.filter(Element => Element === callback).length > 0) return false;
Expand All @@ -211,6 +205,12 @@ export class Events extends Core {
return true;
};

public static readonly emit = <T extends keyof eventType>(eventData: eventType[T]) => {
this.eventStack[eventData.type].forEach(callback => {
(callback as eventCallback<T>)(eventData);
});
};

public static readonly on = this.addListener;

public static readonly once = <T extends keyof eventType>(type: T, callback: eventCallback<T>) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kotori/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Content = keyof typeof Mixed;

export namespace Kotori {
/* Core Class */
export const { config, baseDir } = Mixed;
export const { configs, baseDir } = Mixed;

/* Events Class */
export const { on, once, off, offAll } = Mixed;
Expand Down
2 changes: 1 addition & 1 deletion packages/kotori/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class Message extends Modules {
};

public static readonly notify = (message: Msg) => {
const mainAdapterIdentity = Object.keys(this.config.adapter)[0];
const mainAdapterIdentity = Object.keys(this.configs.adapter)[0];
for (const apis of Object.values(this.apiStack)) {
for (const api of apis) {
if (api.adapter.identity !== mainAdapterIdentity) continue;
Expand Down
7 changes: 6 additions & 1 deletion packages/kotori/src/mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ const http = {
head: requestType('head'),
};

export const Mixed = Object.assign(Message, new Locale('en_US'), { logger: Logger }, { http });
export class Content extends Message {
public static http = http;

public static logger = Logger;
}

/* export declare namespace JSX {
interface IntrinsicElements {
render: any;
}
} */
export const Mixed = Object.assign(Content, new Locale('en_US'));

export default Mixed;
6 changes: 3 additions & 3 deletions packages/loader/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Blog: https://hotaru.icu
* @Date: 2023-06-24 15:12:55
* @LastEditors: Hotaru biyuehuya@gmail.com
* @LastEditTime: 2023-10-06 14:24:25
* @LastEditTime: 2023-10-22 17:00:42
*/
import {
Adapter,
Expand Down Expand Up @@ -85,8 +85,8 @@ class Main extends Mixed {

private readonly loadAllAdapter = () => {
none(this);
for (const botName of Object.keys(Mixed.config.adapter)) {
const botConfig = Mixed.config.adapter[botName];
for (const botName of Object.keys(Mixed.configs.adapter)) {
const botConfig = Mixed.configs.adapter[botName];
if (botConfig.extend in Mixed.AdapterStack) {
const bot = new Mixed.AdapterStack[botConfig.extend](botConfig, botName);
if (!(botConfig.extend in Adapter.apiStack)) Adapter.apiStack[botConfig.extend] = [];
Expand Down

0 comments on commit a440959

Please sign in to comment.