Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
1. Extension.getSettings() actually create a new instance every time
   it's called. To avoid that call it once when the extension is enabled
   and pass the object to all classes where it's needed.
2. Eliminate Extension.lookupByURL() as per code review feedback.
   Instead pass the extension object to all classes where needed.
3. Simplyfy destory in extension.js.
  • Loading branch information
elvetemedve committed Dec 13, 2023
1 parent 7a54844 commit 7b6f16b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
11 changes: 6 additions & 5 deletions [email protected]/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import * as View from './view.js';
import * as PrefsKeys from './prefs_keys.js';

const Timer = class {
constructor(params) {
this._settings = params.settings;
this._view = params.view;
constructor(view, settings) {
this._settings = settings;
this._view = view;
}

start(update_interval) {
Expand Down Expand Up @@ -42,6 +42,7 @@ const Timer = class {
}

destroy() {
this.stop();
this._view.destroy();
this._settings = null;
this._view = null;
Expand All @@ -52,12 +53,12 @@ export default class SystemMonitorExtension extends Extension {
#timer;

enable() {
this.#timer = new Timer({view: new View.Menu({settings: this.getSettings()}), settings: this.getSettings()});
let settings = this.getSettings();
this.#timer = new Timer(new View.Menu(this, settings), settings);
this.#timer.start();
}

disable() {
this.#timer.stop();
this.#timer.destroy();
this.#timer = null;
}
Expand Down
3 changes: 1 addition & 2 deletions [email protected]/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ IconFactory.prototype.concreteClass = IndicatorModule.Icon;
// Create an indicator icon object, options will be passed to the real object's constructor.
//
// For working with themed icons see http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
IconFactory.prototype.create = function(type, options, can_show_activity) {
IconFactory.prototype.create = function(type, options, can_show_activity, extensionObject) {
let default_options = {
style_class: 'system-status-icon system-monitor-icon',
reactive: true,
Expand All @@ -65,7 +65,6 @@ IconFactory.prototype.create = function(type, options, can_show_activity) {
}

let constructor_options = Object.assign(default_options, options);
let extensionObject = Extension.lookupByURL(import.meta.url);

if (type == PrefsKeys.STORAGE_METER) {
constructor_options.icon_name = 'drive-harddisk-symbolic';
Expand Down
3 changes: 1 addition & 2 deletions [email protected]/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ StopWatch.prototype.printElapsed = function (name) {
export const logError = function(error) {
let sourceFile = error.fileName;
let sourceLine = error.lineNumber;
let extensionObject = Extension.lookupByURL(import.meta.url);

console.error(`Extension ${extensionObject.metadata.uuid}: "${error}" ${sourceFile}:${sourceLine}`);
console.error(`Extension [email protected]: "${error}" ${sourceFile}:${sourceLine}`);
};
13 changes: 7 additions & 6 deletions [email protected]/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import * as PrefsKeys from './prefs_keys.js';

export const Menu = GObject.registerClass(
class Menu extends PanelMenu.Button {
_init() {
constructor(extensionObject, settings) {
let menuAlignment = 0.5;
let extensionObject = Extension.lookupByURL(import.meta.url);
super._init(menuAlignment);

super(menuAlignment);

this._layout = new St.BoxLayout();
this._settings = extensionObject.getSettings();
this._settings = settings;
this._extensionObject = extensionObject;
this._icons = {};
this._meters = {};
this._meter_widgets = {};
Expand Down Expand Up @@ -94,7 +95,7 @@ class Menu extends PanelMenu.Button {
}
_createIcon(type) {
let can_show_activity = this._settings.get_boolean(PrefsKeys.SHOW_ACTIVITY);
let icon = FactoryModule.AbstractFactory.create('icon', type, {}, can_show_activity);
let icon = FactoryModule.AbstractFactory.create('icon', type, {}, can_show_activity, this._extensionObject);
let meter = this._meters[type];

if (meter == undefined) {
Expand All @@ -120,7 +121,7 @@ class Menu extends PanelMenu.Button {
meter.addObserver(icon);
this._layout.insert_child_at_index(icon, this.available_meters.indexOf(type));
this._icons[type] = icon;
return FactoryModule.AbstractFactory.create('icon', type, {}, can_show_activity);
return FactoryModule.AbstractFactory.create('icon', type, {}, can_show_activity, this._extensionObject);
}
_destroyIcon(type) {
let icon = this._icons[type];
Expand Down

0 comments on commit 7b6f16b

Please sign in to comment.