经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
鸿蒙极速入门(六)-加载请求状态管理-LoadState+观察者模式
来源:cnblogs  作者:柳云居士  时间:2023/11/1 9:00:50  对本文有异议

背景

1、在ArkTS的架构中,没有明确的可管理的加载请求状态的脚手架,在进行网络请求过程中,无法简单的进行交互响应。

2、参考Android中的LoadState写了一个简单的脚手架,以便在日常开发过程中,管理加载请求状态和UI交互。

脚手架说明与源码

1、状态机LoadState

使用一个状态机,分别对应网络请求过程中的Loading(发起请求)、Loaded(请求成功)、LoadError(请求失败)状态,并支持链式调用:

  1. /**
  2. * 网络请求MVVM数据模型,由子类实现状态机管理,由方法实现回调监听
  3. */
  4. export abstract class LoadState {
  5. /**
  6. * loading函数,如果当前状态是Loading,则调用回调函数
  7. * @param callBack 回调函数
  8. * @returns this
  9. */
  10. loading(callBack?: () => void): this {
  11. if (this instanceof Loading) {
  12. callBack?.call(null);
  13. }
  14. return this;
  15. }
  16. /**
  17. * loaded函数,如果当前状态是Loaded,则调用回调函数
  18. * @param callBack 回调函数
  19. * @returns this
  20. */
  21. loaded(callBack?: (result: Loaded<any>) => void): this {
  22. if (this instanceof Loaded) {
  23. callBack?.call(null, this);
  24. }
  25. return this;
  26. }
  27. /**
  28. * loadError函数,如果当前状态是LoadError,则调用回调函数
  29. * @param callBack 回调函数
  30. * @returns this
  31. */
  32. loadError(callBack?: (error: LoadError) => void): this {
  33. if (this instanceof LoadError) {
  34. callBack?.call(null, this);
  35. }
  36. return this;
  37. }
  38. }
  39. /**
  40. * Loading类,继承自LoadState类
  41. */
  42. export class Loading extends LoadState {}
  43. /**
  44. * Loaded类,继承自LoadState类,包含一个result属性和一个data方法
  45. */
  46. export class Loaded<T> extends LoadState {
  47. result?: T;
  48. constructor(data: T) {
  49. super();
  50. this.result = data;
  51. }
  52. data(): T | undefined {
  53. return this.result;
  54. }
  55. }
  56. /**
  57. * LoadError类,继承自LoadState类,包含code和message属性
  58. */
  59. export class LoadError extends LoadState {
  60. code?: number;
  61. message?: string;
  62. constructor(code: number, message: string) {
  63. super();
  64. this.code = code;
  65. this.message = message;
  66. }
  67. }

2、观察者模式

ArtTS没有提供开箱即用的观察者模式框架,也无法直接使用RxJS框架,所以自己手写一个简单的ValueNotifier作为观察者实现类:

  1. /**
  2. * ValueNotifier类,包含_value、listeners属性和addListener、notifyListeners、value方法
  3. */
  4. export class ValueNotifier<T> {
  5. private _value: T;
  6. listeners: Array<() => void> = [];
  7. constructor(value: T) {
  8. this._value = value;
  9. }
  10. get value(): T {
  11. return this._value;
  12. }
  13. set value(value: T) {
  14. this._value = value;
  15. this.notifyListeners();
  16. }
  17. addListener(listener: () => void) {
  18. this.listeners.push(listener);
  19. }
  20. notifyListeners() {
  21. for (let listener of this.listeners) {
  22. listener();
  23. }
  24. }
  25. }

使用示例

以获取一个车辆详情的场景来模拟网络请求和数据处理

1、ViewModel

  1. import { Loaded, LoadError, Loading, LoadState, ValueNotifier } from './LoadState';
  2. export class VehicleViewModel {
  3. lsVehicleDetail: ValueNotifier<LoadState | null>;
  4. constructor() {
  5. this.lsVehicleDetail = new ValueNotifier<LoadState | null>(null);
  6. }
  7. // 获取车辆详情
  8. async getVehicleDetail() {
  9. // 发起请求
  10. this.lsVehicleDetail.value = new Loading();
  11. await new Promise(resolve => setTimeout(resolve, 3000));
  12. // 获得数据
  13. this.lsVehicleDetail.value = new Loaded("aa");
  14. await new Promise(resolve => setTimeout(resolve, 3000));
  15. // 模拟网络报错
  16. this.lsVehicleDetail.value = new LoadError(123, "error");
  17. }
  18. }

2、页面处理

  1. @Component
  2. export struct VehicleComponent {
  3. private vm: VehicleViewModel = new VehicleViewModel();
  4. aboutToAppear() {
  5. this.vm.lsVehicleDetail.addListener(() => {
  6. this.vm.lsVehicleDetail.value?.loading(() => {
  7. // 开始网络请求
  8. console.log(`hello1:start Loading`);
  9. }).loaded((result) => {
  10. let data = result?.data() as String
  11. console.log(`hello2:${result} - ${data}`);
  12. }).loadError((error) => {
  13. console.log(`hello3:${error?.code} - ${error?.message}`);
  14. });
  15. });
  16. }
  17. }

3、日志打印结果

  1. hello1start Loading
  2. hello2:[object Object] - aa
  3. hello3123 - error

原文链接:https://www.cnblogs.com/anywherego/p/17800794.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号