@cosmos.gl/deck-layers 3.5.0-beta.2 → 3.5.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/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { Layer as U, project32 as W, picking as N, UNIT as M, CompositeLayer as $ } from "@deck.gl/core";
2
- import { GraphSimulation as B, defaultConfigValues as O } from "@cosmos.gl/graph";
3
- import { Model as j, Geometry as G } from "@luma.gl/engine";
4
- const A = {
1
+ import { Layer as H, project32 as B, picking as K, UNIT as $, CompositeLayer as Q } from "@deck.gl/core";
2
+ import { GraphSimulation as X, defaultConfigValues as Y } from "@cosmos.gl/graph";
3
+ import { Model as q, Geometry as V } from "@luma.gl/engine";
4
+ const b = {
5
5
  depthWriteEnabled: !1,
6
6
  depthCompare: "always",
7
7
  blend: !0,
@@ -11,18 +11,18 @@ const A = {
11
11
  blendAlphaOperation: "add",
12
12
  blendAlphaSrcFactor: "one",
13
13
  blendAlphaDstFactor: "one-minus-src-alpha"
14
- }, q = `layout(std140) uniform cosmosPointsUniforms {
14
+ }, tt = `layout(std140) uniform cosmosPointsUniforms {
15
15
  float pointsTextureSize;
16
16
  highp int sizeUnits;
17
17
  } cosmosPoints;
18
- `, V = {
18
+ `, et = {
19
19
  name: "cosmosPoints",
20
- vs: q,
20
+ vs: tt,
21
21
  uniformTypes: {
22
22
  pointsTextureSize: "f32",
23
23
  sizeUnits: "i32"
24
24
  }
25
- }, H = [74, 92, 191, 230], Z = (
25
+ }, Z = [74, 92, 191, 230], it = (
26
26
  /* glsl */
27
27
  `#version 300 es
28
28
  #define SHADER_NAME cosmos-points-layer-vertex-shader
@@ -85,7 +85,7 @@ void main(void) {
85
85
  DECKGL_FILTER_COLOR(vColor, geometry);
86
86
  }
87
87
  `
88
- ), J = (
88
+ ), ot = (
89
89
  /* glsl */
90
90
  `#version 300 es
91
91
  #define SHADER_NAME cosmos-points-layer-fragment-shader
@@ -111,14 +111,14 @@ void main(void) {
111
111
  DECKGL_FILTER_COLOR(fragColor, geometry);
112
112
  }
113
113
  `
114
- ), Q = {
114
+ ), nt = {
115
115
  getPointSize: { type: "accessor", value: 4 },
116
- getPointColor: { type: "accessor", value: H },
116
+ getPointColor: { type: "accessor", value: Z },
117
117
  pointSizeUnits: "pixels",
118
- parameters: { type: "object", value: A, optional: !0, compare: 2 }
119
- }, L = class L extends U {
118
+ parameters: { type: "object", value: b, optional: !0, compare: 2 }
119
+ }, T = class T extends H {
120
120
  getShaders() {
121
- return super.getShaders({ vs: Z, fs: J, modules: [W, N, V] });
121
+ return super.getShaders({ vs: it, fs: ot, modules: [B, K, et] });
122
122
  }
123
123
  initializeState() {
124
124
  this.getAttributeManager().addInstanced({
@@ -133,30 +133,30 @@ void main(void) {
133
133
  transition: !0,
134
134
  type: "unorm8",
135
135
  accessor: "getPointColor",
136
- defaultValue: [...H]
136
+ defaultValue: [...Z]
137
137
  }
138
138
  });
139
139
  }
140
- updateState(e) {
141
- var t;
142
- super.updateState(e), e.changeFlags.extensionsChanged && ((t = this.state.model) == null || t.destroy(), this.state.model = this._getModel(), this.getAttributeManager().invalidateAll());
140
+ updateState(t) {
141
+ var e;
142
+ super.updateState(t), t.changeFlags.extensionsChanged && ((e = this.state.model) == null || e.destroy(), this.state.model = this._getModel(), this.getAttributeManager().invalidateAll());
143
143
  }
144
144
  draw() {
145
- const e = this.props.graph.getPointPositionTexture(), { model: t } = this.state;
146
- if (!e || !t || e.pointCount === 0) return;
145
+ const t = this.props.graph.getPointPositionTexture(), { model: e } = this.state;
146
+ if (!t || !e || t.pointCount === 0) return;
147
147
  const i = {
148
- pointsTextureSize: e.textureSize,
149
- sizeUnits: M[this.props.pointSizeUnits],
150
- positionsTexture: e.texture
148
+ pointsTextureSize: t.textureSize,
149
+ sizeUnits: $[this.props.pointSizeUnits],
150
+ positionsTexture: t.texture
151
151
  };
152
- t.shaderInputs.setProps({ cosmosPoints: i }), t.draw(this.context.renderPass);
152
+ e.shaderInputs.setProps({ cosmosPoints: i }), e.draw(this.context.renderPass);
153
153
  }
154
154
  _getModel() {
155
- return new j(this.context.device, {
155
+ return new q(this.context.device, {
156
156
  ...this.getShaders(),
157
157
  id: this.props.id,
158
158
  bufferLayout: this.getAttributeManager().getBufferLayouts(),
159
- geometry: new G({
159
+ geometry: new V({
160
160
  topology: "triangle-strip",
161
161
  attributes: {
162
162
  positions: { size: 3, value: new Float32Array([-1, -1, 0, 1, -1, 0, -1, 1, 0, 1, 1, 0]) }
@@ -166,20 +166,22 @@ void main(void) {
166
166
  });
167
167
  }
168
168
  };
169
- L.layerName = "CosmosPointsLayer", L.defaultProps = Q;
170
- let _ = L;
171
- const X = `layout(std140) uniform cosmosLinksUniforms {
169
+ T.layerName = "CosmosPointsLayer", T.defaultProps = nt;
170
+ let D = T;
171
+ const st = `layout(std140) uniform cosmosLinksUniforms {
172
172
  float pointsTextureSize;
173
173
  highp int widthUnits;
174
+ float pointCount;
174
175
  } cosmosLinks;
175
- `, Y = {
176
+ `, at = {
176
177
  name: "cosmosLinks",
177
- vs: X,
178
+ vs: st,
178
179
  uniformTypes: {
179
180
  pointsTextureSize: "f32",
180
- widthUnits: "i32"
181
+ widthUnits: "i32",
182
+ pointCount: "f32"
181
183
  }
182
- }, K = [94, 115, 194, 64], ee = (
184
+ }, J = [94, 115, 194, 64], rt = (
183
185
  /* glsl */
184
186
  `#version 300 es
185
187
  #define SHADER_NAME cosmos-links-layer-vertex-shader
@@ -205,6 +207,12 @@ vec2 getExtrusionOffset(vec2 lineClipspace, float offsetDirection, float width)
205
207
  return dirScreenspace * offsetDirection * width / 2.0;
206
208
  }
207
209
 
210
+ // The engine's isPointIndex: an integer in [0, pointCount). A fraction would
211
+ // truncate to a neighbour, NaN would make int() undefined.
212
+ bool namesPoint(float index, float pointCount) {
213
+ return index >= 0.0 && index < pointCount && index == floor(index);
214
+ }
215
+
208
216
  vec4 fetchPointPosition(float index, int textureSize) {
209
217
  int pointIndex = int(index);
210
218
  // Point i lives at texel (i % size, i / size) as [x, y, i, unused] in space coordinates
@@ -212,13 +220,20 @@ vec4 fetchPointPosition(float index, int textureSize) {
212
220
  }
213
221
 
214
222
  void main(void) {
223
+ // An endpoint that names no point (an index outside the point count) is
224
+ // skipped, never dropped, so the instance index stays the link index — the
225
+ // rule the engine applies to the same pair in its link grouping. Its texel
226
+ // would be outside the live data.
227
+ float pointCount = cosmosLinks.pointCount;
228
+ bool namesNoPoint = !namesPoint(instanceSourceIndices, pointCount) || !namesPoint(instanceTargetIndices, pointCount);
229
+
215
230
  int textureSize = int(cosmosLinks.pointsTextureSize);
216
- vec4 sourcePosition = fetchPointPosition(instanceSourceIndices, textureSize);
217
- vec4 targetPosition = fetchPointPosition(instanceTargetIndices, textureSize);
231
+ vec4 sourcePosition = namesNoPoint ? vec4(0.0) : fetchPointPosition(instanceSourceIndices, textureSize);
232
+ vec4 targetPosition = namesNoPoint ? vec4(0.0) : fetchPointPosition(instanceTargetIndices, textureSize);
218
233
 
219
234
  // A removed (absent) endpoint's texel is NaN — see PointPositionTexture;
220
- // collapse the quad so it clips away
221
- if (isnan(sourcePosition.x) || isnan(targetPosition.x)) {
235
+ // collapse the quad, as for an endpoint that names no point, so it clips away
236
+ if (namesNoPoint || isnan(sourcePosition.x) || isnan(targetPosition.x)) {
222
237
  gl_Position = vec4(0.0);
223
238
  vColor = vec4(0.0);
224
239
  uv = vec2(0.0);
@@ -254,7 +269,7 @@ void main(void) {
254
269
  DECKGL_FILTER_COLOR(vColor, geometry);
255
270
  }
256
271
  `
257
- ), te = (
272
+ ), lt = (
258
273
  /* glsl */
259
274
  `#version 300 es
260
275
  #define SHADER_NAME cosmos-links-layer-fragment-shader
@@ -273,16 +288,16 @@ void main(void) {
273
288
  DECKGL_FILTER_COLOR(fragColor, geometry);
274
289
  }
275
290
  `
276
- ), ie = {
277
- getLinkSource: { type: "accessor", value: (d) => d.source },
278
- getLinkTarget: { type: "accessor", value: (d) => d.target },
279
- getLinkColor: { type: "accessor", value: K },
291
+ ), ct = {
292
+ getLinkSource: { type: "accessor", value: (u) => u.source },
293
+ getLinkTarget: { type: "accessor", value: (u) => u.target },
294
+ getLinkColor: { type: "accessor", value: J },
280
295
  getLinkWidth: { type: "accessor", value: 1 },
281
296
  linkWidthUnits: "pixels",
282
- parameters: { type: "object", value: A, optional: !0, compare: 2 }
283
- }, z = class z extends U {
297
+ parameters: { type: "object", value: b, optional: !0, compare: 2 }
298
+ }, A = class A extends H {
284
299
  getShaders() {
285
- return super.getShaders({ vs: ee, fs: te, modules: [W, N, Y] });
300
+ return super.getShaders({ vs: rt, fs: lt, modules: [B, K, at] });
286
301
  }
287
302
  initializeState() {
288
303
  this.getAttributeManager().addInstanced({
@@ -299,7 +314,7 @@ void main(void) {
299
314
  transition: !0,
300
315
  type: "unorm8",
301
316
  accessor: "getLinkColor",
302
- defaultValue: [...K]
317
+ defaultValue: [...J]
303
318
  },
304
319
  instanceWidths: {
305
320
  size: 1,
@@ -309,26 +324,27 @@ void main(void) {
309
324
  }
310
325
  });
311
326
  }
312
- updateState(e) {
313
- var t;
314
- super.updateState(e), e.changeFlags.extensionsChanged && ((t = this.state.model) == null || t.destroy(), this.state.model = this._getModel(), this.getAttributeManager().invalidateAll());
327
+ updateState(t) {
328
+ var e;
329
+ super.updateState(t), t.changeFlags.extensionsChanged && ((e = this.state.model) == null || e.destroy(), this.state.model = this._getModel(), this.getAttributeManager().invalidateAll());
315
330
  }
316
331
  draw() {
317
- const e = this.props.graph.getPointPositionTexture(), { model: t } = this.state;
318
- if (!e || !t || e.pointCount === 0) return;
332
+ const t = this.props.graph.getPointPositionTexture(), { model: e } = this.state;
333
+ if (!t || !e || t.pointCount === 0) return;
319
334
  const i = {
320
- pointsTextureSize: e.textureSize,
321
- widthUnits: M[this.props.linkWidthUnits],
322
- positionsTexture: e.texture
335
+ pointsTextureSize: t.textureSize,
336
+ widthUnits: $[this.props.linkWidthUnits],
337
+ pointCount: t.pointCount,
338
+ positionsTexture: t.texture
323
339
  };
324
- t.shaderInputs.setProps({ cosmosLinks: i }), t.draw(this.context.renderPass);
340
+ e.shaderInputs.setProps({ cosmosLinks: i }), e.draw(this.context.renderPass);
325
341
  }
326
342
  _getModel() {
327
- return new j(this.context.device, {
343
+ return new q(this.context.device, {
328
344
  ...this.getShaders(),
329
345
  id: this.props.id,
330
346
  bufferLayout: this.getAttributeManager().getBufferLayouts(),
331
- geometry: new G({
347
+ geometry: new V({
332
348
  topology: "triangle-strip",
333
349
  attributes: {
334
350
  positions: { size: 3, value: new Float32Array([0, -1, 0, 0, 1, 0, 1, -1, 0, 1, 1, 0]) }
@@ -338,17 +354,18 @@ void main(void) {
338
354
  });
339
355
  }
340
356
  };
341
- z.layerName = "CosmosLinksLayer", z.defaultProps = ie;
342
- let I = z;
343
- const oe = {
357
+ A.layerName = "CosmosLinksLayer", A.defaultProps = ct;
358
+ let E = A;
359
+ const dt = {
360
+ points: { type: "object", value: null, optional: !0 },
344
361
  links: null,
345
362
  getPointId: { type: "accessor", value: null },
346
363
  getPointPosition: { type: "accessor", value: null },
347
364
  getPointSize: { type: "accessor", value: 4 },
348
365
  getPointColor: { type: "accessor", value: [74, 92, 191, 230] },
349
366
  pointSizeUnits: "pixels",
350
- getLinkSource: { type: "accessor", value: (d) => d.source },
351
- getLinkTarget: { type: "accessor", value: (d) => d.target },
367
+ getLinkSource: { type: "accessor", value: (u) => u.source },
368
+ getLinkTarget: { type: "accessor", value: (u) => u.target },
352
369
  getLinkColor: { type: "accessor", value: [94, 115, 194, 64] },
353
370
  getLinkWidth: { type: "accessor", value: 1 },
354
371
  linkWidthUnits: "pixels",
@@ -358,27 +375,36 @@ const oe = {
358
375
  onPointDragStart: { type: "function", value: null, optional: !0 },
359
376
  onPointDrag: { type: "function", value: null, optional: !0 },
360
377
  onPointDragEnd: { type: "function", value: null, optional: !0 },
378
+ simulation: { type: "object", value: null, optional: !0 },
361
379
  simulationConfig: { type: "object", value: {}, compare: 2 },
362
380
  onSimulationCreated: { type: "function", value: null, optional: !0 },
363
381
  // getSubLayerProps forwards `parameters` into every sublayer, so the
364
382
  // composite must carry the same pipeline-state default the primitives do
365
- parameters: { type: "object", value: A, optional: !0, compare: 2 }
366
- }, k = (d, e, t) => typeof d == "function" ? d(e, t) : d, T = class T extends $ {
383
+ parameters: { type: "object", value: b, optional: !0, compare: 2 }
384
+ }, I = (u, t, e) => typeof u == "function" ? u(t, e) : u, U = /* @__PURE__ */ new WeakMap(), M = (u, t) => ({
385
+ length: u.length / 2,
386
+ attributes: {
387
+ getLinkSource: { value: u, size: 1, stride: 8 },
388
+ getLinkTarget: { value: u, size: 1, offset: 4, stride: 8 },
389
+ ...(t == null ? void 0 : t.getLinkColor) && { getLinkColor: t.getLinkColor },
390
+ ...(t == null ? void 0 : t.getLinkWidth) && { getLinkWidth: t.getLinkWidth }
391
+ }
392
+ }), ut = Symbol("seeded"), j = new Float32Array(0), w = class w extends Q {
367
393
  get isLoaded() {
368
- var e;
369
- return super.isLoaded && !!((e = this.state) != null && e.isReady);
394
+ var t;
395
+ return super.isLoaded && !!((t = this.state) != null && t.isReady);
370
396
  }
371
397
  initializeState() {
372
- var s, n;
373
- const { device: e, timeline: t } = this.context;
374
- if (e.type !== "webgl")
398
+ const { device: t, timeline: e } = this.context;
399
+ if (t.type !== "webgl")
375
400
  throw new Error(
376
- `@cosmos.gl/deck-layers requires a WebGL 2 device — the cosmos.gl simulation is WebGL-only, got '${e.type}'`
401
+ `@cosmos.gl/deck-layers requires a WebGL 2 device — the cosmos.gl simulation is WebGL-only, got '${t.type}'`
377
402
  );
378
- const i = new B(this.props.simulationConfig, Promise.resolve(e));
379
403
  this.state = {
380
- simulation: i,
404
+ providedSimulation: null,
381
405
  isReady: !1,
406
+ isIngestPending: !1,
407
+ hasLoaded: !1,
382
408
  pointCount: 0,
383
409
  pointsData: { length: 0 },
384
410
  linksData: null,
@@ -386,185 +412,260 @@ const oe = {
386
412
  draggedPointIndex: null,
387
413
  // Step the simulation exactly once per animation frame, independent of
388
414
  // draw passes (draw runs per viewport and again while picking)
389
- animationHandle: t.attachAnimation({ setTime: () => this._onTimelineTick() })
390
- }, (n = (s = this.props).onSimulationCreated) == null || n.call(s, i), i.ready.then(() => {
391
- var a;
392
- if (((a = this.state) == null ? void 0 : a.simulation) !== i) return;
393
- (this.getCurrentLayer() ?? this).setState({ isReady: !0 });
394
- }).catch((o) => {
395
- console.error("@cosmos.gl/deck-layers: simulation failed to initialize", o);
396
- });
415
+ animationHandle: e.attachAnimation({ setTime: (i) => this._onTimelineTick(i) })
416
+ }, this._attachSimulation(this.props.simulation ?? null);
397
417
  }
398
- updateState(e) {
399
- super.updateState(e);
400
- const { props: t, oldProps: i, changeFlags: s } = e, n = this.state.simulation;
401
- if (!n) return;
402
- s.propsChanged && t.simulationConfig !== i.simulationConfig && n.setConfig(t.simulationConfig);
403
- const o = typeof s.updateTriggersChanged == "object" ? s.updateTriggersChanged : void 0;
404
- (t.points !== i.points || t.links !== i.links || !!(o != null && o.getPointPosition || o != null && o.getPointId || o != null && o.getLinkSource || o != null && o.getLinkTarget)) && this._updateSimulationData();
418
+ updateState(t) {
419
+ super.updateState(t);
420
+ const { props: e, oldProps: i, changeFlags: n } = t, a = e.simulation ?? null, o = a !== this.state.providedSimulation;
421
+ o && (this._releaseSimulation(), this._attachSimulation(a));
422
+ const r = this.state.simulation;
423
+ if (!r) return;
424
+ !a && n.propsChanged && e.simulationConfig !== i.simulationConfig && r.setConfig(e.simulationConfig);
425
+ const l = typeof n.updateTriggersChanged == "object" ? n.updateTriggersChanged : void 0, d = o || e.points !== i.points || !!(l != null && l.getPointPosition), C = !!(l != null && l.getPointId), y = o || e.links !== i.links || !!(l != null && l.getLinkSource || l != null && l.getLinkTarget) || C;
426
+ !d && !y || (e.points ? this.state.isReady ? this._updateSimulationData(d, y, C) : this.state.isIngestPending = !0 : (e.links && console.warn("@cosmos.gl/deck-layers: `links` is ignored without `points` — load them into the simulation instead"), this.state.isIngestPending = !1, this.state.hasLoaded = !1, this.state.isReady && this._syncFromSimulation(!0)));
405
427
  }
406
428
  renderLayers() {
407
- const { simulation: e, isReady: t, pointCount: i, pointsData: s, linksData: n, linkIndices: o } = this.state;
408
- if (!e || !t || i === 0) return null;
409
- const { getPointSize: a, getPointColor: l, pointSizeUnits: c, getLinkColor: P, getLinkWidth: f, linkWidthUnits: y } = this.props, p = this.props.updateTriggers ?? {}, v = [];
410
- return n && v.push(
411
- new I(
429
+ const { simulation: t, isReady: e, pointCount: i, pointsData: n, linksData: a, linkIndices: o } = this.state;
430
+ if (!t || !e || i === 0) return null;
431
+ const { getPointSize: r, getPointColor: l, pointSizeUnits: d, getLinkColor: C, getLinkWidth: y, linkWidthUnits: h } = this.props, f = this.props.updateTriggers ?? {}, p = [];
432
+ return a && p.push(
433
+ new E(
412
434
  this.getSubLayerProps({
413
435
  id: "links",
414
436
  updateTriggers: {
415
- getLinkSource: p.getLinkSource,
416
- getLinkTarget: p.getLinkTarget,
417
- getLinkColor: p.getLinkColor,
418
- getLinkWidth: p.getLinkWidth
437
+ getLinkSource: f.getLinkSource,
438
+ getLinkTarget: f.getLinkTarget,
439
+ getLinkColor: f.getLinkColor,
440
+ getLinkWidth: f.getLinkWidth
419
441
  }
420
442
  }),
421
443
  {
422
- data: n,
423
- graph: e,
424
- getLinkColor: P,
425
- getLinkWidth: f,
426
- linkWidthUnits: y,
444
+ data: a,
445
+ graph: t,
446
+ getLinkColor: C,
447
+ getLinkWidth: y,
448
+ linkWidthUnits: h,
427
449
  // getSubLayerProps does not forward `transitions`
428
450
  transitions: this.props.transitions
429
451
  },
430
- Array.isArray(n) && o ? {
452
+ Array.isArray(a) && o ? {
431
453
  // Array links read the endpoints resolved at ingest, so the
432
454
  // rendered links are exactly the simulated ones
433
- getLinkSource: (S, { index: r }) => o[r * 2],
434
- getLinkTarget: (S, { index: r }) => o[r * 2 + 1]
455
+ getLinkSource: (k, { index: g }) => o[g * 2],
456
+ getLinkTarget: (k, { index: g }) => o[g * 2 + 1]
435
457
  } : {}
436
458
  )
437
- ), v.push(
438
- new _(
459
+ ), p.push(
460
+ new D(
439
461
  this.getSubLayerProps({
440
462
  id: "points",
441
463
  updateTriggers: {
442
- getPointSize: p.getPointSize,
443
- getPointColor: p.getPointColor
464
+ getPointSize: f.getPointSize,
465
+ getPointColor: f.getPointColor
444
466
  }
445
467
  }),
446
468
  {
447
- data: s,
448
- graph: e,
449
- getPointSize: a,
469
+ data: n,
470
+ graph: t,
471
+ getPointSize: r,
450
472
  getPointColor: l,
451
- pointSizeUnits: c,
473
+ pointSizeUnits: d,
452
474
  // getSubLayerProps does not forward `transitions`
453
475
  transitions: this.props.transitions
454
476
  }
455
477
  )
456
- ), v;
478
+ ), p;
457
479
  }
458
- onDragStart(e, t) {
459
- var a, l, c;
460
- const { simulation: i } = this.state, { enablePointDrag: s, dragReheatAlpha: n } = this.props, o = e;
461
- return !s || !i || o.elementType !== "point" || e.index < 0 ? !1 : (this.state.draggedPointIndex = e.index, i.setPinnedPoint(e.index, !0), n !== null && i.start(n), (a = t.stopImmediatePropagation) == null || a.call(t), (c = (l = this.props).onPointDragStart) == null || c.call(l, o), !0);
480
+ onDragStart(t, e) {
481
+ var r, l, d;
482
+ const { simulation: i } = this.state, { enablePointDrag: n, dragReheatAlpha: a } = this.props, o = t;
483
+ return !n || !i || o.elementType !== "point" || t.index < 0 ? !1 : (this.state.draggedPointIndex = t.index, i.setPinnedPoint(t.index, !0), a !== null && i.start(a), (r = e.stopImmediatePropagation) == null || r.call(e), (d = (l = this.props).onPointDragStart) == null || d.call(l, o), !0);
462
484
  }
463
- onDrag(e, t) {
464
- var a, l, c;
465
- const { simulation: i, draggedPointIndex: s } = this.state;
466
- if (!i || s === null) return !1;
467
- const [n, o] = this._dragCoordinate(e);
468
- return i.setPointPosition(s, n, o), (a = t.stopImmediatePropagation) == null || a.call(t), (this.getCurrentLayer() ?? this).setNeedsRedraw(), (c = (l = this.props).onPointDrag) == null || c.call(l, e), !0;
485
+ onDrag(t, e) {
486
+ var r, l, d;
487
+ const { simulation: i, draggedPointIndex: n } = this.state;
488
+ if (!i || n === null) return !1;
489
+ const [a, o] = this._dragCoordinate(t);
490
+ return i.setPointPosition(n, a, o), (r = e.stopImmediatePropagation) == null || r.call(e), (this.getCurrentLayer() ?? this).setNeedsRedraw(), (d = (l = this.props).onPointDrag) == null || d.call(l, t), !0;
469
491
  }
470
- onDragEnd(e, t) {
471
- var n, o, a;
472
- const { simulation: i, draggedPointIndex: s } = this.state;
473
- return !i || s === null ? !1 : (this.props.unpinOnDragEnd && i.setPinnedPoint(s, !1), this.state.draggedPointIndex = null, (n = t.stopImmediatePropagation) == null || n.call(t), (a = (o = this.props).onPointDragEnd) == null || a.call(o, e), !0);
492
+ onDragEnd(t, e) {
493
+ var a, o, r;
494
+ const { simulation: i, draggedPointIndex: n } = this.state;
495
+ return !i || n === null ? !1 : (this.props.unpinOnDragEnd && i.setPinnedPoint(n, !1), this.state.draggedPointIndex = null, (a = e.stopImmediatePropagation) == null || a.call(e), (r = (o = this.props).onPointDragEnd) == null || r.call(o, t), !0);
474
496
  }
475
- getPickingInfo(e) {
476
- const t = e.info;
477
- return e.sourceLayer && (t.elementType = e.sourceLayer.id.endsWith("-links") ? "link" : "point"), t;
497
+ getPickingInfo(t) {
498
+ const e = t.info;
499
+ return t.sourceLayer && (e.elementType = t.sourceLayer.id.endsWith("-links") ? "link" : "point"), e;
478
500
  }
479
501
  /** The whole simulation space; lets deck's viewport helpers frame the graph. */
480
502
  getBounds() {
481
- var t;
482
- const e = ((t = this.props.simulationConfig) == null ? void 0 : t.spaceSize) ?? O.spaceSize;
483
- return [[0, 0, 0], [e, e, 0]];
503
+ const t = this._spaceSize();
504
+ return [[0, 0, 0], [t, t, 0]];
484
505
  }
485
- finalizeState(e) {
486
- var t, i, s;
487
- ((t = this.state) == null ? void 0 : t.animationHandle) !== void 0 && this.context.timeline.detachAnimation(this.state.animationHandle), (s = (i = this.state) == null ? void 0 : i.simulation) == null || s.destroy(), super.finalizeState(e);
506
+ finalizeState(t) {
507
+ var e;
508
+ ((e = this.state) == null ? void 0 : e.animationHandle) !== void 0 && this.context.timeline.detachAnimation(this.state.animationHandle), this.state && this._releaseSimulation(), super.finalizeState(t);
488
509
  }
489
510
  /**
490
511
  * Points and links have separate index spaces, so the same picking color
491
512
  * exists in both sublayers: highlight only the sublayer that was hovered
492
513
  * and clear the other, or hovering point N would also tint link N.
493
514
  */
494
- _updateAutoHighlight(e) {
495
- var t;
515
+ _updateAutoHighlight(t) {
516
+ var e;
496
517
  for (const i of this.getSubLayers())
497
- i.id === ((t = e.sourceLayer) == null ? void 0 : t.id) ? i.updateAutoHighlight(e) : i.updateAutoHighlight({ ...e, picked: !1 });
518
+ i.id === ((e = t.sourceLayer) == null ? void 0 : e.id) ? i.updateAutoHighlight(t) : i.updateAutoHighlight({ ...t, picked: !1 });
498
519
  }
499
- _onTimelineTick() {
500
- const { simulation: e, isReady: t } = this.state ?? {};
501
- if (!e || !t || !e.isSimulationRunning) return;
502
- e.step(), (this.getCurrentLayer() ?? this).setNeedsRedraw();
520
+ /** Adopts the provided simulation, or creates one on deck's device when `provided` is `null`. */
521
+ _attachSimulation(t) {
522
+ var n, a;
523
+ const { device: e } = this.context, i = t ?? new X(this.props.simulationConfig, Promise.resolve(e));
524
+ this.setState({
525
+ simulation: i,
526
+ providedSimulation: t,
527
+ isReady: !1,
528
+ hasLoaded: !1,
529
+ loadedPositions: void 0,
530
+ loadedPairs: null,
531
+ idToIndex: null,
532
+ draggedPointIndex: null
533
+ }), t || (a = (n = this.props).onSimulationCreated) == null || a.call(n, i), i.ready.then(() => {
534
+ var r;
535
+ if (((r = this.state) == null ? void 0 : r.simulation) !== i) return;
536
+ const o = this.getCurrentLayer() ?? this;
537
+ if (i.device !== o.context.device) {
538
+ console.error(
539
+ "@cosmos.gl/deck-layers: the provided simulation runs on a different device than deck — construct it with the device from deck's onDeviceInitialized"
540
+ );
541
+ return;
542
+ }
543
+ o.state.isIngestPending ? (o.state.isIngestPending = !1, o._updateSimulationData(!0, !0, !0)) : o.props.points || o._syncFromSimulation(!0), o.setState({ isReady: !0 });
544
+ }).catch((o) => {
545
+ console.error("@cosmos.gl/deck-layers: simulation failed to initialize", o);
546
+ });
503
547
  }
504
- _dragCoordinate(e) {
505
- if (e.coordinate) return [e.coordinate[0], e.coordinate[1]];
506
- const t = this.context.viewport.unproject([e.x, e.y]);
507
- return [t[0], t[1]];
548
+ /** Destroys a layer-created simulation; a provided one is left to the application. */
549
+ _releaseSimulation() {
550
+ const { simulation: t, providedSimulation: e } = this.state;
551
+ t && !e && t.destroy(), this.state.simulation = void 0;
508
552
  }
509
- _updateSimulationData() {
510
- var S;
511
- const e = this.state.simulation;
553
+ /** The simulation's space size — authoritative for a provided simulation too. */
554
+ _spaceSize() {
555
+ var t, e, i;
556
+ return ((e = (t = this.state) == null ? void 0 : t.simulation) == null ? void 0 : e.config.spaceSize) ?? ((i = this.props.simulationConfig) == null ? void 0 : i.spaceSize) ?? Y.spaceSize;
557
+ }
558
+ _onTimelineTick(t) {
559
+ const { simulation: e, isReady: i } = this.state ?? {};
560
+ if (!e || !i) return;
561
+ const n = this.getCurrentLayer() ?? this;
562
+ n.props.points || n._syncFromSimulation(), e.isSimulationRunning && (U.get(e) !== t && (U.set(e, t), e.step()), n.setNeedsRedraw());
563
+ }
564
+ /**
565
+ * Without `points`: builds the render data from what the simulation holds —
566
+ * its point count and links — and rebuilds it when either changes. Runs every
567
+ * tick, so it only compares until something differs.
568
+ */
569
+ _syncFromSimulation(t = !1) {
570
+ const { simulation: e } = this.state;
512
571
  if (!e) return;
513
- const { points: t, links: i, getPointId: s, getPointPosition: n } = this.props, o = ((S = this.props.simulationConfig) == null ? void 0 : S.spaceSize) ?? O.spaceSize, a = () => o * (0.25 + Math.random() * 0.5);
514
- let l, c, P, f = null;
515
- if (Array.isArray(t)) {
516
- const r = t;
517
- l = r.length, c = new Float32Array(l * 2), s && (f = /* @__PURE__ */ new Map());
518
- for (let u = 0; u < l; u += 1) {
519
- const C = r[u], x = { index: u, data: r, target: [] }, m = n ? k(n, C, x) : null;
520
- c[u * 2] = m ? m[0] : a(), c[u * 2 + 1] = m ? m[1] : a(), f && s && f.set(k(s, C, x), u);
521
- }
522
- P = r;
572
+ const i = e.data.pointsNumber ?? 0, n = e.data.links ?? null;
573
+ !t && i === this.state.pointCount && n === this.state.followedLinks || this.setState({
574
+ pointCount: i,
575
+ pointsData: { length: i },
576
+ linksData: n && n.length > 0 ? M(n) : null,
577
+ linkIndices: null,
578
+ followedLinks: n
579
+ });
580
+ }
581
+ _dragCoordinate(t) {
582
+ if (t.coordinate) return [t.coordinate[0], t.coordinate[1]];
583
+ const e = this.context.viewport.unproject([t.x, t.y]);
584
+ return [e[0], e[1]];
585
+ }
586
+ /**
587
+ * Load mode: puts the layer's `points` and `links` into the simulation and
588
+ * builds the render data from them. Each channel is sent only when its own
589
+ * input changed — a new `points` or `links` object with the same positions
590
+ * or pairs restyles in place and leaves the layout alone. A changed id map
591
+ * re-resolves array links without touching the positions.
592
+ */
593
+ _updateSimulationData(t, e, i) {
594
+ const n = this.state.simulation;
595
+ if (!n) return;
596
+ const { points: a, links: o, getPointId: r, getPointPosition: l } = this.props;
597
+ if (!a) return;
598
+ const d = !this.state.hasLoaded, C = this._spaceSize(), y = () => C * (0.25 + Math.random() * 0.5);
599
+ let h, f, p = null, k = this.state.loadedPositions, g = this.state.idToIndex ?? null;
600
+ if (Array.isArray(a)) {
601
+ const s = a;
602
+ if (h = s.length, f = s, d || t) {
603
+ p = new Float32Array(h * 2), g = r ? /* @__PURE__ */ new Map() : null;
604
+ for (let c = 0; c < h; c += 1) {
605
+ const m = s[c], L = { index: c, data: s, target: [] }, S = l ? I(l, m, L) : null;
606
+ p[c * 2] = S ? S[0] : y(), p[c * 2 + 1] = S ? S[1] : y(), g && r && g.set(I(r, m, L), c);
607
+ }
608
+ k = s;
609
+ } else if (i && (g = r ? /* @__PURE__ */ new Map() : null, g && r))
610
+ for (let c = 0; c < h; c += 1) {
611
+ const m = { index: c, data: s, target: [] };
612
+ g.set(I(r, s[c], m), c);
613
+ }
523
614
  } else {
524
- const r = t;
525
- if (l = r.length, r.initialPositions)
526
- c = r.initialPositions;
527
- else {
528
- c = new Float32Array(l * 2);
529
- for (let u = 0; u < c.length; u += 1) c[u] = a();
615
+ const s = a;
616
+ h = s.length, f = s.attributes ? { length: h, attributes: s.attributes } : { length: h }, g = null;
617
+ const c = s.initialPositions ?? ut;
618
+ if (d || c !== this.state.loadedPositions || h !== this.state.pointCount) {
619
+ if (s.initialPositions)
620
+ p = s.initialPositions;
621
+ else {
622
+ p = new Float32Array(h * 2);
623
+ for (let m = 0; m < p.length; m += 1) p[m] = y();
624
+ }
625
+ k = c;
530
626
  }
531
- P = r.attributes ? { length: l, attributes: r.attributes } : { length: l };
532
627
  }
533
- let y = null, p = null, v = null;
534
- if (i instanceof Float32Array)
535
- y = i, p = {
536
- length: i.length / 2,
537
- attributes: {
538
- getLinkSource: { value: i, size: 1, stride: 8 },
539
- getLinkTarget: { value: i, size: 1, offset: 4, stride: 8 }
628
+ let x = null, _ = this.state.linksData, z = this.state.linkIndices;
629
+ if (o instanceof Float32Array || o && !Array.isArray(o) && "pairs" in o) {
630
+ const s = o instanceof Float32Array ? o : o.pairs;
631
+ _ = M(s, o instanceof Float32Array ? void 0 : o.attributes), z = null, (d || s !== this.state.loadedPairs) && (x = s);
632
+ } else if (Array.isArray(o)) {
633
+ if (d || e || p) {
634
+ const s = o, { getLinkSource: c, getLinkTarget: m } = this.props, L = (P) => {
635
+ const v = g ? g.get(P) : P;
636
+ return typeof v == "number" && Number.isInteger(v) && v >= 0 && v < h ? v : void 0;
637
+ }, S = [], R = [];
638
+ for (let P = 0; P < s.length; P += 1) {
639
+ const v = s[P], N = { index: P, data: s, target: [] }, O = L(I(c, v, N)), W = L(I(m, v, N));
640
+ O === void 0 || W === void 0 || (S.push(v), R.push(O, W));
540
641
  }
541
- };
542
- else if (Array.isArray(i)) {
543
- const r = i, { getLinkSource: u, getLinkTarget: C } = this.props, x = (g) => {
544
- const h = f ? f.get(g) : g;
545
- return typeof h == "number" && Number.isInteger(h) && h >= 0 && h < l ? h : void 0;
546
- }, m = [], b = [];
547
- for (let g = 0; g < r.length; g += 1) {
548
- const h = r[g], E = { index: g, data: r, target: [] }, w = x(k(u, h, E)), R = x(k(C, h, E));
549
- w === void 0 || R === void 0 || (m.push(h), b.push(w, R));
550
- }
551
- const D = r.length - m.length;
552
- if (D > 0) {
553
- const g = f ? "getLinkSource / getLinkTarget against getPointId" : "getLinkSource / getLinkTarget";
554
- console.warn(
555
- `@cosmos.gl/deck-layers: dropped ${D} of ${r.length} links whose source or target is not a point — check ${g}`
556
- );
642
+ const F = s.length - S.length;
643
+ if (F > 0) {
644
+ const P = g ? "getLinkSource / getLinkTarget against getPointId" : "getLinkSource / getLinkTarget";
645
+ console.warn(
646
+ `@cosmos.gl/deck-layers: dropped ${F} of ${s.length} links whose source or target is not a point — check ${P}`
647
+ );
648
+ }
649
+ x = Float32Array.from(R), z = x, _ = S;
557
650
  }
558
- y = Float32Array.from(b), v = y, p = m;
559
- }
560
- e.setPointPositions(c), e.setLinks(y ?? new Float32Array(0)), e.applyData(), this.setState({ pointCount: l, pointsData: P, linksData: p, linkIndices: v });
651
+ } else
652
+ _ = null, z = null, (d || this.state.loadedPairs !== j) && (x = j);
653
+ p && n.setPointPositions(p), x && n.setLinks(x), (p || x) && n.applyData(), this.setState({
654
+ hasLoaded: !0,
655
+ pointCount: h,
656
+ pointsData: f,
657
+ linksData: _,
658
+ linkIndices: z,
659
+ followedLinks: void 0,
660
+ loadedPositions: k,
661
+ loadedPairs: x ?? this.state.loadedPairs,
662
+ idToIndex: g
663
+ });
561
664
  }
562
665
  };
563
- T.layerName = "CosmosGraphLayer", T.defaultProps = oe;
564
- let F = T;
666
+ w.layerName = "CosmosGraphLayer", w.defaultProps = dt;
667
+ let G = w;
565
668
  export {
566
- F as CosmosGraphLayer,
567
- I as CosmosLinksLayer,
568
- _ as CosmosPointsLayer
669
+ G as CosmosGraphLayer
569
670
  };
570
671
  //# sourceMappingURL=index.js.map