1# Global Configuration 2 3In cases where a process-wide singleton is necessary, for example, for maintaining data consistency across multiple concurrent instances for global configuration services, a shared module can be used. 4 5The following example illustrates the service logic where downloads are permitted only when Wi-Fi is enabled and the user is logged in. The implementation involves the following steps: 6 71. Create a global configuration file. 8 9 ```ts 10 // Config.ets 11 12 import { ArkTSUtils } from '@kit.ArkTS'; 13 14 "use shared" 15 16 @Sendable 17 class Config { 18 lock: ArkTSUtils.locks.AsyncLock = new ArkTSUtils.locks.AsyncLock 19 isLogin: boolean = false; 20 loginUser?: string; 21 wifiOn: boolean = false 22 23 async login(user: string) { 24 return this.lock.lockAsync(() => { 25 this.isLogin = true; 26 this.loginUser = user 27 }, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE) 28 } 29 30 async logout(user?: string) { 31 return this.lock.lockAsync(() => { 32 this.isLogin = false 33 this.loginUser = "" 34 }, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE) 35 } 36 37 async getIsLogin(): Promise<boolean> { 38 return this.lock.lockAsync(() => { 39 return this.isLogin 40 }, ArkTSUtils.locks.AsyncLockMode.SHARED) 41 } 42 43 async getUser(): Promise<string> { 44 return this.lock.lockAsync(() => { 45 return this.loginUser! 46 }, ArkTSUtils.locks.AsyncLockMode.SHARED) 47 } 48 49 async setWifiState(state: boolean) { 50 return this.lock.lockAsync(() => { 51 this.wifiOn = state 52 }, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE) 53 } 54 55 async isWifiOn() { 56 return this.lock.lockAsync(() => { 57 return this.wifiOn; 58 }, ArkTSUtils.locks.AsyncLockMode.SHARED) 59 } 60 } 61 62 export let config = new Config() 63 ``` 64 <!-- @[global_config](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ApplicationMultithreadingDevelopment/PracticalCases/entry/src/main/ets/managers/Config.ets) --> 65 662. Enable both the UI main thread and child threads to access the global configuration. 67 68 ```ts 69 import { config } from './Config' 70 import { taskpool } from '@kit.ArkTS'; 71 72 @Concurrent 73 async function download() { 74 if (!await config.isWifiOn()) { 75 console.info("wifi is off") 76 return false; 77 } 78 if (!await config.getIsLogin()) { 79 console.info("not login") 80 return false; 81 } 82 console.info(`User[${await config.getUser()}] start download ...`) 83 return true; 84 } 85 86 @Entry 87 @Component 88 struct Index { 89 @State message: string = 'not login'; 90 @State wifiState: string = "wifi off"; 91 @State downloadResult: string = ""; 92 input: string = "" 93 94 build() { 95 Row() { 96 Column() { 97 Text(this.message) 98 .fontSize(50) 99 .fontWeight(FontWeight.Bold) 100 .alignRules({ 101 center: { anchor: '__container__', align: VerticalAlign.Center }, 102 middle: { anchor: '__container__', align: HorizontalAlign.Center } 103 }) 104 TextInput({ placeholder: "Enter user name" }) 105 .fontSize(20) 106 .fontWeight(FontWeight.Bold) 107 .alignRules({ 108 center: { anchor: '__container__', align: VerticalAlign.Center }, 109 middle: { anchor: '__container__', align: HorizontalAlign.Center } 110 }) 111 .onChange((value) => { 112 this.input = value; 113 }) 114 Text("login") 115 .fontSize(50) 116 .fontWeight(FontWeight.Bold) 117 .alignRules({ 118 center: { anchor: '__container__', align: VerticalAlign.Center }, 119 middle: { anchor: '__container__', align: HorizontalAlign.Center } 120 }) 121 .onClick(async () => { 122 if (!await config.getIsLogin() && this.input) { 123 this.message = "login: " + this.input 124 config.login(this.input) 125 } 126 }) 127 .backgroundColor(0xcccccc) 128 Text("logout") 129 .fontSize(50) 130 .fontWeight(FontWeight.Bold) 131 .alignRules({ 132 center: { anchor: '__container__', align: VerticalAlign.Center }, 133 middle: { anchor: '__container__', align: HorizontalAlign.Center } 134 }) 135 .onClick(async () => { 136 if (await config.getIsLogin()) { 137 this.message = "not login" 138 config.logout() 139 } 140 }) 141 .backgroundColor(0xcccccc) 142 Text(this.wifiState) 143 .fontSize(50) 144 .fontWeight(FontWeight.Bold) 145 .alignRules({ 146 center: { anchor: '__container__', align: VerticalAlign.Center }, 147 middle: { anchor: '__container__', align: HorizontalAlign.Center } 148 }) 149 Toggle({ type: ToggleType.Switch }) 150 .onChange(async (isOn: boolean) => { 151 await config.setWifiState(isOn) 152 this.wifiState = isOn ? "wifi on" : "wifi off"; 153 }) 154 Text("download") 155 .fontSize(50) 156 .fontWeight(FontWeight.Bold) 157 .alignRules({ 158 center: { anchor: '__container__', align: VerticalAlign.Center }, 159 middle: { anchor: '__container__', align: HorizontalAlign.Center } 160 }) 161 .onClick(async () => { 162 let ret = await taskpool.execute(download) 163 this.downloadResult = ret ? "download success" : "download fail"; 164 }) 165 Text(this.downloadResult) 166 .fontSize(20) 167 .fontWeight(FontWeight.Bold) 168 .alignRules({ 169 center: { anchor: '__container__', align: VerticalAlign.Center }, 170 middle: { anchor: '__container__', align: HorizontalAlign.Center } 171 }) 172 } 173 .width('100%') 174 } 175 .height('100%') 176 } 177 } 178 ``` 179 <!-- @[access_global_config](https://gitee.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ApplicationMultithreadingDevelopment/PracticalCases/entry/src/main/ets/managers/GlobalConfigurationGuide.ets) --> 180