Skip to content

Commit fe04ed8

Browse files
committed
refactor: use DocumentEvents for old event names and minimize default exports
1 parent cc7032d commit fe04ed8

File tree

19 files changed

+189
-149
lines changed

19 files changed

+189
-149
lines changed

packages/compass-components/src/components/document-list/document-edit-actions-footer.tsx

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useCallback, useEffect, useRef, useState } from 'react';
22
import type HadronDocument from 'hadron-document';
3-
import { Element } from 'hadron-document';
3+
import { DocumentEvents, ElementEvents } from 'hadron-document';
4+
import type { Element } from 'hadron-document/src/element';
45
import { Button } from '../leafygreen';
56
import { css } from '@leafygreen-ui/emotion';
67
import { palette } from '@leafygreen-ui/palette';
@@ -165,34 +166,34 @@ function useHadronDocumentStatus(
165166
updateStatus('DeleteError', err, errorDetails);
166167
};
167168

168-
doc.on(Element.Events.Added, onUpdate);
169-
doc.on(Element.Events.Edited, onUpdate);
170-
doc.on(Element.Events.Removed, onUpdate);
171-
doc.on(Element.Events.Reverted, onUpdate);
172-
doc.on(Element.Events.Invalid, onElementInvalid);
173-
doc.on(Element.Events.Valid, onElementValid);
174-
doc.on('update-start', onUpdateStart);
175-
doc.on('update-blocked', onUpdateBlocked);
176-
doc.on('update-success', onUpdateSuccess);
177-
doc.on('update-error', onUpdateError);
178-
doc.on('remove-start', onRemoveStart);
179-
doc.on('remove-success', onRemoveSuccess);
180-
doc.on('remove-error', onRemoveError);
169+
doc.on(ElementEvents.Added, onUpdate);
170+
doc.on(ElementEvents.Edited, onUpdate);
171+
doc.on(ElementEvents.Removed, onUpdate);
172+
doc.on(ElementEvents.Reverted, onUpdate);
173+
doc.on(ElementEvents.Invalid, onElementInvalid);
174+
doc.on(ElementEvents.Valid, onElementValid);
175+
doc.on(DocumentEvents.UpdateStarted, onUpdateStart);
176+
doc.on(DocumentEvents.UpdateBlocked, onUpdateBlocked);
177+
doc.on(DocumentEvents.UpdateSuccess, onUpdateSuccess);
178+
doc.on(DocumentEvents.UpdateError, onUpdateError);
179+
doc.on(DocumentEvents.RemoveStarted, onRemoveStart);
180+
doc.on(DocumentEvents.RemoveSuccess, onRemoveSuccess);
181+
doc.on(DocumentEvents.RemoveError, onRemoveError);
181182

182183
return () => {
183-
doc.on(Element.Events.Added, onUpdate);
184-
doc.off(Element.Events.Edited, onUpdate);
185-
doc.off(Element.Events.Removed, onUpdate);
186-
doc.off(Element.Events.Reverted, onUpdate);
187-
doc.off(Element.Events.Invalid, onElementInvalid);
188-
doc.off(Element.Events.Valid, onElementValid);
189-
doc.off('update-start', onUpdateStart);
190-
doc.off('update-blocked', onUpdateBlocked);
191-
doc.off('update-success', onUpdateSuccess);
192-
doc.off('update-error', onUpdateError);
193-
doc.off('remove-start', onRemoveStart);
194-
doc.off('remove-success', onRemoveSuccess);
195-
doc.off('remove-error', onRemoveError);
184+
doc.on(ElementEvents.Added, onUpdate);
185+
doc.off(ElementEvents.Edited, onUpdate);
186+
doc.off(ElementEvents.Removed, onUpdate);
187+
doc.off(ElementEvents.Reverted, onUpdate);
188+
doc.off(ElementEvents.Invalid, onElementInvalid);
189+
doc.off(ElementEvents.Valid, onElementValid);
190+
doc.off(DocumentEvents.UpdateStarted, onUpdateStart);
191+
doc.off(DocumentEvents.UpdateBlocked, onUpdateBlocked);
192+
doc.off(DocumentEvents.UpdateSuccess, onUpdateSuccess);
193+
doc.off(DocumentEvents.UpdateError, onUpdateError);
194+
doc.off(DocumentEvents.RemoveStarted, onRemoveStart);
195+
doc.off(DocumentEvents.RemoveSuccess, onRemoveSuccess);
196+
doc.off(DocumentEvents.RemoveError, onRemoveError);
196197
};
197198
}, [doc, updateStatus]);
198199

