@lvce-editor/main-area-worker 9.57.0 → 9.59.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.
@@ -1503,17 +1503,98 @@ const getPreference = async key => {
1503
1503
  return await invoke$1('Preferences.get', key);
1504
1504
  };
1505
1505
 
1506
+ const maxClosedTabs = 20;
1507
+ const pendingOperations = new Map();
1508
+ const sessionId = encodeURIComponent(`session-${new Date().toISOString()}`);
1509
+ const getKey = uid => {
1510
+ return `https://lvce-editor.invalid/closed-tabs/${sessionId}/${uid}`;
1511
+ };
1512
+ const run = async (uid, operation) => {
1513
+ const previous = pendingOperations.get(uid)?.promise || Promise.resolve();
1514
+ const id = {};
1515
+ const current = (async () => {
1516
+ await previous;
1517
+ try {
1518
+ return await operation();
1519
+ } finally {
1520
+ if (pendingOperations.get(uid)?.id === id) {
1521
+ pendingOperations.delete(uid);
1522
+ }
1523
+ }
1524
+ })();
1525
+ pendingOperations.set(uid, {
1526
+ id,
1527
+ promise: current
1528
+ });
1529
+ return await current;
1530
+ };
1531
+ const isClosedTabEntry = value => {
1532
+ if (!value || typeof value !== 'object') {
1533
+ return false;
1534
+ }
1535
+ const entry = value;
1536
+ return Boolean(entry.group && typeof entry.group === 'object' && entry.tab && typeof entry.tab === 'object' && typeof entry.groupIndex === 'number' && typeof entry.tabIndex === 'number');
1537
+ };
1538
+ const getEntries = async key => {
1539
+ const entries = await invoke$1('CacheStorage.getJson', key);
1540
+ return Array.isArray(entries) ? entries.filter(isClosedTabEntry) : [];
1541
+ };
1542
+ const compact = entry => {
1543
+ return {
1544
+ ...entry,
1545
+ group: {
1546
+ ...entry.group,
1547
+ tabs: []
1548
+ }
1549
+ };
1550
+ };
1551
+ const add = (uid, entries) => {
1552
+ if (entries.length === 0) {
1553
+ return Promise.resolve();
1554
+ }
1555
+ return run(uid, async () => {
1556
+ try {
1557
+ const key = getKey(uid);
1558
+ const existing = await getEntries(key);
1559
+ const compactEntries = entries.map(compact);
1560
+ await invoke$1('CacheStorage.setJson', key, [...existing, ...compactEntries].slice(-maxClosedTabs));
1561
+ } catch {
1562
+ // Closed tab history is optional and must never break editor commands.
1563
+ }
1564
+ });
1565
+ };
1566
+ const clear = uid => {
1567
+ return run(uid, async () => {
1568
+ try {
1569
+ await invoke$1('CacheStorage.setJson', getKey(uid), []);
1570
+ } catch {
1571
+ // Closed tab history is optional and must never break workspace changes.
1572
+ }
1573
+ });
1574
+ };
1575
+ const takeLast = uid => {
1576
+ return run(uid, async () => {
1577
+ try {
1578
+ const key = getKey(uid);
1579
+ const entries = await getEntries(key);
1580
+ const entry = entries.at(-1);
1581
+ if (!isClosedTabEntry(entry)) {
1582
+ return undefined;
1583
+ }
1584
+ await invoke$1('CacheStorage.setJson', key, entries.slice(0, -1));
1585
+ return entry;
1586
+ } catch {
1587
+ return undefined;
1588
+ }
1589
+ });
1590
+ };
1591
+
1506
1592
  const addClosedTabs = (state, entries) => {
1507
- const {
1508
- closedTabs
1509
- } = state;
1510
1593
  if (entries.length === 0) {
1511
1594
  return state;
1512
1595
  }
1513
- return {
1514
- ...state,
1515
- closedTabs: [...closedTabs, ...entries].slice(-20)
1516
- };
1596
+ void add(state.uid, entries);
1597
+ return state;
1517
1598
  };
1518
1599
 
