diff --git a/TaskPoolPractice/README.md b/TaskPoolPractice/README.md index e4f966cf6c3b409b4a9c9bed33567c55b63335a8..189099942990009cfa48c904b9110cb2ae80aec4 100644 --- a/TaskPoolPractice/README.md +++ b/TaskPoolPractice/README.md @@ -25,6 +25,7 @@ │ ├──sample4 // 示例4 │ ├──sample5 // 示例5 │ ├──sample6 // 示例6 +│ ├──sample7 // 示例7 │ ├──Index.ets // 首页 │ └──test.ets // 常见问题示例代码 └──entry/src/main/resources // 应用静态资源目录 diff --git a/TaskPoolPractice/entry/src/main/ets/pages/Index.ets b/TaskPoolPractice/entry/src/main/ets/pages/Index.ets index 42aa01fe7612fee39be5c9eb11c040d947cfc87a..5af53f8a6b40662f171702d8b20b291d6c6bd8ac 100644 --- a/TaskPoolPractice/entry/src/main/ets/pages/Index.ets +++ b/TaskPoolPractice/entry/src/main/ets/pages/Index.ets @@ -114,6 +114,21 @@ struct Index { hilog.error(0x000, 'testTag', `pushUrl failed, code=${err.code}, message=${err.message}`); }) }) + + Button('Sample7') + .height(40) + .width('100%') + .onClick(() => { + this.getUIContext() + .getRouter() + .pushUrl({ url: "pages/sample7/Sample7" }) + .then(() => { + hilog.info(0x000, 'testTag', 'pushUrl success'); + }) + .catch((err: BusinessError) => { + hilog.error(0x000, 'testTag', `pushUrl failed, code=${err.code}, message=${err.message}`); + }) + }) } .height('100%') .width('100%') diff --git a/TaskPoolPractice/entry/src/main/ets/pages/sample7/AsonParse.ets b/TaskPoolPractice/entry/src/main/ets/pages/sample7/AsonParse.ets new file mode 100644 index 0000000000000000000000000000000000000000..92aabeae546241fb3793a374b52c8b7df23517c0 --- /dev/null +++ b/TaskPoolPractice/entry/src/main/ets/pages/sample7/AsonParse.ets @@ -0,0 +1,24 @@ +// [Start AsonParse] +// AsonParse.ets +import { ArkTSUtils } from '@kit.ArkTS'; + +@Sendable +class A1 { + data?: string; +} + +@Sendable +class A2 { + data?: string +} + +@Sendable +class B { + a1?: A1; + a2?: A2; + data?: string; +} + +let result = ArkTSUtils.ASON.parse('{ "a2" : { "data": "Data is here" } }') as B; +console.log(result.a2?.data); +// [End AsonParse] \ No newline at end of file diff --git a/TaskPoolPractice/entry/src/main/ets/pages/sample7/AxiosAdapter.ets b/TaskPoolPractice/entry/src/main/ets/pages/sample7/AxiosAdapter.ets new file mode 100644 index 0000000000000000000000000000000000000000..5d49c9d6defb448351a56a82f2a1adf4ad3c84cf --- /dev/null +++ b/TaskPoolPractice/entry/src/main/ets/pages/sample7/AxiosAdapter.ets @@ -0,0 +1,37 @@ +// [Start AxiosAdapter] +// AxiosAdapter.ets +import axios, { Axios, AxiosInstance, AxiosProxyConfig, AxiosResponse, InternalAxiosRequestConfig } from '@ohos/axios'; +import { AxiosGlobalConfig } from './AxiosConfig'; + +export function getAxios(): Axios { + let instance: AxiosInstance = axios.create(); + if (AxiosGlobalConfig.instance.baseURL) { + instance.defaults.url = AxiosGlobalConfig.instance.baseURL; + } + for (let entry of AxiosGlobalConfig.instance.headers.entries()) { + instance.defaults.headers[entry[0]] = entry[1]; + } + if (AxiosGlobalConfig.instance.timeout) { + instance.defaults.timeout = AxiosGlobalConfig.instance.timeout; + } + if (AxiosGlobalConfig.instance.proxy) { + let config: AxiosProxyConfig = { + host: AxiosGlobalConfig.instance.proxy.host, + port: AxiosGlobalConfig.instance.proxy.port, + exclusionList: [] + }; + instance.defaults.proxy = config; + } + for (let interceptor of AxiosGlobalConfig.instance.requestInterceptors.values()) { + axios.interceptors.request.use((config: InternalAxiosRequestConfig): InternalAxiosRequestConfig + | Promise> => interceptor.handle(config), + (error: object) => interceptor.handleError(error)); + } + for (let interceptor of AxiosGlobalConfig.instance.responseInterceptors.values()) { + axios.interceptors.response.use((response: AxiosResponse): AxiosResponse + | Promise> => interceptor.handle(response), + (error: object) => interceptor.handleError(error)); + } + return axios; +} +// [End AxiosAdapter] \ No newline at end of file diff --git a/TaskPoolPractice/entry/src/main/ets/pages/sample7/AxiosConfig.ets b/TaskPoolPractice/entry/src/main/ets/pages/sample7/AxiosConfig.ets new file mode 100644 index 0000000000000000000000000000000000000000..18941032a9db9fd1df64165f38f8579f2230bf7f --- /dev/null +++ b/TaskPoolPractice/entry/src/main/ets/pages/sample7/AxiosConfig.ets @@ -0,0 +1,62 @@ +// [Start AxiosConfig] +// AxiosConfig.ets +import { collections, lang } from '@kit.ArkTS'; +import { AxiosResponse, InternalAxiosRequestConfig } from '@ohos/axios'; + +export interface IRequestInterceptor extends lang.ISendable { + handle(data: InternalAxiosRequestConfig): InternalAxiosRequestConfig + | Promise>; + handleError(error: object): object; +} + +export interface IResponseInterceptor extends lang.ISendable { + handle(data: AxiosResponse): AxiosResponse + | Promise>; + handleError(error: object): object; +} + +export interface IAuth extends lang.ISendable { + username: string; + password: string; +} + +export interface IHttpProxy extends lang.ISendable { + protocol: string; + host: string; + port: number; + auth: IAuth; + exclusionList: collections.Array; +} + +@Sendable +export class AxiosGlobalConfig { + private constructor() { + this.requestInterceptors = new collections.Array; + this.responseInterceptors = new collections.Array; + this.headers = new collections.Map; + } + + baseURL?: string; + headers: collections.Map; + timeout?: number; + proxy?: IHttpProxy; + xsrfCookieName?: string; + xsrfHeaderName?: string; + maxContentLength?: number; + maxBodyLength?: number; + // [StartExclude AxiosConfig] + // [EndExclude AxiosConfig] + + requestInterceptors: collections.Array; + responseInterceptors: collections.Array; + static instance: AxiosGlobalConfig = new AxiosGlobalConfig(); +} + +@Sendable +export class ImageInfo { + code?: number; + imgurl?: string; + width?: string; + height?: string; +} +// [End AxiosConfig] \ No newline at end of file diff --git a/TaskPoolPractice/entry/src/main/ets/pages/sample7/Interceptor.ets b/TaskPoolPractice/entry/src/main/ets/pages/sample7/Interceptor.ets new file mode 100644 index 0000000000000000000000000000000000000000..a68ff75b9ec9baa09b47cf2c93de6238069d7d39 --- /dev/null +++ b/TaskPoolPractice/entry/src/main/ets/pages/sample7/Interceptor.ets @@ -0,0 +1,27 @@ +// [Start Interceptor] +// Interceptor.ets +import { AxiosResponse, InternalAxiosRequestConfig } from '@ohos/axios'; +import { IRequestInterceptor, IResponseInterceptor } from './AxiosConfig'; + +@Sendable +export class RequestInterceptor implements IRequestInterceptor { + handle(data: InternalAxiosRequestConfig): InternalAxiosRequestConfig + | Promise> { + return data; + } + handleError(error: object): object { + return error; + } +} + +@Sendable +export class ResponseInterceptor implements IResponseInterceptor { + handle(data: AxiosResponse): AxiosResponse + | Promise> { + return data; + } + handleError(error: object): object { + return error; + } +} +// [End Interceptor] \ No newline at end of file diff --git a/TaskPoolPractice/entry/src/main/ets/pages/sample7/LightArt.ets b/TaskPoolPractice/entry/src/main/ets/pages/sample7/LightArt.ets new file mode 100644 index 0000000000000000000000000000000000000000..2a683a1d475e35b3288fe7d55fbb6f9f79c220bd --- /dev/null +++ b/TaskPoolPractice/entry/src/main/ets/pages/sample7/LightArt.ets @@ -0,0 +1,58 @@ +// [Start LightArt] +// LightArt.ets +import { lang } from '@kit.ArkTS'; + +@Sendable +class LightArtDataModel {} + +@Sendable +class LightArtDataDocument {} + +@Sendable +class LightArtViewModel {} + +class UI {} + +interface ISerializableType extends lang.ISendable {} + +@Sendable +class LightArtDataComponent { + dataModel?: LightArtDataModel; + document?: LightArtDataDocument; + parent?: LightArtDataComponent; + components?: LightArtDataComponent; + // [StartExclude LightArt] + // [EndExclude LightArt] + fromJson(jsonObj: object) { + // [StartExclude LightArt] + // [EndExclude LightArt] + } + + mergeFrom(jsonObj: object) { + // [StartExclude LightArt] + // [EndExclude LightArt] + } +} + +@Sendable +class LightArtUIComponent { + data?: LightArtDataComponent; + model?: LightArtViewModel; + parent?: LightArtUIComponent; + components?: LightArtUIComponent; + uiData?: UI; + // [StartExclude LightArt] + // [EndExclude LightArt] +} + +// 把数据填充到LightArtDataComponent. +@Sendable +export class LightArtDataComponentType implements ISerializableType { + fromJson(jsonObj: object): LightArtDataComponent { + let ans = new LightArtDataComponent(); + ans.mergeFrom(jsonObj); + return ans; + } + static instance: LightArtDataComponentType = new LightArtDataComponentType(); +} +// [End LightArt] \ No newline at end of file diff --git a/TaskPoolPractice/entry/src/main/ets/pages/sample7/Sample7.ets b/TaskPoolPractice/entry/src/main/ets/pages/sample7/Sample7.ets new file mode 100644 index 0000000000000000000000000000000000000000..f25803a07016575b2c4c3575ba4dd3ebcf643d89 --- /dev/null +++ b/TaskPoolPractice/entry/src/main/ets/pages/sample7/Sample7.ets @@ -0,0 +1,36 @@ +// [Start old_code] +// Sample7.ets +@Sendable +class DataModel { + +} +@Observed +class Wrapper { + data?: DataModel; +} +// [End old_code] + +@Entry +@Component +struct Sample7 { + @State message: string = 'Hello World'; + + build() { + Row() { + Column({ space: 12 }) { + Blank() + Button('Sample&Hello World') + .height(40) + .width('100%') + .onClick(() => { + this.message = "Welcome"; + }) + } + .height('100%') + .width('100%') + .padding(16) + .justifyContent(FlexAlign.Center) + } + .height('100%') + } +} \ No newline at end of file diff --git a/TaskPoolPractice/entry/src/main/ets/pages/sample7/SendableCollections.ets b/TaskPoolPractice/entry/src/main/ets/pages/sample7/SendableCollections.ets new file mode 100644 index 0000000000000000000000000000000000000000..dee7ef26c9d25a53c7f30c70f97d10dbc94a2304 --- /dev/null +++ b/TaskPoolPractice/entry/src/main/ets/pages/sample7/SendableCollections.ets @@ -0,0 +1,74 @@ +// [Start sebdabke_collection] +// SendableCollections.ets +import { collections } from '@kit.ArtTS'; +// [End sebdabke_collection] + +// [Start array_from] +let arr: number[] = []; +let colArr: collections.Array = collections.Array.from(arr); + +// collections.Array -> [] +arr = Array.from(colArr); +// for loop使用上的区别 +for (let item of arr) { + console.log(item); +} +for (let item of colArr.values()) { + console.log(item); +} +// [End array_from] + +// [Start array_splice] +function splice(data: collections.Array, start: number, deleteCount: number, ...items: T[]): void { + if (start < -data.length) { + start = 0; + } else if (start > data.length) { + start = data.length; + } + if (data.length !== 0) { + start = (start + data.length) % data.length; + } + deleteCount = Math.min(data.length - start, deleteCount); + if (deleteCount > items.length) { + for (let i = 0; start + deleteCount + i < data.length; i++) { + data[start + items.length + i] = data[start + deleteCount + i]; + } + data.shrinkTo(data.length + items.length - deleteCount); + } else if (deleteCount < items.length) { + let oldLen = data.length; + data.extendTo(oldLen + items.length - deleteCount, items[0]); + for (let j = oldLen - 1, k = data.length - 1; j >= start + deleteCount; j--, k--) { + data[k] = data[j]; + } + } + for (let i = 0; i < items.length; i++) { + data[start + i] = items[i]; + } +} +// [End array_splice] + +// [Start map] +// collections.Map -> Map +let colMap: collections.Map = new collections.Map([ + [1, "a"], + [2, "b"], + [3, "c"] +]); +let map: Map = new Map(colMap.entries()); + +// Map to collections.Map +function toMap(map: Map) { + let colMap = new collections.Map(); + map.forEach((value, key) => colMap.set(key, value)); + return colMap; +} + +colMap = toMap(map); +// forEach迭代 (TS/ArkTS Map与collections.Map能力等同) +map.forEach((value, key) => { + console.log(`${key}-${value}`); +}) +colMap.forEach((value, key) => { + console.log(`${key}-${value}`); +}) +// [End map] \ No newline at end of file diff --git a/TaskPoolPractice/entry/src/main/ets/pages/sample7/SendableFunc.ets b/TaskPoolPractice/entry/src/main/ets/pages/sample7/SendableFunc.ets new file mode 100644 index 0000000000000000000000000000000000000000..4d970f6f6416de8be60e06b322ea8fd009aac141 --- /dev/null +++ b/TaskPoolPractice/entry/src/main/ets/pages/sample7/SendableFunc.ets @@ -0,0 +1,59 @@ +// [Start sendable_func] +// SendableFunc.ets +import { lang } from '@kit.ArkTS'; +// [End sendable_func] + +// [Start sendable_interface] +// SendableFunc.ets +interface TestInterface { + test(): void; +} + +@Sendable +class SendableTest implements TestInterface { + test(): void { + console.log('This is a sendable test'); + } +} + +class NonSendableTest implements TestInterface { + test(): void { + console.log('This is a non sendable test'); + } +} +// [End sendable_interface] + +// [Start normal_func] +// SendableFunc.ets +class NormalClass { + constructor(func: () => void) { + this.testFunc = func; + } + testFunc: () => void; +} +let normalClass = new NormalClass(() => { console.log('This is a test') }); +normalClass.testFunc(); +// [End normal_func] + +// [Start sendable_func] +interface TestFuncInterface extends lang.ISendable { + fn(): void; +} + +@Sendable +class SendableTestClass { + constructor(func: TestFuncInterface) { + this.testFunc = func; + } + testFunc: TestFuncInterface; +} + +@Sendable +class SendableTestFunc implements TestFuncInterface { + fn(): void { + console.log('This is a sendable test') + } +} +let sendableTestClass = new SendableTestClass(new SendableTestFunc()); +sendableTestClass.testFunc.fn(); +// [End sendable_func] \ No newline at end of file diff --git a/TaskPoolPractice/entry/src/main/ets/pages/sample7/TestAxios.ets b/TaskPoolPractice/entry/src/main/ets/pages/sample7/TestAxios.ets new file mode 100644 index 0000000000000000000000000000000000000000..0b5d079a4999277925f5c7bc482ab89fddecd9e9 --- /dev/null +++ b/TaskPoolPractice/entry/src/main/ets/pages/sample7/TestAxios.ets @@ -0,0 +1,42 @@ +// [Start axios] +// TestAxios.ets +import { ArkTSUtils, taskpool } from '@kit.ArkTS'; +import { AxiosError, AxiosResponse } from '@ohos/axios'; +import { getAxios } from './AxiosAdapter'; +import { AxiosGlobalConfig, ImageInfo, IRequestInterceptor, IResponseInterceptor } from './AxiosConfig'; +import { RequestInterceptor, ResponseInterceptor } from './Interceptor'; + +function initAxios(url: string): void { + // init axios + AxiosGlobalConfig.instance.baseURL = url; + let requestInterceptors: IRequestInterceptor = new RequestInterceptor(); + let responseInterceptors: IResponseInterceptor = new ResponseInterceptor(); + AxiosGlobalConfig.instance.requestInterceptors.push(requestInterceptors); + AxiosGlobalConfig.instance.responseInterceptors.push(responseInterceptors); + AxiosGlobalConfig.instance.timeout = 1000; +} + +@Concurrent +function testAxios(config: AxiosGlobalConfig) { + AxiosGlobalConfig.instance = config; + let adapter = getAxios(); + adapter.get(AxiosGlobalConfig.instance.baseURL).then((res: AxiosResponse) => { + if (res.status === 200) { + // Convert JSON to ASON + let result = ArkTSUtils.ASON.parse(JSON.stringify(res.data)) as ImageInfo; + console.log("imageUrl: " + result?.imgurl); + } + }).catch((error: AxiosError) => { + console.error("error: " + error.message); + }) +} + +function demo() { + let url = ""; // internet url + initAxios(url); + for (let i = 0; i < 1; i++) { + let task: taskpool.Task = new taskpool.Task(testAxios, AxiosGlobalConfig.instance); + taskpool.execute(task); + } +} +// [End axios] \ No newline at end of file diff --git a/TaskPoolPractice/entry/src/main/resources/base/profile/main_pages.json b/TaskPoolPractice/entry/src/main/resources/base/profile/main_pages.json index 9754f0ec1ff11a3d1b1b6deb8b8555bbd1755f34..2d690d88e1fc521a322fed13d0ace267679d8c50 100644 --- a/TaskPoolPractice/entry/src/main/resources/base/profile/main_pages.json +++ b/TaskPoolPractice/entry/src/main/resources/base/profile/main_pages.json @@ -6,6 +6,7 @@ "pages/sample3/Sample3", "pages/sample4/Sample4", "pages/sample5/Sample5", - "pages/sample6/Sample6" + "pages/sample6/Sample6", + "pages/sample6/Sample7" ] } \ No newline at end of file