1# @ohos.app.ability.sendableContextManager (sendable上下文管理) 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: Ability--> 5<!--Owner: @zhangyafei-echo; @xuzhihao666--> 6<!--Designer: @zhangyafei-echo--> 7<!--Tester: @lixueqing513--> 8<!--Adviser: @huipeizi--> 9 10sendableContextManager模块提供Context与[SendableContext](js-apis-inner-application-sendableContext.md)相互转换的能力。 11 12> **说明:** 13> 14> - 本模块首批接口从API version 12 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> - 本模块接口仅可在Stage模型下使用。 16 17## 使用场景 18 19本模块主要用于ArkTS并发实例间(包括主线程、TaskPool&Worker工作线程)的数据传递。 20 21例如,从主线程向子线程(如TaskPool或Worker工作线程)传递Sendable数据(符合[Sendable协议](../../arkts-utils/arkts-sendable.md#sendable协议)的数据)时,需要通过Context与SendableContext之间的相互转换来实现。过程如下: 22- 主线程向子线程传递Sendable数据时,需要将Context转换为SendableContext。 23- 子线程使用Sendable数据时,需要将SendableContext转换为Context。 24 25这里的Context与[createModuleContext](./js-apis-app-ability-application.md#applicationcreatemodulecontext12)方法创建的Context不同,具体差异如下: 26- 与SendableContext相互转换的Context:ArkTS并发实例持有的应用侧Context是不同的实例,底层对应同一个Context对象。当一个实例中Context属性和方法被修改时,相关实例中的Context属性和方法将会同步修改。其中,Context实例中的eventHub属性比较特殊,不同实例中的eventHub是独立的对象,不支持跨ArkTS实例使用。如果需要使用[EventHub](./js-apis-inner-application-eventHub.md)跨实例传递数据,可以通过[setEventHubMultithreadingEnabled](#sendablecontextmanagerseteventhubmultithreadingenabled20)启用跨线程数据传递功能。 27 28- 通过[createModuleContext](./js-apis-app-ability-application.md#applicationcreatemodulecontext12)创建的Context:ArkTS并发实例持有的应用侧Context是不同的实例,底层对应不同的Context对象。 29 30## 约束限制 31 32“Context转换为SendableContext”和“SendableContext转换为Context”两个环节中的Context类型必须保持一致。例如,主线程使用[convertFromContext](#sendablecontextmanagerconvertfromcontext)将[UIAbilityContext](js-apis-inner-application-uiAbilityContext.md)转换为SendableContext,子线程收到该SendableContext之后,需要通过[convertToUIAbilityContext](#sendablecontextmanagerconverttouiabilitycontext)将SendableContext转换为[UIAbilityContext](js-apis-inner-application-uiAbilityContext.md)。 33 34目前支持转换的Context包括[Context](js-apis-inner-application-context.md)、[ApplicationContext](js-apis-inner-application-applicationContext.md)、[AbilityStageContext](js-apis-inner-application-abilityStageContext.md)、[UIAbilityContext](js-apis-inner-application-uiAbilityContext.md)。 35 36## 导入模块 37 38```ts 39import { sendableContextManager } from '@kit.AbilityKit'; 40``` 41 42## SendableContext 43 44type SendableContext = _SendableContext 45 46Sendable上下文,符合[Sendable协议](../../arkts-utils/arkts-sendable.md#sendable协议),继承自[lang.ISendable](../apis-arkts/js-apis-arkts-lang.md#langisendable)。 47 48**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 49 50**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 51 52| 类型 | 说明 | 53| --- | --- | 54| [_SendableContext](js-apis-inner-application-sendableContext.md) | 表示Sendable上下文,可以与Context对象相互转换,用于ArkTS并发实例间(包括主线程、TaskPool&Worker工作线程)的数据传递。 | 55 56## sendableContextManager.convertFromContext 57 58convertFromContext(context: common.Context): SendableContext 59 60将Context转换为SendableContext对象。 61 62**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 63 64**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 65 66**参数:** 67 68| 参数名 | 类型 | 必填 | 说明 | 69| ------- | ------- | ------- | ------- | 70| context | [common.Context](js-apis-inner-application-context.md) | 是 | Context对象。支持Context基类,[ApplicationContext](js-apis-inner-application-applicationContext.md)、[AbilityStageContext](js-apis-inner-application-abilityStageContext.md)和[UIAbilityContext](js-apis-inner-application-uiAbilityContext.md)子类。 | 71 72**返回值:** 73 74| 类型 | 说明 | 75| -------- | -------- | 76| SendableContext | [SendableContext](js-apis-inner-application-sendableContext.md)对象。 | 77 78**错误码**: 79 80以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 81 82| 错误码ID | 错误信息 | 83| ------- | ------- | 84| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 85 86**示例:** 87 88```ts 89import { AbilityConstant, UIAbility, Want, sendableContextManager } from '@kit.AbilityKit'; 90import { hilog } from '@kit.PerformanceAnalysisKit'; 91import { worker } from '@kit.ArkTS'; 92 93@Sendable 94export class SendableObject { 95 constructor(sendableContext: sendableContextManager.SendableContext) { 96 this.sendableContext = sendableContext; 97 } 98 99 sendableContext: sendableContextManager.SendableContext; 100 // other sendable object 101} 102 103export default class EntryAbility extends UIAbility { 104 worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 105 106 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 107 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 108 109 // convert and post 110 try { 111 let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); 112 let object: SendableObject = new SendableObject(sendableContext); 113 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 114 this.worker.postMessageWithSharedSendable(object); 115 } catch (error) { 116 hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 117 } 118 } 119} 120``` 121 122## sendableContextManager.convertToContext 123 124convertToContext(sendableContext: SendableContext): common.Context 125 126将SendableContext对象转换为Context。 127 128**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 129 130**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 131 132**参数:** 133 134| 参数名 | 类型 | 必填 | 说明 | 135| ------- | ------- | ------- | ------- | 136| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | 137 138**返回值:** 139 140| 类型 | 说明 | 141| -------- | -------- | 142| common.Context | [Context](js-apis-inner-application-context.md)对象。 | 143 144**错误码**: 145 146以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 147 148| 错误码ID | 错误信息 | 149| ------- | ------- | 150| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 151 152**示例:** 153 154主线程传递Context: 155```ts 156import { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; 157import { hilog } from '@kit.PerformanceAnalysisKit'; 158import { worker } from '@kit.ArkTS'; 159 160@Sendable 161export class SendableObject { 162 constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 163 this.sendableContext = sendableContext; 164 this.contextName = contextName; 165 } 166 167 sendableContext: sendableContextManager.SendableContext; 168 contextName: string; 169} 170 171export default class EntryAbility extends UIAbility { 172 worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 173 174 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 175 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 176 177 // convert and post 178 try { 179 let context: common.Context = this.context as common.Context; 180 let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(context); 181 let object: SendableObject = new SendableObject(sendableContext, 'BaseContext'); 182 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 183 this.worker.postMessageWithSharedSendable(object); 184 } catch (error) { 185 hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 186 } 187 } 188} 189``` 190 191Worker线程接收Context: 192```ts 193import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 194import { common, sendableContextManager } from '@kit.AbilityKit'; 195import { hilog } from '@kit.PerformanceAnalysisKit'; 196 197@Sendable 198export class SendableObject { 199 constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 200 this.sendableContext = sendableContext; 201 this.contextName = contextName; 202 } 203 204 sendableContext: sendableContextManager.SendableContext; 205 contextName: string; 206} 207 208const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 209 210workerPort.onmessage = (e: MessageEvents) => { 211 let object: SendableObject = e.data; 212 let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 213 if (object.contextName == 'BaseContext') { 214 hilog.info(0x0000, 'testTag', '%{public}s', 'convert to context.'); 215 try { 216 let context: common.Context = sendableContextManager.convertToContext(sendableContext); 217 // 获取context后获取沙箱路径 218 hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 219 } catch (error) { 220 hilog.error(0x0000, 'testTag', 'convertToContext failed %{public}s', JSON.stringify(error)); 221 } 222 } 223} 224 225workerPort.onmessageerror = (e: MessageEvents) => { 226 hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 227} 228 229workerPort.onerror = (e: ErrorEvent) => { 230 hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 231} 232``` 233 234## sendableContextManager.convertToApplicationContext 235 236convertToApplicationContext(sendableContext: SendableContext): common.ApplicationContext 237 238将SendableContext对象转换为ApplicationContext。 239 240**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 241 242**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 243 244**参数:** 245 246| 参数名 | 类型 | 必填 | 说明 | 247| ------- | ------- | ------- | ------- | 248| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | 249 250**返回值:** 251 252| 类型 | 说明 | 253| -------- | -------- | 254| common.ApplicationContext | [ApplicationContext](js-apis-inner-application-applicationContext.md)对象。 | 255 256**错误码**: 257 258以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 259 260| 错误码ID | 错误信息 | 261| ------- | ------- | 262| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 263 264**示例:** 265 266主线程传递Context: 267```ts 268import { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; 269import { hilog } from '@kit.PerformanceAnalysisKit'; 270import { worker } from '@kit.ArkTS'; 271 272@Sendable 273export class SendableObject { 274 constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 275 this.sendableContext = sendableContext; 276 this.contextName = contextName; 277 } 278 279 sendableContext: sendableContextManager.SendableContext; 280 contextName: string; 281} 282 283export default class EntryAbility extends UIAbility { 284 worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 285 286 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 287 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 288 289 // convert and post 290 try { 291 let context: common.Context = this.context as common.Context; 292 let applicationContext = context.getApplicationContext(); 293 let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(applicationContext); 294 let object: SendableObject = new SendableObject(sendableContext, 'ApplicationContext'); 295 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 296 this.worker.postMessageWithSharedSendable(object); 297 } catch (error) { 298 hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 299 } 300 } 301} 302``` 303 304Worker线程接收Context: 305```ts 306import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 307import { common, sendableContextManager } from '@kit.AbilityKit'; 308import { hilog } from '@kit.PerformanceAnalysisKit'; 309 310@Sendable 311export class SendableObject { 312 constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 313 this.sendableContext = sendableContext; 314 this.contextName = contextName; 315 } 316 317 sendableContext: sendableContextManager.SendableContext; 318 contextName: string; 319} 320 321const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 322 323workerPort.onmessage = (e: MessageEvents) => { 324 let object: SendableObject = e.data; 325 let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 326 if (object.contextName == 'ApplicationContext') { 327 hilog.info(0x0000, 'testTag', '%{public}s', 'convert to application context.'); 328 try { 329 let context: common.ApplicationContext = sendableContextManager.convertToApplicationContext(sendableContext); 330 // 获取context后获取沙箱路径 331 hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 332 } catch (error) { 333 hilog.error(0x0000, 'testTag', 'convertToApplicationContext failed %{public}s', JSON.stringify(error)); 334 } 335 } 336} 337 338workerPort.onmessageerror = (e: MessageEvents) => { 339 hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 340} 341 342workerPort.onerror = (e: ErrorEvent) => { 343 hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 344} 345``` 346 347## sendableContextManager.convertToAbilityStageContext 348 349convertToAbilityStageContext(sendableContext: SendableContext): common.AbilityStageContext 350 351将SendableContext对象转换为AbilityStageContext。 352 353**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 354 355**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 356 357**参数:** 358 359| 参数名 | 类型 | 必填 | 说明 | 360| ------- | ------- | ------- | ------- | 361| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | 362 363**返回值:** 364 365| 类型 | 说明 | 366| -------- | -------- | 367| common.AbilityStageContext | [AbilityStageContext](js-apis-inner-application-abilityStageContext.md)对象。 | 368 369**错误码**: 370 371以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 372 373| 错误码ID | 错误信息 | 374| ------- | ------- | 375| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 376 377**示例:** 378 379主线程传递Context: 380```ts 381import { UIAbility, sendableContextManager } from '@kit.AbilityKit'; 382import { hilog } from '@kit.PerformanceAnalysisKit'; 383import { worker } from '@kit.ArkTS'; 384 385@Sendable 386export class SendableObject { 387 constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 388 this.sendableContext = sendableContext; 389 this.contextName = contextName; 390 } 391 392 sendableContext: sendableContextManager.SendableContext; 393 contextName: string; 394} 395 396export default class EntryAbility extends UIAbility { 397 worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 398 399 onCreate(): void { 400 hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage onCreate'); 401 402 // convert and post 403 try { 404 let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); 405 let object: SendableObject = new SendableObject(sendableContext, 'AbilityStageContext'); 406 hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage post message'); 407 this.worker.postMessageWithSharedSendable(object); 408 } catch (error) { 409 hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 410 } 411 } 412} 413``` 414 415Worker线程接收Context: 416```ts 417import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 418import { common, sendableContextManager } from '@kit.AbilityKit'; 419import { hilog } from '@kit.PerformanceAnalysisKit'; 420 421@Sendable 422export class SendableObject { 423 constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 424 this.sendableContext = sendableContext; 425 this.contextName = contextName; 426 } 427 428 sendableContext: sendableContextManager.SendableContext; 429 contextName: string; 430} 431 432const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 433 434workerPort.onmessage = (e: MessageEvents) => { 435 let object: SendableObject = e.data; 436 let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 437 if (object.contextName == 'AbilityStageContext') { 438 hilog.info(0x0000, 'testTag', '%{public}s', 'convert to abilitystage context.'); 439 try { 440 let context: common.AbilityStageContext = sendableContextManager.convertToAbilityStageContext(sendableContext); 441 // 获取context后获取沙箱路径 442 hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 443 } catch (error) { 444 hilog.error(0x0000, 'testTag', 'convertToAbilityStageContext failed %{public}s', JSON.stringify(error)); 445 } 446 } 447} 448 449workerPort.onmessageerror = (e: MessageEvents) => { 450 hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 451} 452 453workerPort.onerror = (e: ErrorEvent) => { 454 hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 455} 456``` 457 458## sendableContextManager.convertToUIAbilityContext 459 460convertToUIAbilityContext(sendableContext: SendableContext): common.UIAbilityContext 461 462将SendableContext对象转换为UIAbilityContext。 463 464**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 465 466**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 467 468**参数:** 469 470| 参数名 | 类型 | 必填 | 说明 | 471| ------- | ------- | ------- | ------- | 472| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | 473 474**返回值:** 475 476| 类型 | 说明 | 477| -------- | -------- | 478| common.UIAbilityContext | [UIAbilityContext](js-apis-inner-application-uiAbilityContext.md)对象。 | 479 480**错误码**: 481 482以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 483 484| 错误码ID | 错误信息 | 485| ------- | ------- | 486| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 487 488**示例:** 489 490主线程传递Context: 491```ts 492import { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; 493import { hilog } from '@kit.PerformanceAnalysisKit'; 494import { worker } from '@kit.ArkTS'; 495 496@Sendable 497export class SendableObject { 498 constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 499 this.sendableContext = sendableContext; 500 this.contextName = contextName; 501 } 502 503 sendableContext: sendableContextManager.SendableContext; 504 contextName: string; 505} 506 507export default class EntryAbility extends UIAbility { 508 worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 509 510 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 511 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 512 513 // convert and post 514 try { 515 let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); 516 let object: SendableObject = new SendableObject(sendableContext, 'EntryAbilityContext'); 517 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 518 this.worker.postMessageWithSharedSendable(object); 519 } catch (error) { 520 hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 521 } 522 } 523} 524``` 525 526Worker线程接收Context: 527```ts 528import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 529import { common, sendableContextManager } from '@kit.AbilityKit'; 530import { hilog } from '@kit.PerformanceAnalysisKit'; 531 532@Sendable 533export class SendableObject { 534 constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 535 this.sendableContext = sendableContext; 536 this.contextName = contextName; 537 } 538 539 sendableContext: sendableContextManager.SendableContext; 540 contextName: string; 541} 542 543const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 544 545workerPort.onmessage = (e: MessageEvents) => { 546 let object: SendableObject = e.data; 547 let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 548 if (object.contextName == 'EntryAbilityContext') { 549 hilog.info(0x0000, 'testTag', '%{public}s', 'convert to uiability context.'); 550 try { 551 let context: common.UIAbilityContext = sendableContextManager.convertToUIAbilityContext(sendableContext); 552 // 获取context后获取沙箱路径 553 hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 554 } catch (error) { 555 hilog.error(0x0000, 'testTag', 'convertToUIAbilityContext failed %{public}s', JSON.stringify(error)); 556 } 557 } 558} 559 560workerPort.onmessageerror = (e: MessageEvents) => { 561 hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 562} 563 564workerPort.onerror = (e: ErrorEvent) => { 565 hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 566} 567``` 568## sendableContextManager.setEventHubMultithreadingEnabled<sup>20+<sup> 569 570setEventHubMultithreadingEnabled(context: common.Context, enabled: boolean): void 571 572设置[Context](js-apis-inner-application-context.md)中的[EventHub](./js-apis-inner-application-eventHub.md)是否启用跨线程通信能力。 573 574> **说明:** 575> 576> - 当多个Context进行通信时,需要调用该接口设置每个Context都支持EventHub跨线程数据传递功能。 577 578**原子化服务API**:从API version 20开始,该接口支持在原子化服务中使用。 579 580**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 581 582**参数:** 583 584| 参数名 | 类型 | 必填 | 说明 | 585| ------- | -------------- | ---- | ------------------------------------------------------------ | 586| context | [common.Context](js-apis-inner-application-context.md) | 是 | Context对象。其中,Eventhub支持传递的序列化数据类型参见[序列化支持的类型](../apis-arkts/js-apis-taskpool.md#序列化支持类型),数据大小不超过16MB。 | 587| enabled | boolean | 是 | 表示是否启用Context的EventHub跨线程通信能力。<br>- true:表示启用跨线程通信能力,数据将通过引用的方式传递。<br>- false:表示禁用跨线程通信能力,数据将通过序列化的方式传递,即发送端线程与接收端线程的数据相互独立。 | 588 589**示例:** 590 591主线程启用[Context](./js-apis-inner-application-context.md)中[EventHub](./js-apis-inner-application-eventHub.md)的跨线程通信能力,并将Context转换为[SenableContext](js-apis-inner-application-sendableContext.md)后发送到[Worker](../apis-arkts/js-apis-worker.md)线程。 592 593```ts 594import { common, sendableContextManager } from '@kit.AbilityKit'; 595import { worker } from '@kit.ArkTS'; 596import { hilog } from '@kit.PerformanceAnalysisKit'; 597 598const DOMAIN = 0x0000; 599 600@Sendable 601export class SendableObject { 602 constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 603 this.sendableContext = sendableContext; 604 this.contextName = contextName; 605 } 606 607 sendableContext: sendableContextManager.SendableContext; 608 contextName: string; 609} 610 611@Entry 612@Component 613struct Index { 614 @State context: common.Context | undefined = this.getUIContext().getHostContext(); 615 worker1: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 616 617 aboutToAppear(): void { 618 let context: common.Context = this.context as common.Context; 619 context.eventHub.on('event1', this.eventFunc); 620 context.eventHub.emit('event1', 'xingming', 22); 621 } 622 623 eventFunc(name: string, age: number) { 624 hilog.info(DOMAIN, 'testTag', 'name %{public}s age %{public}d', name, age); 625 } 626 627 build() { 628 Column() { 629 Row() { 630 Button('thread 1') 631 .size({ width: 100, height: 100 }) 632 .onClick(() => { 633 if (this.context == undefined) { 634 return; 635 } 636 sendableContextManager.setEventHubMultithreadingEnabled(this.context, true); 637 let sendableContext: sendableContextManager.SendableContext = 638 sendableContextManager.convertFromContext(this.context); 639 let object: SendableObject = new SendableObject(sendableContext, 'BaseContext'); 640 this.worker1.postMessageWithSharedSendable(object); 641 }) 642 } 643 } 644 } 645} 646``` 647 648[Worker](../apis-arkts/js-apis-worker.md)线程接收到[SendableContext](js-apis-inner-application-sendableContext.md)后,将其转换为[Context](./js-apis-inner-application-context.md)。然后,在Worker线程内,启用Context中[EventHub](./js-apis-inner-application-eventHub.md)的跨线程通信能力,并通过该功能向主线程发送消息。 649 650```ts 651import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 652import { common, sendableContextManager } from '@kit.AbilityKit'; 653import { hilog } from '@kit.PerformanceAnalysisKit'; 654 655const DOMAIN = 0x0000; 656 657@Sendable 658export class SendableObject { 659 constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 660 this.sendableContext = sendableContext; 661 this.contextName = contextName; 662 } 663 664 sendableContext: sendableContextManager.SendableContext; 665 contextName: string; 666} 667 668const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 669 670workerPort.onmessage = (e: MessageEvents) => { 671 let object: SendableObject = e.data; 672 let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 673 if (object.contextName == 'BaseContext') { 674 let context: common.Context = sendableContextManager.convertToContext(sendableContext); 675 sendableContextManager.setEventHubMultithreadingEnabled(context, true); 676 context.eventHub.emit('event1', 'xingming', 40); 677 } 678}; 679 680workerPort.onmessageerror = (e: MessageEvents) => { 681 hilog.error(DOMAIN, 'testTag', '%{public}s', 'onmessageerror'); 682}; 683 684workerPort.onerror = (e: ErrorEvent) => { 685 hilog.error(DOMAIN, 'testTag', '%{public}s', 'onerror'); 686}; 687```