// 1. Реализуйте класс Mailbox
// Доп. задания:
// 2. Если считаете, что Mailbox не хватает еще каких-то технических функций для более готового к использованию API, добавьте их или напишите, что бы вы добавили. Если считаете, что данное API содержит какие-то проблемы с точки зрения проектирования, опишите их и предложите рефакторинг.
// 3. Какие части кода можно было бы концептуально абстрагировать из реализации Mailbox и сделать их переиспользуемыми? Попробуйте максимально разбить реализацию на переиспользуемые части.
class SingleInstance {
constructor(instanceName) {
if (!this.constructor._instances) { this.constructor._instances = new Map() }
if (this.constructor._instances.has(instanceName)) { return this.constructor._instances.get(instanceName) }
this.constructor._instances.set(instanceName, this)
}
}
class HooksMixin {
constructor(...hooks) {
const addPre = function(hook) {
if (!this.preHooks) { this.preHooks = [] }
this.preHooks.push(hook)
}
const addNotify = function(hook) {
if (!this.notifyHooks) { this.notifyHooks = [] }
this.notifyHooks.push(hook)
}
this.callNotifyHooks = function(...args) {
this.notifyHooks && this.notifyHooks.forEach(notify => notify(...args))
}
this.callPreHooks = function(param) {
let counter = -1
let errorText
const isValid = (!this.preHooks || this.preHooks.every((preHook, index) => {
preHook(param, (updatedParam) => {
param = updatedParam
counter++
}, err => errorText = err)
return (index === counter)
}))
return {result: isValid && param, err: errorText}
}
hooks.forEach(hook => {
if (hook === 'pre') { this.pre = addPre }
if (hook === 'notify') { this.notify = addNotify }
})
}
}
class Mailbox extends SingleInstance {
sendMail(message) {
const preResult = this.callPreHooks(message)
if (preResult.result) {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.