-
Notifications
You must be signed in to change notification settings - Fork 103
(fix) Reduce re-render churn in FormProcessorFactory #670
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
Open
ibacher
wants to merge
5
commits into
main
Choose a base branch
from
fix/improve-context-rerenders
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+204
−113
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
930bdf8
(fix) Re-write some core hooks in the form-processor-factory to reduc…
ibacher b0adb12
Reduce churn in processor context
ibacher 9f278eb
Restore deleted comments
ibacher 55f37ca
Merge branch 'main' into fix/improve-context-rerenders
denniskigen 6169aa9
Update src/components/processor-factory/form-processor-factory.compon…
ibacher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,35 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { useMemo, useRef } from 'react'; | ||
| import useSWRImmutable from 'swr/immutable'; | ||
| import { getRegisteredValidator } from '../registry/registry'; | ||
| import { type FormField, type FormFieldValidator } from '../types'; | ||
|
|
||
| export function useFormFieldValidators(fields: FormField[]) { | ||
| const [validators, setValidators] = useState<Record<string, FormFieldValidator>>(); | ||
|
|
||
| useEffect(() => { | ||
| const supportedTypes = new Set<string>(); | ||
| const validatorTypesKey = useMemo(() => { | ||
| const uniqueTypes = new Set<string>(); | ||
| fields.forEach((field) => { | ||
| field.validators?.forEach((validator) => supportedTypes.add(validator.type)); | ||
| }); | ||
| const supportedTypesArray = Array.from(supportedTypes); | ||
| Promise.all(supportedTypesArray.map((type) => getRegisteredValidator(type))).then((validators) => { | ||
| setValidators( | ||
| Object.assign({}, ...validators.map((validator, index) => ({ [supportedTypesArray[index]]: validator }))), | ||
| ); | ||
| field.validators?.forEach((validator) => uniqueTypes.add(validator.type)); | ||
| }); | ||
| return Array.from(uniqueTypes).sort().join(','); | ||
| }, [fields]); | ||
|
|
||
| return validators; | ||
| const validatorsRef = useRef<Record<string, FormFieldValidator>>({}); | ||
|
|
||
| const { data: validators } = useSWRImmutable( | ||
| validatorTypesKey ? ['formFieldValidators', validatorTypesKey] : null, | ||
| async ([, key]) => { | ||
| const types = key.split(','); | ||
| const loadedValidators = await Promise.all(types.map((type) => getRegisteredValidator(type))); | ||
| const validatorsByType: Record<string, FormFieldValidator> = {}; | ||
| types.forEach((type, index) => { | ||
| validatorsByType[type] = loadedValidators[index]; | ||
| }); | ||
| return validatorsByType; | ||
| }, | ||
| ); | ||
|
|
||
| if (validators && validators !== validatorsRef.current) { | ||
| validatorsRef.current = validators; | ||
| } | ||
|
|
||
| return validatorsRef.current; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,33 @@ | ||
| import { useState, useEffect } from 'react'; | ||
| import { useMemo, useRef } from 'react'; | ||
| import useSWRImmutable from 'swr/immutable'; | ||
| import { type FormField, type FormFieldValueAdapter } from '../types'; | ||
| import { getRegisteredFieldValueAdapter } from '../registry/registry'; | ||
|
|
||
| export const useFormFieldValueAdapters = (fields: FormField[]) => { | ||
| const [adapters, setAdapters] = useState<Record<string, FormFieldValueAdapter>>({}); | ||
|
|
||
| useEffect(() => { | ||
| const supportedTypes = new Set<string>(); | ||
| fields.forEach((field) => { | ||
| supportedTypes.add(field.type); | ||
| }); | ||
| const supportedTypesArray = Array.from(supportedTypes); | ||
| Promise.all(supportedTypesArray.map((type) => getRegisteredFieldValueAdapter(type))).then((adapters) => { | ||
| const adaptersByType = supportedTypesArray.map((type, index) => ({ | ||
| [type]: adapters[index], | ||
| })); | ||
| setAdapters(Object.assign({}, ...adaptersByType)); | ||
| }); | ||
| const typesKey = useMemo(() => { | ||
| const uniqueTypes = new Set<string>(); | ||
| fields.forEach((field) => uniqueTypes.add(field.type)); | ||
| return Array.from(uniqueTypes).sort().join(','); | ||
| }, [fields]); | ||
|
|
||
| return adapters; | ||
| const adaptersRef = useRef<Record<string, FormFieldValueAdapter>>({}); | ||
|
|
||
| const { data: adapters } = useSWRImmutable( | ||
| typesKey ? ['formFieldValueAdapters', typesKey] : null, | ||
| async ([, key]) => { | ||
| const types = key.split(','); | ||
| const loadedAdapters = await Promise.all(types.map((type) => getRegisteredFieldValueAdapter(type))); | ||
| const adaptersByType: Record<string, FormFieldValueAdapter> = {}; | ||
| types.forEach((type, index) => { | ||
| adaptersByType[type] = loadedAdapters[index]; | ||
| }); | ||
| return adaptersByType; | ||
| }, | ||
| ); | ||
|
|
||
| if (adapters && adapters !== adaptersRef.current) { | ||
| adaptersRef.current = adapters; | ||
| } | ||
|
|
||
| return adaptersRef.current; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,65 +1,55 @@ | ||
| import { useMemo } from 'react'; | ||
| import { useMemo, useRef } from 'react'; | ||
| import { type FormField, type FormSchema } from '../types'; | ||
|
|
||
| export function useFormFields(form: FormSchema): { formFields: FormField[]; conceptReferences: Set<string> } { | ||
| const [flattenedFields, conceptReferences] = useMemo(() => { | ||
| const conceptReferencesRef = useRef<Set<string>>(new Set()); | ||
|
|
||
| const [flattenedFields, conceptReferencesKey] = useMemo(() => { | ||
| const flattenedFieldsTemp: FormField[] = []; | ||
| const conceptReferencesTemp = new Set<string>(); | ||
|
|
||
| const processFlattenedFields = ( | ||
| fields: FormField[], | ||
| ): { | ||
| flattenedFields: FormField[]; | ||
| conceptReferences: Set<string>; | ||
| } => { | ||
| const flattenedFields: FormField[] = []; | ||
| const conceptReferences = new Set<string>(); | ||
| const processField = (field: FormField, parentGroupId?: string) => { | ||
| // Add group ID to nested fields if applicable | ||
| const processedField = parentGroupId ? { ...field, meta: { ...field.meta, groupId: parentGroupId } } : field; | ||
|
|
||
| const processField = (field: FormField, parentGroupId?: string) => { | ||
| // Add group ID to nested fields if applicable | ||
| const processedField = parentGroupId ? { ...field, meta: { ...field.meta, groupId: parentGroupId } } : field; | ||
| // Add field to flattened list | ||
| flattenedFieldsTemp.push(processedField); | ||
|
|
||
| // Add field to flattened list | ||
| flattenedFields.push(processedField); | ||
| // Collect concept references | ||
| if (processedField.questionOptions?.concept) { | ||
| conceptReferencesTemp.add(processedField.questionOptions.concept); | ||
| } | ||
|
|
||
| // Collect concept references | ||
| if (processedField.questionOptions?.concept) { | ||
| conceptReferences.add(processedField.questionOptions.concept); | ||
| // Collect concept references from answers | ||
| processedField.questionOptions?.answers?.forEach((answer) => { | ||
| if (answer.concept) { | ||
| conceptReferencesTemp.add(answer.concept); | ||
| } | ||
| }); | ||
|
|
||
| // Collect concept references from answers | ||
| processedField.questionOptions?.answers?.forEach((answer) => { | ||
| if (answer.concept) { | ||
| conceptReferences.add(answer.concept); | ||
| } | ||
| // Recursively process nested questions for obsGroup | ||
| if (processedField.type === 'obsGroup' && processedField.questions) { | ||
| processedField.questions.forEach((nestedField) => { | ||
| processField(nestedField, processedField.id); | ||
| }); | ||
|
|
||
| // Recursively process nested questions for obsGroup | ||
| if (processedField.type === 'obsGroup' && processedField.questions) { | ||
| processedField.questions.forEach((nestedField) => { | ||
| processField(nestedField, processedField.id); | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| // Process all input fields | ||
| fields.forEach((field) => processField(field)); | ||
|
|
||
| return { flattenedFields, conceptReferences }; | ||
| } | ||
| }; | ||
|
|
||
| // Process all input fields | ||
| form.pages?.forEach((page) => | ||
| page.sections?.forEach((section) => { | ||
| if (section.questions) { | ||
| const { flattenedFields, conceptReferences } = processFlattenedFields(section.questions); | ||
| flattenedFieldsTemp.push(...flattenedFields); | ||
| conceptReferences.forEach((conceptReference) => conceptReferencesTemp.add(conceptReference)); | ||
| } | ||
| section.questions?.forEach((field) => processField(field)); | ||
| }), | ||
| ); | ||
|
|
||
| return [flattenedFieldsTemp, conceptReferencesTemp]; | ||
| const sortedRefs = Array.from(conceptReferencesTemp).sort(); | ||
| return [flattenedFieldsTemp, sortedRefs.join(',')]; | ||
| }, [form]); | ||
|
|
||
| return { formFields: flattenedFields, conceptReferences }; | ||
| const currentKey = Array.from(conceptReferencesRef.current).sort().join(','); | ||
| if (conceptReferencesKey !== currentKey) { | ||
| conceptReferencesRef.current = new Set(conceptReferencesKey.split(',').filter(Boolean)); | ||
| } | ||
|
|
||
| return { formFields: flattenedFields, conceptReferences: conceptReferencesRef.current }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.