[gnome-sound-recorder/wip/cdavis/typescript: 5/8] Port to ES Modules




commit d02eb0a97adb81a6d12c201f59ec6ac382a0d2b4
Author: Christopher Davis <christopherdavis gnome org>
Date:   Sun Aug 7 14:13:59 2022 -0400

    Port to ES Modules

 src/application.js             | 19 ++++++++++++-------
 src/main.js                    | 16 ++--------------
 src/meson.build                |  1 +
 src/org.gnome.SoundRecorder.in | 16 ++++++++++++++--
 src/recorder.js                | 15 ++++++++++-----
 src/recorderWidget.js          | 11 +++++++----
 src/recording.js               | 19 ++++++++++++-------
 src/recordingList.js           | 16 +++++++++-------
 src/recordingListWidget.js     | 11 ++++++++---
 src/row.js                     | 14 +++++++++-----
 src/utils.js                   |  9 +++++----
 src/waveform.js                | 10 ++++++----
 src/window.js                  | 20 +++++++++++++-------
 13 files changed, 108 insertions(+), 69 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 83125c6..186dca3 100644
--- a/src/application.js
+++ b/src/application.js
@@ -18,15 +18,20 @@
 *
 */
 
-const { Adw, Gio, GLib, GObject, Gst, Gtk } = imports.gi;
+import Adw from 'gi://Adw';
+import Gio from 'gi://Gio';
+import GLib from 'gi://GLib';
+import GObject from 'gi://GObject';
+import Gst from 'gi://Gst';
+import Gtk from 'gi://Gtk?version=4.0';
 
-var RecordingsDir = Gio.file_new_for_path(GLib.build_filenamev([GLib.get_user_data_dir(), pkg.name]));
-var CacheDir = Gio.file_new_for_path(GLib.build_filenamev([GLib.get_user_cache_dir(), pkg.name]));
-var Settings = new Gio.Settings({ schema: pkg.name });
+export const RecordingsDir = Gio.file_new_for_path(GLib.build_filenamev([GLib.get_user_data_dir(), 
pkg.name]));
+export const CacheDir = Gio.file_new_for_path(GLib.build_filenamev([GLib.get_user_cache_dir(), pkg.name]));
+export const Settings = new Gio.Settings({ schema: pkg.name });
 
-const { Window } = imports.window;
+import { Window } from './window.js';
 
