Freezing Sendable Objects

Sendable objects can be frozen, making them read-only and preventing any modifications to their properties. Once frozen, these objects can be safely accessed across multiple concurrent instances without the need for locks. This is achieved using the Object.freeze method.

NOTE

The Object.freeze API cannot be used in .ets files.

Usage Example

  1. Encapsulate the Object.freeze method in a TS file.

    // helper.ts
    export function freezeObj(obj: any) {
      Object.freeze(obj);
    }
    
  2. Call the freeze method to freeze an object and send it to a child thread.

    // Index.ets
    import { freezeObj } from './helper';
    import { worker } from '@kit.ArkTS';
     
    @Sendable
    export class GlobalConfig {
      // Configuration properties and methods
      init() {
        // Initialization logic
        freezeObj(this) // Freeze the object after the initialization is complete.
      }
    }
     
    @Entry
    @Component
    struct Index {
      build() {
        Column() {
          Text("Sendable freezeObj Test")
            .id('HelloWorld')
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .onClick(() => {
              let gConfig = new GlobalConfig();
              gConfig.init();
              const workerInstance = new worker.ThreadWorker('entry/ets/workers/Worker.ets', { name: "Worker1" });
              workerInstance.postMessage(gConfig);
            })
        }
        .height('100%')
        .width('100%')
      }
    }
    
  3. Perform operations on the frozen object directly in the child thread without locking.

    // Worker.ets
    import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
    import { GlobalConfig } from '../pages/Index';
    
    const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
    workerPort.onmessage = (e: MessageEvents) => {
      let gConfig: GlobalConfig = e.data;
      // Use the gConfig object.
    }