Skip to content

Commit

Permalink
lint: address all lint warnings 😋
Browse files Browse the repository at this point in the history
Signed-off-by: zFernand0 <[email protected]>
  • Loading branch information
zFernand0 committed Jul 2, 2024
1 parent a4e268a commit ac67acc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("Event Operator and Processor", () => {
});
// have to reset because test environment doesn't add .zowe to ZOWE_CLI_HOME :(
process.env.ZOWE_CLI_HOME = path.join(process.env.ZOWE_CLI_HOME || '', ".zowe");
zoweCliHome = process.env.ZOWE_CLI_HOME;
zoweCliHome = process.env.ZOWE_CLI_HOME;
EventUtils.ensureEventsDirExists(zoweCliHome);
const extJson: IExtendersJsonOpts = ConfigUtils.readExtendersJson();
sampleApps.forEach(app => extJson.profileTypes[app] = { from: [app] });
Expand Down Expand Up @@ -148,30 +148,31 @@ describe("Event Operator and Processor", () => {
});

describe("Custom Events", () => {
it("should create an event file upon first subscription if file does not exist - specific to CustomUserEvent directory structure", async () => {
const setupWatcherSpy = jest.spyOn(EventUtils, "setupWatcher");
const callback = jest.fn();
const Watcher = EventOperator.getWatcher(sampleApps[1]);
const Emitter = EventOperator.getEmitter(sampleApps[1]);
const eventDir = path.join(zoweCliHome, ".events", sampleApps[1]); //corresponds to emitter's event file

expect((Watcher as EventProcessor).subscribedEvents.get(customUserEvent)).toBeFalsy();

// Subscribe to event
Watcher.subscribeUser(customUserEvent, callback);
const eventDetails: IEventJson = (Watcher as any).subscribedEvents.get(customUserEvent).toJson();
expect(callback).not.toHaveBeenCalled();
expect(fs.existsSync(eventDetails.eventFilePath)).toBeTruthy();

// Emit event and trigger callback
Emitter.emitEvent(customUserEvent);
setupWatcherSpy.mock.calls.forEach(call => (call[2] as Function)());

expect(eventDetails.eventName).toEqual(customUserEvent);
expect(eventDetails.eventFilePath).toContain(eventDir);
expect(callback).toHaveBeenCalled();
expect(EventUtils.isUserEvent(eventDetails.eventName)).toBeFalsy(); //ensuring this custom event isnt a Zowe event
EventOperator.deleteProcessor(sampleApps[1]);
});
it("should create an event file upon first subscription if file does not exist - specific to CustomUserEvent directory structure",
async () => {
const setupWatcherSpy = jest.spyOn(EventUtils, "setupWatcher");
const callback = jest.fn();
const Watcher = EventOperator.getWatcher(sampleApps[1]);
const Emitter = EventOperator.getEmitter(sampleApps[1]);
const eventDir = path.join(zoweCliHome, ".events", sampleApps[1]); //corresponds to emitter's event file

expect((Watcher as EventProcessor).subscribedEvents.get(customUserEvent)).toBeFalsy();

// Subscribe to event
Watcher.subscribeUser(customUserEvent, callback);
const eventDetails: IEventJson = (Watcher as any).subscribedEvents.get(customUserEvent).toJson();
expect(callback).not.toHaveBeenCalled();
expect(fs.existsSync(eventDetails.eventFilePath)).toBeTruthy();

// Emit event and trigger callback
Emitter.emitEvent(customUserEvent);
setupWatcherSpy.mock.calls.forEach(call => (call[2] as Function)());

expect(eventDetails.eventName).toEqual(customUserEvent);
expect(eventDetails.eventFilePath).toContain(eventDir);
expect(callback).toHaveBeenCalled();
expect(EventUtils.isUserEvent(eventDetails.eventName)).toBeFalsy(); //ensuring this custom event isnt a Zowe event
EventOperator.deleteProcessor(sampleApps[1]);
});
});
});
19 changes: 12 additions & 7 deletions packages/imperative/src/events/src/EventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ import { IProcessorTypes } from "./doc/IEventInstanceTypes";
* It uses a map where event names are keys, and values are Event objects that hold detailed event information and subscriptions.
*
* An `EventProcessor` handles three main functionalities:
* - **Subscribing to Events**: Registration of a callback function that will be executed when that event occurs.
* - **Emitting Events**: Notifying other applications or parts of the same application about certain actions or changes.
* - **Managing Event Subscriptions**: Mapping subscribed events and their corresponding callbacks, ensuring that events are properly handled and dispatched.
* - **Subscribing to Events**:
* Registration of a callback function that will be executed when that event occurs.
* - **Emitting Events**:
* Notifying other applications or parts of the same application about certain actions or changes.
* - **Managing Event Subscriptions**:
* Mapping subscribed events and their corresponding callbacks, ensuring that events are properly handled and dispatched.
*
* ### Understanding Event Types
* - **Predefined Zowe Events**: Zowe provides a set of predefined shared and user events that can be watched.
* - **Custom Events**: Applications can define their own shared and user events, allowing for custom event-driven behavior.
* - **Predefined Zowe Events**:
* Zowe provides a set of predefined shared and user events that can be watched.
* - **Custom Events**:
* Applications can define their own shared and user events, allowing for custom event-driven behavior.
*
* @export
* @class EventProcessor
Expand Down Expand Up @@ -78,8 +83,8 @@ export class EventProcessor {
eventType = EventUtils.isSharedEvent(eventName) ? EventTypes.ZoweSharedEvents : EventTypes.SharedEvents;
} else if (type === "user") {
eventType = EventUtils.isUserEvent(eventName) ? EventTypes.ZoweUserEvents : EventTypes.UserEvents;
}
}

const disposable = EventUtils.createSubscription(this, eventName, eventType);
EventUtils.setupWatcher(this, eventName, callbacks);
return disposable;
Expand Down
1 change: 1 addition & 0 deletions packages/imperative/src/events/src/EventUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export class EventUtils {
*/
public static ensureFileExists(filePath: string) {
try {
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const fd = fs.openSync(filePath, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_RDWR, 0o640);
fs.closeSync(fd);
} catch (err) {
Expand Down

0 comments on commit ac67acc

Please sign in to comment.