@ulm/core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +163 -0
- package/dist/core.cjs +1 -0
- package/dist/core.d.ts +1293 -0
- package/dist/core.js +908 -0
- package/package.json +54 -0
package/README.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
<img src="./assets/universal-layer-manager.svg" alt="Universal Layer Manager logo" width="160" />
|
|
2
|
+
|
|
3
|
+
# @ulm/core
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@ulm/core)
|
|
6
|
+
[](../../LICENSE)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
|
|
9
|
+
State-machine-powered layer management for map applications. Framework-agnostic core library built on [XState](https://xstate.js.org/).
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @ulm/core
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
`LayerManager` is the primary public API. It wraps the XState machines and starts automatically on construction.
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import type { AddGroupLayerParams, AddLayerParams } from '@ulm/core';
|
|
23
|
+
import { LayerManager } from '@ulm/core';
|
|
24
|
+
|
|
25
|
+
interface LayerData {
|
|
26
|
+
url: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface GroupData {
|
|
30
|
+
category: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const manager = new LayerManager<LayerData, GroupData>({
|
|
34
|
+
allowNestedGroupLayers: true,
|
|
35
|
+
|
|
36
|
+
onLayerAdded(info) {
|
|
37
|
+
console.log('added:', info.layerId);
|
|
38
|
+
},
|
|
39
|
+
onVisibilityChanged(info, visible) {
|
|
40
|
+
console.log(info.layerId, 'visible:', visible);
|
|
41
|
+
},
|
|
42
|
+
onOpacityChanged(info, computedOpacity) {
|
|
43
|
+
console.log(info.layerId, 'opacity:', computedOpacity);
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Add a layer
|
|
48
|
+
manager.addLayer({
|
|
49
|
+
layerConfig: {
|
|
50
|
+
layerId: 'basemap',
|
|
51
|
+
layerName: 'Basemap',
|
|
52
|
+
layerType: 'layer',
|
|
53
|
+
parentId: null,
|
|
54
|
+
layerData: { url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png' },
|
|
55
|
+
},
|
|
56
|
+
visible: true,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Add a group with a child layer
|
|
60
|
+
manager.addGroup({
|
|
61
|
+
layerConfig: {
|
|
62
|
+
layerId: 'overlays',
|
|
63
|
+
layerName: 'Overlays',
|
|
64
|
+
layerType: 'layerGroup',
|
|
65
|
+
parentId: null,
|
|
66
|
+
layerData: { category: 'overlays' },
|
|
67
|
+
},
|
|
68
|
+
visible: true,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
manager.addLayer({
|
|
72
|
+
layerConfig: {
|
|
73
|
+
layerId: 'markers',
|
|
74
|
+
layerName: 'Markers',
|
|
75
|
+
layerType: 'layer',
|
|
76
|
+
parentId: 'overlays',
|
|
77
|
+
layerData: { url: '' },
|
|
78
|
+
},
|
|
79
|
+
visible: true,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Control layers
|
|
83
|
+
manager.setVisibility('basemap', false);
|
|
84
|
+
manager.setOpacity('markers', 0.5);
|
|
85
|
+
manager.removeLayer('markers');
|
|
86
|
+
|
|
87
|
+
// Teardown
|
|
88
|
+
manager.destroy();
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## API
|
|
92
|
+
|
|
93
|
+
### `new LayerManager<TLayer, TGroup>(options?)`
|
|
94
|
+
|
|
95
|
+
`TLayer` is the type of `layerData` stored on each layer; `TGroup` is the type for groups (defaults to `TLayer`).
|
|
96
|
+
|
|
97
|
+
**Options** — all optional:
|
|
98
|
+
|
|
99
|
+
| Option | Type | Description |
|
|
100
|
+
|--------|------|-------------|
|
|
101
|
+
| `allowNestedGroupLayers` | `boolean` | Allow groups inside other groups (default `false`) |
|
|
102
|
+
| `onLayerAdded` | `(info, visible) => void` | Called when a layer or group is added |
|
|
103
|
+
| `onLayerRemoved` | `(layerId) => void` | Called when a layer or group is removed |
|
|
104
|
+
| `onVisibilityChanged` | `(info, visible) => void` | Called on visibility toggle |
|
|
105
|
+
| `onOpacityChanged` | `(info, computedOpacity) => void` | Called when opacity changes (cascades from parents) |
|
|
106
|
+
| `onTimeInfoChanged` | `(info, timeInfo) => void` | Called when a layer's time info changes |
|
|
107
|
+
| `onError` | `(error) => void` | Called on internal errors |
|
|
108
|
+
|
|
109
|
+
The `info` object passed to callbacks is a `ManagedLayerInfo` with two distinct boolean fields:
|
|
110
|
+
|
|
111
|
+
- `enabled` — the user has enabled (`true`) or disabled (`false`) this layer
|
|
112
|
+
- `visible` — the layer is actually rendering, i.e. it is enabled *and* its parent group is not hidden
|
|
113
|
+
|
|
114
|
+
A layer can be `enabled: true` but `visible: false` when a parent group is hidden.
|
|
115
|
+
|
|
116
|
+
**Methods:**
|
|
117
|
+
|
|
118
|
+
| Method | Description |
|
|
119
|
+
|--------|-------------|
|
|
120
|
+
| `addLayer(params)` | Add a single layer |
|
|
121
|
+
| `addGroup(params)` | Add a layer group |
|
|
122
|
+
| `removeLayer(layerId)` | Remove a layer or group by ID |
|
|
123
|
+
| `setVisibility(layerId, visible)` | Show or hide a layer |
|
|
124
|
+
| `setOpacity(layerId, opacity)` | Set opacity (0–1) |
|
|
125
|
+
| `setTimeInfo(layerId, timeInfo)` | Set the time info (`LayerTimeInfo`) for a layer |
|
|
126
|
+
| `updateLayerData(layerId, data)` | Replace the `layerData` payload |
|
|
127
|
+
| `setAdapter(adapter \| null)` | Attach or detach a map-library adapter |
|
|
128
|
+
| `reset()` | Remove all layers and groups |
|
|
129
|
+
| `destroy()` | Teardown — stops the actor and cleans up |
|
|
130
|
+
| `getLayer(id)` | Return the managed item for an ID |
|
|
131
|
+
|
|
132
|
+
**Properties:**
|
|
133
|
+
|
|
134
|
+
| Property | Description |
|
|
135
|
+
|----------|-------------|
|
|
136
|
+
| `layers` | Current top-level items in display order |
|
|
137
|
+
| `actor` | Raw XState actor — escape hatch for `@xstate/react` (`useSelector`, etc.) |
|
|
138
|
+
| `isReady` | `true` while the actor is running |
|
|
139
|
+
| `destroyed` | `true` after `destroy()` |
|
|
140
|
+
|
|
141
|
+
### Lower-level access
|
|
142
|
+
|
|
143
|
+
`createLayerManagerMachine` (and the individual `layerMachine` / `layerGroupMachine`) are exported for use cases where you want to work with the XState actor model directly:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { createLayerManagerMachine } from '@ulm/core';
|
|
147
|
+
import { createActor } from 'xstate';
|
|
148
|
+
|
|
149
|
+
const actor = createActor(createLayerManagerMachine<LayerData>(), {
|
|
150
|
+
input: { allowNestedGroupLayers: true },
|
|
151
|
+
});
|
|
152
|
+
actor.start();
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
If you are using `LayerManager`, the same actor is also available as `manager.actor`.
|
|
156
|
+
|
|
157
|
+
## Adapters
|
|
158
|
+
|
|
159
|
+
To sync layer manager state with a map library, implement `LayerManagerAdapter` and pass it to `manager.setAdapter()`. See [`@ulm/leaflet`](../leaflet/README.md) for a ready-made Leaflet adapter.
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
MIT
|
package/dist/core.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("xstate");function P(e){return e?.type==="single"}function G(e){return e?.type==="range"}function L(e){return e.getSnapshot().context.layerType==="layer"}function g(e){return e.getSnapshot().context.layerType==="layerGroup"}function p(e,t){return e.find(a=>a.layerActor.id===t)}function V(e,t){const a=p(e,t);if(a)return a.layerActor.getSnapshot().context.layerData}function E(e,t){return t.parentId?R(e,t.parentId):null}function R(e,t){const a=p(e,t);return!a||!g(a.layerActor)?null:a.layerActor}function T(e,{layers:t,allowNestedGroupLayers:a}){return e.layerType==="layerGroup"&&e.parentId&&!a?(console.warn("Nested group layers are not allowed."),!1):t.some(r=>r.layerActor.id===e.layerId)?(console.warn(`Layer with ID ${e.layerId} already exists. Layer not added.`),!1):!0}function m(e,t){return e.parentId&&!t?(console.warn("Unable to find valid parent layer. Layer not added."),!1):!0}function b(e){if(!e)return console.warn("Unable to find layer to remove."),!1;const{layerType:t}=e.layerActor.getSnapshot().context;if(t==="layerGroup"){const{children:a}=e.layerActor.getSnapshot().context;if(a.length>0)return console.warn("Layer group has children. Not removed."),!1}return!0}function _(e,t){return e>=0&&e<=t}function I(e,t,a,r){const i=[...e];if(a!==void 0&&_(a,e.length)){const y=a>e.length?e.length:a;i.splice(y,0,t)}else r==="top"?i.push(t):i.unshift(t);return i}function D(e,t){const a=[],r=i=>{i.forEach(y=>{const l=e.find(n=>n.layerActor.id===y);if(!l)return;a.push(y);const o=l.layerActor.getSnapshot();if(o.context.layerType==="layerGroup"){const n=o.context;r(n.childLayerOrder)}})};return r(t),a}function O(e,t){return e.map(a=>t.find(r=>r.layerActor.id===a)).filter(a=>a!==void 0)}function H(e,t){return e.map(a=>t.find(r=>r.id===a)).filter(a=>a!==void 0)}function M(e,t,a,r,i){return a?U(e.layers,t,a,r,i):B(e.layers,t,e.childLayerOrder,r,i)}function Y(e){const{parentRef:t}=e.layerActor.getSnapshot().context;t&&t.send({type:"LAYERS.REMOVE_CHILD",id:e.layerActor.id})}function C(e,t){return{layers:e.layers.filter(a=>a.layerActor.id!==t),childLayerOrder:e.childLayerOrder.filter(a=>a!==t)}}function c(e,t){return e?e.getSnapshot().context.computedOpacity*t:t}function U(e,t,a,r,i){return a.send({type:"LAYERS.ADD_CHILD",child:t.layerActor,index:r,position:i}),{layers:[...e,t]}}function B(e,t,a,r,i){const y=I(a,t.layerActor.id,r,i);return{layers:[...e,t],childLayerOrder:y}}function S(){return s.setup({types:{context:{},events:{},input:{}},actions:{"Notify children of visibility change":s.enqueueActions(({context:e,enqueue:t},a)=>{e.children.forEach(r=>{t.sendTo(r,{type:a.visible?"PARENT.VISIBLE":"PARENT.HIDDEN"})})}),"Notify children of opacity change":s.enqueueActions(({context:e,enqueue:t})=>{e.children.forEach(a=>{t.sendTo(a,{type:"PARENT.OPACITY_CHANGED",opacity:e.computedOpacity})})}),"Notify Parent of visibility change":s.enqueueActions(({context:e,enqueue:t})=>{e.parentRef&&t.sendTo(e.parentRef,{type:"CHILD.VISIBLE",layerId:e.layerId})}),"Notify Manager of visibility change":s.enqueueActions(({context:e,enqueue:t},a)=>{t.sendTo(e.layerManagerRef,{type:"LAYER.UPDATE_VISIBILITY",layerId:e.layerId,visible:a.visible})}),"Change Layer Data":s.enqueueActions(({context:e,enqueue:t},a)=>{t.assign({layerData:a.layerData}),t.sendTo(e.layerManagerRef,{type:"LAYER.UPDATE_LAYER_DATA",layerId:e.layerId,layerData:a.layerData})}),"Update Computed Opacity":s.enqueueActions(({context:e,enqueue:t})=>{const a=c(e.parentRef,e.opacity);t.assign({computedOpacity:a}),e.children.forEach(r=>{t.sendTo(r,{type:"PARENT.OPACITY_CHANGED",opacity:a})}),t.sendTo(e.layerManagerRef,{type:"LAYER.UPDATE_OPACITY",layerId:e.layerId,opacity:e.opacity,computedOpacity:e.computedOpacity})}),"Change Layer Opacity":s.enqueueActions(({context:e,enqueue:t},a)=>{const r=c(e.parentRef,a.opacity);t.assign({opacity:a.opacity,computedOpacity:r}),t.sendTo(e.layerManagerRef,{type:"LAYER.UPDATE_OPACITY",layerId:e.layerId,opacity:a.opacity,computedOpacity:r}),e.children.forEach(i=>{t.sendTo(i,{type:"PARENT.OPACITY_CHANGED",opacity:r})})}),"Change Layer Time Info":s.enqueueActions(({context:e,enqueue:t},a)=>{t.assign({timeInfo:a.timeInfo}),t.sendTo(e.layerManagerRef,{type:"LAYER.UPDATE_TIME_INFO",layerId:e.layerId,timeInfo:a.timeInfo})}),"Update Children":s.assign(({context:e},t)=>{const a=I(e.childLayerOrder,t.child.id,t.index,t.position);return{children:[...e.children,t.child],childLayerOrder:a}}),"Remove Child":s.assign(({context:e},t)=>({children:e.children.filter(a=>a.id!==t.id),childLayerOrder:e.childLayerOrder.filter(a=>a!==t.id)}))}}).createMachine({id:"layerGroup",description:"A machine that represents a collection of layers that can be toggled as a group",initial:"disabled",context:({input:e})=>{const t=e.opacity??1,a=c(e.parentRef,t);return{layerManagerRef:e.layerManagerRef,layerId:e.layerId,parentRef:e.parentRef,children:[],childLayerOrder:[],layerName:e.layerName,layerType:"layerGroup",listMode:e.listMode??"show",timeInfo:e.timeInfo,layerData:e.layerData,opacity:t,computedOpacity:a}},states:{enabled:{initial:"visible",description:"The layer group is enabled",states:{visible:{description:"The layer group should appear visible on the map",entry:[{type:"Notify Parent of visibility change"},{type:"Notify children of visibility change",params:{visible:!0}},{type:"Notify Manager of visibility change",params:{visible:!0}}],exit:[{type:"Notify Manager of visibility change",params:{visible:!1}},{type:"Notify children of visibility change",params:{visible:!1}}],on:{"PARENT.HIDDEN":{target:"hidden"}}},hidden:{description:"The layer group should appear hidden on the map as its parent is hidden",on:{"PARENT.VISIBLE":{target:"visible"}}}},on:{"LAYER.DISABLED":{target:"disabled"}}},disabled:{description:"The layer group is disabled",initial:"hidden",states:{hidden:{description:"The layer group and its children always appear hidden on the map"}},on:{"LAYER.ENABLED":{target:"enabled"},"CHILD.VISIBLE":{target:"enabled"}}}},on:{"PARENT.OPACITY_CHANGED":{actions:"Update Computed Opacity"},"CHILD.VISIBLE":{actions:"Notify Parent of visibility change"},"LAYERS.ADD_CHILD":{actions:[{type:"Update Children",params:({event:e})=>e}]},"LAYERS.REMOVE_CHILD":{actions:[{type:"Remove Child",params:({event:e})=>e}]},"LAYER.SET_LAYER_DATA":{actions:[{type:"Change Layer Data",params:({event:e})=>e}]},"LAYER.SET_OPACITY":{actions:[{type:"Change Layer Opacity",params:({event:e})=>e}]},"LAYER.SET_TIME_INFO":{actions:[{type:"Change Layer Time Info",params:({event:e})=>e}]}}})}function f(e,t){return s.setup({types:{context:{},events:{},input:{}},actions:{"Notify Parent that layer is visible":s.enqueueActions(({context:a,enqueue:r})=>{a.parentRef&&r.sendTo(a.parentRef,{type:"CHILD.VISIBLE",layerId:a.layerId})}),"Notify Manager of visibility change":s.enqueueActions(({context:a,enqueue:r},i)=>r.sendTo(a.layerManagerRef,{type:"LAYER.UPDATE_VISIBILITY",layerId:a.layerId,visible:i.visible})),"Update Computed Opacity":s.enqueueActions(({context:a,enqueue:r},i)=>{const y=i.opacity*a.opacity;r.assign({computedOpacity:y}),r.sendTo(a.layerManagerRef,{type:"LAYER.UPDATE_OPACITY",layerId:a.layerId,opacity:a.opacity,computedOpacity:y})}),"Change Layer Opacity":s.enqueueActions(({context:a,enqueue:r},i)=>{const y=c(a.parentRef,i.opacity);r.assign({opacity:i.opacity,computedOpacity:y}),r.sendTo(a.layerManagerRef,{type:"LAYER.UPDATE_OPACITY",layerId:a.layerId,opacity:i.opacity,computedOpacity:y})}),"Change Layer Time Info":s.enqueueActions(({context:a,enqueue:r},i)=>{r.assign({timeInfo:i.timeInfo}),r.sendTo(a.layerManagerRef,{type:"LAYER.UPDATE_TIME_INFO",layerId:a.layerId,timeInfo:i.timeInfo})}),"Change Layer Data":s.enqueueActions(({context:a,enqueue:r},i)=>{r.assign({layerData:i.layerData}),r.sendTo(a.layerManagerRef,{type:"LAYER.UPDATE_LAYER_DATA",layerId:a.layerId,layerData:i.layerData})})}}).createMachine({id:"layer",description:"A machine that represents a layer on the map.",context:({input:a})=>{const r=a.opacity??1,i=c(a.parentRef,r);return{layerManagerRef:a.layerManagerRef,parentRef:a.parentRef,layerId:a.layerId,layerName:a.layerName,listMode:a.listMode??"show",opacity:r,computedOpacity:i,layerType:"layer",timeInfo:a.timeInfo,layerData:a.layerData}},initial:e,states:{enabled:{initial:t,description:"The layer is enabled",states:{visible:{description:"The layer should appear visible on the map",entry:[{type:"Notify Parent that layer is visible"},{type:"Notify Manager of visibility change",params:{visible:!0}}],exit:[{type:"Notify Manager of visibility change",params:{visible:!1}}],on:{"PARENT.HIDDEN":{target:"hidden"}}},hidden:{description:"The layer should appear hidden on the map as its parent is hidden",on:{"PARENT.VISIBLE":{target:"visible"}}}},on:{"LAYER.DISABLED":{target:"disabled"}}},disabled:{description:"The layer is disabled",initial:"hidden",states:{hidden:{description:"The layer should always appear hidden on the map"}},on:{"LAYER.ENABLED":{target:"enabled.visible"}}}},on:{"PARENT.OPACITY_CHANGED":{actions:[{type:"Update Computed Opacity",params:({event:a})=>a}]},"LAYER.SET_OPACITY":{actions:[{type:"Change Layer Opacity",params:({event:a})=>a}]},"LAYER.SET_TIME_INFO":{actions:[{type:"Change Layer Time Info",params:({event:a})=>a}]},"LAYER.SET_LAYER_DATA":{actions:[{type:"Change Layer Data",params:({event:a})=>a}]}}})}function N(){return s.setup({types:{context:{},events:{},emitted:{},input:{}},actors:{layerMachineEnabledVisible:f("enabled","visible"),layerMachineEnabledHidden:f("enabled","hidden"),layerMachineDisabled:f("disabled","hidden"),layerGroupMachine:S()},actions:{"Add new layer":s.enqueueActions(({enqueue:e,context:t,self:a},r)=>{const{layerConfig:i,index:y,visible:l,enabled:o,position:n}=r,d=E(t.layers,i);if(!m(i,d))return;function v(h,u){return u||h&&!d?"layerMachineEnabledVisible":h&&d&&d.getSnapshot().value&&d.getSnapshot().matches({disabled:"hidden"})?"layerMachineEnabledHidden":"layerMachineDisabled"}e.assign(({spawn:h})=>{let u;if(i.layerType==="layerGroup"){const A=h("layerGroupMachine",{id:i.layerId,input:{layerManagerRef:a,parentRef:d,...i}});l&&A.send({type:"LAYER.ENABLED"}),u={type:"layerGroup",layerActor:A}}else u={type:"layer",layerActor:h(v(o??!1,l??!1),{id:i.layerId,input:{layerManagerRef:a,parentRef:d,...i}})};return M(t,u,d,y,n)}),e.emit({type:"LAYER.ADDED",layerId:i.layerId,visible:l??!1})}),"Remove layer":s.enqueueActions(({enqueue:e,context:t},a)=>{const{layerId:r}=a,i=p(t.layers,r);!i||!b(i)||(Y(i),e.stopChild(r),e.assign(()=>C(t,r)),e.emit({type:"LAYER.REMOVED",layerId:r}))}),"Emit update layer order":s.emit(({context:e})=>({type:"LAYER.ORDER_CHANGED",layerOrder:D(e.layers,e.childLayerOrder)})),"Reset layer manager":s.assign(({context:e})=>(e.layers.forEach(t=>{s.stopChild(t.layerActor.id)}),{layers:[],childLayerOrder:[]}))},guards:{isValidLayerConfig:({context:e},t)=>T(t.layerConfig,e)}}).createMachine({id:"layerManager",context:({input:e})=>({layers:[],childLayerOrder:[],allowNestedGroupLayers:e.allowNestedGroupLayers}),on:{"LAYER.UPDATE_VISIBILITY":{actions:s.emit(({event:e})=>({type:"LAYER.VISIBILITY_CHANGED",layerId:e.layerId,visible:e.visible}))},"LAYER.UPDATE_OPACITY":{actions:[s.emit(({event:e})=>({type:"LAYER.OPACITY_CHANGED",layerId:e.layerId,opacity:e.opacity,computedOpacity:e.computedOpacity}))]},"LAYER.UPDATE_TIME_INFO":{actions:s.emit(({event:e})=>({type:"LAYER.TIME_INFO_CHANGED",layerId:e.layerId,timeInfo:e.timeInfo}))},"LAYER.UPDATE_LAYER_DATA":{actions:s.emit(({event:e})=>({type:"LAYER.LAYER_DATA_CHANGED",layerId:e.layerId,layerData:e.layerData}))},"LAYER.ADD":{guard:{type:"isValidLayerConfig",params:({event:e})=>e.params},actions:[{type:"Add new layer",params:({event:e})=>e.params},{type:"Emit update layer order"}]},"LAYER.REMOVE":{actions:[{type:"Remove layer",params:({event:e})=>e},{type:"Emit update layer order"}]},RESET:{actions:["Reset layer manager"]}}})}class w{_actor;_options;_adapter=null;_subscriptions=[];_destroyed=!1;constructor(t={}){this._options=t,this._actor=s.createActor(N(),{input:{allowNestedGroupLayers:this._options.allowNestedGroupLayers??!1}}),this.start()}get actor(){return this._actor}get isReady(){return this._actor.getSnapshot().status==="active"}get destroyed(){return this._destroyed}get layers(){const{childLayerOrder:t,layers:a}=this._actor.getSnapshot().context;return O(t,a)}getLayer(t){const{layers:a}=this._actor.getSnapshot().context;return p(a,t)}start(){this._actor.start(),this._wireSubscriptions(),this._adapter&&this._registerAdapter(this._adapter)}stop(){this.destroy()}destroy(){this._cleanupSubscriptions(),this._adapter?.unregister?.(),this.reset(),this._actor.stop(),this._destroyed=!0}reset(){this._actor.send({type:"RESET"})}setAdapter(t){this._adapter?.unregister?.(),this._adapter=t,t&&this.isReady&&this._registerAdapter(t)}addLayer(t){this._actor.send({type:"LAYER.ADD",params:t})}addGroup(t){this._actor.send({type:"LAYER.ADD",params:t})}removeLayer(t){this._actor.send({type:"LAYER.REMOVE",layerId:t})}setVisibility(t,a){const r=this.getLayer(t);r&&r.layerActor.send(a?{type:"LAYER.ENABLED"}:{type:"LAYER.DISABLED"})}setEnabled(t,a){this.setVisibility(t,a)}setOpacity(t,a){const r=this.getLayer(t);r&&r.layerActor.send({type:"LAYER.SET_OPACITY",opacity:a})}setTimeInfo(t,a){const r=this.getLayer(t);r&&r.layerActor.send({type:"LAYER.SET_TIME_INFO",timeInfo:a})}updateLayerData(t,a){const r=this.getLayer(t);r&&r.layerActor.send({type:"LAYER.SET_LAYER_DATA",layerData:a})}_wireSubscriptions(){const t=this._actor.on("LAYER.ADDED",o=>{const n=this._toManagedLayerInfo(o.layerId,o.visible);n&&(this._adapter?.onLayerAdded?.(n),this._options.onLayerAdded?.(n))});this._subscriptions.push(()=>t.unsubscribe());const a=this._actor.on("LAYER.REMOVED",o=>{this._adapter?.onLayerRemoved?.(o.layerId),this._options.onLayerRemoved?.(o.layerId)});this._subscriptions.push(()=>a.unsubscribe());const r=this._actor.on("LAYER.VISIBILITY_CHANGED",o=>{const n=this._toManagedLayerInfo(o.layerId,o.visible);n&&(this._adapter?.onVisibilityChanged?.(n,o.visible),this._options.onVisibilityChanged?.(n,o.visible))});this._subscriptions.push(()=>r.unsubscribe());const i=this._actor.on("LAYER.OPACITY_CHANGED",o=>{const n=this._toManagedLayerInfo(o.layerId);n&&(this._adapter?.onOpacityChanged?.(n,o.computedOpacity),this._options.onOpacityChanged?.(n,o.computedOpacity))});this._subscriptions.push(()=>i.unsubscribe());const y=this._actor.on("LAYER.TIME_INFO_CHANGED",o=>{const n=this._toManagedLayerInfo(o.layerId);n&&(this._adapter?.onTimeInfoChanged?.(n,o.timeInfo),this._options.onTimeInfoChanged?.(n,o.timeInfo))});this._subscriptions.push(()=>y.unsubscribe());const l=this._actor.on("LAYER.LAYER_DATA_CHANGED",o=>{const n=this._toManagedLayerInfo(o.layerId);n&&this._adapter?.onLayerDataChanged?.(n)});this._subscriptions.push(()=>l.unsubscribe())}_cleanupSubscriptions(){for(const t of this._subscriptions)t();this._subscriptions.length=0}_registerAdapter(t){const a={getSnapshot:()=>this.layers,getLayer:r=>this.getLayer(r)};t.register?.(this,a)}_toManagedLayerInfo(t,a){const{layers:r}=this._actor.getSnapshot().context,i=p(r,t);if(!i)return null;const y=i.layerActor.getSnapshot(),l=y.matches("enabled"),o=a??y.matches({enabled:"visible"});if(L(i.layerActor)){const n=i.layerActor.getSnapshot().context;return{layerId:n.layerId,layerName:n.layerName,layerType:"layer",layerData:n.layerData,opacity:n.opacity,computedOpacity:n.computedOpacity,enabled:l,visible:o,timeInfo:n.timeInfo,listMode:n.listMode,parentId:n.parentRef?.getSnapshot().context.layerId??null}}else{const n=i.layerActor.getSnapshot().context;return{layerId:n.layerId,layerName:n.layerName,layerType:"layerGroup",layerData:n.layerData,opacity:n.opacity,computedOpacity:n.computedOpacity,enabled:l,visible:o,timeInfo:n.timeInfo,listMode:n.listMode,parentId:n.parentRef?.getSnapshot().context.layerId??null}}}}exports.LayerManager=w;exports.calculateComputedOpacity=c;exports.canRemoveLayer=b;exports.cleanupLayerReferences=Y;exports.createLayerManagerMachine=N;exports.findManagedLayerById=p;exports.findParentActor=E;exports.findParentLayerGroupActor=R;exports.getFlatLayerOrder=D;exports.getLayerDataFromLayerId=V;exports.getLayerGroupChildrenInOrder=H;exports.getTopLevelLayersInOrder=O;exports.getUpdatedLayerStructure=M;exports.getUpdatedLayerStructureAfterRemoval=C;exports.isLayerGroupMachine=g;exports.isLayerMachine=L;exports.isRangeTimeInfo=G;exports.isSingleTimeInfo=P;exports.isValidLayerConfig=T;exports.isValidLayerIndex=_;exports.isValidParentRef=m;exports.layerGroupMachine=S;exports.layerMachine=f;exports.updateLayerOrder=I;
|