Skip to content

Commit

Permalink
Migrate dynamic imports using Gio
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Sep 30, 2023
1 parent 272cf1b commit fa54b0c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 16 deletions.
9 changes: 5 additions & 4 deletions src/preferences/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import Gtk from 'gi://Gtk';
import Pango from 'gi://Pango';

import Config from '../config.js';
import plugins from '../service/plugins/index.js';
import * as Keybindings from './keybindings.js';


// Build a list of plugins and shortcuts for devices
const DEVICE_PLUGINS = [];
const DEVICE_SHORTCUTS = {};

for (const name in imports.service.plugins) {
const module = imports.service.plugins[name];
for (const name in plugins) {
const module = plugins[name];

if (module.Metadata === undefined)
continue;
Expand Down Expand Up @@ -422,7 +423,7 @@ export const Panel = GObject.registerClass({
this._pluginSettings = {};

if (!this._pluginSettings.hasOwnProperty(name)) {
const meta = imports.service.plugins[name].Metadata;
const meta = plugins[name].Metadata;

this._pluginSettings[name] = new Gio.Settings({
settings_schema: Config.GSCHEMA.lookup(meta.id, -1),
Expand Down Expand Up @@ -1056,7 +1057,7 @@ export const Panel = GObject.registerClass({
}

_addPlugin(name) {
const plugin = imports.service.plugins[name];
const plugin = plugins[name];

const row = new SectionRow({
height_request: 48,
Expand Down
25 changes: 23 additions & 2 deletions src/service/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,33 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later

import Gio from 'gi://Gio';
import GLib from 'gi://GLib';

/*
* Singleton Tracker
*/
const Default = new Map();

// Load all components using dynamic import
const components = {};

const dir = Gio.File.new_for_uri(import.meta.url).get_parent();
const iter = await dir.enumerate_children_async(
Gio.FILE_ATTRIBUTE_STANDARD_NAME,
Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
GLib.PRIORITY_DEFAULT,
null);
const infos = await iter.next_files_async(100, GLib.PRIORITY_DEFAULT, null);
iter.close_async(GLib.PRIORITY_DEFAULT, null, null);

for (let i = 0; i < infos.length; i++) {
const info = infos[i];
const name = info.get_name().replace(/\.js$/, '');
if (name === 'index')
continue;
components[name] = await import(`./${name}.js`);
}

/**
* Acquire a reference to a component. Calls to this function should always be
Expand All @@ -23,10 +44,10 @@ export function acquire(name) {
let info = Default.get(name);

if (info === undefined) {
const module = imports.service.components[name];
const module = components[name];

info = {
instance: new module.Component(),
instance: new module.default(),
refcount: 0,
};

Expand Down
2 changes: 2 additions & 0 deletions src/service/components/pulseaudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const typelibDir = GLib.build_filenamev([Config.GNOME_SHELL_LIBDIR, 'gnome-shell
GIRepository.Repository.prepend_search_path(typelibDir);
GIRepository.Repository.prepend_library_path(typelibDir);

const Gvc = (await import('gi://Gvc')).default;


/**
* Extend Gvc.MixerStream with a property for returning a user-visible name
Expand Down
6 changes: 4 additions & 2 deletions src/service/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';

import plugins from './plugins/index.js';


/**
* Get the local device type.
Expand Down Expand Up @@ -407,12 +409,12 @@ export const ChannelService = GObject.registerClass({
},
});

for (const name in imports.service.plugins) {
for (const name in plugins) {
// Exclude mousepad/presenter capability in unsupported sessions
if (!HAVE_REMOTEINPUT && ['mousepad', 'presenter'].includes(name))
continue;

const meta = imports.service.plugins[name].Metadata;
const meta = plugins[name].Metadata;

if (meta === undefined)
continue;
Expand Down
11 changes: 6 additions & 5 deletions src/service/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import GObject from 'gi://GObject';
import Config from '../config.js';
import * as Components from './components/index.js';
import * as Core from './core.js';
import plugins from './plugins/index.js';

/**
* An object representing a remote device.
Expand Down Expand Up @@ -284,12 +285,12 @@ const Device = GObject.registerClass({
// Determine supported plugins by matching incoming to outgoing types
const supported = [];

for (const name in imports.service.plugins) {
for (const name in plugins) {
// Exclude mousepad/presenter plugins in unsupported sessions
if (!HAVE_REMOTEINPUT && ['mousepad', 'presenter'].includes(name))
continue;

const meta = imports.service.plugins[name].Metadata;
const meta = plugins[name].Metadata;

if (meta === undefined)
continue;
Expand Down Expand Up @@ -1002,8 +1003,8 @@ const Device = GObject.registerClass({
try {
if (this.paired && !this._plugins.has(name)) {
// Instantiate the handler
handler = imports.service.plugins[name];
plugin = new handler.Plugin(this);
handler = plugins[name];
plugin = new handler.default(this);

// Register packet handlers
for (const packetType of handler.Metadata.incomingCapabilities)
Expand Down Expand Up @@ -1044,7 +1045,7 @@ const Device = GObject.registerClass({
try {
if (this._plugins.has(name)) {
// Unregister packet handlers
handler = imports.service.plugins[name];
handler = plugins[name];

for (const type of handler.Metadata.incomingCapabilities)
this._handlers.delete(type);
Expand Down
23 changes: 21 additions & 2 deletions src/service/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ const DEVICE_PATH = '/org/gnome/Shell/Extensions/GSConnect/Device';
const DEVICE_IFACE = Config.DBUS.lookup_interface(DEVICE_NAME);


// Load all backends using dynamic import
const backends = {};

const dir = Gio.File.new_for_uri(import.meta.url).get_parent().resolve_relative_path('backends');
const iter = await dir.enumerate_children_async(
Gio.FILE_ATTRIBUTE_STANDARD_NAME,
Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
GLib.PRIORITY_DEFAULT,
null);
const infos = await iter.next_files_async(100, GLib.PRIORITY_DEFAULT, null);
iter.close_async(GLib.PRIORITY_DEFAULT, null, null);

for (let i = 0; i < infos.length; i++) {
const info = infos[i];
const name = info.get_name().replace(/\.js$/, '');
backends[name] = await import(`./backends/${name}.js`);
}


/**
* A manager for devices.
*/
Expand Down Expand Up @@ -233,9 +252,9 @@ const Manager = GObject.registerClass({
}

_loadBackends() {
for (const name in imports.service.backends) {
for (const name in backends) {
try {
const module = imports.service.backends[name];
const module = backends[name];

if (module.ChannelService === undefined)
continue;
Expand Down
3 changes: 2 additions & 1 deletion src/service/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import GLib from 'gi://GLib';
import GObject from 'gi://GObject';

import Config from '../config.js';
import plugins from './plugins/index.js';


/**
Expand Down Expand Up @@ -40,7 +41,7 @@ const Plugin = GObject.registerClass({
this._meta = meta;

if (this._meta === null)
this._meta = imports.service.plugins[name].Metadata;
this._meta = plugins[name].Metadata;

// GSettings
const schema = Config.GSCHEMA.lookup(this._meta.id, false);
Expand Down
29 changes: 29 additions & 0 deletions src/service/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: GSConnect Developers https://github.com/GSConnect
//
// SPDX-License-Identifier: GPL-2.0-or-later

import Gio from 'gi://Gio';
import GLib from 'gi://GLib';


// Load all plugins using dynamic import
const plugins = {};

const dir = Gio.File.new_for_uri(import.meta.url).get_parent();
const iter = await dir.enumerate_children_async(
Gio.FILE_ATTRIBUTE_STANDARD_NAME,
Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
GLib.PRIORITY_DEFAULT,
null);
const infos = await iter.next_files_async(100, GLib.PRIORITY_DEFAULT, null);
iter.close_async(GLib.PRIORITY_DEFAULT, null, null);

for (let i = 0; i < infos.length; i++) {
const info = infos[i];
const name = info.get_name().replace(/\.js$/, '');
if (name === 'index')
continue;
plugins[name] = await import(`./${name}.js`);
}

export default plugins;

0 comments on commit fa54b0c

Please sign in to comment.