跳至内容

useCollectionScope

将 Cesium 相关的 Collection 副作用限制在当前作用域内,并在卸载时自动移除。

WARNING

这是一个底层基础函数,旨在由其他更高级的函数(如 useEntityScope)调用。除非你需要实现自定义的集合管理逻辑,否则建议优先使用更高级的 Hook。

Usage

ts
import { useCollectionScope } from 'vesium';

const { add, remove, scope } = useCollectionScope({
  addEffect: instance => viewer.entities.add(instance),
  removeEffect: instance => viewer.entities.remove(instance)
});

const entity = add({ id: 'test' });
// 当组件卸载时,entity 会自动从 viewer.entities 中移除

Type Definitions

typescript
import type { ShallowReactive } from 'vue';
export type EffcetRemovePredicate<T> = (instance: T) => boolean;
export interface UseCollectionScopeOptions<T, AddArgs extends any[], RemoveArgs extends any[] = [], RemoveReturn = any> {
    /**
     * add SideEffect function.  e.g. `entities.add`
     */
    addEffect: (instance: T | Promise<T>, ...args: AddArgs) => T | Promise<T>;
    /**
     * Clean SideEffect function.  eg.`entities.remove`
     */
    removeEffect: (instance: T, ...args: RemoveArgs) => RemoveReturn;
    /**
     * The parameters to pass for `removeScope` triggered when the component is unmounted
     */
    removeScopeArgs?: RemoveArgs;
}
export interface UseCollectionScopeReturn<T, AddArgs extends any[], RemoveArgs extends any[], RemoveReturn = any> {
    /**
     * A `Set` for storing SideEffect instance,
     * which is encapsulated using `ShallowReactive` to provide Vue's reactive functionality
     */
    scope: Readonly<ShallowReactive<Set<T>>>;
    /**
     * Add SideEffect instance
     */
    add: <R extends T | Promise<T>>(instance: R, ...args: AddArgs) => R extends Promise<infer U> ? Promise<U> : T;
    /**
     * Remove specified SideEffect instance
     */
    remove: (instance: T, ...args: RemoveArgs) => RemoveReturn;
    /**
     * Remove all SideEffect instance that meets the specified criteria
     */
    removeWhere: (predicate: EffcetRemovePredicate<T>, ...args: RemoveArgs) => void;
    /**
     * Remove all SideEffect instance within current scope
     */
    removeScope: (...args: RemoveArgs) => void;
}
/**
 * Scope the SideEffects of Cesium-related `Collection` and automatically remove them when unmounted.
 * - note: This is a basic function that is intended to be called by other lower-level function
 * @returns Contains side effect addition and removal functions
 */
export declare function useCollectionScope<T, AddArgs extends any[] = any[], RemoveArgs extends any[] = any[], RemoveReturn = any>(options: UseCollectionScopeOptions<T, AddArgs, RemoveArgs, RemoveReturn>): UseCollectionScopeReturn<T, AddArgs, RemoveArgs, RemoveReturn>;
//# sourceMappingURL=index.d.ts.map