Skip to content

Commit

Permalink
dispatch notifications asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
liady committed Oct 20, 2024
1 parent a283b4f commit 20f4dc5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
8 changes: 6 additions & 2 deletions editor/src/core/shared/import/import-operation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export function notifyOperationStarted(dispatch: EditorDispatch, operation: Impo
timeStarted: Date.now(),
timeDone: null,
}
dispatch([updateImportOperations([operationWithTime], ImportOperationAction.Update)])
setTimeout(() => {
dispatch([updateImportOperations([operationWithTime], ImportOperationAction.Update)])
}, 0)
}

export function notifyOperationFinished(
Expand All @@ -54,7 +56,9 @@ export function notifyOperationFinished(
timeDone: timeDone,
result: result,
}
dispatch([updateImportOperations([operationWithTime], ImportOperationAction.Update)])
setTimeout(() => {
dispatch([updateImportOperations([operationWithTime], ImportOperationAction.Update)])
}, 0)
}

export function areSameOperation(existing: ImportOperation, incoming: ImportOperation): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function checkProjectLanguage(
let jsCount = 0
let tsCount = 0
applyToAllUIJSFiles(projectContents, (filename, uiJSFile) => {
if (filename.endsWith('.ts') || filename.endsWith('.tsx')) {
if ((filename.endsWith('.ts') || filename.endsWith('.tsx')) && !filename.endsWith('.d.ts')) {
tsCount++
} else if (filename.endsWith('.js') || filename.endsWith('.jsx')) {
jsCount++
Expand All @@ -101,7 +101,7 @@ function checkProjectLanguage(
dispatch,
'language',
RequirementResolutionResult.Critical,
'Majority of project files are in TS/TSX',
'There are Typescript files in the project',
'typescript',
)
} else if (jsCount == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ const initialTexts: Record<ProjectRequirement, string> = {
reactVersion: 'Checking React version',
}

export function updateProjectRequirementsStatus(
dispatch: EditorDispatch,
projectRequirements: Partial<ProjectRequirements>,
) {
setTimeout(() => {
dispatch([updateProjectRequirements(projectRequirements)])
}, 0)
}

export function resetRequirementsResolutions(dispatch: EditorDispatch) {
let projectRequirements = emptyProjectRequirements()
dispatch([updateProjectRequirements(projectRequirements)])
updateProjectRequirementsStatus(dispatch, projectRequirements)
notifyOperationStarted(dispatch, {
type: 'checkRequirements',
children: Object.keys(projectRequirements).map((key) =>
Expand All @@ -35,13 +44,11 @@ export function notifyCheckingRequirement(
requirement: ProjectRequirement,
text: string,
) {
dispatch([
updateProjectRequirements({
[requirement]: {
status: RequirementResolutionStatus.Pending,
},
}),
])
updateProjectRequirementsStatus(dispatch, {
[requirement]: {
status: RequirementResolutionStatus.Pending,
},
})
notifyOperationStarted(dispatch, {
type: 'checkRequirementAndFix',
id: requirement,
Expand All @@ -56,15 +63,13 @@ export function notifyResolveRequirement(
text: string,
value?: string,
) {
dispatch([
updateProjectRequirements({
[requirementName]: {
status: RequirementResolutionStatus.Done,
resolution: resolution,
value: value,
},
}),
])
updateProjectRequirementsStatus(dispatch, {
[requirementName]: {
status: RequirementResolutionStatus.Done,
resolution: resolution,
value: value,
},
})
const result =
resolution === RequirementResolutionResult.Found ||
resolution === RequirementResolutionResult.Fixed
Expand Down Expand Up @@ -100,9 +105,7 @@ export function updateRequirements(

const aggregatedDoneStatus = getAggregatedStatus(result)
if (aggregatedDoneStatus != null) {
setTimeout(() => {
notifyOperationFinished(dispatch, { type: 'checkRequirements' }, aggregatedDoneStatus)
}, 0)
notifyOperationFinished(dispatch, { type: 'checkRequirements' }, aggregatedDoneStatus)
}

return result
Expand Down

0 comments on commit 20f4dc5

Please sign in to comment.