control-model: restructuring
This commit is contained in:
@ -6,43 +6,46 @@
|
||||
*/
|
||||
|
||||
import { Persistent } from './Persistent';
|
||||
import { PersistentType, ControlObserver, Observer } from './Types';
|
||||
|
||||
export abstract class ControlModel extends Persistent {
|
||||
public constructor (obj?: Record<string, string|number|boolean>) {
|
||||
private _before_change: Record<string, ControlObserver[]> = {};
|
||||
private _observers: Record<string, Observer[]> = {};
|
||||
|
||||
public constructor (obj?: Record<string, PersistentType>) {
|
||||
super ();
|
||||
this.define_properties ();
|
||||
for (const prop of Object.keys (this.properties)) {
|
||||
if ([
|
||||
'string',
|
||||
'number',
|
||||
'boolean'
|
||||
].indexOf (this.properties[prop]) < 0) {
|
||||
throw new Error (
|
||||
'property types have to be either string, number or boolean'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof obj !== 'undefined')
|
||||
this.assign_object (obj);
|
||||
}
|
||||
|
||||
public to_object (): Record<string, string|number|boolean> {
|
||||
return super.to_object () as Record<string, string|number|boolean>;
|
||||
}
|
||||
public set (key: string, value: PersistentType): void {
|
||||
const prev = this.get (key);
|
||||
if (typeof this._before_change[key] !== 'undefined') {
|
||||
for (const obs of this._before_change[key]) {
|
||||
if (!obs (value, prev, key))
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public get (key: string): string|number|boolean {
|
||||
return super.get (key) as string|number|boolean;
|
||||
}
|
||||
|
||||
public set (key: string, value: string|number|boolean): void {
|
||||
super.set (key, value);
|
||||
if (typeof this._observers[key] !== 'undefined') {
|
||||
for (const obs of this._observers[key])
|
||||
obs (value, prev, key);
|
||||
}
|
||||
}
|
||||
|
||||
public update (): void {
|
||||
this.verify ();
|
||||
public before_change (key: string, func: ControlObserver): void {
|
||||
if (typeof this._before_change[key] === 'undefined')
|
||||
this._before_change[key] = [];
|
||||
this._before_change[key].push (func);
|
||||
}
|
||||
|
||||
public register_observer (key: string, func: Observer): void {
|
||||
if (typeof this._observers[key] === 'undefined')
|
||||
this._observers[key] = [];
|
||||
this._observers[key].push (func);
|
||||
}
|
||||
|
||||
public abstract verify(): void;
|
||||
protected abstract define_properties(): void;
|
||||
}
|
||||
|
@ -7,10 +7,7 @@
|
||||
|
||||
import { copy_object } from '@sapphirecode/utilities';
|
||||
import { Assignable, Serializable } from './interfaces';
|
||||
|
||||
type PersistentTypeString = 'string'|'number'|'boolean'|'array';
|
||||
type PersistentPrimitive = string|number|boolean;
|
||||
type PersistentType = PersistentPrimitive|PersistentPrimitive[];
|
||||
import { PersistentTypeString, PersistentType } from './Types';
|
||||
|
||||
export abstract class Persistent implements Assignable, Serializable {
|
||||
private _data: Record<string, PersistentType> = {};
|
||||
|
15
lib/Types.ts
Normal file
15
lib/Types.ts
Normal file
@ -0,0 +1,15 @@
|
||||
export type PersistentTypeString = 'string'|'number'|'boolean'|'array';
|
||||
export type PersistentPrimitive = string|number|boolean;
|
||||
export type PersistentType = PersistentPrimitive|PersistentPrimitive[];
|
||||
|
||||
export type Observer = (
|
||||
value: PersistentType,
|
||||
prev: PersistentType,
|
||||
key: string
|
||||
) => void;
|
||||
|
||||
export type ControlObserver = (
|
||||
value: PersistentType,
|
||||
prev: PersistentType,
|
||||
key: string
|
||||
) => boolean;
|
Reference in New Issue
Block a user