1519
1600
  const getGroupIndexById = (state, groupId) => {
@@ -1758,8 +1839,39 @@ const wrapSerialCommand = fn => {
1758
1839
  return registry.wrapSerialCommand(withActiveEditorChange(fn));
1759
1840
  };
1760
1841
 
1842
+ const state$1 = {
1843
+ configured: false,
1844
+ lastId: -1,
1845
+ nextId: 0
1846
+ };
1847
+ const configure = (start, end) => {
1848
+ const {
1849
+ configured
1850
+ } = state$1;
1851
+ if (configured) {
1852
+ throw new Error('Component id range is already configured');
1853
+ }
1854
+ if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start <= 0 || end < start) {
1855
+ throw new Error('Invalid component id range');
1856
+ }
1857
+ state$1.nextId = start;
1858
+ state$1.lastId = end;
1859
+ state$1.configured = true;
1860
+ };
1761
1861
  const create$2 = () => {
1762
- return Math.random();
1862
+ const {
1863
+ configured,
1864
+ lastId,
1865
+ nextId
1866
+ } = state$1;
1867
+ if (!configured) {
1868
+ // Compatibility with renderer versions that do not assign worker id ranges.
1869
+ return Math.random();
1870
+ }
1871
+ if (nextId > lastId) {
1872
+ throw new Error('Component id range exhausted');
1873
+ }
1874
+ return state$1.nextId++;
1763
1875
  };
1764
1876
 
