source
stringlengths
14
113
code
stringlengths
10
21.3k
application-dev\arkts-utils\long-time-task-guide.md
// Index.ets import { sensor } from '@kit.SensorServiceKit'; import { taskpool } from '@kit.ArkTS'; import { BusinessError, emitter } from '@kit.BasicServicesKit';
application-dev\arkts-utils\long-time-task-guide.md
// Index.ets @Concurrent async function SensorListener() : Promise<void> { sensor.on(sensor.SensorId.ACCELEROMETER, (data) => { emitter.emit({ eventId: 0 }, { data: data }); }, { interval: 1000000000 }); emitter.on({ eventId: 1 }, () => { sensor.off(sensor.SensorId.ACCELEROMETER) ...
application-dev\arkts-utils\long-time-task-guide.md
// Index.ets @Entry @Component struct Index { sensorTask?: taskpool.LongTask build() { Column() { Text("Add listener") .id('HelloWorld') .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { this.sensorTask = new taskp...
application-dev\arkts-utils\makeobserved-sendable.md
// SendableData.ets @Sendable export class SendableData { name: string = 'Tom'; age: number = 20; gender: number = 1; likes: number = 1; follow: boolean = false; }
application-dev\arkts-utils\makeobserved-sendable.md
import { taskpool } from '@kit.ArkTS'; import { SendableData } from './SendableData'; import { UIUtils } from '@kit.ArkUI'; @Concurrent function threadGetData(param: string): SendableData { // Process data in the child thread. let ret = new SendableData(); console.info(`Concurrent threadGetData, param ${param}`)...
application-dev\arkts-utils\multi-thread-cancel-task.md
// Mock.ets @Sendable export class SendableTest { // Store the task ID. private taskId: number = 0; constructor(id: number) { this.taskId = id; } public getTaskId(): number { return this.taskId; } }
application-dev\arkts-utils\multi-thread-cancel-task.md
// Index.ets import { taskpool } from '@kit.ArkTS'; import { SendableTest } from './Mock' @Concurrent function cancel(send: SendableTest) { console.info("cancel task finished"); // Cancel the task in the child thread based on the task ID. taskpool.cancel(send.getTaskId()); } @Concurr...
application-dev\arkts-utils\multi-thread-concurrency-overview.md
// The pseudocode is used here to help you understand the differences between the shared memory model and the actor model. class Queue { // ... push(value: number) { } empty(): boolean { // ... return true; } pop(value: number): number { // ... return value; } } class Mutex { // ... ...
application-dev\arkts-utils\multi-thread-concurrency-overview.md
import { taskpool } from '@kit.ArkTS'; // Cross-thread concurrent tasks @Concurrent async function produce(): Promise<number> { // Add production logic here. console.info('producing...'); return Math.random(); } class Consumer { public consume(value: Object) { // Add consumption logic here. console.in...
application-dev\arkts-utils\multi-thread-concurrency-overview.md
import { taskpool } from '@kit.ArkTS'; // Cross-thread concurrent tasks @Concurrent async function produce(): Promise<number> { // Add production logic here. console.info('producing...'); return Math.random(); } class Consumer { public consume(value: Object) { // Add consumption logic here. console.in...
application-dev\arkts-utils\multi-time-consuming-tasks.md
// IconItemSource.ets export class IconItemSource { image: string | Resource = ''; text: string | Resource = ''; constructor(image: string | Resource = '', text: string | Resource = '') { this.image = image; this.text = text; } }
application-dev\arkts-utils\multi-time-consuming-tasks.md
// IndependentTask.ets import { IconItemSource } from './IconItemSource'; // Methods executed in the TaskPool thread must be decorated by @Concurrent. Otherwise, they cannot be called. @Concurrent export function loadPicture(count: number): IconItemSource[] { let iconItemSourceList: IconItemSource...
application-dev\arkts-utils\multi-time-consuming-tasks.md
// MultiTask.ets import { taskpool } from '@kit.ArkTS'; import { IconItemSource } from './IconItemSource'; import { loadPicture } from './IndependentTask'; let iconItemSourceList: IconItemSource[][]; let taskGroup: taskpool.TaskGroup = new taskpool.TaskGroup(); taskGroup.addTask(new taskpool...
application-dev\arkts-utils\napi-coerce-to-native-binding-object.md
// Index.d.ts export const getAddress: () => number; export const getSetSize: () => number; export const store: (a: number) => void; export const erase: (a: number) => void; export const clear: () => void; export const setTransferDetached: (b : boolean) => number;
application-dev\arkts-utils\napi-coerce-to-native-binding-object.md
import testNapi from 'libentry.so'; import { taskpool } from '@kit.ArkTS'; @Concurrent function getAddress() { let address: number = testNapi.getAddress(); console.info("taskpool:: address is " + address); } @Concurrent function store(a:number, b:number, c:number) { let size:num...
application-dev\arkts-utils\napi-coerce-to-native-binding-object.md
import testNapi from 'libentry.so'; import { taskpool } from '@kit.ArkTS'; @Concurrent function getAddress() { let address: number = testNapi.getAddress(); console.info("taskpool:: address is " + address); } @Concurrent function store(a:number, b:number, c:number) { let size:num...
application-dev\arkts-utils\napi-define-sendable-object.md
// Index.d.ts @Sendable export class MyObject { constructor(arg: number); plusOne(): number; public get value(); public set value(newVal: number); }
application-dev\arkts-utils\napi-define-sendable-object.md
// Index.ets import { MyObject } from 'libentry.so'; import { taskpool } from '@kit.ArkTS'; @Concurrent async function Sum(object: MyObject) { object.value = 2000; let num = object.plusOne(); console.info("taskpool thread num is " + num); // taskpool thread num is 2001 return num; ...
application-dev\arkts-utils\native-interthread-shared.md
// SendableObjTest.ets @Sendable export class SendableObjTest { static newSendable() { return 1024; } }
application-dev\arkts-utils\native-interthread-shared.md
// SendableObjTest.ets @Sendable export class SendableObjTest { static newSendable() { return 1024; } }
application-dev\arkts-utils\native-interthread-shared.md
// Index.ets import { hilog } from '@kit.PerformanceAnalysisKit'; import testNapi from 'libentry.so'; import { SendableObjTest } from './SendableObjTest' @Entry @Component struct Index { @State message: string = 'Hello World'; build() { Row() { Column() { Text(this.message) .fontSize(5...
application-dev\arkts-utils\nonlinear-container.md
// HashMap import { HashMap } from '@kit.ArkTS'; // Import the HashMap module. let hashMap1: HashMap<string, number> = new HashMap(); hashMap1.set('a', 123); // Add an element with key 'a' and value 123. let hashMap2: HashMap<number, number> = new HashMap(); hashMap2.set(4, 123); // Add an element with key 4 and value...
application-dev\arkts-utils\normal-object.md
// Test.ets // Custom class TestA. export class TestA { constructor(name: string) { this.name = name; } name: string = 'ClassA'; }
application-dev\arkts-utils\normal-object.md
// Index.ets import { taskpool } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; import { TestA } from './Test'; @Concurrent async function test1(arg: TestA) { console.info("TestA name is: " + arg.name); } @Entry @Component struct Index { @State message: string = 'Hello World'; build(...
application-dev\arkts-utils\resident-task-guide.md
// Index.ets import { worker } from '@kit.ArkTS';
application-dev\arkts-utils\resident-task-guide.md
// Index.ets const workerInstance: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets');
application-dev\arkts-utils\resident-task-guide.md
// Index.ets @Entry @Component struct Index { build() { Column() { Text("Listener task") .id('HelloWorld') .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { workerInstance.postMessage({type: 'start'}) worke...
application-dev\arkts-utils\resident-task-guide.md
// Worker.ets import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; const workerPort: ThreadWorkerGlobalScope = worker.workerPort; let isRunning = false; workerPort.onmessage = (e: MessageEvents) => { const type = e.data.type as string; if (type === 'start') { ...
application-dev\arkts-utils\sendable-constraints.md
@Sendable class A { constructor() { } } @Sendable class B extends A { constructor() { super() } }
application-dev\arkts-utils\sendable-constraints.md
class A { constructor() { } } @Sendable class B extends A { // A is not a Sendable class. B cannot inherit it. A compilation error is reported. constructor() { super() } }
application-dev\arkts-utils\sendable-constraints.md
class A { constructor() { } } class B extends A { constructor() { super() } }
application-dev\arkts-utils\sendable-constraints.md
@Sendable class A { constructor() { } } class B extends A { // A is a Sendable class. B cannot inherit it. A compilation error is reported. constructor() { super() } }
application-dev\arkts-utils\sendable-constraints.md
interface I {}; class B implements I {};
application-dev\arkts-utils\sendable-constraints.md
import { lang } from '@kit.ArkTS'; type ISendable = lang.ISendable; interface I extends ISendable {}; class B implements I {}; // I is a Sendable interface. B cannot implement it. A compilation error is reported.
application-dev\arkts-utils\sendable-constraints.md
@Sendable class A { constructor() { } a: number = 0; }
application-dev\arkts-utils\sendable-constraints.md
@Sendable class A { constructor() { } b: Array<number> = [1, 2, 3] // A compilation error is reported. Use collections.Array instead. }
application-dev\arkts-utils\sendable-constraints.md
@Sendable class A { constructor() { } a: number = 0; }
application-dev\arkts-utils\sendable-constraints.md
@Sendable class A { constructor() { } a!: number; // A compilation error is reported. The exclamation mark (!) is not supported. }
application-dev\arkts-utils\sendable-constraints.md
@Sendable class A { num1: number = 1; num2: number = 2; add(): number { return this.num1 + this.num2; } }
application-dev\arkts-utils\sendable-constraints.md
enum B { b1 = "bbb" } @Sendable class A { ["aaa"]: number = 1; // A compilation error is reported. ["aaa"] is not supported. [B.b1]: number = 2; // A compilation error is reported. [B.b1] is not supported. }
application-dev\arkts-utils\sendable-constraints.md
import { collections } from '@kit.ArkTS'; try { let arr1: collections.Array<number> = new collections.Array<number>(); let num: number = 1; arr1.push(num); } catch (e) { console.error(`taskpool execute: Code: ${e.code}, message: ${e.message}`); }
application-dev\arkts-utils\sendable-constraints.md
import { collections } from '@kit.ArkTS'; try { let arr1: collections.Array<Array<number>> = new collections.Array<Array<number>>(); // A compilation error is reported. The template type must be Sendable. let arr2: Array<number> = new Array<number>(); arr2.push(1); arr1.push(arr2); } catch (e) { console.erro...
application-dev\arkts-utils\sendable-constraints.md
import { lang } from '@kit.ArkTS'; type ISendable = lang.ISendable; interface I extends ISendable {} @Sendable class B implements I { static o: number = 1; static bar(): B { return new B(); } } @Sendable class C { v: I = new B(); u: number = B.o; foo() { return B.bar(); } }
application-dev\arkts-utils\sendable-constraints.md
import { lang } from '@kit.ArkTS'; type ISendable = lang.ISendable; interface I extends ISendable {} @Sendable class B implements I {} function bar(): B { return new B(); } let b = new B(); { @Sendable class A implements I {} @Sendable class C { u: I = bar(); // bar is not a Sendable class object. ...
application-dev\arkts-utils\sendable-constraints.md
@Sendable type SendableFuncType = () => void; @Sendable class C {} @Sendable function SendableFunc() { console.info("Sendable func"); }
application-dev\arkts-utils\sendable-constraints.md
@Sendable type A = number; // A compile-time error is reported. @Sendable type D = C; // A compile-time error is reported.
application-dev\arkts-utils\sendable-constraints.md
@Sendable class A { num: number = 1; }
application-dev\arkts-utils\sendable-constraints.md
@Sendable @Observed // A compilation error is reported. class C { num: number = 1; }
application-dev\arkts-utils\sendable-constraints.md
import { collections } from '@kit.ArkTS'; let arr1: collections.Array<number> = new collections.Array<number>(1, 2, 3); // The type is Sendable.
application-dev\arkts-utils\sendable-constraints.md
import { collections } from '@kit.ArkTS'; let arr2: collections.Array<number> = [1, 2, 3]; // The type is not Sendable. A compile-time error is reported. let arr3: number[] = [1, 2, 3]; // The type is not Sendable. No error is reported. let arr4: number[] = new collections.Array<number>(1, 2, 3); // A compile-time err...
application-dev\arkts-utils\sendable-constraints.md
class A { state: number = 0; } @Sendable class SendableA { state: number = 0; } let a1: A = new SendableA() as A;
application-dev\arkts-utils\sendable-constraints.md
class A { state: number = 0; } @Sendable class SendableA { state: number = 0; } let a2: SendableA = new A() as SendableA; // A compilation error is reported.
application-dev\arkts-utils\sendable-constraints.md
@Sendable type SendableFuncType = () => void; @Sendable function SendableFunc() { console.info("Sendable func"); } @Sendable class SendableClass { constructor(f: SendableFuncType) { this.func = f; } func: SendableFuncType; } let sendableClass = new SendableClass(SendableFunc);
application-dev\arkts-utils\sendable-constraints.md
@Sendable type SendableFuncType = () => void; let func: SendableFuncType = () => {}; // A compile-time error is reported. @Sendable class SendableClass { func: SendableFuncType = () => {}; // A compile-time error is reported. }
application-dev\arkts-utils\sendable-freeze.md
// helper.ts export function freezeObj(obj: any) { Object.freeze(obj); }
application-dev\arkts-utils\sendable-freeze.md
// 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. ...
application-dev\arkts-utils\sendable-freeze.md
// 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; ...
application-dev\arkts-utils\sendable-guide.md
// Index.ets import { taskpool } from '@kit.ArkTS'; import { testTypeA, testTypeB, Test } from './sendable'; import { BusinessError, emitter } from '@kit.BasicServicesKit'; // Simulate data processing in a concurrent function. @Concurrent async function taskFunc(obj: Test) { console.info("test task res1 is: " + obj...
application-dev\arkts-utils\sendable-guide.md
// sendable.ets // Assemble large data in a Sendable class. @Sendable export class testTypeA { name: string = "A"; constructor(name: string) { this.name = name; } } @Sendable export class testTypeB { name: string = "B"; constructor(name: string) { this.name = name; } } @Sendable export class Test ...
application-dev\arkts-utils\sendable-guide.md
// Index.ets import { taskpool, ArkTSUtils } from '@kit.ArkTS'; import { SendableTestClass, ISendable } from './sendable'; // Simulate data processing in a concurrent function. @Concurrent async function taskFunc(sendableObj: SendableTestClass) { console.info("SendableTestClass: name is: " + sendableObj.printName()...
application-dev\arkts-utils\sendable-guide.md
// sendable.ets // Define a Test class to simulate the operation of passing a class instance carrying methods. import { lang, collections } from '@kit.ArkTS' export type ISendable = lang.ISendable; @Sendable export class SendableTestClass { name: string = 'John'; age: number = 20; sex: string = "man"; arr: co...
application-dev\arkts-utils\sendablelrucache-recent-list.md
// LruCache.ets import { ArkTSUtils } from '@kit.ArkTS'; // Use the 'use shared' marker to mark a module shareable. "use shared" // The SendableClass instance can be shared across different threads. @Sendable class SendableClass { // Lock the SendableLruCache instances to prevent data inconsis...
application-dev\arkts-utils\sendablelrucache-recent-list.md
// Book1.ets @Entry @Component struct Index1 { @State message: string = 'Hello World!'; build() { RelativeContainer() { Text("Content of book 1") .id('first book') .fontSize(20) .padding(10) .fontWeight(FontWeight.Bold) .alignRu...
application-dev\arkts-utils\sendablelrucache-recent-list.md
// Book2.ets @Entry @Component struct Index2 { @State message: string = 'Hello World!'; build() { RelativeContainer() { Text("Content of book 2") .id('second book') .fontSize(20) .padding(10) .fontWeight(FontWeight.Bold) .alignR...
application-dev\arkts-utils\sendablelrucache-recent-list.md
// Book3.ets @Entry @Component struct Index3 { @State message: string = 'Hello World!'; build() { RelativeContainer() { Text("Content of book 3") .id('third book') .fontSize(20) .padding(10) .fontWeight(FontWeight.Bold) .alignRu...
application-dev\arkts-utils\sendablelrucache-recent-list.md
// Book4.ets @Entry @Component struct Index4 { @State message: string = 'Hello World!'; build() { RelativeContainer() { Text("Content of book 4") .id('fourth book') .fontSize(20) .padding(10) .fontWeight(FontWeight.Bold) .alignR...
application-dev\arkts-utils\sendablelrucache-recent-list.md
// Index.ets import { taskpool } from '@kit.ArkTS'; import { lruCache } from './LruCache' @Concurrent async function updateBooks(key: string, value: string) { // Update the latest access list in the child thread. await lruCache.put(key, value); } @Entry @Component struct Index { ...
application-dev\arkts-utils\shared-arraybuffer-object.md
import { taskpool } from '@kit.ArkTS'; @Concurrent function transferAtomics(arg1: Int32Array) { console.info("wait begin::"); // Use Atomics to perform operations. let res = Atomics.wait(arg1, 0, 0, 3000); return res; } // Define an object that can be shared. let sab: SharedArrayBuffer = new SharedArrayBuffer...
application-dev\arkts-utils\sync-task-development.md
// Index.ets code import { taskpool} from '@kit.ArkTS'; // Step 1: Define a concurrent function to implement service logic. @Concurrent async function taskpoolFunc(num: number): Promise<number> { // Implement the corresponding functionality based on the service logic. let tmpNum: number = num + 100; return tmpNu...
application-dev\arkts-utils\sync-task-development.md
// Index.ets import { worker } from '@kit.ArkTS'; @Entry @Component struct Index { @State message: string = 'Hello World'; build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) ...
application-dev\arkts-utils\sync-task-development.md
// handle.ts code export default class Handle { syncGet() { return; } syncSet(num: number) { return; } }
application-dev\arkts-utils\sync-task-development.md
// MyWorker.ts code import { worker, ThreadWorkerGlobalScope, MessageEvents } from '@kit.ArkTS'; import Handle from './handle' // Import the handle. let workerPort : ThreadWorkerGlobalScope = worker.workerPort; // Handle that cannot be transferred. All operations depend on this handle. le...
application-dev\arkts-utils\taskpool-async-task-guide.md
// Index.ets import { taskpool } from '@kit.ArkTS'; import { BusinessError, emitter } from '@kit.BasicServicesKit';
application-dev\arkts-utils\taskpool-async-task-guide.md
// Index.ets @Concurrent function collectFrame() { // Collect and process data. // Simulate the processing task, which is time consuming. let t = new Date().getTime() while (new Date().getTime() - t < 30000) { continue; } }
application-dev\arkts-utils\taskpool-async-task-guide.md
// Index.ets @Entry @Component struct Index { sensorTask?: taskpool.LongTask build() { Column() { Text("HELLO WORLD") .id('HelloWorld') .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { // Create an asynchronous queue...
application-dev\arkts-utils\taskpool-communicates-with-mainthread.md
// TaskSendDataUsage.ets function notice(data: number): void { console.info("Child thread task completed. Total images loaded:", data) }
application-dev\arkts-utils\taskpool-communicates-with-mainthread.md
// IconItemSource.ets export class IconItemSource { image: string | Resource = ''; text: string | Resource = ''; constructor(image: string | Resource = '', text: string | Resource = '') { this.image = image; this.text = text; } }
application-dev\arkts-utils\taskpool-communicates-with-mainthread.md
// TaskSendDataUsage.ets import { taskpool } from '@kit.ArkTS'; import { IconItemSource } from './IconItemSource'; // Use sendData to notify the host thread of information in real time. @Concurrent export function loadPictureSendData(count: number): IconItemSource[] { let iconItemSourceList: Ico...
application-dev\arkts-utils\taskpool-communicates-with-mainthread.md
// TaskSendDataUsage.ets @Entry @Component struct Index { @State message: string = 'Hello World'; build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { let iconIt...
application-dev\arkts-utils\taskpool-introduction.md
import { taskpool } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; @Concurrent function printArrayBuffer(buffer: ArrayBuffer) { return buffer; } function testArrayBuffer() { const buffer = new ArrayBuffer(1); const group = new taskpool.TaskGroup(); const task =...
application-dev\arkts-utils\taskpool-introduction.md
> function bar() { > } > > @Concurrent > function foo() { > bar(); // This violates the closure principle. An error is reported. > } >
application-dev\arkts-utils\taskpool-introduction.md
import { taskpool } from '@kit.ArkTS'; @Concurrent function add(num1: number, num2: number): number { return num1 + num2; } async function concurrentFunc(): Promise<void> { try { const task: taskpool.Task = new taskpool.Task(add, 1, 2); console.info(`taskpool res is: ${await taskpool.execute(task)}`); // ...
application-dev\arkts-utils\taskpool-introduction.md
import { taskpool } from '@kit.ArkTS'; @Concurrent function testPromise(args1: number, args2: number): Promise<number> { return new Promise<number>((resolve, reject) => { resolve(args1 + args2); }); } @Concurrent async function testPromise1(args1: number, args2: number): Promise<number> { return new Promise...
application-dev\arkts-utils\taskpool-introduction.md
// Index.ets import { taskpool } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; import { testAdd, MyTestA, MyTestB } from './Test'; function add(arg: number) { return ++arg; } class TestA { constructor(name: string) { this.name = name; } name: string = 'ClassA'; } class TestB ...
application-dev\arkts-utils\taskpool-introduction.md
// Test.ets export function testAdd(arg: number) { return ++arg; } @Sendable export class MyTestA { constructor(name: string) { this.name = name; } name: string = 'MyTestA'; } export class MyTestB { static nameStr:string = 'MyTestB'; }
application-dev\arkts-utils\taskpool-introduction.md
import { taskpool } from '@kit.ArkTS'; @Concurrent async function testPromiseError() { await new Promise<number>((resolve, reject) => { resolve(1); }).then(() => { throw new Error('testPromise Error'); }) } @Concurrent async function testPromiseError1() { await new Promise<string>((resolve, reject) =>...
application-dev\arkts-utils\taskpool-waterflow.md
// Mock.ets import { taskpool } from '@kit.ArkTS'; import { fillImg } from './Index'; @Concurrent function query() { console.info("TaskPoolTest-this is query"); let result = new Array<string>(33); for (let i = 0; i < 33; i++) { result[i] = 'Image' + i; } taskpool.T...
application-dev\arkts-utils\taskpool-waterflow.md
// WaterFlowDataSource.ets // An object that implements the IDataSource interface, which is used by the WaterFlow component to load data. export class WaterFlowDataSource implements IDataSource { private dataArray: number[] = []; private listeners: DataChangeListener[] = []; constructor() { ...
application-dev\arkts-utils\taskpool-waterflow.md
// Index.ets import { WaterFlowDataSource } from './WaterFlowDataSource'; import { getImgFromDB } from './Mock'; // Simulate an image array. let img = new Array<string>(33); export function fillImg(imgArr : Array<string>) { img = imgArr; } @Entry @Component struct WaterFlowDe...
application-dev\arkts-utils\transferabled-object.md
// Index.ets import { taskpool } from '@kit.ArkTS'; import { loadPixelMap } from './pixelMapTest'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct Index { @State message: string = 'Hello World'; @State pixelMap: PixelMap | undefined = undefined; private loadImageFromThread(): voi...
application-dev\arkts-utils\transferabled-object.md
// pixelMapTest.ets import { image } from '@kit.ImageKit'; @Concurrent export async function loadPixelMap(rawFileDescriptor: number): Promise<PixelMap> { // Create an imageSource. const imageSource = image.createImageSource(rawFileDescriptor); // Create a pixelMap. const pixelMap = imageSource.createPixelMapSy...
application-dev\arkts-utils\worker-and-taskpool.md
// Index.ets import { worker } from '@kit.ArkTS'; @Entry @Component struct Index { @State message: string = 'Hello World'; build() { RelativeContainer() { Text(this.message) .id('HelloWorld') .fontSize($r('app.float.page_text_font_size')) .f...
application-dev\arkts-utils\worker-and-taskpool.md
// Worker.ets import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; import { taskpool } from '@kit.ArkTS'; const workerPort: ThreadWorkerGlobalScope = worker.workerPort; workerPort.onmessage = async (e: MessageEvents) => { if (e.data.type === 'start') { // ...
application-dev\arkts-utils\worker-communicates-with-mainthread.md
// Worker.ets import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; const workerPort: ThreadWorkerGlobalScope = worker.workerPort; // The Worker receives messages from the host thread and processes them accordingly. workerPort.onmessage = (e: MessageEvents): void => { ...
application-dev\arkts-utils\worker-communicates-with-mainthread.md
// Index.ets import { worker } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; function promiseCase() { let p: Promise<void> = new Promise<void>((resolve: Function, reject: Function) => { setTimeout(() => { resolve(1); }, 100) }).then(undefined, (e...
application-dev\arkts-utils\worker-introduction.md
// Import the module. import { worker } from '@kit.ArkTS'; // Use the following function in API version 9 and later versions: const worker1: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/worker.ets'); // Use the following function in API version 8 and earlier versions: const worker2: worker.Worker =...
application-dev\arkts-utils\worker-introduction.md
import { worker } from '@kit.ArkTS'; // URL of the Worker thread file: "entry/src/main/ets/workers/worker.ets" const workerStage1: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/worker.ets'); // URL of the Worker thread file: "testworkers/src/main/ets/ThreadFile/workers/worker.ets" const workerStage...
application-dev\arkts-utils\worker-introduction.md
import { worker } from '@kit.ArkTS'; // URL of the Worker thread file: "hsp/src/main/ets/workers/worker.ets" const workerStage3: worker.ThreadWorker = new worker.ThreadWorker('hsp/ets/workers/worker.ets');
application-dev\arkts-utils\worker-introduction.md
import { worker } from '@kit.ArkTS'; // @ path loading: // URL of the Worker thread file: "har/src/main/ets/workers/worker.ets" const workerStage4: worker.ThreadWorker = new worker.ThreadWorker('@har/ets/workers/worker.ets'); // Relative path loading: // URL of the Worker thread file: "har/src/main/ets/workers/worker...
application-dev\arkts-utils\worker-introduction.md
import { worker } from '@kit.ArkTS'; // The following three scenarios are involved. // Scenario 1: URL of the Worker thread file: "{moduleName}/src/main/ets/MainAbility/workers/worker.ets" const workerFA1: worker.ThreadWorker = new worker.ThreadWorker('workers/worker.ets', {name:'first worker in FA model'}); // Scen...