Skip to content

Commit

Permalink
add warning message to Utils.notify
Browse files Browse the repository at this point in the history
  • Loading branch information
Aylur committed Jan 14, 2024
1 parent f8a3ec9 commit d243a45
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/service/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
loadInterfaceXML, readFileAsync,
timeout, writeFile,
} from '../utils.js';
import { daemon } from '../utils/notify.js';

const NOTIFICATIONS_CACHE_PATH = `${CACHE_DIR}/notifications`;
const CACHE_FILE = NOTIFICATIONS_CACHE_PATH + '/notifications.json';
Expand Down Expand Up @@ -441,8 +442,11 @@ export class Notifications extends Service {

this._dbus.export(connection, '/org/freedesktop/Notifications');
},
null,
() => {
daemon.running = true;
},
() => {
// TODO: get running daemon's name
print('Another notification daemon is already running, ' +
'make sure you stop Dunst ' +
'or any other daemon you have running');
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const fetch = Fetch.fetch;
export const notify = Notify.notify;

export default {
USER, CACHE_DIR,
exec, execAsync, subprocess,
readFile, readFileAsync, writeFile, monitorFile,
timeout, interval, idle,
Expand Down
37 changes: 24 additions & 13 deletions src/utils/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import GLib from 'gi://GLib?version=2.0';
import { type Urgency } from '../service/notifications.js';

type ClosedReason = ReturnType<typeof _CLOSED_REASON>
type Notification = NonNullable<Awaited<ReturnType<typeof libnotify>>>['Notification']
type Notification = InstanceType<NonNullable<Awaited<ReturnType<typeof libnotify>>>['Notification']>

// TODO: libnotify is sync, so it halts the js engine
// when the notification daemon is in the same process
export const daemon = {
running: false,
};

const _URGENCY = (urgency: Urgency) => {
switch (urgency) {
Expand Down Expand Up @@ -78,7 +84,7 @@ export async function notify(
argsOrSummary: NotificationArgs | string,
body = '',
iconName = '',
) {
): Promise<Notification> {
const Notify = await libnotify();
if (!Notify) {
console.error(Error('missing dependency: libnotify'));
Expand All @@ -87,6 +93,13 @@ export async function notify(
return null as unknown as Notification;
}

// TODO: use Notifications.Notify wrapper if daemon is running
if (daemon.running) {
console.error(Error('Notification Deamon is in the same process ' +
' Utils.notify will freeze. This will be fixed in a future version'));
return null as unknown as Notification;
}

const args = typeof argsOrSummary === 'object'
? argsOrSummary
: {
Expand Down Expand Up @@ -122,16 +135,14 @@ export async function notify(
hint('x', 'i', args.x);
hint('y', 'i', args.y);

return await new Promise(resolve => {
Object.keys(args.actions || {}).forEach((action, i) => {
print(`${i}`, action, args.actions![action]);
n.add_action(`${i}`, action, args.actions![action]);
});
n.connect('closed', () => {
if (args.onClosed)
args.onClosed(_CLOSED_REASON(n.get_closed_reason()));
});
n.show();
resolve(n);
Object.keys(args.actions || {}).forEach((action, i) => {
n.add_action(`${i}`, action, args.actions![action]);
});
n.connect('closed', () => {
if (args.onClosed)
args.onClosed(_CLOSED_REASON(n.get_closed_reason()));
});
n.show();

return n;
}

0 comments on commit d243a45

Please sign in to comment.