<commit_before>import {MacAddress} from "../../types";
import util from "util";
import createDebug from "debug";
import {ControllerIdentifier, SerializableController} from "../controller";
import {Accessory} from "../Accessory";
import { HAPStorage } from "./HAPStorage";
import Timeout = NodeJS.Timeout;

const debug = createDebug("HAP-NodeJS:ControllerStorage");

interface StorageLayout {
    accessories: Record<string, StoredControllerData[]>, // indexed by accessory UUID
}

interface StoredControllerData {
    type: ControllerIdentifier, // this field is called type out of history
    controllerData: ControllerData,
}

interface ControllerData {
    data: any,
    /*
    This property and the exact sequence this property is accessed solves the following problems:
      - Orphaned ControllerData won't be there forever and gets cleared at some point
      - When storage is loaded, there is no fixed time frame after which Controllers need to be configured
     */
    purgeOnNextLoad?: boolean,
}

export class ControllerStorage {

    private readonly accessoryUUID: string;
    private initialized: boolean = false;

    // ----- properties only set in parent storage object ------
    private username?: MacAddress;
    private fileCreated: boolean = false;
    purgeUnidentifiedAccessoryData: boolean = true;
    // ---------------------------------------------------------

    private trackedControllers: SerializableController[] = []; // used to track controllers before data was loaded from disk
    private controllerData: Record<ControllerIdentifier, ControllerData> = {};
    private restoredAccessories?: Record<string, StoredControllerData[]>; // indexed by accessory UUID

    private parent?: ControllerStorage;
    private linkedAccessories?: ControllerStorage[];

    private queuedSaveTimeout?: Timeout;
    private queuedSaveTime?: number;

    public constructor(accessory: Accessory) {
        this.accessoryUUID = accessory.UUID;
    }

    private enqueueSaveRequest(timeout: number = 0): void {
        const plannedTime = Date.now() + timeout;

        if (this.queuedSaveTimeout) {
            if (plannedTime <= (this.queuedSaveTime ?? 0)) {
                return;
            }
<commit_msg>Execute the save request only on the main ControllerStorage object<commit_after>import {MacAddress} from "../../types";
import util from "util";
import createDebug from "debug";
import {ControllerIdentifier, SerializableController} from "../controller";
import {Accessory} from "../Accessory";
import { HAPStorage } from "./HAPStorage";
import Timeout = NodeJS.Timeout;

const debug = createDebug("HAP-NodeJS:ControllerStorage");

interface StorageLayout {
    accessories: Record<string, StoredControllerData[]>, // indexed by accessory UUID
}

interface StoredControllerData {
    type: ControllerIdentifier, // this field is called type out of history
    controllerData: ControllerData,
}

interface ControllerData {
    data: any,
    /*
    This property and the exact sequence this property is accessed solves the following problems:
      - Orphaned ControllerData won't be there forever and gets cleared at some point
      - When storage is loaded, there is no fixed time frame after which Controllers need to be configured
     */
    purgeOnNextLoad?: boolean,
}

export class ControllerStorage {

    private readonly accessoryUUID: string;
    private initialized: boolean = false;

    // ----- properties only set in parent storage object ------
    private username?: MacAddress;
    private fileCreated: boolean = false;
    purgeUnidentifiedAccessoryData: boolean = true;
    // ---------------------------------------------------------

    private trackedControllers: SerializableController[] = []; // used to track controllers before data was loaded from disk
    private controllerData: Record<ControllerIdentifier, ControllerData> = {};
    private restoredAccessories?: Record<string, StoredControllerData[]>; // indexed by accessory UUID

    private parent?: ControllerStorage;
    private linkedAccessories?: ControllerStorage[];

    private queuedSaveTimeout?: Timeout;
    private queuedSaveTime?: number;

    public constructor(accessory: Accessory) {
        this.accessoryUUID = accessory.UUID;
    }

    private enqueueSaveRequest(timeout: number = 0): void {
        if (this.parent) {
            this.parent.enqueueSaveRequest(timeout);
            return;
        }

        const plannedTime = Date.now() + timeout;

        if (this.queuedSaveTimeout) {
            if (plannedTime <= (this.queuedSaveTime ?? 0)) {
                return;
            }

{'fraction_non_alphanumeric': 0.11257562662057044, 'fraction_numerical': 0.000864304235090752, 'mean_word_length': 3.1552962298025133, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6521004', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1087, 'n_words': 391}