1e484b35bSopenharmony_ci/* 2e484b35bSopenharmony_ci * Licensed to the Apache Software Foundation (ASF) under one 3e484b35bSopenharmony_ci * or more contributor license agreements. See the NOTICE file 4e484b35bSopenharmony_ci * distributed with this work for additional information 5e484b35bSopenharmony_ci * regarding copyright ownership. The ASF licenses this file 6e484b35bSopenharmony_ci * to you under the Apache License, Version 2.0 (the 7e484b35bSopenharmony_ci * "License"); you may not use this file except in compliance 8e484b35bSopenharmony_ci * with the License. You may obtain a copy of the License at 9e484b35bSopenharmony_ci * 10e484b35bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 11e484b35bSopenharmony_ci * 12e484b35bSopenharmony_ci * Unless required by applicable law or agreed to in writing, 13e484b35bSopenharmony_ci * software distributed under the License is distributed on an 14e484b35bSopenharmony_ci * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15e484b35bSopenharmony_ci * KIND, either express or implied. See the License for the 16e484b35bSopenharmony_ci * specific language governing permissions and limitations 17e484b35bSopenharmony_ci * under the License. 18e484b35bSopenharmony_ci */ 19e484b35bSopenharmony_ci/* 20e484b35bSopenharmony_ci * 2021.01.08 - Rewrite the function 'register' and make it simpler. 21e484b35bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 22e484b35bSopenharmony_ci */ 23e484b35bSopenharmony_ci 24e484b35bSopenharmony_ciimport { Log } from '../utils/utils'; 25e484b35bSopenharmony_ci 26e484b35bSopenharmony_ciexport interface OptionsInterface { 27e484b35bSopenharmony_ci create: (id: string) => any 28e484b35bSopenharmony_ci destroy: (id: string) => void 29e484b35bSopenharmony_ci refresh?: (id:string, data: object) => Error | void 30e484b35bSopenharmony_ci} 31e484b35bSopenharmony_ci 32e484b35bSopenharmony_ciexport interface ServicesInterface { 33e484b35bSopenharmony_ci name: string 34e484b35bSopenharmony_ci options: OptionsInterface 35e484b35bSopenharmony_ci} 36e484b35bSopenharmony_ci 37e484b35bSopenharmony_ciexport const services: ServicesInterface[] = []; 38e484b35bSopenharmony_ci 39e484b35bSopenharmony_ci/** 40e484b35bSopenharmony_ci * Register a service. 41e484b35bSopenharmony_ci * @param {string} name - Service name. 42e484b35bSopenharmony_ci * @param {OptionsInterface} options - Could have { create, destroy, refresh } lifecycle methods. 43e484b35bSopenharmony_ci */ 44e484b35bSopenharmony_ciexport function register(name: string, options: OptionsInterface): void { 45e484b35bSopenharmony_ci const hasName = services.map( 46e484b35bSopenharmony_ci service => service.name 47e484b35bSopenharmony_ci ).indexOf(name) >= 0; 48e484b35bSopenharmony_ci 49e484b35bSopenharmony_ci if (hasName) { 50e484b35bSopenharmony_ci Log.warn(`Service '${name}' has been registered already!`); 51e484b35bSopenharmony_ci } else { 52e484b35bSopenharmony_ci options = Object.assign({}, options); 53e484b35bSopenharmony_ci services.push({ 54e484b35bSopenharmony_ci name, 55e484b35bSopenharmony_ci options 56e484b35bSopenharmony_ci }); 57e484b35bSopenharmony_ci } 58e484b35bSopenharmony_ci} 59e484b35bSopenharmony_ci 60e484b35bSopenharmony_ci/** 61e484b35bSopenharmony_ci * Unregister a service by name. 62e484b35bSopenharmony_ci * @param {string} name - Service name. 63e484b35bSopenharmony_ci */ 64e484b35bSopenharmony_ciexport function unregister(name: string): void { 65e484b35bSopenharmony_ci services.some((service: ServicesInterface, index: number) => { 66e484b35bSopenharmony_ci if (service.name === name) { 67e484b35bSopenharmony_ci services.splice(index, 1); 68e484b35bSopenharmony_ci return true; 69e484b35bSopenharmony_ci } 70e484b35bSopenharmony_ci }); 71e484b35bSopenharmony_ci} 72e484b35bSopenharmony_ci 73e484b35bSopenharmony_ciexport default { 74e484b35bSopenharmony_ci register, 75e484b35bSopenharmony_ci unregister 76e484b35bSopenharmony_ci}; 77