ai/src/module.ts

74 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-01-21 04:27:02 +00:00
import { bindThis } from '@/decorators.js';
import , { InstallerResult } from '@/ai.js';
2019-01-14 15:14:22 +00:00
export default abstract class Module {
2019-01-15 01:26:48 +00:00
public abstract readonly name: string;
2019-01-14 15:14:22 +00:00
protected ai: ;
2019-05-10 02:55:07 +00:00
private doc: any;
2019-01-14 15:14:22 +00:00
2019-01-15 17:10:42 +00:00
public init(ai: ) {
2019-01-14 15:14:22 +00:00
this.ai = ai;
2019-05-10 02:55:07 +00:00
this.doc = this.ai.moduleData.findOne({
module: this.name
});
if (this.doc == null) {
this.doc = this.ai.moduleData.insertOne({
module: this.name,
data: {}
});
}
2019-01-14 15:14:22 +00:00
}
2019-01-14 15:14:22 +00:00
public abstract install(): InstallerResult;
2024-01-21 04:27:02 +00:00
@bindThis
2019-01-14 15:14:22 +00:00
protected log(msg: string) {
2019-01-24 08:47:13 +00:00
this.ai.log(`[${this.name}]: ${msg}`);
2019-01-14 15:14:22 +00:00
}
2019-02-01 16:53:06 +00:00
/**
*
* @param key
* @param id ID稿ID
* @param data
*/
2024-01-21 04:27:02 +00:00
@bindThis
protected subscribeReply(key: string | null, id: string, data?: any) {
this.ai.subscribeReply(this, key, id, data);
2019-01-14 15:14:22 +00:00
}
2019-02-01 16:53:06 +00:00
/**
*
* @param key
*/
2024-01-21 04:27:02 +00:00
@bindThis
2020-09-02 12:54:01 +00:00
protected unsubscribeReply(key: string | null) {
2019-01-14 15:14:22 +00:00
this.ai.unsubscribeReply(this, key);
}
2019-02-01 17:06:51 +00:00
/**
*
*
* @param delay
* @param data
*/
2024-01-21 04:27:02 +00:00
@bindThis
2019-02-01 17:06:51 +00:00
public setTimeoutWithPersistence(delay: number, data?: any) {
this.ai.setTimeoutWithPersistence(this, delay, data);
}
2019-05-10 02:55:07 +00:00
2024-01-21 04:27:02 +00:00
@bindThis
2019-05-10 02:55:07 +00:00
protected getData() {
return this.doc.data;
}
2024-01-21 04:27:02 +00:00
@bindThis
2019-05-10 02:55:07 +00:00
protected setData(data: any) {
this.doc.data = data;
this.ai.moduleData.update(this.doc);
}
2019-01-14 15:14:22 +00:00
}