Tutorial: View.EventEmitter

View.EventEmitter

When an instance of fin.View is created, it inherits an EventEmitter with the below methods so that it is possible to listen to OpenFin events. The below methods are asynchronous as they must cross process boundaries and setup the listener in the browser process. When the EventEmitter receives an event from the browser process and emits on the renderer, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and will be discarded. If the execution context of the window is destroyed by page navigation or reload, any events that have been setup in that context will be destroyed. It is important to keep in mind that when an ordinary listener function is called, the standard this keyword is intentionally set to reference the EventEmitter instance to which the listener is attached. It is possible to use ES6 Arrow Functions as listeners, however, when doing so, the this keyword will no longer reference the EventEmitter instance.

addListener(event, listener)

Adds a listener to the end of the listeners array for the specified event.

const view = await fin.View.getCurrent();

view.addListener("closed", function(event) {
    console.log("The View has closed.");
});

on(event, listener)

Adds a listener to the end of the listeners array for the specified event.

const view = await fin.View.getCurrent();

view.on("closed", function(event) {
    console.log("The View has closed.");
});

once(event, listener)

Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed.

const view = await fin.View.getCurrent();

view.once("closed", function(event) {
    console.log("The View has closed.");
});

prependListener(event, listener)

Adds a listener to the beginning of the listeners array for the specified event.

const view = await fin.View.getCurrent();

view.prependListener("closed", function(event) {
    console.log("The View has closed.");
});

prependOnceListener(event, listener)

Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed. The listener is added to the beginning of the listeners array.

const view = await fin.View.getCurrent();

view.prependListener("closed", function(event) {
    console.log("The View has closed.");
});

removeListener(event, listener)

Remove a listener from the listener array for the specified event. Caution: Calling this method changes the array indices in the listener array behind the listener.

const view = await fin.View.getCurrent();
const callback = function(event) {
  console.log('The View closed');
};

view.on('closed', callback);
view.removeListener("closed", callback);

removeAllListeners([event])

Removes all listeners, or those of the specified event.

const view = await fin.View.getCurrent();
view.removeAllListeners("closed");

Supported View event types

  • crashed
  • created
  • destroyed
  • did-change-theme-color
  • file-download-completed
  • file-download-progress
  • file-download-started
  • hidden
  • hotkey
  • navigation-rejected
  • options-changed
  • page-favicon-updated
  • page-title-updated
  • resource-load-failed
  • resource-response-received
  • shown
  • target-changed

View Events

crashed

Generated when a view has crashed.

//This response has the following shape:
{
    name: "ViewName" // the name of the View
    reason: "killed", //possible values:
                      //  "normal-termination"    zero exit status
                      //  "abnormal-termination"  non-zero exit status
                      //  "killed"                e.g. SIGKILL or task manager kill
                      //  "crashed"               e.g. Segmentation fault
                      //  "still-running"         child hasn't exited yet
                      //  "launch-failed"         child process never launched
                      //  "out-of-memory"         process died due to oom
    topic: "view",
    type: "crashed",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // (string) the UUID of the Application the View belongs to.
}

created

Generated when a View is created.

//This response has the following shape:
{
    name: "ViewName" // the name of the View
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View is attached to
    topic: "view",
    type: "created",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // (string) the UUID of the Application the View belongs to.
}

destroyed

Generated when a View is destroyed.

//This response has the following shape:
{
    name: "ViewName" // the name of the View
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View was attached to
    topic: "view",
    type: "destroyed",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // (string) the UUID of the Application the View belongs to.
}

did-change-theme-color

Emitted when a page's theme color changes. This is usually due to encountering a meta tag:

{
    color: "#FF0000",
    name: "ViewName" // the name of the View
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View is attached to
    topic: "view",
    type: "did-change-theme-color",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // (string) the UUID of the Application the View belongs to.
}

file-download-completed

Generated when a file download has completed.

//This response has the following shape:
{
    type: "file-download-completed",
    state: "started" | "completed" | "cancelled" |  "interrupted" | "paused"
    url: "https://download-location/file.pdf", //the url where the file is being downloaded from.
    mimeType: "application/pdf", //the file mime type.
    fileName: "file(1).pdf", //the name of the file as chosen by the user.
    originalFileName: "file.pdf", //the original name of the file.
    totalBytes: 347110, //the total size in bytes of the file.
    startTime: 1537994725.335115, //the number of seconds since the UNIX epoch when the download was started.
    contentDisposition: "", //the Content-Disposition field from the response header.
    lastModifiedTime: "Fri, 24 Aug 2018 03:12:32 GMT", //the Last-Modified header value.
    eTag: `W"54be6-16569eb4bad"`, //the ETag header value.
    downloadedBytes: 347110, //the downloaded bytes of the download item.
    topic: "view",
    uuid: "AppUUID", //(string) the UUID of the application the View belongs to.
    name: "viewOne" //the name of the View.
}

file-download-progress

Generated during file download progress.

//This response has the following shape:
{
    type: "file-download-progress",
    state: "started" | "completed" | "cancelled" |  "interrupted" | "paused"
    url: "https://download-location/file.pdf", //the url where the file is being downloaded from.
    mimeType: "application/pdf", //the file mime type.
    fileName: null, //the name of the file as chosen by the user, can be null for progress events.
    originalFileName: "file.pdf", //the original name of the file.
    totalBytes: 347110, //the total size in bytes of the file.
    startTime: 1537994725.335115, //the number of seconds since the UNIX epoch when the download was started.
    contentDisposition: "", //the Content-Disposition field from the response header.
    lastModifiedTime: "Fri, 24 Aug 2018 03:12:32 GMT", //the Last-Modified header value.
    eTag: `W"54be6-16569eb4bad"`, //the ETag header value.
    downloadedBytes: 23820, //the downloaded bytes of the download item.
    topic: "view",
    uuid: "AppUUID", //(string) the UUID of the application the View belongs to.
    name: "viewOne" //the name of the View.
}