1765
1877
  const updateTab = (state, tabId, updates) => {
@@ -2530,7 +2642,6 @@ const normalizeLayoutDirection = direction => {
2530
2642
  const create$1 = (uid, uri, x, y, width, height, platform, assetDir, tabHeight = 35) => {
2531
2643
  const state = {
2532
2644
  assetDir,
2533
- closedTabs: [],
2534
2645
  fileIconCache: {},
2535
2646
  height,
2536
2647
  homeDirUri: '',
@@ -2541,8 +2652,8 @@ const create$1 = (uid, uri, x, y, width, height, platform, assetDir, tabHeight =
2541
2652
  direction: Horizontal,
2542
2653
  groups: []
2543
2654
  },
2544
- maxOpenEditorGroups: Infinity,
2545
- maxOpenEditors: Infinity,
2655
+ maxOpenEditorGroups: -1,
2656
+ maxOpenEditors: -1,
2546
2657
  minGroupHeightPx: 80,
2547
2658
  minGroupWidthPx: 250,
2548
2659
  platform,
@@ -4245,15 +4356,6 @@ const getRestoredTab = tab => {
4245
4356
  loadingState: undefined
4246
4357
  });
4247
4358
  };
4248
- const getStateWithoutLastClosedTab = state => {
4249
- const {
4250
- closedTabs
4251
- } = state;
4252
- return {
4253
- ...state,
4254
- closedTabs: closedTabs.slice(0, -1)
4255
- };
4256
- };
4257
4359
  const restoreExistingUri = (state, entry) => {
4258
4360
  const {
4259
4361
  layout
@@ -4276,14 +4378,13 @@ const restoreExistingUri = (state, entry) => {
4276
4378
  if (tabIndex === -1) {
4277
4379
  return undefined;
4278
4380
  }
4279
- const newStateWithoutClosedTab = getStateWithoutLastClosedTab(state);
4280
- const groups = focusGroup(newStateWithoutClosedTab.layout.groups, existing.groupId, existing.tab.id);
4381
+ const groups = focusGroup(state.layout.groups, existing.groupId, existing.tab.id);
4281
4382
  return {
4282
4383
  groupIndex,
4283
4384
  newState: {
4284
- ...newStateWithoutClosedTab,
4385
+ ...state,
4285
4386
  layout: {
4286
- ...newStateWithoutClosedTab.layout,
4387
+ ...state.layout,
4287
4388
  activeGroupId: existing.groupId,
4288
4389
  groups
4289
4390
  }
@@ -4307,7 +4408,6 @@ const restoreIntoExistingGroup = (state, entry) => {
4307
4408
  const tabIndex = clampIndex(entry.tabIndex, 0, group.tabs.length);
4308
4409
  const tabs = [...group.tabs];
4309
4410
  tabs.splice(tabIndex, 0, restoredTab);
4310
- const newStateWithoutClosedTab = getStateWithoutLastClosedTab(state);
4311
4411
  const updatedGroups = groups.map((currentGroup, index) => {
4312
4412
  if (index === groupIndex) {
4313
4413
  return {
@@ -4326,9 +4426,9 @@ const restoreIntoExistingGroup = (state, entry) => {
4326
4426
  return {
4327
4427
  groupIndex,
4328
4428
  newState: {
4329
- ...newStateWithoutClosedTab,
4429
+ ...state,
4330
4430
  layout: {
4331
- ...newStateWithoutClosedTab.layout,
4431
+ ...state.layout,
4332
4432
  activeGroupId: group.id,
4333
4433
  groups: updatedGroups
4334
4434
  }
@@ -4346,18 +4446,17 @@ const createRestoredGroup = entry => {
4346
4446
  };
4347
4447
  };
4348
4448
  const restoreIntoRecreatedGroup = (state, entry) => {
4349
- const newStateWithoutClosedTab = getStateWithoutLastClosedTab(state);
4350
4449
  const restoredGroup = createRestoredGroup(entry);
4351
4450
  const {
4352
4451
  groups
4353
- } = newStateWithoutClosedTab.layout;
4452
+ } = state.layout;
4354
4453
  if (groups.length === 0) {
4355
4454
  return {
4356
4455
  groupIndex: 0,
4357
4456
  newState: {
4358
- ...newStateWithoutClosedTab,
4457
+ ...state,
4359
4458
  layout: {
4360
- ...newStateWithoutClosedTab.layout,
4459
+ ...state.layout,
4361
4460
  activeGroupId: restoredGroup.id,
4362
4461
  groups: [restoredGroup]
4363
4462
  }
@@ -4374,9 +4473,9 @@ const restoreIntoRecreatedGroup = (state, entry) => {
4374
4473
  return {
4375
4474
  groupIndex,
4376
4475
  newState: {
4377
- ...newStateWithoutClosedTab,
4476
+ ...state,
4378
4477
  layout: {
4379
- ...newStateWithoutClosedTab.layout,
4478
+ ...state.layout,
4380
4479
  activeGroupId: restoredGroup.id,
4381
4480
  groups: redistributeSizesWithRounding(insertedGroups)
4382
4481
  }
@@ -4384,22 +4483,16 @@ const restoreIntoRecreatedGroup = (state, entry) => {
4384
4483
  tabIndex: 0
4385
4484
  };
4386
4485
  };
4387
- const restoreClosedTabState = state => {
4388
- const {
4389
- closedTabs
4390
- } = state;
4391
- const entry = closedTabs.at(-1);
4392
- if (!entry) {
4393
- return undefined;
4394
- }
4486
+ const restoreClosedTabState = (state, entry) => {
4395
4487
  return restoreExistingUri(state, entry) || restoreIntoExistingGroup(state, entry) || restoreIntoRecreatedGroup(state, entry);
4396
4488
  };
4397
4489
 
4398
4490
  const restoreClosedTab = async state => {
4399
- const restored = restoreClosedTabState(state);
4400
- if (!restored) {
4491
+ const entry = await takeLast(state.uid);
4492
+ if (!entry) {
4401
4493
  return state;
4402
4494
  }
4495
+ const restored = restoreClosedTabState(state, entry);
4403
4496
  const newState = await selectTab(restored.newState, restored.groupIndex, restored.tabIndex);
4404
4497
  set$1(state.uid, state, newState);
4405
4498
  return newState;
@@ -6395,11 +6488,8 @@ const loadContent$1 = async (state, savedState) => {
6395
6488
  };
6396
6489
 
6397
6490
  const handleWorkspaceChange = async (state, _workspacePath, savedState) => {
6398
- const newState = await loadContent$1(state, savedState);
6399
- return {
6400
- ...newState,
6401
- closedTabs: []
6402
- };
6491
+ await clear(state.uid);
6492
+ return loadContent$1(state, savedState);
6403
6493
  };
6404
6494
 
6405
6495
  const getEditorInputUris = editorInput => {
@@ -8768,6 +8858,7 @@ const resize = async (uid, dimensions) => {
8768
8858
  const handleDirectMessagePort = (port, setAsRendererProcessFallback = true) => handleMessagePort(port, commandMap, setAsRendererProcessFallback);
8769
8859
  const handleTestWorkerMessagePort = port => handleMessagePort(port, commandMap, false);
8770
8860
  const commandMap = {
8861
+ 'Id.configure': configure,
8771
8862
  'Main.closeActiveEditor': wrapSerialCommand(closeActiveEditor),
8772
8863
  'Main.closeAll': wrapSerialCommand(closeAll$1),
8773
8864
  'Main.closeAllEditors': wrapSerialCommand(closeAll$1),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/main-area-worker",
3
- "version": "9.57.0",
3
+ "version": "9.59.0",
4
4
  "description": "Main Area Worker",
5
5
  "repository": {
6
6
  "type": "git",