Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MessageService

Service to log and categorize messages, show progress information and offer actions.

The messages are processed by this service and forwarded to an injected MessageClient. For example "@theia/messages" provides such a client, rendering messages as notifications in the frontend.

Example usage

  @inject(MessageService)
  protected readonly messageService: MessageService;

  messageService.warn("Typings not available");

  messageService.error("Could not restore state", ["Rollback", "Ignore"])
  .then(action => action === "Rollback" && rollback());

Hierarchy

  • MessageService

Index

Constructors

constructor

Properties

Protected Readonly client

Private counter

counter: number = 0

Private progressIdPrefix

progressIdPrefix: string = Math.random().toString(36).substring(5)

Methods

error

  • error<T>(message: string, ...actions: T[]): Promise<T | undefined>
  • error<T>(message: string, options?: MessageOptions, ...actions: T[]): Promise<T | undefined>
  • Logs the message as "error" and, if given, offers actions to act on it.

    Type parameters

    • T: string

    Parameters

    • message: string

      the message to log.

    • Rest ...actions: T[]

      the actions to offer. Can be omitted.

    Returns Promise<T | undefined>

    the selected action if there is any, undefined when there was no action or none was selected.

  • Logs the message as "error" and, if given, offers actions to act on it.

    Type parameters

    • T: string

    Parameters

    • message: string

      the message to log.

    • Optional options: MessageOptions

      additional options. Can be omitted

    • Rest ...actions: T[]

      the actions to offer. Can be omitted.

    Returns Promise<T | undefined>

    the selected action if there is any, undefined when there was no action or none was selected.

info

  • info<T>(message: string, ...actions: T[]): Promise<T | undefined>
  • info<T>(message: string, options?: MessageOptions, ...actions: T[]): Promise<T | undefined>
  • Logs the message as "info" and, if given, offers actions to act on it.

    Type parameters

    • T: string

    Parameters

    • message: string

      the message to log.

    • Rest ...actions: T[]

      the actions to offer. Can be omitted.

    Returns Promise<T | undefined>

    the selected action if there is any, undefined when there was no action or none was selected.

  • Logs the message as "info" and, if given, offers actions to act on it.

    Type parameters

    • T: string

    Parameters

    • message: string

      the message to log.

    • Optional options: MessageOptions

      additional options. Can be omitted

    • Rest ...actions: T[]

      the actions to offer. Can be omitted.

    Returns Promise<T | undefined>

    the selected action if there is any, undefined when there was no action or none was selected.

log

  • log<T>(message: string, ...actions: T[]): Promise<T | undefined>
  • log<T>(message: string, options?: MessageOptions, ...actions: T[]): Promise<T | undefined>
  • Logs the message and, if given, offers actions to act on it.

    Type parameters

    • T: string

    Parameters

    • message: string

      the message to log.

    • Rest ...actions: T[]

      the actions to offer. Can be omitted.

    Returns Promise<T | undefined>

    the selected action if there is any, undefined when there was no action or none was selected.

  • Logs the message and, if given, offers actions to act on it.

    Type parameters

    • T: string

    Parameters

    • message: string

      the message to log.

    • Optional options: MessageOptions

      additional options. Can be omitted

    • Rest ...actions: T[]

      the actions to offer. Can be omitted.

    Returns Promise<T | undefined>

    the selected action if there is any, undefined when there was no action or none was selected.

Protected newProgressId

  • newProgressId(): string

Protected processMessage

  • processMessage(type: MessageType, text: string, args?: any[]): Promise<string | undefined>

showProgress

  • Shows the given message as a progress.

    Parameters

    • message: ProgressMessage

      the message to show for the progress.

    • Optional onDidCancel: undefined | (() => void)

      an optional callback which will be invoked if the progress indicator was canceled.

    Returns Promise<Progress>

    a promise resolving to a Progress object with which the progress can be updated.

    Example usage

      @inject(MessageService)
      protected readonly messageService: MessageService;
    
      // this will show "Progress" as a cancelable message
      this.messageService.showProgress({text: 'Progress'});
    
      // this will show "Rolling back" with "Cancel" and an additional "Skip" action
      this.messageService.showProgress({
        text: `Rolling back`,
        actions: ["Skip"],
      },
      () => console.log("canceled"))
      .then((progress) => {
        // register if interested in the result (only necessary for custom actions)
        progress.result.then((result) => {
          // will be 'Cancel', 'Skip' or `undefined`
          console.log("result is", result);
        });
        progress.report({message: "Cleaning references", work: {done: 10, total: 100}});
        progress.report({message: "Restoring previous state", work: {done: 80, total: 100}});
        progress.report({message: "Complete", work: {done: 100, total: 100}});
        // we are done so we can cancel the progress message, note that this will also invoke `onDidCancel`
        progress.cancel();
      });

warn

  • warn<T>(message: string, ...actions: T[]): Promise<T | undefined>
  • warn<T>(message: string, options?: MessageOptions, ...actions: T[]): Promise<T | undefined>
  • Logs the message as "warning" and, if given, offers actions to act on it.

    Type parameters

    • T: string

    Parameters

    • message: string

      the message to log.

    • Rest ...actions: T[]

      the actions to offer. Can be omitted.

    Returns Promise<T | undefined>

    the selected action if there is any, undefined when there was no action or none was selected.

  • Logs the message as "warning" and, if given, offers actions to act on it.

    Type parameters

    • T: string

    Parameters

    • message: string

      the message to log.

    • Optional options: MessageOptions

      additional options. Can be omitted

    • Rest ...actions: T[]

      the actions to offer. Can be omitted.

    Returns Promise<T | undefined>

    the selected action if there is any, undefined when there was no action or none was selected.