Skip to content

Introduction

Like most frameworks, it receives frontend requests, organizes parameters, and calls Services to handle business logic. (Not a singleton)

It is the entry point for frontend rendering layer access and cannot be called from other business layers.

Example

javascript
/**
 * Example controller
 * @class
 */
class ExampleController {

  /**
   * All methods receive two parameters
   * @param args Parameters passed from the frontend
   * @param event - Only has a value during communication
   */

  /**
   * test
   */
  async test (args, event) {

    // Frontend parameters
    const params = args;

    // Call service
    const result = await exampleService.test('electron');

    // Proactively send a request to the frontend
    // channel is the route monitored by frontend ipc.on()
    const channel = "controller/example/something"
    // IpcMainInvokeEvent
    event.reply(channel, {age:21})
    // IpcMainEvent
    event.sender.send(`${channel}`, data)

    // Return data
    const data = {}
    return data;
  }
}

args

Parameters passed from the frontend

event

Only has a value during IPC communication, and the value is a dynamic object;

  • When the frontend uses the invoke() method, event equals IpcMainInvokeEvent
  • When the frontend uses send()/sendSync() methods, event equals IpcMainEvent
  • When an HTTP server is enabled, it is the Koa ctx object

Description below:

event - IpcMainInvokeEvent

When the frontend uses the invoke() method, event == IpcMainInvokeEvent

See: Detailed Description

event - IpcMainEvent

When the frontend uses send()/sendSync() methods, event == IpcMainEvent

See: Detailed Description