refactor: move WrapperMap to driver file

This commit is contained in:
Mikhail Zolotukhin 2021-09-07 23:23:32 +03:00
parent 137da2793c
commit fb7bbb033f
2 changed files with 40 additions and 39 deletions

View File

@ -23,7 +23,6 @@ import KWinMousePoller from "./kwin_mouse_poller";
import { KWinSetTimeout } from "./kwin_set_timeout";
import { ILayoutClass } from "../layouts/ilayout";
import { WindowState } from "../engine/window";
import WrapperMap from "../util/wrappermap";
import IConfig, { Config } from "../config";
import Debug from "../util/debug";
@ -504,3 +503,43 @@ export default class KWinDriver implements IDriverContext {
// }
/* NOTE: check `bindEvents` for details */
}
/**
* Window wrapper map
*/
class WrapperMap<F, T> {
private items: { [key: string]: T };
constructor(
public readonly hasher: (item: F) => string,
public readonly wrapper: (item: F) => T
) {
this.items = {};
}
public add(item: F): T {
const key = this.hasher(item);
if (this.items[key] !== undefined) {
throw "WrapperMap: the key [" + key + "] already exists!";
}
const wrapped = this.wrapper(item);
this.items[key] = wrapped;
return wrapped;
}
public get(item: F): T | null {
const key = this.hasher(item);
return this.items[key] || null;
}
public getByKey(key: string): T | null {
return this.items[key] || null;
}
public remove(item: F): boolean {
const key = this.hasher(item);
return delete this.items[key];
}
}

View File

@ -1,38 +0,0 @@
// SPDX-FileCopyrightText: 2018-2019 Eon S. Jeon <esjeon@hyunmu.am>
// SPDX-FileCopyrightText: 2021 Mikhail Zolotukhin <mail@genda.life>
//
// SPDX-License-Identifier: MIT
export default class WrapperMap<F, T> {
private items: { [key: string]: T };
constructor(
public readonly hasher: (item: F) => string,
public readonly wrapper: (item: F) => T
) {
this.items = {};
}
public add(item: F): T {
const key = this.hasher(item);
if (this.items[key] !== undefined)
throw "WrapperMap: the key [" + key + "] already exists!";
const wrapped = this.wrapper(item);
this.items[key] = wrapped;
return wrapped;
}
public get(item: F): T | null {
const key = this.hasher(item);
return this.items[key] || null;
}
public getByKey(key: string): T | null {
return this.items[key] || null;
}
public remove(item: F): boolean {
const key = this.hasher(item);
return delete this.items[key];
}
}