hivest-js
Version:
A simple, fast and minimalist framework for Node.js that allows you to create modular applications with dependency injection using decorators
143 lines • 6.14 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoggingService = exports.NotificationService = exports.UserService = void 0;
const event_manager_1 = require("../event-manager");
const decorators_1 = require("../lib/decorators");
// Example service that emits events
let UserService = class UserService {
constructor(eventManager) {
this.eventManager = eventManager;
}
async createUser(userData) {
// Business logic to create user
const user = { id: 1, ...userData };
// Emit the event
await this.eventManager.emit('user.created', user);
return user;
}
async updateUser(id, userData) {
// Business logic to update user
const user = { id, ...userData };
// Emit the event
await this.eventManager.emit('user.updated', user);
return user;
}
async createUserEndpoint(ctx) {
const user = await this.createUser(ctx.req.body);
ctx.res.json(user);
}
};
exports.UserService = UserService;
__decorate([
(0, decorators_1.EventEmitter)('user.created'),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], UserService.prototype, "createUser", null);
__decorate([
(0, decorators_1.EventEmitter)('user.updated'),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, Object]),
__metadata("design:returntype", Promise)
], UserService.prototype, "updateUser", null);
__decorate([
(0, decorators_1.HttpPost)('/'),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], UserService.prototype, "createUserEndpoint", null);
exports.UserService = UserService = __decorate([
(0, decorators_1.Controller)({ path: '/users' }),
__param(0, (0, decorators_1.Inject)('EventManager')),
__metadata("design:paramtypes", [event_manager_1.EventManager])
], UserService);
// Example service that listens to events
let NotificationService = class NotificationService {
constructor(eventManager) {
this.eventManager = eventManager;
}
async onUserCreated(user) {
console.log('User created:', user);
// Send welcome email, create profile, etc.
await this.sendWelcomeEmail(user);
}
async onUserUpdated(user) {
console.log('User updated:', user);
// Update cache, notify admins, etc.
await this.updateUserCache(user);
}
async sendWelcomeEmail(user) {
// Implementation for sending welcome email
console.log(`Sending welcome email to ${user.email}`);
}
async updateUserCache(user) {
// Implementation for updating user cache
console.log(`Updating cache for user ${user.id}`);
}
async sendNotification(ctx) {
// Manual event emission
await this.eventManager.emit('notification.sent', ctx.req.body);
ctx.res.json({ message: 'Notification sent' });
}
};
exports.NotificationService = NotificationService;
__decorate([
(0, decorators_1.EventListener)('user.created'),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], NotificationService.prototype, "onUserCreated", null);
__decorate([
(0, decorators_1.EventListener)('user.updated'),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], NotificationService.prototype, "onUserUpdated", null);
__decorate([
(0, decorators_1.HttpPost)('/send'),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], NotificationService.prototype, "sendNotification", null);
exports.NotificationService = NotificationService = __decorate([
(0, decorators_1.Controller)({ path: '/notifications' }),
__param(0, (0, decorators_1.Inject)('EventManager')),
__metadata("design:paramtypes", [event_manager_1.EventManager])
], NotificationService);
// Example of manual event handling
let LoggingService = class LoggingService {
constructor(eventManager) {
this.eventManager = eventManager;
// Manual event registration
this.eventManager.on('user.created', this.logUserCreated.bind(this));
this.eventManager.on('user.updated', this.logUserUpdated.bind(this));
this.eventManager.on('notification.sent', this.logNotification.bind(this));
}
logUserCreated(user) {
console.log(`[LOG] User created at ${new Date().toISOString()}:`, user);
}
logUserUpdated(user) {
console.log(`[LOG] User updated at ${new Date().toISOString()}:`, user);
}
logNotification(notification) {
console.log(`[LOG] Notification sent at ${new Date().toISOString()}:`, notification);
}
};
exports.LoggingService = LoggingService;
exports.LoggingService = LoggingService = __decorate([
__param(0, (0, decorators_1.Inject)('EventManager')),
__metadata("design:paramtypes", [event_manager_1.EventManager])
], LoggingService);
//# sourceMappingURL=event-example.js.map