useDataSourceScope
Scopes DataSourceCollection mutations to the component lifecycle: when the component unmounts, every data source added through add is removed automatically. Data sources are usually loaded asynchronously and keep loading on the viewer if left unmanaged; add accepts a Promise (the resolved data source joins the collection automatically), and for declarative data source loading prefer useDataSource.
WARNING
This is a low-level helper for custom collection management. Prefer the high-level hook (useDataSource) unless you need custom collection management.
Usage
ts
import { CustomDataSource } from 'cesium';
import { useDataSourceScope } from 'vesium';
const { add } = useDataSourceScope({
destroyOnRemove: true, // Used for the unmount cleanup: destroy the data source on removal
});
const dataSource = add(new CustomDataSource('demo'));
add(Promise.resolve(new CustomDataSource('async-demo'))); // add accepts a PromiseReturn Value
add(instance)- Adds a data source; accepts a Promise and joins the collection once it resolves; throwscollection is not definedwhen no collection is availableremove(instance, destroy?)- Removes the data source;destroyis forwarded todataSources.remove; returns whether the removal succeededscope- A readonly reactiveSetof the added data sources
Notes
- The target collection defaults to
useViewer().value.dataSources; pass a customDataSourceCollectionviacollection(accepts a ref or getter). destroyOnRemoveis also used as the second argument ofdataSources.remove(dataSource, destroy)during unmount cleanup.
Type Definitions
typescript
import type { CesiumDataSource } from '@vesium/shared';
import type { DataSourceCollection } from 'cesium';
import type { MaybeRefOrGetter } from 'vue';
export interface UseDataSourceScopeOptions {
/**
* The collection of DataSource to be added
* @default useViewer().value.dataSources
*/
collection?: MaybeRefOrGetter<DataSourceCollection | undefined>;
/**
* The second parameter passed to the `remove` function
*
* `dataSources.remove(dataSource,destroyOnRemove)`
*/
destroyOnRemove?: boolean;
}
/**
* Scope the SideEffects of `DataSourceCollection` operations and automatically remove them when unmounted
*/
export declare function useDataSourceScope(options?: UseDataSourceScopeOptions): import("..").UseCollectionScopeReturn<CesiumDataSource, [], [destroy?: boolean | undefined], boolean>;