-var Application = GObject.registerClass(class Application extends Adw.Application {
+export const Application = GObject.registerClass(class Application extends Adw.Application {
     _init() {
         super._init({ application_id: pkg.name, resource_base_path: '/org/gnome/SoundRecorder/' });
         GLib.set_application_name(_('Sound Recorder'));
@@ -95,7 +100,7 @@ var Application = GObject.registerClass(class Application extends Adw.Applicatio
             RecordingsDir.make_directory_with_parents(null);
         } catch (e) {
             if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
-                error(`Failed to create directory ${e}`);
+                console.error(`Failed to create directory ${e}`);
 
         }
         this._initAppMenu();
diff --git a/src/main.js b/src/main.js
index bb49874..33315dc 100644
--- a/src/main.js
+++ b/src/main.js
@@ -28,21 +28,9 @@
 
 pkg.initGettext();
 pkg.initFormat();
-pkg.require({
-    'Gdk': '4.0',
-    'GdkPixbuf': '2.0',
-    'GLib': '2.0',
-    'GObject': '2.0',
-    'Gtk': '4.0',
-    'Gst': '1.0',
-    'GstAudio': '1.0',
-    'GstPlayer': '1.0',
-    'GstPbutils': '1.0',
-    'Adw': '1',
-});
 
-const { Application } = imports.application;
+import { Application } from './application.js';
 
-function main(argv) {
+export function main(argv) {
     return new Application().run(argv);
 }
diff --git a/src/meson.build b/src/meson.build
index 64c043c..f5a2bb2 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -1,4 +1,5 @@
 app_conf = configuration_data()
+app_conf.set('profile', profile)
 app_conf.set('prefix', sound_recorder_prefix)
 app_conf.set('libdir', sound_recorder_libdir)
 app_conf.set('PACKAGE_VERSION', meson.project_version())
diff --git a/src/org.gnome.SoundRecorder.in b/src/org.gnome.SoundRecorder.in
index 4a14c67..aaca661 100755
--- a/src/org.gnome.SoundRecorder.in
+++ b/src/org.gnome.SoundRecorder.in
@@ -1,8 +1,20 @@
-#!@GJS@
+#!@GJS@ -m
+import GLib from 'gi://GLib';
+import System from 'system'
+
 imports.package.init({
     name: "@APPLICATION_ID@",
     version: "@PACKAGE_VERSION@",
     prefix: "@prefix@",
     libdir: "@libdir@"
 });
-imports.package.run(imports.main);
+
+const loop = new GLib.MainLoop(null, false);
+const main = await import('resource:///org/gnome/SoundRecorder@profile@/js/main.js');
+GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
+  loop.quit();
+  const exitCode = imports.package.run(main);
+  System.exit(exitCode);
+  return GLib.SOURCE_REMOVE;
+});
+loop.run();
\ No newline at end of file
diff --git a/src/recorder.js b/src/recorder.js
index 2987e04..962aec7 100644
--- a/src/recorder.js
+++ b/src/recorder.js
@@ -17,12 +17,17 @@
  *  Author: Meg Ford <megford gnome org>
  *
  */
-const { GLib, GObject, Gst, GstPbutils } = imports.gi;
-const { RecordingsDir, Settings } = imports.application;
-const { Recording } = imports.recording;
+
+import GLib from 'gi://GLib';
+import GObject from 'gi://GObject';
+import Gst from 'gi://Gst';
+import GstPbutils from 'gi://GstPbutils';
+
+import { RecordingsDir, Settings } from './application.js';
+import { Recording } from './recording.js';
 
 // All supported encoding profiles.
-var EncodingProfiles = [
+export const EncodingProfiles = [
     { name: 'VORBIS',
         containerCaps: 'application/ogg;audio/ogg;video/ogg',
         audioCaps: 'audio/x-vorbis',
@@ -59,7 +64,7 @@ var AudioChannels = {
     1: { name: 'mono', channels: 1 },
 };
 
-var Recorder = new GObject.registerClass({
+export const Recorder = new GObject.registerClass({
     Properties: {
         'duration': GObject.ParamSpec.int(
             'duration',
diff --git a/src/recorderWidget.js b/src/recorderWidget.js
index d40e5d3..7b43e0e 100644
--- a/src/recorderWidget.js
+++ b/src/recorderWidget.js
@@ -1,7 +1,10 @@
 /* exported RecorderState RecorderWidget */
-const { Gio, GObject, Gtk } = imports.gi;
-const { formatTime } = imports.utils;
-const { WaveForm, WaveType } = imports.waveform;
+import Gio from 'gi://Gio';
+import GObject from 'gi://GObject';
+import Gtk from 'gi://Gtk?version=4.0';
+
+import { formatTime } from './utils.js';
+import { WaveForm, WaveType } from  './waveform.js';
 
 var RecorderState = {
     RECORDING: 0,
@@ -9,7 +12,7 @@ var RecorderState = {
     STOPPED: 2,
 };
 
-var RecorderWidget = GObject.registerClass({
+export const RecorderWidget = GObject.registerClass({
     Template: 'resource:///org/gnome/SoundRecorder/ui/recorder.ui',
     InternalChildren: [
         'recorderBox', 'playbackStack', 'recorderTime',
diff --git a/src/recording.js b/src/recording.js
index 9a05f2c..b26d8ae 100644
--- a/src/recording.js
+++ b/src/recording.js
@@ -1,10 +1,14 @@
 /* exported Recording */
-const { Gio, GLib, GObject, Gst, GstPbutils } = imports.gi;
-const { CacheDir } = imports.application;
-const ByteArray = imports.byteArray;
-const Recorder = imports.recorder;
+import Gio from 'gi://Gio';
+import GLib from 'gi://GLib';
+import GObject from 'gi://GObject';
+import Gst from 'gi://Gst';
+import GstPbutils from 'gi://GstPbutils';
 
-var Recording = new GObject.registerClass({
+import { CacheDir } from './application.js';
+import { EncodingProfiles } from './recorder.js';
+
+export const Recording = new GObject.registerClass({
     Signals: {
         'peaks-updated': {},
         'peaks-loading': {},
@@ -31,7 +35,7 @@ var Recording = new GObject.registerClass({
         let info = file.query_info('time::created,time::modified,standard::content-type', 0, null);
         const contentType = info.get_attribute_string('standard::content-type');
 
-        for (let profile of Recorder.EncodingProfiles) {
+        for (let profile of EncodingProfiles) {
             if (profile.contentType === contentType) {
                 this._extension = profile.extension;
                 break;
@@ -131,7 +135,8 @@ var Recording = new GObject.registerClass({
             this.waveformCache.load_bytes_async(null, (obj, res) => {
                 const bytes = obj.load_bytes_finish(res)[0];
                 try {
-                    this._peaks = JSON.parse(ByteArray.toString(bytes.get_data()));
+                    let decoder = new TextDecoder('utf-8');
+                    this._peaks = JSON.parse(decoder.decode(bytes.get_data()));
                     this.emit('peaks-updated');
                 } catch (error) {
                     log(`Error reading waveform data file: ${this.name}_data`);
diff --git a/src/recordingList.js b/src/recordingList.js
index f747ab7..cf54215 100644
--- a/src/recordingList.js
+++ b/src/recordingList.js
@@ -1,10 +1,12 @@
 /* exported RecordingList */
-const { Gio, GLib, GObject } = imports.gi;
+import Gio from 'gi://Gio';
+import GLib from 'gi://GLib';
+import GObject from 'gi://GObject';
 
-const { RecordingsDir } = imports.application;
-const { Recording } = imports.recording;
+import { RecordingsDir } from './application.js';
+import { Recording } from './recording.js';
 
-var RecordingList = new GObject.registerClass(class RecordingList extends Gio.ListStore {
+export const RecordingList = new GObject.registerClass(class RecordingList extends Gio.ListStore {
     _init() {
         super._init({ });
 
@@ -65,7 +67,7 @@ var RecordingList = new GObject.registerClass(class RecordingList extends Gio.Li
                                 this.dirMonitor.emit_event(dest, src, Gio.FileMonitorEvent.MOVED_IN);
                             } catch (e) {
                                 if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED)) {
-                                    error(`Failed to copy recording ${name} to the new location`);
+                                    console.error(`Failed to copy recording ${name} to the new location`);
                                     log(e);
                                 }
                                 allCopied = false;
@@ -89,7 +91,7 @@ var RecordingList = new GObject.registerClass(class RecordingList extends Gio.Li
                 }
             } catch (e) {
                 if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
-                    error(`Failed to copy old  recordings ${e}`);
+                    console.error(`Failed to copy old  recordings ${e}`);
 
             }
         }.bind(this);
@@ -120,7 +122,7 @@ var RecordingList = new GObject.registerClass(class RecordingList extends Gio.Li
             }
         } catch (e) {
             if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
-                error(`Failed to load recordings ${e}`);
+                console.error(`Failed to load recordings ${e}`);
 
         }
     }
diff --git a/src/recordingListWidget.js b/src/recordingListWidget.js
index c0bce5b..819f89d 100644
--- a/src/recordingListWidget.js
+++ b/src/recordingListWidget.js
@@ -1,8 +1,13 @@
 /* exported RecordingsListWidget */
-const { Adw, GObject, GstPlayer, Gtk, Gst } = imports.gi;
-const { Row, RowState } = imports.row;
+import Adw from 'gi://Adw'
+import GObject from 'gi://GObject'
+import Gst from 'gi://Gst'
+import GstPlayer from 'gi://GstPlayer'
+import Gtk from 'gi://Gtk?version=4.0'
 
-var RecordingsListWidget = new GObject.registerClass({
+import { Row, RowState } from './row.js';
+
+export const RecordingsListWidget = new GObject.registerClass({
     Signals: {
         'row-deleted': { param_types: [GObject.TYPE_OBJECT, GObject.TYPE_INT] },
     },
diff --git a/src/row.js b/src/row.js
index 425a3c8..ee8c37a 100644
--- a/src/row.js
+++ b/src/row.js
@@ -1,14 +1,18 @@
 /* exported Row */
-const { Gdk, Gio, GObject, Gtk } = imports.gi;
-const { displayDateTime, formatTime } = imports.utils;
-const { WaveForm, WaveType } = imports.waveform;
+import Gdk from 'gi://Gdk?version=4.0'
+import Gio from 'gi://Gio'
+import GObject from 'gi://GObject'
+import Gtk from 'gi://Gtk?version=4.0'
 
-var RowState = {
+import { displayDateTime, formatTime } from './utils.js';
+import { WaveForm, WaveType } from './waveform.js';
+
+export const RowState = {
     PLAYING: 0,
     PAUSED: 1,
 };
 
-var Row = GObject.registerClass({
+export const Row = GObject.registerClass({
     Template: 'resource:///org/gnome/SoundRecorder/ui/row.ui',
     InternalChildren: [
         'playbackStack', 'mainStack', 'waveformStack', 'rightStack',
diff --git a/src/utils.js b/src/utils.js
index b5d6b83..7398954 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -17,10 +17,11 @@
  * Author: Meg Ford <megford gnome org>
  *
  */
-const Gettext = imports.gettext;
-const { GLib, Gst } = imports.gi;
+import Gettext from 'gettext'
+import GLib from 'gi://GLib'
+import Gst from 'gi://Gst'
 
-var formatTime = nanoSeconds => {
+export const formatTime = nanoSeconds => {
     const time = new Date(0, 0, 0, 0, 0, 0, parseInt(nanoSeconds / Gst.MSECOND));
 
     const miliseconds = parseInt(time.getMilliseconds() / 100).toString();
@@ -32,7 +33,7 @@ var formatTime = nanoSeconds => {
     return `${hours} ∶ ${minutes} ∶ ${seconds} . <small>${miliseconds}</small>`;
 };
 
-var displayDateTime = time => {
+export const displayDateTime = time => {
     const DAY = 86400000000;
     const now = GLib.DateTime.new_now_local();
     const difference = now.difference(time);
diff --git a/src/waveform.js b/src/waveform.js
index 5ea2c3d..b2109bd 100644
--- a/src/waveform.js
+++ b/src/waveform.js
@@ -23,17 +23,19 @@
 
 // based on code from Pitivi
 
-const { Adw, GObject, Gtk } = imports.gi;
-const Cairo = imports.cairo;
+import Adw from 'gi://Adw';
+import GObject from 'gi://GObject';
+import Gtk from 'gi://Gtk?version=4.0';
+import Cairo from 'cairo';
 
-var WaveType = {
+export const WaveType = {
     RECORDER: 0,
     PLAYER: 1,
 };
 
 const GUTTER = 4;
 
-var WaveForm = GObject.registerClass({
+export const WaveForm = GObject.registerClass({
     Properties: {
         'position': GObject.ParamSpec.float(
             'position',
diff --git a/src/window.js b/src/window.js
index 8d2d054..7b6b0c1 100644
--- a/src/window.js
+++ b/src/window.js
@@ -18,12 +18,18 @@
 *
 */
 
-const { Adw, Gio, GLib, GObject, Gst, GstPlayer, Gtk } = imports.gi;
-
-const { Recorder } = imports.recorder;
-const { RecordingList } = imports.recordingList;
-const { RecordingsListWidget } = imports.recordingListWidget;
-const { RecorderWidget } = imports.recorderWidget;
+import Adw from 'gi://Adw'
+import Gio from 'gi://Gio'
+import GLib from 'gi://GLib'
+import GObject from 'gi://GObject'
+import Gst from 'gi://Gst'
+import GstPlayer from 'gi://GstPlayer'
+import Gtk from 'gi://Gtk?version=4.0'
+
+import { Recorder } from './recorder.js';
+import { RecordingList } from './recordingList.js';
+import { RecordingsListWidget } from './recordingListWidget.js';
+import { RecorderWidget } from './recorderWidget.js';
 
 var WindowState = {
     EMPTY: 0,
@@ -31,7 +37,7 @@ var WindowState = {
     RECORDER: 2,
 };
 
-var Window = GObject.registerClass({
+export const Window = GObject.registerClass({
     Template: 'resource:///org/gnome/SoundRecorder/ui/window.ui',
     InternalChildren: [
         'mainStack', 'emptyPage', 'column', 'headerRevealer', 'toastOverlay',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]