Skip to content

Commit

Permalink
rename event enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ylc395 committed Oct 17, 2021
1 parent cad122d commit 4d510d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
32 changes: 19 additions & 13 deletions src/domain/service/PublishService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ import { AppService, FORBIDDEN } from './AppService';
import { isEmpty, isError, noop, omit, pick, some } from 'lodash';

export enum GeneratorEvents {
PAGE_GENERATED = 'PAGE_GENERATED',
PageGenerated = 'pageGenerated',
}

export enum GitEvents {
PROGRESS = 'PROGRESS',
MESSAGE = 'MESSAGE',
TERMINATED = 'TERMINATED',
INIT_REPO_STATUS_CHANGED = 'INIT_REPO_STATUS_CHANGED',
Progress = 'progress',
Message = 'message',
Terminated = 'terminated',
LocalRepoStatusChanged = 'localRepoStatusChanged',
}

export enum LocalRepoStatus {
Ready,
Fail,
Initializing,
}

const MESSAGES: Record<string, string | undefined> = {
Expand Down Expand Up @@ -55,7 +61,7 @@ export class PublishService {
private readonly generator = container.resolve(generatorToken);
private readonly git = container.resolve(gitClientToken);
private files: string[] = [];
private gitRepoStatus: 'ready' | 'fail' | 'initializing' = 'initializing';
private localRepoStatus: LocalRepoStatus = LocalRepoStatus.Initializing;
readonly githubInfo: Ref<Github | null> = ref(null);
readonly isGenerating = ref(false);
readonly isPublishing = ref(false);
Expand All @@ -73,20 +79,20 @@ export class PublishService {

private async init() {
this.outputDir.value = await this.generator.getOutputDir();
this.git.on(GitEvents.PROGRESS, (e) => this.refreshPublishingProgress(e));
this.git.on(GitEvents.MESSAGE, (message) => this.refreshPublishingProgress({ message }));
this.git.on(GitEvents.INIT_REPO_STATUS_CHANGED, (e) => {
this.gitRepoStatus = e;
this.git.on(GitEvents.Progress, (e) => this.refreshPublishingProgress(e));
this.git.on(GitEvents.Message, (message) => this.refreshPublishingProgress({ message }));
this.git.on(GitEvents.LocalRepoStatusChanged, (e) => {
this.localRepoStatus = e;

if (this.gitRepoStatus === 'initializing') {
if (this.localRepoStatus === LocalRepoStatus.Initializing) {
this.refreshPublishingProgress({
phase: 'Local repository initializing...',
message: '',
});
}
});

this.generator.on(GeneratorEvents.PAGE_GENERATED, this.refreshGeneratingProgress.bind(this));
this.generator.on(GeneratorEvents.PageGenerated, this.refreshGeneratingProgress.bind(this));

this.githubInfo.value = {
...DEFAULT_GITHUB,
Expand Down Expand Up @@ -170,7 +176,7 @@ export class PublishService {
if (!this.isGithubInfoValid.value) {
throw new Error('invalid github info');
}
const initGit_ = initGit || this.gitRepoStatus === 'fail';
const initGit_ = initGit || this.localRepoStatus === LocalRepoStatus.Fail;

if (initGit_) {
this.refreshPublishingProgress();
Expand Down
2 changes: 1 addition & 1 deletion src/driver/generator/webviewApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Generator extends EventEmitter<GeneratorEvents> {
event: 'getGeneratingProgress',
});
if (!isEqual(this.lastProgress, progress)) {
this.emit(GeneratorEvents.PAGE_GENERATED, progress);
this.emit(GeneratorEvents.PageGenerated, progress);
this.lastProgress = progress;
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/driver/git/webviewApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { wrap, Remote, releaseProxy, expose } from 'comlink';
import fs from 'driver/fs/webviewApi';
import type { FsWorkerCallRequest, FsWorkerCallResponse } from 'driver/fs/type';
import { gitClientToken, GitEvents } from 'domain/service/PublishService';
import { gitClientToken, GitEvents, LocalRepoStatus } from 'domain/service/PublishService';
import { Github, PublishResults } from 'domain/model/Publishing';
import { joplinToken } from 'domain/service/AppService';
import type { GitEventHandler, WorkerGit } from './type';
Expand Down Expand Up @@ -84,16 +84,16 @@ class Git extends EventEmitter<GitEvents> {
this.initRepoPromise = new Promise((resolve, reject) => {
const reject_ = (e: unknown) => {
reject(e);
this.emit(GitEvents.INIT_REPO_STATUS_CHANGED, 'fail');
this.emit(GitEvents.LocalRepoStatusChanged, LocalRepoStatus.Fail);
};

const resolve_ = () => {
resolve();
this.emit(GitEvents.INIT_REPO_STATUS_CHANGED, 'ready');
this.emit(GitEvents.LocalRepoStatusChanged, LocalRepoStatus.Ready);
};

this.stopInitRepo = reject_;
this.emit(GitEvents.INIT_REPO_STATUS_CHANGED, 'initializing');
this.emit(GitEvents.LocalRepoStatusChanged, LocalRepoStatus.Initializing);
workerGit
.initRepo({
githubInfo,
Expand All @@ -118,7 +118,7 @@ class Git extends EventEmitter<GitEvents> {
this.worker?.terminate();

if (triggerTerminate) {
this.emit(GitEvents.TERMINATED);
this.emit(GitEvents.Terminated);
}

this.workerGit?.[releaseProxy]();
Expand Down Expand Up @@ -160,7 +160,7 @@ class Git extends EventEmitter<GitEvents> {

this.isPushing = true;
const terminatePromise = new Promise<never>((resolve, reject) => {
this.once(GitEvents.TERMINATED, () => reject(Error(PublishResults.TERMINATED)));
this.once(GitEvents.Terminated, () => reject(Error(PublishResults.TERMINATED)));
});

try {
Expand Down Expand Up @@ -224,11 +224,11 @@ class Git extends EventEmitter<GitEvents> {
}

private handleProgress = (e: GitProgressEvent) => {
this.emit(GitEvents.PROGRESS, e);
this.emit(GitEvents.Progress, e);
};

private handleMessage = (e: string) => {
this.emit(GitEvents.MESSAGE, e);
this.emit(GitEvents.Message, e);
};

terminate() {
Expand Down

0 comments on commit 4d510d4

Please sign in to comment.