@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
142 lines (141 loc) • 4.21 kB
JavaScript
import process from 'node:process';
import { randomWord } from './utils/randomWord.js';
export class Container {
constructor(params, paramsArr = {}) {
Object.defineProperty(this, "params", {
enumerable: true,
configurable: true,
writable: true,
value: params
});
Object.defineProperty(this, "paramsArr", {
enumerable: true,
configurable: true,
writable: true,
value: paramsArr
});
Object.defineProperty(this, "engine", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "filesService", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
if (!this.params.name) {
this.params.name = randomWord() + '_' + randomWord();
}
}
async mount(fileService) {
this.filesService = fileService;
}
async init(engine) {
this.engine = engine;
}
async run(command) {
switch (command) {
case 'scan':
break;
}
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async destroy() {
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async flushData() {
}
}
export class ContainerError extends Error {
constructor(msg) {
super(msg);
}
}
export class ContainerEngine {
constructor(logger, rootFileService) {
Object.defineProperty(this, "logger", {
enumerable: true,
configurable: true,
writable: true,
value: logger
});
Object.defineProperty(this, "rootFileService", {
enumerable: true,
configurable: true,
writable: true,
value: rootFileService
});
Object.defineProperty(this, "containers", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
Object.defineProperty(this, "listeners", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
process.on('SIGINT', async () => {
console.log('SIGINT');
await this.flushData();
process.exit();
});
}
async registerContainer(container) {
if (this.containers[container.params.name]) {
throw new ContainerError(`Container already exists: ${container.params.name}`);
}
try {
await container.init(this);
this.containers[container.params.name] = container;
}
catch (err) {
this.logger.error(`Error starting container ${container.params.name} ${err.stack ? err.stack : err.message}`, err);
}
return container;
}
async unregisterContainer(name) {
const container = this.containers[name];
if (!container) {
throw new ContainerError(`No such container: ${name}`);
}
await container.destroy();
delete this.containers[container.params.name];
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async run() {
}
getContainer(name) {
if (!this.containers[name]) {
throw new Error(`Unknown container: ${name}`);
}
return this.containers[name];
}
hasContainer(name) {
return !!this.containers[name];
}
async flushData() {
for (const container of Object.values(this.containers)) {
await container.flushData();
}
}
emit(driveId, eventName, payload) {
if (!this.listeners[eventName]) {
return;
}
for (const listener of this.listeners[eventName]) {
listener(driveId, payload);
}
}
subscribe(eventName, callback) {
if (!this.listeners[eventName]) {
this.listeners[eventName] = new Set();
}
this.listeners[eventName].add(callback);
}
}