Skip to content

Commit

Permalink
refactor(events): introduce EventType enum in separate module (#8530)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
cpcallen authored Aug 20, 2024
1 parent 806f8f9 commit 7ccdcc5
Show file tree
Hide file tree
Showing 67 changed files with 419 additions and 530 deletions.
21 changes: 9 additions & 12 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from './contextmenu_registry.js';
import {BlockDragStrategy} from './dragging/block_drag_strategy.js';
import type {BlockMove} from './events/events_block_move.js';
import {EventType} from './events/type.js';
import * as eventUtils from './events/utils.js';
import type {Field} from './field.js';
import {FieldLabel} from './field_label.js';
Expand Down Expand Up @@ -377,10 +378,8 @@ export class BlockSvg
const eventsEnabled = eventUtils.isEnabled();
let event: BlockMove | null = null;
if (eventsEnabled) {
event = new (eventUtils.get(eventUtils.BLOCK_MOVE)!)(this) as BlockMove;
if (reason) {
event.setReason(reason);
}
event = new (eventUtils.get(EventType.BLOCK_MOVE)!)(this) as BlockMove;
if (reason) event.setReason(reason);
}

const delta = new Coordinate(dx, dy);
Expand Down
15 changes: 8 additions & 7 deletions core/bump_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {CommentCreate} from './events/events_comment_create.js';
import type {CommentMove} from './events/events_comment_move.js';
import type {CommentResize} from './events/events_comment_resize.js';
import type {ViewportChange} from './events/events_viewport.js';
import {BUMP_EVENTS, EventType} from './events/type.js';
import * as eventUtils from './events/utils.js';
import type {IBoundedElement} from './interfaces/i_bounded_element.js';
import type {ContainerRegion} from './metrics_manager.js';
Expand Down Expand Up @@ -99,7 +100,7 @@ export function bumpIntoBoundsHandler(
return;
}

if (eventUtils.BUMP_EVENTS.includes(e.type ?? '')) {
if (BUMP_EVENTS.includes(e.type ?? '')) {
const scrollMetricsInWsCoords = metricsManager.getScrollMetrics(true);

// Triggered by move/create event
Expand Down Expand Up @@ -127,7 +128,7 @@ export function bumpIntoBoundsHandler(
);
}
eventUtils.setGroup(existingGroup);
} else if (e.type === eventUtils.VIEWPORT_CHANGE) {
} else if (e.type === EventType.VIEWPORT_CHANGE) {
const viewportEvent = e as ViewportChange;
if (
viewportEvent.scale &&
Expand Down Expand Up @@ -155,16 +156,16 @@ function extractObjectFromEvent(
): IBoundedElement | null {
let object = null;
switch (e.type) {
case eventUtils.BLOCK_CREATE:
case eventUtils.BLOCK_MOVE:
case EventType.BLOCK_CREATE:
case EventType.BLOCK_MOVE:
object = workspace.getBlockById((e as BlockCreate | BlockMove).blockId!);
if (object) {
object = object.getRootBlock();
}
break;
case eventUtils.COMMENT_CREATE:
case eventUtils.COMMENT_MOVE:
case eventUtils.COMMENT_RESIZE:
case EventType.COMMENT_CREATE:
case EventType.COMMENT_MOVE:
case EventType.COMMENT_RESIZE:
object = workspace.getCommentById(
(e as CommentCreate | CommentMove | CommentResize).commentId!,
) as RenderedWorkspaceComment;
Expand Down
3 changes: 2 additions & 1 deletion core/clipboard/block_paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import {BlockSvg} from '../block_svg.js';
import * as common from '../common.js';
import {config} from '../config.js';
import {EventType} from '../events/type.js';
import * as eventUtils from '../events/utils.js';
import {ICopyData} from '../interfaces/i_copyable.js';
import {IPaster} from '../interfaces/i_paster.js';
Expand Down Expand Up @@ -52,7 +53,7 @@ export class BlockPaster implements IPaster<BlockCopyData, BlockSvg> {
if (!block) return block;

if (eventUtils.isEnabled() && !block.isShadow()) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(block));
eventUtils.fire(new (eventUtils.get(EventType.BLOCK_CREATE))(block));
}
common.setSelected(block);
return block;
Expand Down
3 changes: 2 additions & 1 deletion core/clipboard/workspace_comment_paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import {RenderedWorkspaceComment} from '../comments/rendered_workspace_comment.js';
import * as common from '../common.js';
import {EventType} from '../events/type.js';
import * as eventUtils from '../events/utils.js';
import {ICopyData} from '../interfaces/i_copyable.js';
import {IPaster} from '../interfaces/i_paster.js';
Expand Down Expand Up @@ -46,7 +47,7 @@ export class WorkspaceCommentPaster
if (!comment) return null;

if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.COMMENT_CREATE))(comment));
eventUtils.fire(new (eventUtils.get(EventType.COMMENT_CREATE))(comment));
}
common.setSelected(comment);
return comment;
Expand Down
13 changes: 7 additions & 6 deletions core/comments/workspace_comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import {CommentMove} from '../events/events_comment_move.js';
import {CommentResize} from '../events/events_comment_resize.js';
import {EventType} from '../events/type.js';
import * as eventUtils from '../events/utils.js';
import {Coordinate} from '../utils/coordinate.js';
import * as idGenerator from '../utils/idgenerator.js';
Expand Down Expand Up @@ -63,21 +64,21 @@ export class WorkspaceComment {

private fireCreateEvent() {
if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.COMMENT_CREATE))(this));
eventUtils.fire(new (eventUtils.get(EventType.COMMENT_CREATE))(this));
}
}

