0.4.1 • Published 11 months ago

@glue42/web-gns v0.4.1

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
-
Last release
11 months ago

GNS Web

This is a Glue42 Core plugin designed to be provided to the Glue42 Web Platform factory function.

Initialization

The GNS Web initialization config has the following interface:

export interface StompProviderConfig {
    type: "stomp";
    url: string;
    recipient?: string;
    authorization?: () => Promise<string>;
    // Defaults to false. When set to true all calls to get a snapshot will NOT request a snapshot from the providers, but
    // will return 2 fake notifications from a fake in-memory store
    useFakeStore?: boolean;
    // Defaults not undefined. If provided, when a snapshot is requested, the elements of this array will be streamed as snapshot batches
    fakeStore?: Array<any>;
    // Defaults to 60000. This is the time which GNS will wait for a STOMP provider to accept the connection or reconnection.
    connectionTimeoutMS?: number;
}

export interface GnsWebConfig {
    providers: Array<StompProviderConfig>;
    showToasts?: boolean;
    showToastActions?: boolean;
    autoRequestPermission?: boolean;
    formatNotificationBody?: (notificationData: any) => Promise<string>;
    focusPlatformOnDefaultClick?: (notificationData: any) => boolean;
}

The only required property is providers, which must include at least one provider. The plugin right now supports only one provider, which must be of type "stomp".

Initializing the plugin:

const plugins = {
    definitions: [
        {
            name: "gns.web",
            start: window.GNSWeb,
            config: {
                providers: [
                    {
                        type: "stomp",
                        url: "http://localhost:8084/",
                        recipient: "U000001"
                    }
                ]
            }
        }
    ]
};

const config = {
    glue: {
        // this defines a handler which will be executed once the browser toast is clicked
        notifications: {
            defaultClick: (glue, data) => {
                console.log(glue.version);
                console.log(data);
            }
        }
    },
    plugins
};

GlueWebPlatform(config)
    .then((data) => {
        window.glue = data.glue;
    })
    .then(() => console.log("done"))
    .catch(console.warn);

Other options:

const config = {
    providers: [
        {
            type: "stomp",
            url: "http://localhost:8084/",
            recipient: "U000001"
        }
    ],
    // toggles whether or not to display the browser toasts, defaults to true
    showToasts: true
    // toggles whether or not to display the actions buttons in the browser toast, defaults to true, but right now GNS Web does not support actions
    showToastActions: false, 
    // toggles to whether or not to automatically request notifications permission from the user as soon as a notification toast needs to be raised, defaults to true
    autoRequestPermission: false,
    // a callback which when defined receives the notification data and returns a Promise<string>, which will be used as a body of the notification toast.
    formatNotificationBody: async (data) => `${data.description.toLowerCase()}`,
    // a predicate which indicates if the platform app must be focused, when he notification toast is clicked. Accepts the notification object and must return a boolean, if missing, the platform app will not be focused on a default toast click
    focusPlatformOnDefaultClick: (data) => true
}

Exposed Streams

There are two exposed Glue42 Streams: T42.GNS.Notifications and T42.GNS.GetNotifications.

Subscribing to T42.GNS.Notifications will allow the user to receive new notifications from the providers by defining a onData handler. Optionally an arguments object can be provided, which will instruct GNS Web to start by streaming a snapshot of the existing notifications, filtered by the provided limit and optionally a since property.

Usage:

// will only receive new notifications
glue.interop.subscribe("T42.GNS.Notifications").then((sub) => sub.onData(console.log));

// will receive snapshot batches and new notifications
glue.interop.subscribe("T42.GNS.Notifications", { arguments: { snapshot: { limit: 10, since: 12983671298 } } }).then((sub) => sub.onData(console.log));

Subscribing to T42.GNS.GetNotifications will stream to the user the existing notifications, filtered by the limit and since properties. Once a snapshot-end message is send, the subscriber's connection to the stream will be closed.

Usage:

glue.interop.subscribe("T42.GNS.GetNotifications", { arguments: { snapshot: { limit: 10, since: 12983671298 } } }).then((sub) => sub.onData(console.log));

Exposed Methods

GNS Web registered a single Glue42 Interop method, called T42.GNS.Control. This method is designed to be a single point for receiving control instructions from clients.

At the moments a single control type is supported: snooze.

Usage:

// The payload's id must be the notification id

glue.interop.invoke("T42.GNS.Control", {type: "snooze", payload: {id: "asdasdasd", until: 19821739812}}).then(console.log);

The id property is required, but the until property is not and it signals until when the notification should be snoozed, if not provided the notification will be snoozed for 10 minutes.

Debug

By setting the log level to "verbose" in the chrome console, you can see debug messages which show all the messages send to and received from the stomp provider.