From 04b88b91615b43c97c5ffc51b9a4da8ef723dbee Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 25 Jan 2022 14:09:29 +0000 Subject: [PATCH] remove IProtectionSetting, just use AbstractProtectionSetting --- src/protections/IProtection.ts | 4 +-- src/protections/ProtectionSettings.ts | 44 +++++++++++---------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/protections/IProtection.ts b/src/protections/IProtection.ts index 1d41ae3..61a9a05 100644 --- a/src/protections/IProtection.ts +++ b/src/protections/IProtection.ts @@ -15,7 +15,7 @@ limitations under the License. */ import { Mjolnir } from "../Mjolnir"; -import { IProtectionSetting } from "./ProtectionSettings"; +import { AbstractProtectionSetting } from "./ProtectionSettings"; /** * Represents a protection mechanism of sorts. Protections are intended to be @@ -25,6 +25,6 @@ import { IProtectionSetting } from "./ProtectionSettings"; */ export interface IProtection { readonly name: string; - settings: { [setting: string]: IProtectionSetting }; + settings: { [setting: string]: AbstractProtectionSetting }; handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise; } diff --git a/src/protections/ProtectionSettings.ts b/src/protections/ProtectionSettings.ts index 2ef332a..3acde9f 100644 --- a/src/protections/ProtectionSettings.ts +++ b/src/protections/ProtectionSettings.ts @@ -16,7 +16,7 @@ limitations under the License. export class ProtectionSettingValidationError extends Error {}; -export interface IProtectionSetting { +export class AbstractProtectionSetting { // the current value of this setting value: TValue @@ -26,7 +26,9 @@ export interface IProtectionSetting { * @param data Serialised value * @returns Deserialised value or undefined if deserialisation failed */ - fromString(data: string): TChange | undefined; + fromString(data: string): TChange | undefined { + throw new Error("not Implemented"); + } /* * Check whether a given value is valid for this setting @@ -34,53 +36,41 @@ export interface IProtectionSetting { * @param data Setting value * @returns Validity of provided value */ - validate(data: TChange): boolean; + validate(data: TChange): boolean { + throw new Error("not Implemented"); + } /* * Store a value in this setting, only to be used after `validate()` * @param data Validated setting value */ - setValue(data: TValue): void; + setValue(data: TValue) { + this.value = data; + } } -export interface IProtectionListSetting extends IProtectionSetting { +export class AbstractProtectionListSetting extends AbstractProtectionSetting { /* * Add `data` to the current setting value, and return that new object * * @param data Value to add to the current setting value * @returns The potential new value of this setting object */ - addValue(data: TChange): TValue; + addValue(data: TChange): TValue { + throw new Error("not Implemented"); + } + /* * Remove `data` from the current setting value, and return that new object * * @param data Value to remove from the current setting value * @returns The potential new value of this setting object */ - removeValue(data: TChange): TValue; -} - -class AbstractProtectionSetting implements IProtectionSetting { - value: TValue - fromString(data: string): TChange | undefined { - throw new Error("not Implemented"); - } - validate(data: TChange): boolean { - throw new Error("not Implemented"); - } - setValue(data: TValue) { - this.value = data; - } -} -class AbstractProtectionListSetting extends AbstractProtectionSetting implements IProtectionListSetting { - addValue(data: TChange): TValue { - throw new Error("not Implemented"); - } removeValue(data: TChange): TValue { throw new Error("not Implemented"); } } -export function isListSetting(object: any): object is IProtectionListSetting { - return ("addValue" in object && "removeValue" in object); +export function isListSetting(object: any): object is AbstractProtectionListSetting { + return object instanceof AbstractProtectionListSetting; }