or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli-tools.mdconfiguration.mdindex.mdlow-level-communication.mdmodule-setup.mdpush-notifications.mdtesting.mdupdate-management.md
tile.json

low-level-communication.mddocs/

Low-Level Communication

Low-level service worker communication channel providing direct message passing and event handling capabilities for advanced Angular service worker integration.

Capabilities

NgswCommChannel

Core communication channel for direct interaction with the service worker, providing observables for worker state and message handling.

/**
 * Low-level communication channel with the Angular Service Worker
 * Provides direct message passing and event handling capabilities
 */
class NgswCommChannel {
  /**
   * Observable that emits the current ServiceWorker instance
   * Emits when a new service worker becomes active
   */
  readonly worker: Observable<ServiceWorker>;
  
  /**
   * Observable that emits the ServiceWorkerRegistration
   * Provides access to the registration object
   */
  readonly registration: Observable<ServiceWorkerRegistration>;
  
  /**
   * Observable that emits all typed events from the service worker
   * Includes version events, push events, and error states
   */
  readonly events: Observable<TypedEvent>;
  
  /**
   * Whether the service worker is enabled and supported
   */
  get isEnabled(): boolean;
  
  /**
   * Send a message to the service worker without expecting a response
   * @param action - The action type to send
   * @param payload - The message payload
   * @returns Promise that resolves when message is sent
   */
  postMessage(action: string, payload: Object): Promise<void>;
  
  /**
   * Send a message and wait for an operation completion response
   * @param type - The message type
   * @param payload - The message payload  
   * @param operationNonce - Unique identifier for the operation
   * @returns Promise that resolves with operation result
   */
  postMessageWithOperation(
    type: string,
    payload: Object,
    operationNonce: number
  ): Promise<boolean>;
  
  /**
   * Generate a unique nonce for operation tracking
   * @returns Random number for operation identification
   */
  generateNonce(): number;
  
  /**
   * Filter events by type and return typed observable
   * @param type - Event type or array of types to filter
   * @returns Observable of filtered events
   */
  eventsOfType<T extends TypedEvent>(type: T['type'] | T['type'][]): Observable<T>;
  
  /**
   * Get the next event of a specific type
   * @param type - Event type to wait for
   * @returns Observable that emits once and completes
   */
  nextEventOfType<T extends TypedEvent>(type: T['type']): Observable<T>;
  
  /**
   * Wait for an operation to complete by nonce
   * @param nonce - Operation identifier to wait for
   * @returns Promise that resolves with operation success status
   */
  waitForOperationCompleted(nonce: number): Promise<boolean>;
}

Usage Examples:

import { inject } from '@angular/core';
import { NgswCommChannel } from '@angular/service-worker';

@Component({...})
export class AdvancedSwComponent {
  private sw = inject(NgswCommChannel);
  
  ngOnInit() {
    if (this.sw.isEnabled) {
      // Listen for all service worker events
      this.sw.events.subscribe(event => {
        console.log('SW Event:', event);
      });
      
      // Listen for specific event types
      this.sw.eventsOfType('VERSION_READY').subscribe(event => {
        console.log('New version ready:', event);
      });
      
      // Send custom message to service worker
      this.sendCustomMessage();
    }
  }
  
  async sendCustomMessage() {
    await this.sw.postMessage('CUSTOM_ACTION', {
      data: 'example payload'
    });
  }
  
  async performOperation() {
    const nonce = this.sw.generateNonce();
    try {
      const result = await this.sw.postMessageWithOperation(
        'CUSTOM_OPERATION',
        { nonce, param: 'value' },
        nonce
      );
      console.log('Operation result:', result);
    } catch (error) {
      console.error('Operation failed:', error);
    }
  }
}

Event Types

Core event interfaces for service worker communication.

/**
 * Base interface for all typed events
 */
interface TypedEvent {
  type: string;
}

/**
 * Event emitted when a push notification is received
 */
interface PushEvent {
  type: 'PUSH';
  data: any;
}

Low-Level Constants

/**
 * Error message constant for unsupported service workers
 */
const ERR_SW_NOT_SUPPORTED: string;

Types

Communication Events

type IncomingEvent = UnrecoverableStateEvent | VersionEvent;