19b256929Sopenharmony_ci/* 29b256929Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 39b256929Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 49b256929Sopenharmony_ci * you may not use this file except in compliance with the License. 59b256929Sopenharmony_ci * You may obtain a copy of the License at 69b256929Sopenharmony_ci * 79b256929Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 89b256929Sopenharmony_ci * 99b256929Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 109b256929Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 119b256929Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 129b256929Sopenharmony_ci * See the License for the specific language governing permissions and 139b256929Sopenharmony_ci * limitations under the License. 149b256929Sopenharmony_ci */ 159b256929Sopenharmony_ci 169b256929Sopenharmony_ciimport {Log} from "../Log"; 179b256929Sopenharmony_ci 189b256929Sopenharmony_ciexport type EventTarget = "local" | "remote" | "ability" | "commonEvent"; 199b256929Sopenharmony_ciexport type Event = { 209b256929Sopenharmony_ci target: EventTarget; 219b256929Sopenharmony_ci data: { [key: string]: any }; 229b256929Sopenharmony_ci}; 239b256929Sopenharmony_ciexport type EventParser = { 249b256929Sopenharmony_ci [key in EventTarget]: (data: any) => boolean; 259b256929Sopenharmony_ci}; 269b256929Sopenharmony_ciexport type LocalEvent = { 279b256929Sopenharmony_ci eventName: string; 289b256929Sopenharmony_ci args: any; 299b256929Sopenharmony_ci}; 309b256929Sopenharmony_ci 319b256929Sopenharmony_ciexport const START_ABILITY_EVENT = "startAbilityEvent"; 329b256929Sopenharmony_ciexport const PUBLISH_COMMON_EVENT = "publishCommonEvent"; 339b256929Sopenharmony_ci 349b256929Sopenharmony_ciconst TAG = "EventUtil"; 359b256929Sopenharmony_ciconst LOCAL_EVENT_TYPE = "local"; 369b256929Sopenharmony_ciconst START_ABILITY_TYPE = "ability"; 379b256929Sopenharmony_ci 389b256929Sopenharmony_ciexport function obtainLocalEvent(event: string, args: any): Event & { data: LocalEvent } { 399b256929Sopenharmony_ci return { 409b256929Sopenharmony_ci target: LOCAL_EVENT_TYPE, 419b256929Sopenharmony_ci data: { 429b256929Sopenharmony_ci eventName: event, 439b256929Sopenharmony_ci args, 449b256929Sopenharmony_ci }, 459b256929Sopenharmony_ci }; 469b256929Sopenharmony_ci} 479b256929Sopenharmony_ci 489b256929Sopenharmony_ciexport function obtainStartAbility(bundleName: string, abilityName: string, args?: any): Event { 499b256929Sopenharmony_ci return { 509b256929Sopenharmony_ci target: START_ABILITY_TYPE, 519b256929Sopenharmony_ci data: { 529b256929Sopenharmony_ci bundleName, 539b256929Sopenharmony_ci abilityName, 549b256929Sopenharmony_ci args 559b256929Sopenharmony_ci }, 569b256929Sopenharmony_ci }; 579b256929Sopenharmony_ci} 589b256929Sopenharmony_ci 599b256929Sopenharmony_ciexport function parseEventString(eventString: string | undefined): Event | undefined { 609b256929Sopenharmony_ci // string must be "local=eventName|args" or "ability=bundleName|abilityName" 619b256929Sopenharmony_ci if (!eventString) { 629b256929Sopenharmony_ci return; 639b256929Sopenharmony_ci } 649b256929Sopenharmony_ci let [eventType, eventData] = eventString.split("="); 659b256929Sopenharmony_ci if (eventType == LOCAL_EVENT_TYPE && eventData) { 669b256929Sopenharmony_ci let [localEventName, args] = eventData.split("|"); 679b256929Sopenharmony_ci if (localEventName) { 689b256929Sopenharmony_ci Log.showDebug(TAG, `parseEventData name:${localEventName}, args: ${args}`); 699b256929Sopenharmony_ci return obtainLocalEvent(localEventName, args); 709b256929Sopenharmony_ci } 719b256929Sopenharmony_ci } 729b256929Sopenharmony_ci if (eventType == START_ABILITY_TYPE && eventData) { 739b256929Sopenharmony_ci let [bundleName, abilityName] = eventData.split("|"); 749b256929Sopenharmony_ci if (bundleName && abilityName) { 759b256929Sopenharmony_ci Log.showDebug(TAG, `parseEventData bundleName:${bundleName}, abilityName: ${abilityName}`); 769b256929Sopenharmony_ci return obtainStartAbility(bundleName, abilityName); 779b256929Sopenharmony_ci } 789b256929Sopenharmony_ci } 799b256929Sopenharmony_ci Log.showError(TAG, `Can't parse event data: ${eventString}`); 809b256929Sopenharmony_ci return undefined; 819b256929Sopenharmony_ci} 82