Skip to content

Commit

Permalink
feat: events class move to @kotori-bot/tools
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Jan 30, 2024
1 parent b2bf016 commit a4e016d
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 60 deletions.
45 changes: 0 additions & 45 deletions packages/core/src/base/events.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/core/src/base/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { isClass, none } from '@kotori-bot/tools';
import fs from 'fs';
import { Parser, TsuError } from 'tsukiko';
import { resolve } from 'path';
import Events from './events';
import Context from '../context';
import Adapter from '../components/adapter';
import {
Expand All @@ -17,8 +16,9 @@ import { DevError, ModuleError } from '../utils/errror';
import { ADAPTER_PREFIX, DATABASE_PREFIX } from '../consts';
import Service from '../components/service';
import { Database } from '../components/database';
import Core from './core';

export class Modules extends Events {
export class Modules extends Core {
static isServiceConsructor(Obj: object): Obj is ServiceConstructor {
return Service.isPrototypeOf.call(Service, Obj);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.1.0",
"description": "Tools For Kotori",
"main": "dist/index.js",
"browser": "dist/exports/common.js",
"license": "GPL-3.0",
"author": "Hotaru <biyuehuya@gmail.com>",
"dependencies": {
Expand All @@ -27,4 +28,4 @@
"url": "git+https://github.com/kotorijs/kotori.git"
},
"homepage": "https://kotori.js.org "
}
}
55 changes: 55 additions & 0 deletions packages/tools/src/common/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export interface EventsList {
ready: { type: 'ready' };
error: { type: 'type' };
dispose: { type: 'dispose' };
}

type EventCallback<A, T extends keyof A> = (data: A[T]) => void;
type EventBeforeKeys<T> = T extends `before_${infer U}` ? U : never;
type StackType<A> = {
[K in keyof A]: EventCallback<A, K>[];
};

export class Events<A extends { [propName: string]: any } = EventsList> {
private eventStack: StackType<A> = {} as StackType<A>;

emit<T extends keyof A>(type: T, data: Omit<A[T], 'type'>) {
const session = Object.assign(data, { type }) as unknown as A[T];
if (!this.eventStack[type]) return;
this.eventStack[type].forEach((el) => el(session));
}

on<T extends keyof A>(type: T, callback: EventCallback<A, T>) {
if (!(type in this.eventStack)) this.eventStack[type] = [];
const index = this.eventStack[type].length;
this.eventStack[type].push(callback);
return () => {
if (!this.eventStack[type] || !this.eventStack[type][index]) return;
delete this.eventStack[type][index];
};
}

before<T extends EventBeforeKeys<keyof A>>(type: T, callback: EventCallback<A, `before_${T}`>) {
this.on(`before_${type}`, callback);
}

once<T extends keyof A>(type: T, callback: EventCallback<A, T>) {
const index = this.eventStack[type] ? this.eventStack[type].length : 0;
return this.on(type, (data) => {
delete this.eventStack[type][index];
callback(data);
});
}

off<T extends keyof A>(type: T, callback: EventCallback<A, T>) {
if (!this.eventStack[type]) return;
this.eventStack[type] = this.eventStack[type].filter((el) => callback !== el) as StackType<A>[T];
}

offAll<T extends keyof A>(type: T) {
if (!this.eventStack[type]) return;
this.eventStack[type] = [];
}
}

export default Events;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { obj } from './types';
import { obj } from '../types';

/* export const initialize: MethodDecorator = (_, __, val) => {
if (val.value instanceof Function) val.value();
Expand Down Expand Up @@ -39,7 +39,7 @@ export function isClass(obj: unknown, strict: boolean = true): obj is new (...ar

export function clearObject(val: obj, strict: boolean = false, stacks: Set<unknown> = new Set()) {
const handle = val;
Object.keys(handle).forEach(key => {
Object.keys(handle).forEach((key) => {
if (handle[key] !== undefined && (strict || handle[key] !== null)) {
if (typeof handle[key] === 'object' && stacks.has(handle[key])) handle[key] = clearObject(handle[key]);
return;
Expand Down Expand Up @@ -107,7 +107,7 @@ export function stringTemp(template: string, args: obj<string | number | void>)
const params = Object.assign(args, { break: '\n' });
let templateString = template;
if (!params || typeof params !== 'object') return templateString;
Object.keys(params).forEach(param => {
Object.keys(params).forEach((param) => {
if (typeof params[param] !== 'string' && typeof args[param] !== 'number') params[param] = '';
if (params[param]?.toString instanceof Function) params[param] = (params[param] as number).toString();
templateString = templateString.replace(new RegExp(`%${param}%`, 'g'), params[param] as string);
Expand Down Expand Up @@ -137,7 +137,7 @@ export function getDate() {
}

export function getSpecStr(template: string) {
return template.replace(/[xy]/g, char => {
return template.replace(/[xy]/g, (char) => {
const r = Math.random() * 16;
let v: number | string;
if (char === 'x') {
Expand Down Expand Up @@ -170,7 +170,7 @@ export function createProxy<T extends object>(val: T | (() => T)) {
let value = val;
if (typeof val === 'function') value = val();
return value[property as keyof typeof val];
},
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosRequestConfig } from 'axios';
import { FuncStringProcessStr } from './types';
import { FuncStringProcessStr } from '../types';

type HttpMethod<T = unknown> = (
url: string,
Expand Down
3 changes: 3 additions & 0 deletions packages/tools/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './events';
export * from './function';
export * from './http';
2 changes: 2 additions & 0 deletions packages/tools/src/exports/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from '../common';
export * from '../types';
6 changes: 2 additions & 4 deletions packages/tools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export * from './function';
export * from './function.node';
export * from './http';
export * from './types';
export * from './exports/common';
export * from './server';
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fs from 'fs';
import path from 'path';
import YAML from 'yaml';
import { ConfigFileType } from './types';
import { ConfigFileType } from '../types';

export function loadConfig(
filename: string,
type: ConfigFileType = 'json',
init: object | string = {},
autoCreate: boolean = false,
autoCreate: boolean = false
): object | null | unknown[] | string {
const dirname: string = path.dirname(filename);
if (!fs.existsSync(dirname)) fs.mkdirSync(dirname, { recursive: true });
Expand Down
1 change: 1 addition & 0 deletions packages/tools/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './function';

0 comments on commit a4e016d

Please sign in to comment.