首页 > 编程语言 >智能小程序 Ray 开发面板 SDK —— 状态管理 SDK 汇总(二)

智能小程序 Ray 开发面板 SDK —— 状态管理 SDK 汇总(二)

时间:2024-08-14 11:27:20浏览次数:14  
标签:... const network ts export SDK import 面板 Ray

Redux

  • src 业务源码目录
    • constant 业务项目常量目录
      • index.ts 常量定义文件
    • devices 智能设备模型目录
      • index.ts 定义并导出智能设备模型
      • schema.ts 当前智能设备 DP 功能点描述
    • pages 页面目录
      • home 首页
        • index.tsx 首页组件
    • redux Redux 目录
      • actions Redux Actions 目录
        • network.ts Network Actions
      • reducers Redux Reducers 目录
        • network.ts Network Reducers
      • index.ts Redux 入口
      • store.ts Redux Store 定义
    • app.tsx App 根组件

创建 Actions

// src/constant/index.ts
export const INIT_NETWORK = 'INIT_NETWORK';
export const CHANGE_NETWORK = 'CHANGE_NETWORK';
// src/redux/actions/network.ts
import { SmartDeviceModelNetwork } from '@ray-js/panel-sdk';
import { INIT_NETWORK, CHANGE_NETWORK } from '@/constant';
 
type UpdatePayload = Partial<SmartDeviceModelNetwork>;
 
const initNetwork = (payload: UpdatePayload) => {
  return {
    type: INIT_NETWORK,
    payload,
  };
};
 
const changeNetwork = (payload: UpdatePayload) => {
  return {
    type: CHANGE_NETWORK,
    payload,
  };
};
 
export const actions = {
  initNetwork,
  changeNetwork,
};

创建 Reducers

// src/redux/reducers/network.ts
import { SmartDeviceModel, SmartDeviceModelNetwork } from '@ray-js/panel-sdk';
import { INIT_NETWORK, CHANGE_NETWORK } from '@/constant';
import { devices } from '@/devices';
import { actions } from '../actions/network';
import { store } from '../store';
 
export type Actions = {
  [K in keyof typeof actions]: ReturnType<typeof actions[K]>;
};
 
SmartDeviceModel.onInitialized(() => {
  /**
   * 这里 setTimeout 是为了保证在 devices.xxx 挂载上去以后再去 dispatch
   */
  setTimeout(() => {
    store.dispatch(actions.initNetwork({ ...devices.robot.network }));
    devices.robot.onNetworkStatusChange((data) => {
      store.dispatch(actions.changeNetwork(data));
    });
  }, 0);
});
 
const network = (
  state = {} as Partial<SmartDeviceModelNetwork>,
  action: Actions['changeNetwork'],
) => {
  switch (action.type) {
    case INIT_NETWORK: {
      return {
        ...state,
        ...action.payload,
      };
    }
    case CHANGE_NETWORK: {
      return {
        ...state,
        ...action.payload,
      };
    }
    default:
      return state;
  }
};
 
export const reducers = {
  network,
};

定义 Store

// src/redux/store.ts
import { applyMiddleware, combineReducers, compose, createStore } from 'redux';
import { createLogger } from 'redux-logger';
import thunk from 'redux-thunk';
import { reducers as commonReducers } from './reducers/common';
import { reducers as networkReducers } from './reducers/network';
 
const reducers = {
  ...commonReducers,
  ...networkReducers,
};
 
type Reducers = typeof reducers;
 
export type ReduxState = { [K in keyof Reducers]: ReturnType<Reducers[K]> };
 
export const rootReducers = combineReducers(reducers);
 
const isDebuggingInChrome = !!window.navigator.userAgent;
const logger = createLogger({
  predicate: () => true,
  collapsed: true,
  duration: true,
});
 
const middleware = isDebuggingInChrome ? [thunk, logger] : [thunk];
 
function configureStore(initialState?: Partial<ReduxState>) {
  const appliedMiddleware = applyMiddleware(...middleware);
 
  const store = createStore(
    rootReducers,
    initialState,
    compose(appliedMiddleware),
  );
  return store;
}
 
export const store = configureStore();

使用 State

// src/pages/home/index.tsx
import React from 'react';
import { View } from '@ray-js/ray';
import { useSelector } from '@/redux';
 
