扩展
数字空间 SDK 中的各个模块,均由内部的扩展机制开发而来
定义 SDK 模块
tacos-sdk内部默认集成了 space
、 device
、 mode
、 rule
、 plan
、 layout
、 chart
等七个SDK模块,另外你也可以通过 defineSDK
来自定义SDK模块;
step1:通过defineSDK创建自定义模块
import { defineSDK } from "@tslfe/tacos-sdk";
defineSDK({
setup({context, props, emitter}, { onDestroy }){
const api = this.createRequest(driverApi);
const base = this.createInstance(BaseModule);
const list: SpaceMetaData[] = toArray(props.list);
let spaceTree: SpaceTree | null = null;
/**
* 根据ID查询
* @param id
* @returns
*/
const findById = (id: string) => {
const space = list.find((item) => item.id === id);
if (!space) {
return;
}
return this.createInstance(SpaceSDK, { list: space });
};
/**
* 根据节点id获取不包含自身节点的所有父级节点name
* @param id 目标节点id
* @param list 查询树
* @param paths 目标路径数组
*/
const getSpacePathBy = (id: string, list: SpaceNode[], paths: string[]): string[] => {
if (!list.length) return [];
for (const node of list) {
if (node.id === id) return paths;
paths.push(node.name);
if (node?.children?.length) {
const subPaths = getSpacePathBy(id, node.children, paths);
if (subPaths.length) return subPaths;
}
paths.pop();
}
return [];
}
...
return {
list,
findById,
getSpacePathBy,
...
}
}
})
defineSDK
创建的模块内部继承自TacosSDK
对象 ,可通过this
访问该对象上的方法
type TacosSDK = {
version: string;
interceptor: <T extends keyof Interceptor>(type: T) => Interceptor[T];
createInstance: <M extends (...args: any) => any, P>(
sdkCreator: M,
props?: P,
extension?: any
) => ReturnType<M>;
createRequest: <T extends (...args: any) => any>(
api: T,
config?: AxiosRequestConfig
) => ReturnType<T>;
createSocket: <T extends keyof CreateSocketReturn, K extends keyof PayloadMap>(
config: CreateSocketConfig[T],
callback: (res: SocketResponseBody<K, PayloadMap[K]>) => void,
type?: T
) => () => void;
// 销毁
$destroy: () => void;
};
参数
version
:sdk
模块版本号;intercepter
: 创建局部的ajax
/socket
拦截器;createInstance
: 创建sdk
子类实例对象;createRequest
: 创建请求api
对象;createSocket
: 创建socket
对象;$destory
: 清除sdk
模块依赖的模块列表以及事件监听列表;
step2:创建sdk模块
Tacos.connect((core)=> core.createInstacne(SpaceSDK, { list }))
或
defineSDK({
setup(){
this.createInstacne(SpaceSDK, { list })
}
})
定义 API 模块
通过 defineApi
自定义 api
模块对象, 其内部继承自Request对象
const Api = defineApi({
getList(xpath: string, spaceId?: string[]) {
return this.get(URL.getList, { xpath, spaceId: spaceId?.jodefineSDKin(",") });
},
getDrivers(names?: string[]) {
return this.get(URL.getDrivers, { drivers: names?.join(",") });
},
execCommands(params: CommandParams[]) {
return this.post(URL.execCommands, params);
}
...
});
...
defineSDK({
setup(){
const api = this.createRequest(Api);
api.getList('xxxx/xxx');
}
})
扩展其它模块
通过 defineExtension
来扩展其他SDK模块
const ModeSDK = defineExtension(spaceSDK, {
setup({ props }: { props: SpaceModuleProps }) {
const api = this.createRequest(driverApi);
const list = toArray(props.list);
// const spaceIds = [...new Set(list.map((mode) => mode.spaceId))].join(",");
// eslint-disable-next-line @typescript-eslint/no-empty-function
const update = () => {};
/**
* 模型切换
* @param name
*/
const toggle = (name: string) => {
const spaceId = list.reduce((list: string[], item) => {
if (!list.includes(item.spaceId) && item.type === name) {
list.push(item.spaceId);
}
return list;
}, []);
return api.toggleMode(name, spaceId);
};
return {
toggle,
update
};
}
});