protected trigger()

in src/StoreBase.ts [81:143]


    protected trigger(keyOrKeys?: KeyOrKeys): void {
        const throttleMs = this._throttleMs !== undefined
            ? this._throttleMs
            : Options.defaultThrottleMs;

        // If we're throttling, save execution time
        let throttledUntil: number | undefined;
        if (throttleMs) {
            if (!this._throttleData) {
                // Needs to accumulate and trigger later -- start a timer if we don't have one running already
                // If there are no callbacks, don't bother setting up the timer
                this._throttleData = {
                    timerId: Options.setTimeout(this._handleThrottledCallbacks, this._throttleMs),
                    callbackTime: Date.now() + throttleMs,
                };
            }
            throttledUntil = this._throttleData.callbackTime;
        }

        const bypassBlock = this._bypassTriggerBlocks;

        // trigger(0) is valid, ensure that we catch this case
        if (!keyOrKeys && !isNumber(keyOrKeys)) {
            // Inspecific key, so generic callback call
            for (const subs of this._subscriptions.values()) {
                for (const sub of subs) {
                    this._setupAllKeySubscription(sub, throttledUntil, bypassBlock);
                }
            }

            for (const subs of this._autoSubscriptions.values()) {
                for (const sub of subs) {
                    this._setupAllKeySubscription(sub.callback, throttledUntil, bypassBlock);
                }
            }
        } else {
            const keys = normalizeKeys(keyOrKeys);

            // Key list, so go through each key and queue up the callback
            for (const key of keys) {
                for (const callback of (this._subscriptions.get(key) || [])){
                    this._setupSpecificKeySubscription([key], callback, throttledUntil, bypassBlock);
                }

                for (const sub of (this._autoSubscriptions.get(key) || [])) {
                    this._setupSpecificKeySubscription([key], sub.callback, throttledUntil, bypassBlock);
                }
            }

            // Go through each of the all-key subscriptions and add the full key list to their gathered list
            for (const callback of (this._subscriptions.get(StoreBase.Key_All) || [])) {
                this._setupSpecificKeySubscription(keys, callback, throttledUntil, bypassBlock);
            }

            for (const sub of (this._autoSubscriptions.get(StoreBase.Key_All) || [])) {
                this._setupSpecificKeySubscription(keys, sub.callback, throttledUntil, bypassBlock);
            }
        }

        if (!throttledUntil || bypassBlock) {
            StoreBase._resolveCallbacks();
        }
    }