@lvce-editor/main-area-worker 9.57.0 → 9.58.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) => {
@@ -2530,7 +2611,6 @@ const normalizeLayoutDirection = direction => {
2530
2611
  const create$1 = (uid, uri, x, y, width, height, platform, assetDir, tabHeight = 35) => {
2531
2612
  const state = {
2532
2613
  assetDir,
2533
- closedTabs: [],
2534
2614
  fileIconCache: {},
2535
2615
  height,
2536
2616
  homeDirUri: '',
@@ -2541,8 +2621,8 @@ const create$1 = (uid, uri, x, y, width, height, platform, assetDir, tabHeight =
2541
2621
  direction: Horizontal,
2542
2622
  groups: []
2543
2623
  },
2544
- maxOpenEditorGroups: Infinity,
2545
- maxOpenEditors: Infinity,
2624
+ maxOpenEditorGroups: -1,
2625
+ maxOpenEditors: -1,
2546
2626
  minGroupHeightPx: 80,
2547
2627
  minGroupWidthPx: 250,
2548
2628
  platform,
@@ -4245,15 +4325,6 @@ const getRestoredTab = tab => {
4245
4325
  loadingState: undefined
4246
4326
  });
4247
4327
  };
4248
- const getStateWithoutLastClosedTab = state => {
4249
- const {
4250
- closedTabs
4251
- } = state;
4252
- return {
4253
- ...state,
4254
- closedTabs: closedTabs.slice(0, -1)
4255
- };
4256
- };
4257
4328
  const restoreExistingUri = (state, entry) => {
4258
4329
  const {
4259
4330
  layout
@@ -4276,14 +4347,13 @@ const restoreExistingUri = (state, entry) => {
4276
4347
  if (tabIndex === -1) {
4277
4348
  return undefined;
4278
4349
  }
4279
- const newStateWithoutClosedTab = getStateWithoutLastClosedTab(state);
4280
- const groups = focusGroup(newStateWithoutClosedTab.layout.groups, existing.groupId, existing.tab.id);
4350
+ const groups = focusGroup(state.layout.groups, existing.groupId, existing.tab.id);
4281
4351
  return {
4282
4352
  groupIndex,
4283
4353
  newState: {
4284
- ...newStateWithoutClosedTab,
4354
+ ...state,
4285
4355
  layout: {
4286
- ...newStateWithoutClosedTab.layout,
4356
+ ...state.layout,
4287
4357
  activeGroupId: existing.groupId,
4288
4358
  groups
4289
4359
  }
@@ -4307,7 +4377,6 @@ const restoreIntoExistingGroup = (state, entry) => {
4307
4377
  const tabIndex = clampIndex(entry.tabIndex, 0, group.tabs.length);
4308
4378
  const tabs = [...group.tabs];
4309
4379
  tabs.splice(tabIndex, 0, restoredTab);
4310
- const newStateWithoutClosedTab = getStateWithoutLastClosedTab(state);
4311
4380
  const updatedGroups = groups.map((currentGroup, index) => {
4312
4381
  if (index === groupIndex) {
4313
4382
  return {
@@ -4326,9 +4395,9 @@ const restoreIntoExistingGroup = (state, entry) => {
4326
4395
  return {
4327
4396
  groupIndex,
4328
4397
  newState: {
4329
- ...newStateWithoutClosedTab,
4398
+ ...state,
4330
4399
  layout: {
4331
- ...newStateWithoutClosedTab.layout,
4400
+ ...state.layout,
4332
4401
  activeGroupId: group.id,
4333
4402
  groups: updatedGroups
4334
4403
  }
@@ -4346,18 +4415,17 @@ const createRestoredGroup = entry => {
4346
4415
  };
4347
4416
  };
4348
4417
  const restoreIntoRecreatedGroup = (state, entry) => {
4349
- const newStateWithoutClosedTab = getStateWithoutLastClosedTab(state);
4350
4418
  const restoredGroup = createRestoredGroup(entry);
4351
4419
  const {
4352
4420
  groups
4353
- } = newStateWithoutClosedTab.layout;
4421
+ } = state.layout;
4354
4422
  if (groups.length === 0) {
4355
4423
  return {
4356
4424
  groupIndex: 0,
4357
4425
  newState: {
4358
- ...newStateWithoutClosedTab,
4426
+ ...state,
4359
4427
  layout: {
4360
- ...newStateWithoutClosedTab.layout,
4428
+ ...state.layout,
4361
4429
  activeGroupId: restoredGroup.id,
4362
4430
  groups: [restoredGroup]
4363
4431
  }
@@ -4374,9 +4442,9 @@ const restoreIntoRecreatedGroup = (state, entry) => {
4374
4442
  return {
4375
4443
  groupIndex,
4376
4444
  newState: {
4377
- ...newStateWithoutClosedTab,
4445
+ ...state,
4378
4446
  layout: {
4379
- ...newStateWithoutClosedTab.layout,
4447
+ ...state.layout,
4380
4448
  activeGroupId: restoredGroup.id,
4381
4449
  groups: redistributeSizesWithRounding(insertedGroups)
4382
4450
  }
@@ -4384,22 +4452,16 @@ const restoreIntoRecreatedGroup = (state, entry) => {
4384
4452
  tabIndex: 0
4385
4453
  };
4386
4454
  };
4387
- const restoreClosedTabState = state => {
4388
- const {
4389
- closedTabs
4390
- } = state;
4391
- const entry = closedTabs.at(-1);
4392
- if (!entry) {
4393
- return undefined;
4394
- }
4455
+ const restoreClosedTabState = (state, entry) => {
4395
4456
  return restoreExistingUri(state, entry) || restoreIntoExistingGroup(state, entry) || restoreIntoRecreatedGroup(state, entry);
4396
4457
  };
4397
4458
 
4398
4459
  const restoreClosedTab = async state => {
4399
- const restored = restoreClosedTabState(state);
4400
- if (!restored) {
4460
+ const entry = await takeLast(state.uid);
4461
+ if (!entry) {
4401
4462
  return state;
4402
4463
  }
4464
+ const restored = restoreClosedTabState(state, entry);
4403
4465
  const newState = await selectTab(restored.newState, restored.groupIndex, restored.tabIndex);
4404
4466
  set$1(state.uid, state, newState);
4405
4467
  return newState;
@@ -6395,11 +6457,8 @@ const loadContent$1 = async (state, savedState) => {
6395
6457
  };
6396
6458
 
6397
6459
  const handleWorkspaceChange = async (state, _workspacePath, savedState) => {
6398
- const newState = await loadContent$1(state, savedState);
6399
- return {
6400
- ...newState,
6401
- closedTabs: []
6402
- };
6460
+ await clear(state.uid);
6461
+ return loadContent$1(state, savedState);
6403
6462
  };
6404
6463
 
6405
6464
  const getEditorInputUris = editorInput => {
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.58.0",
4
4
  "description": "Main Area Worker",
5
5
  "repository": {
6
6
  "type": "git",