Files
threshold-ui/ui/admin/app/helpers/can.js
T
Lisbet Alvarez 3ba93927fe Update copyright headers (#3146)
* empty commit

* Add missing copyright headers

* chore: 🤖 update copywrite config file with correct year

* Add missing copyright headers

* empty commit (gh action merge checks are stuck)
2026-01-28 17:09:06 -08:00

54 lines
1.4 KiB
JavaScript

/**
* Copyright IBM Corp. 2021, 2026
* SPDX-License-Identifier: BUSL-1.1
*/
import CanHelper from 'ember-can/helpers/can';
import { service } from '@ember/service';
import config from 'admin/config/environment';
const isDevelopment = config.environment === 'development';
/**
* Normally, abilities that access tracked values are not recomputed on change.
* This helper overrides the Ember Can helper to trigger recompute whenever
* a feature flag changes. This functionality is available only in the
* development environment, since feature flags may be toggled in the UI, and
* also for performance reasons as this effectively recomputes _all_ `can`
* helpers on any feature flag change.
*/
class DevelopmentCanHelper extends CanHelper {
// =services
@service features;
// =methods
constructor() {
super(...arguments);
this.observeAllFlags();
}
willDestroy() {
this.removeAllFlags();
super.willDestroy();
}
observeAllFlags() {
this.features.flags.forEach((flag) =>
/* eslint-disable-next-line ember/no-observers */
this.features.addObserver(flag, this, 'recompute'),
);
}
removeAllFlags() {
this.features.flags.forEach((flag) =>
/* eslint-disable-next-line ember/no-observers */
this.features.removeObserver(flag, this, 'recompute'),
);
}
}
// In development, export the development helper, otherwise the original.
export default isDevelopment ? DevelopmentCanHelper : CanHelper;