@@ -353,8 +354,8 @@ const EditActionsFooter: React.FunctionComponent<{
353354
data-testid="cancel-button"
354355
onClick={() => {
355356
doc.cancel();
356-
onCancel?.();
357357
updateStatus('Initial');
358+
onCancel?.();
358359
}}
359360
disabled={isCancelDisabled(status)}
360361
>

packages/compass-crud/src/components/table-view/full-width-cell-renderer.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import type Document from 'hadron-document';
88
import type { CellEditorProps } from './cell-editor';
99
import type { GridActions } from '../../stores/grid-store';
10-
import type { Element } from 'hadron-document';
10+
import { DocumentEvents, type Element } from 'hadron-document';
1111
import type { BSONObject, CrudActions } from '../../stores/crud-store';
1212

1313
export type FullWidthCellRendererProps = Pick<
@@ -51,16 +51,22 @@ class FullWidthCellRenderer extends React.Component<
5151
* Subscribe to the update store on mount.
5252
*/
5353
componentDidMount() {
54-
this.doc.on('remove-success', this.handleRemoveSuccess);
55-
this.doc.on('update-success', this.handleUpdateSuccess);
54+
this.doc.on(DocumentEvents.RemoveSuccess, this.handleRemoveSuccess);
55+
this.doc.on(DocumentEvents.UpdateSuccess, this.handleUpdateSuccess);
5656
}
5757

5858
/**
5959
* Unsubscribe from the update store on unmount.
6060
*/
6161
componentWillUnmount() {
62-
this.doc.removeListener('remove-success', this.handleRemoveSuccess);
63-
this.doc.removeListener('update-success', this.handleUpdateSuccess);
62+
this.doc.removeListener(
63+
DocumentEvents.RemoveSuccess,
64+
this.handleRemoveSuccess
65+
);
66+
this.doc.removeListener(
67+
DocumentEvents.UpdateSuccess,
68+
this.handleUpdateSuccess
69+
);
6470
}
6571

6672
/**

packages/compass-crud/src/stores/crud-store.spec.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import { connect } from 'mongodb-data-service';
44
import AppRegistry, {
55
createActivateHelpers,
66
} from '@mongodb-js/compass-app-registry';
7-
import HadronDocument, { Element } from 'hadron-document';
7+
import HadronDocument, {
8+
DocumentEvents,
9+
Element,
10+
type DocumentEventsType,
11+
} from 'hadron-document';
812
import { MongoDBInstance } from 'mongodb-instance-model';
13+
import type { EventEmitter } from 'events';
914
import { once } from 'events';
1015
import sinon from 'sinon';
1116
import chai, { expect } from 'chai';
@@ -110,6 +115,15 @@ function waitForState(store, cb, timeout?: number) {
110115
return waitForStates(store, [cb], timeout);
111116
}
112117

118+
function onceDocumentEvent(
119+
doc: HadronDocument,
120+
event: DocumentEventsType
121+
): Promise<unknown[]> {
122+
// The once function was not meant for strongly typed events, so we need to
123+
// do some additional type casting.
124+
return once(doc as unknown as EventEmitter, event as string);
125+
}
126+
113127
const mockFieldStoreService = {
114128
updateFieldsFromDocuments() {},
115129
updateFieldsFromSchema() {},
@@ -503,7 +517,7 @@ describe('store', function () {
503517
});
504518

505519
it('sets the error for the document', function (done) {
506-
hadronDoc.on('remove-error', ({ message }) => {
520+
hadronDoc.on(DocumentEvents.RemoveError, ({ message }) => {
507521
expect(message).to.equal('error happened');
508522
done();
509523
});
@@ -547,11 +561,11 @@ describe('store', function () {
547561
done();
548562
}, store);
549563

550-
hadronDoc.on('update-blocked', () => {
564+
hadronDoc.on(DocumentEvents.UpdateBlocked, () => {
551565
done(new Error("Didn't expect update to be blocked."));
552566
});
553567

554-
hadronDoc.on('update-error', (errorMessage) => {
568+
hadronDoc.on(DocumentEvents.UpdateError, (errorMessage) => {
555569
done(
556570
new Error(
557571
`Didn't expect update to error. Errored with message: ${errorMessage}`
@@ -586,11 +600,11 @@ describe('store', function () {
586600
setTimeout(() => done(), 100);
587601
}, store);
588602

589-
hadronDoc.on('update-blocked', () => {
603+
hadronDoc.on(DocumentEvents.UpdateBlocked, () => {
590604
done(new Error("Didn't expect update to be blocked."));
591605
});
592606

593-
hadronDoc.on('update-error', (errorMessage) => {
607+
hadronDoc.on(DocumentEvents.UpdateError, (errorMessage) => {
594608
done(
595609
new Error(
596610
`Didn't expect update to error. Errored with message: ${errorMessage}`
@@ -613,7 +627,7 @@ describe('store', function () {
613627
});
614628

615629
it('sets the error for the document', function (done) {
616-
hadronDoc.on('update-error', ({ message }) => {
630+
hadronDoc.on(DocumentEvents.UpdateError, ({ message }) => {
617631
expect(message).to.equal(
618632
'Unable to update, no changes have been made.'
619633
);
@@ -636,7 +650,7 @@ describe('store', function () {
636650
});
637651

638652
it('sets the error for the document', function (done) {
639-
hadronDoc.on('update-error', ({ message }) => {
653+
hadronDoc.on(DocumentEvents.UpdateError, ({ message }) => {
640654
expect(message).to.equal('error happened');
641655
done();
642656
});
@@ -655,7 +669,7 @@ describe('store', function () {
655669
});
656670

657671
it('sets the update blocked for the document', function (done) {
658-
hadronDoc.on('update-blocked', () => {
672+
hadronDoc.on(DocumentEvents.UpdateBlocked, () => {
659673
done();
660674
});
661675

@@ -728,7 +742,7 @@ describe('store', function () {
728742
const invalidHadronDoc = new HadronDocument(doc);
729743
(invalidHadronDoc as any).getId = null;
730744

731-
invalidHadronDoc.on('update-error', ({ message }) => {
745+
invalidHadronDoc.on(DocumentEvents.UpdateError, ({ message }) => {
732746
expect(message).to.equal(
733747
'An error occured when attempting to update the document: this.getId is not a function'
734748
);
@@ -765,7 +779,10 @@ describe('store', function () {
765779
});
766780

767781
it('rejects the update and emits update-error', async function () {
768-
const updateErrorEvent = once(hadronDoc, 'update-error');
782+
const updateErrorEvent = onceDocumentEvent(
783+
hadronDoc,
784+
DocumentEvents.UpdateError
785+
);
769786

770787
await store.updateDocument(hadronDoc);
771788
expect((await updateErrorEvent)[0]).to.match(/Update blocked/);
@@ -998,7 +1015,7 @@ describe('store', function () {
9981015
});
9991016

10001017
it('sets the error for the document', function (done) {
1001-
hadronDoc.on('update-error', ({ message }) => {
1018+
hadronDoc.on(DocumentEvents.UpdateError, ({ message }) => {
10021019
expect(message).to.equal('error happened');
10031020
done();
10041021
});
@@ -1085,7 +1102,10 @@ describe('store', function () {
10851102
});
10861103

10871104
it('rejects the update and emits update-error', async function () {
1088-
const updateErrorEvent = once(hadronDoc, 'update-error');
1105+
const updateErrorEvent = onceDocumentEvent(
1106+
hadronDoc,
1107+
DocumentEvents.UpdateError
1108+
);
10891109

10901110
await store.replaceDocument(hadronDoc);
10911111
expect((await updateErrorEvent)[0]).to.match(/Update blocked/);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
/**
3+
* The event constant.
4+
*/
5+
6+
export const DocumentEvents = {
7+
Cancel: 'Document::Cancel',
8+
Expanded: 'Document::Expanded',
9+
Collapsed: 'Document::Collapsed',
10+
VisibleElementsChanged: 'Document::VisibleElementsChanged',
11+
EditingStarted: 'Document::EditingStarted',
12+
EditingFinished: 'Document::EditingFinished',
13+
MarkedForDeletion: 'Document::MarkedForDeletion',
14+
DeletionFinished: 'Document::DeletionFinished',
15+
UpdateStarted: 'Document::UpdateStarted',
16+
UpdateSuccess: 'Document::UpdateSuccess',
17+
UpdateBlocked: 'Document::UpdateBlocked',
18+
UpdateError: 'Document::UpdateError',
19+
RemoveStarted: 'Document::RemoveStarted',
20+
RemoveSuccess: 'Document::RemoveSuccess',
21+
RemoveError: 'Document::RemoveError',
22+
} as const;
23+
24+
export type DocumentEventsType =
25+
typeof DocumentEvents[keyof typeof DocumentEvents];

0 commit comments

Comments
 (0)