File

src/layer/LayerTools.ts

Index

Properties
Methods

Constructor

Public constructor(layer: Layer)
Parameters :
Name Type Optional
layer Layer No

Properties

Public Readonly layer
Type : Layer

Methods

Public clearCache
clearCache()
Returns : void
Public delete
delete(name: K)
Type parameters :
  • K
Parameters :
Name Type Optional
name K No
Returns : any
Public get
get(name: K, cache?: boolean)
Type parameters :
  • K
Parameters :
Name Type Optional
name K No
cache boolean Yes
Returns : InstanceType<>
Public get
get(config: LayerToolConfig<K>)
Type parameters :
  • K
Parameters :
Name Type Optional
config LayerToolConfig<K> No
Returns : InstanceType<>
Public get
get(nameOrConfig: LayerToolConfig<K> | K, cache?: boolean)
Type parameters :
  • K
Parameters :
Name Type Optional
nameOrConfig LayerToolConfig<K> | K No
cache boolean Yes
Returns : InstanceType<>
Public isCached
isCached(name: K)
Type parameters :
  • K
Parameters :
Name Type Optional
name K No
Returns : any
Public isValidTool
isValidTool(name: K)
Type parameters :
  • K
Parameters :
Name Type Optional
name K No
Returns : boolean
import { Layer } from "./Layer";
import * as Tools from "../toolbox/exports";
import { IllustratorCollection } from "../utils/IllustratorCollection";

export interface LayerToolConfig<Name> {
    name: Name;
    cache?: boolean;
}

export class LayerTools {
    #toolsCache = new IllustratorCollection<string, Tools.ToolBox>();
    public constructor(public readonly layer: Layer) {}

    public clearCache() {
        this.#toolsCache.clear();
    }

    public delete<K extends keyof typeof Tools>(name: K) {
        return this.#toolsCache.delete(name);
    }

    public isCached<K extends keyof typeof Tools>(name: K) {
        return this.#toolsCache.has(name);
    }

    public get<K extends keyof typeof Tools>(name: K, cache?: boolean): InstanceType<typeof Tools[K]>;
    public get<K extends keyof typeof Tools>(config: LayerToolConfig<K>): InstanceType<typeof Tools[K]>;
    public get<K extends keyof typeof Tools>(
        nameOrConfig: LayerToolConfig<K> | K,
        cache?: boolean
    ): InstanceType<typeof Tools[K]> {
        const name = typeof nameOrConfig === "string" ? nameOrConfig : nameOrConfig.name;
        const shouldCache = !!(typeof nameOrConfig === "object" ? nameOrConfig.cache : cache);
        if (typeof name !== "string" || !name)
            throw new TypeError(`tool name must be a string, received ${typeof name}`);
        if (!shouldCache && this.#toolsCache.has(name))
            return this.#toolsCache.get(name) as unknown as InstanceType<typeof Tools[K]>;
        if (!Tools[name]) throw new Error(`Unknown tool ${name}`);
        const toolConstructor = Tools[name];
        const tool = new toolConstructor(this.layer);
        if (shouldCache) this.#toolsCache.set(name, tool);
        return tool as unknown as InstanceType<typeof Tools[K]>;
    }

    public isValidTool<K extends keyof typeof Tools>(name: K) {
        return name in Tools;
    }
}

results matching ""

    No results matching ""