file-download-started

Generated when a file download has started.

//This response has the following shape:
{
    type: "file-download-started",
    state: "started" | "completed" | "cancelled" |  "interrupted" | "paused"
    url: "https://download-location/file.pdf", //the url where the file is being downloaded from.
    mimeType: "application/pdf", //the file mime type.
    fileName: null, //the name of the file as chosen by the user, will be null for started events
    originalFileName: "file.pdf", //the original name of the file.
    totalBytes: 347110, //the total size in bytes of the file.
    startTime: 1537994725.335115, //the number of seconds since the UNIX epoch when the download was started.
    contentDisposition: "", //the Content-Disposition field from the response header.
    lastModifiedTime: "Fri, 24 Aug 2018 03:12:32 GMT", //the Last-Modified header value.
    eTag: `W"54be6-16569eb4bad"`, //the ETag header value.
    downloadedBytes: 23820, //the downloaded bytes of the download item.
    topic: "view",
    uuid: "AppUUID", //(string) the UUID of the application the View belongs to.
    name: "viewOne" //the name of the View.
}

hidden

Generated when a View is hidden.

//This response has the following shape:
{
    name: "ViewName" // the name of the View
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View is attached to
    topic: "view",
    type: "hidden",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // (string) the UUID of the Application the View belongs to.
}

hotkey

Generated when a keyboard shortcut defined in the hotkeys array in View options is pressed inside the view. For reference on keyboard event properties see KeyboardEvent.

//This response has the following shape:
{
    name: "ViewName", // the name of the View
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View is attached to
    topic: "view",
    type: "hotkey",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52", // (string) the UUID of the Application the View belongs to.
    altKey: false,
    code: "KeyT",
    ctrlKey: true,
    inputType: "keyUp", // "keyUp" or "keyDown"
    key: "t",
    metaKey: false,
    repeat: false,
    shiftKey: false
}

navigation-rejected

Generated when view navigation is rejected as per contentNavigation whitelist/blacklist rules.

{
    name: "view1", //the name of the view.
    topic: "view",
    type: "navigation-rejected",
    url: "http://blocked-content.url",
    uuid: "AppUUID" // (string) the UUID of the Application the View belongs to.
}

options-changed

Generated when a view's options are updated.

{
    diff: { autoResize: { // The diff shows which options were updated, and what their old and new values are
        oldVal: {horizontal: false, width: false, vertical: false, height: false},
        newVal: {horizontal: true, width: true},
    },
    invalidOptions: ["notARealOption"], // Array of invalid options
    name: "view1", //the name of the view.
    options: { // The updated options object.
        autoResize: {width: true, height: false, horizontal: false, vertical: false},
        backgroundColor: "#FFF"
        // ...
    },
    topic: "view",
    type: "options-changed",
    uuid: "AppUUID" // (string) the UUID of the Application the View belongs to.
}

page-favicon-updated

Emitted when page receives favicon urls.

{
    favicons: [
        "http://www.openfin.co/favicon.ico"
    ],
    name: "ViewName" // the name of the View
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View is attached to
    topic: "view",
    type: "page-favicon-updated",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // (string) the UUID of the Application the View belongs to.
}

page-title-updated

Fired when page title is set during navigation. explicitSet is false when title is synthesized from file url.

{
    explicitSet: true, // false when title is synthesized from file url.
    name: "ViewName" // the name of the View
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View is attached to
    title: "testTitle",
    topic: "view",
    type: "page-title-updated",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // (string) the UUID of the Application the View belongs to.
}

resource-load-failed

Generated when an HTTP load was cancelled or failed.

{
    name: "ViewName",
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View is attached to
    topic: "view",
    type: "resource-response-failed",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52", // (string) the UUID of the Application the View belongs to.
    errorCode: -102,                              //the Chromium error code
    errorDescription: "",
    validatedURL: "http://bad-domain/dead-link.html",
    isMainFrame: true
}

resource-response-received

Generated when an HTTP resource request has received response details.

{
    name: "ViewName",
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View is attached to
    topic: "view",
    type: "resource-response-received",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52", // (string) the UUID of the Application the View belongs to.
    status: false,
    newUrl: "https://www.google.com/?gws_rd=ssl", //the URL of the responded resource
    originalUrl: "http://www.google.com/",        //the requested URL
    httpResponseCode: 200,
    requestMethod: "GET",
    referrer: "",                                 //the URL of the referrer
    headers: {                                    //HTTP response headers
        "cache-control": [
            "private, max-age=0"
        ],
        "content-type": [
            "text/html; charset=UTF-8"
        ]
    },
    resourceType: "mainFrame"                     //"mainFrame", "subFrame",
                                                  //"styleSheet", "script", "image",
                                                  //"object", "xhr", or "other"
}

shown

Generated when a View is shown. This event will fire during creation of a View.

//This response has the following shape:
{
    name: "ViewName" // the name of the View
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View is attached to
    topic: "view",
    type: "shown",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // (string) the UUID of the Application the View belongs to.
}

target-changed

Generated when a View changes its Window target. This event will fire during creation of a View. In that case, previousTarget identity will be the same as target identity.

//This response has the following shape:
{
    name: "ViewName" // the name of the View
    previousTarget: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'previousWindowName'}, // the Window this View was previously attached to
    target: {uuid: '454C7F31-A915-4EA2-83F2-CFA655453C52', name: 'windowName'}, // the Window this View will attach to
    topic: "view",
    type: "target-changed",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // (string) the UUID of the Application the View belongs to.
}