Skip to content

Commit

Permalink
feat(customization): add clear methods
Browse files Browse the repository at this point in the history
  • Loading branch information
CSharperMantle committed Apr 25, 2023
1 parent ebb1183 commit a3684ee
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
7 changes: 6 additions & 1 deletion src/customization/CustomizationFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ import { History } from "./history"
import { Settings } from "./settings"

/**
* A unified facade for all user customization preferences.
* A facade for all user customization preferences.
*/
export class CustomizationFacade {
public readonly history = History.fromLocalStorage()

public readonly settings = Settings.fromLocalStorage()

public clear(): void {
this.history.clear()
this.settings.clear()
}
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/customization/history/History.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export class History implements ILocalStorageSerializable {
public get fastestRecord(): number | null {
return this._fastestRecord ?? null
}
private set fastestRecord(v: number | null) {
private set fastestRecord(v) {
this._fastestRecord = v
}

private _records: number[] | undefined
public get records(): number[] {
return this._records ?? []
}
private set records(v: number[]) {
private set records(v) {
this._records = v
}

Expand All @@ -48,21 +48,24 @@ export class History implements ILocalStorageSerializable {
return isFastestRecordUpdated
}

private constructor() {
public clear(): void {
this._fastestRecord = null
this._records = []
}

private constructor() {
this.clear()
}

public static fromLocalStorage(): History {
const result = retrieve(HistoryLocalStorageKey)

if (isNil(result)) return new History()
if (isNil(result)) return History.Empty

const repairedHistory = Object.create(
return Object.create(
History.prototype,
Object.getOwnPropertyDescriptors(result)
) as History
return repairedHistory
)
}

/**
Expand Down
25 changes: 16 additions & 9 deletions src/customization/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Settings implements ILocalStorageSerializable {
public get showGridLine(): boolean {
return this._showGridLine ?? true
}
public set showGridLine(v: boolean) {
public set showGridLine(v) {
this._showGridLine = v
this.toLocalStorage()
}
Expand All @@ -58,7 +58,7 @@ export class Settings implements ILocalStorageSerializable {
public get gameMap(): IMap {
return this._gameMap ?? defaultMap
}
public set gameMap(v: IMap) {
public set gameMap(v) {
this._gameMap = v
this.toLocalStorage()
}
Expand All @@ -67,7 +67,7 @@ export class Settings implements ILocalStorageSerializable {
public get colorScheme(): IColorScheme {
return this._colorScheme ?? defaultColorScheme
}
public set colorScheme(v: IColorScheme) {
public set colorScheme(v) {
this._colorScheme = v
this.toLocalStorage()
}
Expand All @@ -76,25 +76,32 @@ export class Settings implements ILocalStorageSerializable {
public get borderThickness(): number {
return this._borderThickness ?? DefaultBorderThickness
}
public set borderThickness(v: number) {
public set borderThickness(v) {
this._borderThickness = v
this.toLocalStorage()
}

public clear(): void {
this._showGridLine = undefined
this._gameUpdateIntervalMilliseconds = undefined
this._gameMap = undefined
this._colorScheme = undefined
this._borderThickness = undefined
}

private constructor() {
// Do nothing.
this.clear()
}

public static fromLocalStorage(): Settings {
const result = retrieve(SettingsLocalStorageKey)

if (isNil(result)) return new Settings()
if (isNil(result)) return Settings.Default

const repairedSettings = Object.create(
return Object.create(
Settings.prototype,
Object.getOwnPropertyDescriptors(result)
) as Settings
return repairedSettings
)
}

/**
Expand Down
12 changes: 5 additions & 7 deletions src/model/Tetrimino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,11 @@ export class Tetrimino {
export function repairBrokenTetriminos(
brokenTetriminos: Tetrimino[]
): Tetrimino[] {
const repairedTetriminos = map(
brokenTetriminos,
(brokenTetrimino) =>
Object.create(
Tetrimino.prototype,
Object.getOwnPropertyDescriptors(brokenTetrimino)
) as Tetrimino
const repairedTetriminos = map(brokenTetriminos, (brokenTetrimino) =>
Object.create(
Tetrimino.prototype,
Object.getOwnPropertyDescriptors(brokenTetrimino)
)
)
return repairedTetriminos
}

0 comments on commit a3684ee

Please sign in to comment.