This document explains how to use the useNotify composable to emit notification events through the shared UiEventService, using Quasar's QNotifyCreateOptions.
The notification system provides a centralized event-based approach to emit notifications across all modules. It ensures that all modules can emit and listen to notifications consistently through a shared event stream.
The useNotify composable allows you to emit notification events with a QNotifyCreateOptions payload, which can be consumed by any observer subscribed to the UiEventService.
Notifications go through uiEventSubject, the shared UI event bus described in
UI Event Service:
import { uiEventSubject } from '@linagora/lin-pulsar-core';
// Subscribe to all notification events
uiEventSubject.subscribe((event) => {
if (event.key === 'notify') {
console.log('Notification:', event.data);
}
});- All notification events have
key: 'notify'. - Ensures a consistent event flow across modules.
Each notification event follows the UiEvent interface, whose data carries the QNotifyCreateOptions payload:
interface UiEvent {
key: string;
data: unknown;
}data is typed unknown: subscribers cast it to QNotifyCreateOptions once they have checked that key is
'notify'.
The useNotify() composable provides a Notify function to emit notification events.
import { useNotify } from '@linagora/lin-pulsar-core';
import type { QNotifyCreateOptions } from 'quasar';
const { Notify } = useNotify();- The
Notifyfunction emits events through the shareduiEventSubject. - Can be used anywhere in your Vue components or services.
The Notify function accepts a single parameter:
- data (
QNotifyCreateOptions): The notification payload, as expected by Quasar's$q.notify.
Notify({ type: 'positive', message: 'User profile updated successfully!' });Notify({
type: 'negative',
message: 'Failed to save changes',
caption: 'Network connection lost',
color: 'red',
timeout: 5000,
});Notify({
type: 'info',
message: 'New features are available. Check the changelog.',
});Notify({
type: 'warning',
message: 'Your session will expire soon',
timeout: 10000,
});<template>
<q-btn
@click="handleNotify"
label="Show notification"
/>
</template>
<script setup lang="ts">
import { useNotify } from '@linagora/lin-pulsar-core';
const { Notify } = useNotify();
function handleNotify() {
Notify({ type: 'positive', message: 'Hello from Notify!' });
}
</script>- Use consistent notification types: Stick to standard types like
'positive','negative','info', and'warning'(as per Quasar). - Provide meaningful messages: Make sure the data payload contains clear, user-friendly messages.
- Centralize notification handling: Create a single component or service to subscribe to notifications and display them consistently.
- Structured payloads: For complex notifications, use the
QNotifyCreateOptionsfields such asmessage,captionandactions. - Avoid notification spam: Don't emit multiple notifications for the same event in quick succession.
The composable is fully typed and ensures type safety:
import { useNotify } from '@linagora/lin-pulsar-core';
import type { QNotifyCreateOptions } from 'quasar';
const { Notify } = useNotify();
// Type-safe notification emission
Notify({ type: 'positive', message: 'Operation completed' });
// With structured data
const errorData: QNotifyCreateOptions = {
type: 'negative',
message: 'An error occurred',
caption: 'Unable to connect to server',
color: 'red',
};
Notify(errorData);- The
dataparameter is typed asQNotifyCreateOptions, allowing any Quasar notification structure. - Return type is
voidas the function emits events without returning values.