refactor(events): introduce EventType enum in separate module (#8530)

* refactor(events): Use "export ... from" where applicable

* refactor(events): Introduce EventType enum

  Introduce an enum for the event .type values.  We can't actually
  use it as the type of the .type property on Abstract events,
  because we want to allow developers to add their own custom
  event types inheriting from this type, but at least this way we
  can be reasonably sure that all of our own event subclasses have
  distinct .type values—plus consistent use of enum syntax
  (EventType.TYPE_NAME) is probably good for readability overall.

  Put it in a separate module from the rest of events/utils.ts
  because it would be helpful if event utils could use

      event instanceof SomeEventType

  for type narrowing but but at the moment most events are in
  modules that depend on events/utils.ts for their .type
  constant, and although circular ESM dependencies should work
  in principle there are various restrictions and this
  particular circularity causes issues at the moment.

  A few of the event classes also depend on utils.ts for fire()
  or other functions, which will be harder to deal with, but at
  least this commit is win in terms of reducing the complexity
  of our dependencies, making most of the Abstract event subclass
  module dependent on type.ts, which has no imports, rather than
  on utils.ts which has multiple imports.
This commit is contained in:
Christopher Allen
2024-08-20 08:36:33 +01:00
committed by GitHub
parent 806f8f95d2
commit 7ccdcc5cef
67 changed files with 419 additions and 530 deletions

View File

@@ -26,6 +26,7 @@ import * as constants from './constants.js';
import type {Abstract} from './events/events_abstract.js';
import type {BlockChange} from './events/events_block_change.js';
import type {BlockMove} from './events/events_block_move.js';
import {EventType} from './events/type.js';
import * as eventUtils from './events/utils.js';
import * as Extensions from './extensions.js';
import type {Field} from './field.js';
@@ -304,7 +305,7 @@ export class Block implements IASTNodeLocation {
// Fire a create event.
if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(this));
eventUtils.fire(new (eventUtils.get(EventType.BLOCK_CREATE))(this));
}
} finally {
eventUtils.setGroup(existingGroup);
@@ -339,7 +340,7 @@ export class Block implements IASTNodeLocation {
this.unplug(healStack);
if (eventUtils.isEnabled()) {
// Constructing the delete event is costly. Only perform if necessary.
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_DELETE))(this));
eventUtils.fire(new (eventUtils.get(EventType.BLOCK_DELETE))(this));
}
this.workspace.removeTopBlock(this);
this.disposeInternal();
@@ -1329,7 +1330,7 @@ export class Block implements IASTNodeLocation {
setInputsInline(newBoolean: boolean) {
if (this.inputsInline !== newBoolean) {
eventUtils.fire(
new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
new (eventUtils.get(EventType.BLOCK_CHANGE))(
this,
'inline',
null,
@@ -1491,7 +1492,7 @@ export class Block implements IASTNodeLocation {
} else {
this.disabledReasons.delete(reason);
}
const blockChangeEvent = new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
const blockChangeEvent = new (eventUtils.get(EventType.BLOCK_CHANGE))(
this,
'disabled',
/* name= */ null,
@@ -1559,7 +1560,7 @@ export class Block implements IASTNodeLocation {
setCollapsed(collapsed: boolean) {
if (this.collapsed_ !== collapsed) {
eventUtils.fire(
new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
new (eventUtils.get(EventType.BLOCK_CHANGE))(
this,
'collapsed',
null,
@@ -2358,7 +2359,7 @@ export class Block implements IASTNodeLocation {
}
eventUtils.fire(
new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
new (eventUtils.get(EventType.BLOCK_CHANGE))(
this,
'comment',
null,
@@ -2458,12 +2459,8 @@ export class Block implements IASTNodeLocation {
if (this.parentBlock_) {
throw Error('Block has parent');
}
const event = new (eventUtils.get(eventUtils.BLOCK_MOVE))(
this,
) as BlockMove;
if (reason) {
event.setReason(reason);
}
const event = new (eventUtils.get(EventType.BLOCK_MOVE))(this) as BlockMove;
if (reason) event.setReason(reason);
this.xy_.translate(dx, dy);
event.recordNew();
eventUtils.fire(event);