export default function PageAppState() {
  const network = useSelector((state) => state.network);
  return (
    <View style={{ flex: 1 }}>
      {Object.keys(network || {}).map((key) => {
        return <View key={key}>{`network.${key}: ${network[key]}`}</View>;
      })}
    </View>
  );
}

标签:...,const,network,ts,export,SDK,import,面板,Ray
From: https://blog.csdn.net/Ms_Smart/article/details/141125942

相关文章

  • Java数组05:Arrays 类
    数组的工具类java.util.Arrays由于数组对象本身并没有什么方法可以供我们调用,但API中提供了一个工具类Arrays供我们使用,从而可以对数据对象进行一些基本的操作。文档简介:此类包含用来操作数组(比如排序和搜索)的各种方法。此类还包含一个允许将数组作为列表来查看的静态工厂。......
  • 【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑
    AzureContainerInstance服务介绍Azure容器实例(AzureContainerInstances,简称ACI)是一个无服务器容器解决方案,允许用户在Azure云环境中运行Docker容器,而无需设置虚拟机、集群或编排器。ACI适用于任何可以在隔离容器中操作的场景,包括事件驱动的应用程序、从容器开发管道......
  • 数组拷贝System.arraycopy
    数组拷贝第一种方式:packagecom.coding.demo.concurrent;importjava.util.Arrays;/***使用Arrays.copyOf()*/publicclassTestArraysCopyOf{publicstaticvoidmain(String[]args){int[]src={1,2,3,4,5,6,7,8,9};int[]dest=Arrays......
  • 4-ArrayList
    ArrayList实现类(JDK1.7)//接口=实现类--->为了扩展性这样做--->因为可以Collectioncol=newLinkedList();Collectioncol=newArrayList();Listlist=newArrayList();//直接创建实现类对象ArrayListal=newArrayList();......
  • 巧用Array.forEach:简化循环与增强代码可读性;Array.forEach怎么用;面对大量数据时怎么提
    目录Vue.js中的Array.forEach:简化循环与增强代码可读性一、引言二、Array.forEach()的使用与技巧1、基本语法2、返回值3、使用Array.forEach()的优势4、Array.forEachvsfor循环5、Array.forEach()使用技巧三、Array.forEach()的应用情景1、复杂数据处理2、实时更......
  • 中移ML307A(4G Cat1,C-SDK,OpenCPU)模组学习开发-UART串口
    <p><iframename="ifd"src="https://mnifdv.cn/resource/cnblogs/ML307A_OPEN"frameborder="0"scrolling="auto"width="100%"height="1500"></iframe></p> 测试1,配置串口0为115200波......
  • pytorch_geometric的Planetoid出现“TypeError: expected np.ndarray (got matrix)”
    问题和解决方案运行GCN的例子的时候,出现了这个错误:out=torch.from_numpy(out).to(torch.float)TypeError:expectednp.ndarray(gotmatrix)解决方案:在torch_geometric.io.planetoid.py中添加importnumpyasnp,将out=torch.from_numpy(out).to(torch.float)......
  • ArrayList集合及例题 day12
    packagecom.shujia.day13;importjava.util.ArrayList;importjava.util.Iterator;/*Collection:-List(有序【指的是存储和取出的顺序是一致的】且可以发生重复,且有索引的概念)-ArrayList:底层数据结构是数组,查询快,增删慢,线程不安......
  • Luckfox开发--SDK环境部署
    前言嵌入式Linux,在我的认识当中一直属于是嵌入式技术上最顶层的技术,之前一直有学习过相关的基础知识,这次打算系统性的记录学习过程,将完整的从零开始,一步步自学提高相关知识。虚拟机安装在学习嵌入式Linux,首先需要的是一个虚拟机,虚拟机软件我接触过两个,一个是VMware,一个是Virtua......
  • 集合:(ArrayList)的插值和去重,包含(Iterator和listIterator)迭代器相关使用
    总结:去重用for循环,插值可用for循环和迭代器(可以方便在中间插值),如果要修改集合,就用listIterator,防止父类的Iterator没有add添加功能,也避免版本号不一致报错去重:用contains方法,确认新集合中是否存在旧值1、基本数据类型String去重publicclassArrayListQuChong{public......