refactor(events): Introduce and use event type predicates (#8538)

* refactor(events): Introduce type predicates for event classes

  Introduce predicates for testing Abstract event subclasses based on
  their .type properties.  These are useful because there are places
  where it is not possible to use instanceof <ClassConstructor> tests
  for type narrowing due to load ordering issues that would be caused
  by the need to import (rather than just import type) the class
  constructors in question.

* refactor(events): Use event type predicates

  Simplify several sections of code by using type predicates for
  type narrowing and thereby avoiding the need for explicit casts.

* chore(events): Fix copyright date of recently-added files

* chore: Remove unused import
This commit is contained in:
Christopher Allen
2024-08-20 19:50:29 +01:00
committed by GitHub
parent bde216d120
commit 032b5ed9ea
7 changed files with 231 additions and 74 deletions

View File

@@ -17,8 +17,8 @@ import * as browserEvents from './browser_events.js';
import {ComponentManager} from './component_manager.js';
import {DeleteArea} from './delete_area.js';
import type {Abstract} from './events/events_abstract.js';
import type {BlockDelete} from './events/events_block_delete.js';
import './events/events_trashcan_open.js';
import {isBlockDelete} from './events/predicates.js';
import {EventType} from './events/type.js';
import * as eventUtils from './events/utils.js';
import type {IAutoHideable} from './interfaces/i_autohideable.js';
@@ -604,30 +604,22 @@ export class Trashcan
private onDelete(event: Abstract) {
if (
this.workspace.options.maxTrashcanContents <= 0 ||
event.type !== EventType.BLOCK_DELETE
!isBlockDelete(event) ||
event.wasShadow
) {
return;
}
const deleteEvent = event as BlockDelete;
if (event.type === EventType.BLOCK_DELETE && !deleteEvent.wasShadow) {
if (!deleteEvent.oldJson) {
throw new Error('Encountered a delete event without proper oldJson');
}
const cleanedJson = JSON.stringify(
this.cleanBlockJson(deleteEvent.oldJson),
);
if (this.contents.includes(cleanedJson)) {
return;
}
this.contents.unshift(cleanedJson);
while (
this.contents.length > this.workspace.options.maxTrashcanContents
) {
this.contents.pop();
}
this.setMinOpenness(HAS_BLOCKS_LID_ANGLE);
if (!event.oldJson) {
throw new Error('Encountered a delete event without proper oldJson');
}
const cleanedJson = JSON.stringify(this.cleanBlockJson(event.oldJson));
if (this.contents.includes(cleanedJson)) return;
this.contents.unshift(cleanedJson);
while (this.contents.length > this.workspace.options.maxTrashcanContents) {
this.contents.pop();
}
this.setMinOpenness(HAS_BLOCKS_LID_ANGLE);
}
/**