Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(event-bus): implement simple eventbus class #43

Merged
merged 15 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
- uses: ./.github/actions/setup
- name: Run ESLint check
run: yarn lint:ci
Expand All @@ -15,6 +18,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
- uses: ./.github/actions/setup
- name: Run unit tests
uses: ArtiomTr/jest-coverage-report-action@v2
Expand All @@ -28,6 +34,9 @@ jobs:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc

- name: Setup environment
uses: ./.github/actions/setup
Expand Down Expand Up @@ -99,6 +108,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
- uses: ./.github/actions/setup
- name: Build the package
run: yarn build
12 changes: 12 additions & 0 deletions .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
- uses: ./.github/actions/setup
- name: Run ESLint check
run: yarn lint:ci
Expand All @@ -15,6 +18,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
- uses: ./.github/actions/setup
- name: Run unit tests
uses: ArtiomTr/jest-coverage-report-action@v2
Expand All @@ -28,6 +34,9 @@ jobs:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc

- uses: ./.github/actions/setup

Expand All @@ -40,6 +49,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
- uses: ./.github/actions/setup
- name: Build the package
run: yarn build
4 changes: 4 additions & 0 deletions src/utils/EventBus/EventBus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Provides an event bus for using in the document model
*/
export class EventBus extends EventTarget {}
30 changes: 30 additions & 0 deletions src/utils/EventBus/events/BlockAddedEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { BlockNodeSerialized } from '../../../entities/BlockNode/types/index.js';
import type { EventAction } from '../types/EventAction.js';
import type { EventPayloadBase } from '../types/EventPayloadBase.js';
import type { BlockIndex } from '../types/indexing.js';
import { EventType } from '../types/EventType.js';

/**
* Add Block Event Payload
*/
interface BlockAddedEventPayload extends EventPayloadBase<BlockIndex, EventAction.Added> {
/**
* The data of the added block
*/
data: BlockNodeSerialized;
}

/**
* Add Block Custom Event
*/
export class BlockAddedEvent extends CustomEvent<BlockAddedEventPayload> {
/**
* Constructor
*
* @param payload - The event payload
*/
constructor(payload: BlockAddedEventPayload) {
// Stryker disable next-line ObjectLiteral
super(EventType.Changed, { detail: payload });
}
}
30 changes: 30 additions & 0 deletions src/utils/EventBus/events/BlockModifiedEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { BlockNodeSerialized } from '../../../entities/BlockNode/types/index.js';
import type { EventAction } from '../types/EventAction.js';
import type { EventPayloadBase } from '../types/EventPayloadBase.js';
import type { BlockIndex } from '../types/indexing.js';
import { EventType } from '../types/EventType.js';

/**
* Modify Block Event Payload
*/
interface BlockModifiedEventPayload extends EventPayloadBase<BlockIndex, EventAction.Modified> {
/**
* The data of the modified block
*/
data: BlockNodeSerialized;
}

/**
* Modify Block Custom Event
*/
export class BlockModifiedEvent extends CustomEvent<BlockModifiedEventPayload> {
/**
* Constructor
*
* @param payload - The event payload
*/
constructor(payload: BlockModifiedEventPayload) {
// Stryker disable next-line ObjectLiteral
super(EventType.Changed, { detail: payload });
}
}
30 changes: 30 additions & 0 deletions src/utils/EventBus/events/BlockRemovedEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { EventAction } from '../types/EventAction.js';
import type { BlockNodeSerialized } from '../../../entities/BlockNode/types/index.js';
import type { EventPayloadBase } from '../types/EventPayloadBase.js';
import type { BlockIndex } from '../types/indexing.js';
import { EventType } from '../types/EventType.js';

/**
* Remove Block Event Payload
*/
interface BlockRemovedEventPayload extends EventPayloadBase<BlockIndex, EventAction.Removed> {
/**
* The data of the removed block
*/
data: BlockNodeSerialized;
}

/**
* Remove Block Custom Event
*/
export class BlockRemovedEvent extends CustomEvent<BlockRemovedEventPayload> {
/**
* Constructor
*
* @param payload - The event payload
*/
constructor(payload: BlockRemovedEventPayload) {
// Stryker disable next-line ObjectLiteral
super(EventType.Changed, { detail: payload });
}
}
19 changes: 19 additions & 0 deletions src/utils/EventBus/types/EventAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Enumeration of the possible event actions
*/
export enum EventAction {
/**
* Event indicating that some new information was added to the document model
*/
Added = 'added',

/**
* Event indicating that some information was removed from the document model
*/
Removed = 'removed',

/**
* Event indicating that some existing information was modified in the document model
*/
Modified = 'modified',
}
16 changes: 16 additions & 0 deletions src/utils/EventBus/types/EventMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { EventType } from './EventType.js';
import type { BlockAddedEvent } from '../events/BlockAddedEvent.js';
import type { BlockModifiedEvent } from '../events/BlockModifiedEvent.js';
import type { BlockRemovedEvent } from '../events/BlockRemovedEvent.js';

/**
* Alias for all block events
*/
type BlockEvents = BlockAddedEvent | BlockModifiedEvent | BlockRemovedEvent;

/**
* Map of all events that can be emitted inside the DocumentModel
*/
export type EventMap = {
[EventType.Changed]: BlockEvents;
};
22 changes: 22 additions & 0 deletions src/utils/EventBus/types/EventPayloadBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { EventAction } from './EventAction.js';
import type { Index as IndexType } from './indexing.js';

/**
* Common fields for all events related to the document model
*/
export interface EventPayloadBase<Index extends IndexType, Action extends EventAction> {
/**
* The index of changed information
*/
index: Index;

/**
* The action that was performed on the information
*/
action: Action;

/**
* The data of the changed information
*/
data: unknown;
}
10 changes: 10 additions & 0 deletions src/utils/EventBus/types/EventTarget.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { EventMap } from './EventMap.js';

/**
* Augment EventTarget's addEventListener method to accept CustomEvent
*/
declare global {
interface EventTarget {
addEventListener<T extends keyof EventMap>(type: T, listener: (event: EventMap[T]) => void): void;
}
}
9 changes: 9 additions & 0 deletions src/utils/EventBus/types/EventType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Event types enum that can be emitted by the document model
*/
export enum EventType {
/**
* The document model has been changed
*/
Changed = 'changed',
}
41 changes: 41 additions & 0 deletions src/utils/EventBus/types/indexing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { BlockTuneName, DataKey } from '../../../entities/index.js';

/**
* Alias for a document id
*/
type DocumentId = string;

/**
* Numeric id for a block node
*/
type BlockIndexAlias = number;

/**
* Index for a document node
*/
export type DocumentIndex = DocumentId;

/**
* Index for a block node
*/
export type BlockIndex = `${DocumentIndex}:${BlockIndexAlias}`;

/**
* Numeric index for data or tune changes in block node
*/
type StartIndex = number;

/**
* Index for data changes in block node
*/
export type DataIndex = `${BlockIndex}:data@${DataKey}:${StartIndex}`;

/**
* Index for tune changes in block node
*/
export type TuneIndex = `${BlockIndex}:tune@${BlockTuneName}`;

/**
* Possible index types
*/
export type Index = DocumentIndex | BlockIndex | DataIndex | TuneIndex;
4 changes: 4 additions & 0 deletions stryker.conf.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const config = {
checkers: ["typescript"],
timeoutMS: 10000,
mutate: ["./src/**/*.ts", "!./src/**/__mocks__/*.ts", "!./src/**/*.spec.ts"],
/*
* In some cases PRs might not have any unit-tests
*/
allowEmpty: true,
};

export default config;