private fireDeleteEvent() {
if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.COMMENT_DELETE))(this));
eventUtils.fire(new (eventUtils.get(EventType.COMMENT_DELETE))(this));
}
}

/** Fires a comment change event. */
private fireChangeEvent(oldText: string, newText: string) {
if (eventUtils.isEnabled()) {
eventUtils.fire(
new (eventUtils.get(eventUtils.COMMENT_CHANGE))(this, oldText, newText),
new (eventUtils.get(EventType.COMMENT_CHANGE))(this, oldText, newText),
);
}
}
Expand All @@ -86,7 +87,7 @@ export class WorkspaceComment {
private fireCollapseEvent(newCollapsed: boolean) {
if (eventUtils.isEnabled()) {
eventUtils.fire(
new (eventUtils.get(eventUtils.COMMENT_COLLAPSE))(this, newCollapsed),
new (eventUtils.get(EventType.COMMENT_COLLAPSE))(this, newCollapsed),
);
}
}
Expand All @@ -105,7 +106,7 @@ export class WorkspaceComment {

/** Sets the comment's size in workspace units. */
setSize(size: Size) {
const event = new (eventUtils.get(eventUtils.COMMENT_RESIZE))(
const event = new (eventUtils.get(EventType.COMMENT_RESIZE))(
this,
) as CommentResize;

Expand Down Expand Up @@ -196,7 +197,7 @@ export class WorkspaceComment {

/** Moves the comment to the given location in workspace coordinates. */
moveTo(location: Coordinate, reason?: string[] | undefined) {
const event = new (eventUtils.get(eventUtils.COMMENT_MOVE))(
const event = new (eventUtils.get(EventType.COMMENT_MOVE))(
this,
) as CommentMove;
if (reason) event.setReason(reason);
Expand Down
3 changes: 2 additions & 1 deletion core/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {Block} from './block.js';
import {ISelectable} from './blockly.js';
import {BlockDefinition, Blocks} from './blocks.js';
import type {Connection} from './connection.js';
import {EventType} from './events/type.js';
import * as eventUtils from './events/utils.js';
import type {Workspace} from './workspace.js';
import type {WorkspaceSvg} from './workspace_svg.js';
Expand Down Expand Up @@ -107,7 +108,7 @@ export function getSelected(): ISelectable | null {
export function setSelected(newSelection: ISelectable | null) {
if (selected === newSelection) return;

const event = new (eventUtils.get(eventUtils.SELECTED))(
const event = new (eventUtils.get(EventType.SELECTED))(
selected?.id ?? null,
newSelection?.id ?? null,
newSelection?.workspace.id ?? selected?.workspace.id ?? '',
Expand Down
5 changes: 3 additions & 2 deletions core/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import type {Block} from './block.js';
import {ConnectionType} from './connection_type.js';
import type {BlockMove} from './events/events_block_move.js';
import {EventType} from './events/type.js';
import * as eventUtils from './events/utils.js';
import type {Input} from './inputs/input.js';
import type {IASTNodeLocationWithBlock} from './interfaces/i_ast_node_location_with_block.js';
Expand Down Expand Up @@ -114,7 +115,7 @@ export class Connection implements IASTNodeLocationWithBlock {
// Connect the new connection to the parent.
let event;
if (eventUtils.isEnabled()) {
event = new (eventUtils.get(eventUtils.BLOCK_MOVE))(
event = new (eventUtils.get(EventType.BLOCK_MOVE))(
childBlock,
) as BlockMove;
event.setReason(['connect']);
Expand Down Expand Up @@ -281,7 +282,7 @@ export class Connection implements IASTNodeLocationWithBlock {

let event;
if (eventUtils.isEnabled()) {
event = new (eventUtils.get(eventUtils.BLOCK_MOVE))(
event = new (eventUtils.get(EventType.BLOCK_MOVE))(
childConnection.getSourceBlock(),
) as BlockMove;
event.setReason(['disconnect']);
Expand Down
3 changes: 2 additions & 1 deletion core/contextmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
ContextMenuOption,
LegacyContextMenuOption,
} from './contextmenu_registry.js';
import {EventType} from './events/type.js';
import * as eventUtils from './events/utils.js';
import {Menu} from './menu.js';
import {MenuItem} from './menuitem.js';
Expand Down Expand Up @@ -260,7 +261,7 @@ export function callbackFactory(
eventUtils.enable();
}
if (eventUtils.isEnabled() && !newBlock.isShadow()) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(newBlock));
eventUtils.fire(new (eventUtils.get(EventType.BLOCK_CREATE))(newBlock));
}
common.setSelected(newBlock);
return newBlock;
Expand Down
7 changes: 4 additions & 3 deletions core/dragging/block_drag_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {config} from '../config.js';
import {Connection} from '../connection.js';
import {ConnectionType} from '../connection_type.js';
import type {BlockMove} from '../events/events_block_move.js';
import {EventType} from '../events/type.js';
import * as eventUtils from '../events/utils.js';
import {IConnectionPreviewer} from '../interfaces/i_connection_previewer.js';
import {IDragStrategy} from '../interfaces/i_draggable.js';
Expand Down Expand Up @@ -177,7 +178,7 @@ export class BlockDragStrategy implements IDragStrategy {

/** Fire a UI event at the start of a block drag. */
private fireDragStartEvent() {
const event = new (eventUtils.get(eventUtils.BLOCK_DRAG))(
const event = new (eventUtils.get(EventType.BLOCK_DRAG))(
this.block,
true,
this.block.getDescendants(false),
Expand All @@ -187,7 +188,7 @@ export class BlockDragStrategy implements IDragStrategy {

/** Fire a UI event at the end of a block drag. */
private fireDragEndEvent() {
const event = new (eventUtils.get(eventUtils.BLOCK_DRAG))(
const event = new (eventUtils.get(EventType.BLOCK_DRAG))(
this.block,
false,
this.block.getDescendants(false),
Expand All @@ -198,7 +199,7 @@ export class BlockDragStrategy implements IDragStrategy {
/** Fire a move event at the end of a block drag. */
private fireMoveEvent() {
if (this.block.isDeadOrDying()) return;
const event = new (eventUtils.get(eventUtils.BLOCK_MOVE))(
const event = new (eventUtils.get(EventType.BLOCK_MOVE))(
this.block,
) as BlockMove;
event.setReason(['drag']);
Expand Down
7 changes: 4 additions & 3 deletions core/dragging/comment_drag_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import {RenderedWorkspaceComment} from '../comments.js';
import {CommentMove} from '../events/events_comment_move.js';
import {EventType} from '../events/type.js';
import * as eventUtils from '../events/utils.js';
import {IDragStrategy} from '../interfaces/i_draggable.js';
import * as layers from '../layers.js';
Expand Down Expand Up @@ -63,7 +64,7 @@ export class CommentDragStrategy implements IDragStrategy {

/** Fire a UI event at the start of a comment drag. */
private fireDragStartEvent() {
const event = new (eventUtils.get(eventUtils.COMMENT_DRAG))(
const event = new (eventUtils.get(EventType.COMMENT_DRAG))(
this.comment,
true,
);
Expand All @@ -72,7 +73,7 @@ export class CommentDragStrategy implements IDragStrategy {

/** Fire a UI event at the end of a comment drag. */
private fireDragEndEvent() {
const event = new (eventUtils.get(eventUtils.COMMENT_DRAG))(
const event = new (eventUtils.get(EventType.COMMENT_DRAG))(
this.comment,
false,
);
Expand All @@ -82,7 +83,7 @@ export class CommentDragStrategy implements IDragStrategy {
/** Fire a move event at the end of a comment drag. */
private fireMoveEvent() {
if (this.comment.isDeadOrDying()) return;
const event = new (eventUtils.get(eventUtils.COMMENT_MOVE))(
const event = new (eventUtils.get(EventType.COMMENT_MOVE))(
this.comment,
) as CommentMove;
event.setReason(['drag']);
Expand Down
Loading

0 comments on commit 7ccdcc5

Please sign in to comment.