diff --git a/MIGRATION.md b/MIGRATION.md index 8020822aea..7eed4d8b55 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -62,6 +62,21 @@ As part of this upgrade, you may notice several visible changes due to Vuetify 4 * The grid system is now gap-based. * The default theme is now `system` instead of `light`. +### Vue keeps the state of a form shallow + +`JsonForms` of `@jsonforms/vue` now keeps its state shallow. +Vue no longer makes a proxy for each node of the `schema`, the `uischema`, the `renderers`, the `cells` and the form data, which makes a large form much faster to mount. + +The form unwraps a proxy of the `data`, the `schema`, the `uischema` and `ajv` with `toRaw`, and gives the other props to the state as they are. +JSON Forms writes no flag on the objects of the application. +An application that keeps the `renderers`, the `cells` or the `uischemas` out of deep reactive state thus no longer needs `markRaw` for them, and Vue no longer gives the "received a Component that was made a reactive object" warning for a renderer set. +An application that keeps them in `reactive()` state gives a proxy of them to the form. +Use `shallowRef`, `shallowReactive` or `markRaw` for a renderer set, for which Vue gives the warning above. + +The form data that a renderer gets is no longer a proxy. +A change of the data thus needs a `dispatch` of an `update` action, which the `useJsonFormsControl` composition and its relatives do for you through `handleChange`. +A custom renderer that writes into the data object directly, for example with a `v-model` on a nested field, must use `handleChange` instead. + ## Migrating to JSON Forms 3.8 ### `Translator` type changed from overloaded signatures to a generic conditional type diff --git a/packages/vue-vuetify/dev/validate/index.ts b/packages/vue-vuetify/dev/validate/index.ts index c454e69279..5a1b95905a 100644 --- a/packages/vue-vuetify/dev/validate/index.ts +++ b/packages/vue-vuetify/dev/validate/index.ts @@ -1,5 +1,4 @@ import type { Options } from 'ajv'; -import { markRaw } from 'vue'; import { createAjv as createDefaultAjv } from '../../src'; import { ajvKeywords } from './keywords'; @@ -13,6 +12,5 @@ export const createAjv = () => { const ajv = createDefaultAjv(options); ajvKeywords(ajv); - // when ajv is used in component properties do not make it reactive - return markRaw(ajv); + return ajv; }; diff --git a/packages/vue-vuetify/dev/views/ExampleView.vue b/packages/vue-vuetify/dev/views/ExampleView.vue index b95099015e..bee442c683 100644 --- a/packages/vue-vuetify/dev/views/ExampleView.vue +++ b/packages/vue-vuetify/dev/views/ExampleView.vue @@ -5,7 +5,6 @@ import cloneDeep from 'lodash/cloneDeep'; import find from 'lodash/find'; import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; import { - markRaw, onMounted, provide, ref, @@ -66,7 +65,7 @@ const initialState = (exampleProp: ExampleDescription) => { // Get custom renderers for this example (if any) const customRenderers = getCustomRenderersForExample(example.name); - const renderers = markRaw([...customRenderers, ...extendedVuetifyRenderers]); + const renderers = [...customRenderers, ...extendedVuetifyRenderers]; return { data: example.data, @@ -284,10 +283,6 @@ const handleAction = (action: Action) => { if (action) { const newState = action.apply(state); if (newState) { - if (newState.renderers) { - newState.renderers = markRaw(newState.renderers); - } - Object.assign(state, newState); } } diff --git a/packages/vue-vuetify/src/complex/components/AdditionalProperties.vue b/packages/vue-vuetify/src/complex/components/AdditionalProperties.vue index d38df0b857..ce102a07af 100644 --- a/packages/vue-vuetify/src/complex/components/AdditionalProperties.vue +++ b/packages/vue-vuetify/src/complex/components/AdditionalProperties.vue @@ -126,7 +126,6 @@ import { IsDynamicPropertyContext } from '@/util/inject'; import { computed, defineComponent, - markRaw, provide, ref, unref, @@ -433,12 +432,12 @@ export default defineComponent({ return { validationMode: validationMode, - i18n: i18n ? markRaw(i18n) : i18n, - middleware: middleware ? markRaw(middleware) : middleware, + i18n, + middleware, t, mdAndUp, vuetifyProps, - ajv: ajv ? markRaw(ajv) : ajv, + ajv, control, styles, appliedOptions, diff --git a/packages/vue-vuetify/tests/unit/additional/AdditionalPropertiesAjv.spec.ts b/packages/vue-vuetify/tests/unit/additional/AdditionalPropertiesAjv.spec.ts index d99ae5a5e1..56eb58fa5f 100644 --- a/packages/vue-vuetify/tests/unit/additional/AdditionalPropertiesAjv.spec.ts +++ b/packages/vue-vuetify/tests/unit/additional/AdditionalPropertiesAjv.spec.ts @@ -1,6 +1,5 @@ import { describe, it, expect, beforeEach } from 'vitest'; import { clearAllIds, createAjv } from '@jsonforms/core'; -import { markRaw } from 'vue'; import { extendedVuetifyRenderers } from '../../../src'; import { mountJsonForms } from '../util/util'; @@ -27,7 +26,7 @@ describe('AdditionalProperties nested AJV', () => { it('mounts a map whose key pattern is only valid without the `u` flag', () => { // A parent form configured with `unicodeRegExp: false` - const ajv = markRaw(createAjv({ unicodeRegExp: false })); + const ajv = createAjv({ unicodeRegExp: false }); expect(() => mountJsonForms( { secretFiles: {} }, diff --git a/packages/vue/src/components/JsonForms.vue b/packages/vue/src/components/JsonForms.vue index 9473db176c..183edb9532 100644 --- a/packages/vue/src/components/JsonForms.vue +++ b/packages/vue/src/components/JsonForms.vue @@ -27,7 +27,14 @@ import { ValidationMode, } from '@jsonforms/core'; import isEqual from 'lodash/isEqual'; -import { defineComponent, PropType, reactive, toRaw } from 'vue'; +import { + defineComponent, + PropType, + reactive, + shallowReactive, + shallowRef, + toRaw, +} from 'vue'; import { JsonFormsChangeEvent, MaybeReadonly } from '../types'; import DispatchRenderer from './DispatchRenderer.vue'; @@ -120,11 +127,25 @@ export default defineComponent({ }, }, emits: ['change', 'update:data'], + /** + * The state of a form is shallow. + * + * JSON Forms only reads the schema and the UI schema, thus Vue does not need + * to make them reactive. A deep proxy made a new proxy for each nested schema + * node at the first read of that node. Testers and the scope resolution read + * the schema many times for each control, thus these proxies made a large + * form slow to mount. + * + * `toRaw` unwraps a proxy of the data, the schema and the UI schema, thus the + * form always works on the plain objects. `shallowRef` and `shallowReactive` + * keep these objects as they are. A reducer replaces `core` for each change + * and never changes it in place, thus shallow tracking is sufficient. + */ data() { - const dataToUse = this.data; + const dataToUse = toRaw(this.data); const schemaToUse: JsonSchema = - this.schema ?? Generate.jsonSchema(dataToUse); - const uischemaToUse = this.uischema ?? generateUISchema(schemaToUse); + toRaw(this.schema) ?? Generate.jsonSchema(dataToUse); + const uischemaToUse = toRaw(this.uischema) ?? generateUISchema(schemaToUse); const initCore = (): JsonFormsCore => { const initialCore = { data: dataToUse, @@ -135,7 +156,8 @@ export default defineComponent({ initialCore, Actions.init(dataToUse, schemaToUse, uischemaToUse, { validationMode: this.validationMode, - ajv: this.ajv, + // Ajv cannot compile a schema through a proxy of itself. + ajv: toRaw(this.ajv), additionalErrors: this.additionalErrors, }), coreReducer @@ -143,10 +165,10 @@ export default defineComponent({ return core; }; return { - schemaToUse, - dataToUse, - uischemaToUse, - jsonforms: { + schemaToUse: shallowRef(schemaToUse), + dataToUse: shallowRef(dataToUse), + uischemaToUse: shallowRef(uischemaToUse), + jsonforms: shallowReactive({ core: initCore(), config: configReducer(undefined, Actions.setConfig(this.config)), i18n: i18nReducer( @@ -161,7 +183,7 @@ export default defineComponent({ cells: this.cells, uischemas: this.uischemas, readonly: this.readonly, - }, + }), }; }, computed: { @@ -184,17 +206,20 @@ export default defineComponent({ }, watch: { schema(newSchema) { - this.schemaToUse = newSchema ?? Generate.jsonSchema(this.dataToUse); + this.schemaToUse = + toRaw(newSchema) ?? Generate.jsonSchema(this.dataToUse); if (!this.uischema) { this.uischemaToUse = generateUISchema(this.schemaToUse); } }, uischema(newUischema) { - this.uischemaToUse = newUischema ?? generateUISchema(this.schemaToUse); + this.uischemaToUse = + toRaw(newUischema) ?? generateUISchema(this.schemaToUse); }, data(newData) { - const isSameAsCurrentData = newData === this.jsonforms.core.data; - this.dataToUse = newData; + const isSameAsCurrentData = + toRaw(newData) === toRaw(this.jsonforms.core.data); + this.dataToUse = toRaw(newData); if ( (this.schema === undefined || this.schema === null) && @@ -239,7 +264,8 @@ export default defineComponent({ this.uischemaToUse, { validationMode: this.validationMode, - ajv: this.ajv, + // Ajv cannot compile a schema through a proxy of itself. + ajv: toRaw(this.ajv), additionalErrors: this.additionalErrors, } ), diff --git a/packages/vue/tests/unit/JsonForms.spec.ts b/packages/vue/tests/unit/JsonForms.spec.ts index 1a49500952..6a1ef0cc1b 100644 --- a/packages/vue/tests/unit/JsonForms.spec.ts +++ b/packages/vue/tests/unit/JsonForms.spec.ts @@ -1,14 +1,48 @@ import { Actions, JsonFormsUISchemaRegistryEntry, + JsonFormsRendererRegistryEntry, + JsonFormsCellRendererRegistryEntry, Generate, Middleware, + createAjv, } from '@jsonforms/core'; -import { shallowMount } from '@vue/test-utils'; -import { nextTick } from 'vue'; +import { + defineComponent, + h, + isReactive, + nextTick, + reactive, + shallowReactive, + watch, +} from 'vue'; +import { flushPromises, mount, shallowMount } from '@vue/test-utils'; import { JsonForms } from '../../src'; import { bindings } from '../testHelper'; +/** + * Mounts a form as a child of an application. + * + * `@vue/test-utils` keeps the props of a mount in `reactive()`, thus a plain + * object of a test reaches `JsonForms` as a proxy. Vue keeps the props of a + * component shallow, thus a parent gives the object itself. The + * `shallowReactive` state of this parent replaces a prop. + */ +const mountInParent = (props: Record) => { + const state = shallowReactive({ ...props }); + const parent = mount( + defineComponent({ + render: () => h(JsonForms as any, { ...state }), + }) + ); + const vm = () => parent.findComponent(JsonForms).vm as any; + return { + state, + jsonforms: () => vm().jsonforms, + core: () => vm().jsonforms.core, + }; +}; + describe('JsonForms.vue', () => { it('uses undefined as schema prop when not given', () => { const data = { number: 5 }; @@ -311,4 +345,213 @@ describe('JsonForms.vue', () => { expect((wrapper.vm as any).schemaToUse).toBe(initialSchema); expect((wrapper.vm as any).uischemaToUse).toBe(initialUiSchema); }); + + it('adds no reactivity to the given schema and ui schema', () => { + const data = { number: 5.5 }; + const schema = { + type: 'object', + properties: { number: { type: 'number' } }, + }; + const uischema = { + type: 'Control', + scope: '#/properties/number', + }; + const renderers: JsonFormsUISchemaRegistryEntry[] = []; + const form = mountInParent({ data, schema, uischema, renderers }); + const core = form.core(); + + expect(isReactive(core.schema)).toBe(false); + expect(isReactive(core.uischema)).toBe(false); + // A renderer gets the original nested nodes, and not proxies of them. + expect(core.schema).toBe(schema); + expect(core.schema.properties.number).toBe(schema.properties.number); + expect(core.uischema).toBe(uischema); + expect(isReactive(core.data)).toBe(false); + expect(core.data).toBe(data); + }); + + it('adds no reactivity to a generated schema and ui schema', () => { + const data = { number: 5.5 }; + const renderers: JsonFormsUISchemaRegistryEntry[] = []; + const core = mountInParent({ data, renderers }).core(); + + expect(isReactive(core.schema)).toBe(false); + expect(isReactive(core.uischema)).toBe(false); + }); + + it('applies a new schema prop to the core state', async () => { + const data = { number: 5.5 }; + const schema = { + type: 'object', + properties: { number: { type: 'number' } }, + }; + const newSchema = { + type: 'object', + properties: { number: { type: 'string' } }, + }; + const renderers: JsonFormsUISchemaRegistryEntry[] = []; + const form = mountInParent({ data, schema, renderers }); + expect(form.core().schema).toBe(schema); + expect(form.core().errors).toHaveLength(0); + + form.state.schema = newSchema; + await flushPromises(); + + expect(form.core().schema).toBe(newSchema); + // The validation runs again for the new schema. + expect(form.core().errors).toHaveLength(1); + }); + + it('applies a new ui schema prop to the core state', async () => { + const data = { number: 5.5 }; + const schema = { + type: 'object', + properties: { number: { type: 'number' } }, + }; + const uischema = { type: 'Control', scope: '#/properties/number' }; + const newUischema = { + type: 'VerticalLayout', + elements: [{ type: 'Control', scope: '#/properties/number' }], + }; + const renderers: JsonFormsUISchemaRegistryEntry[] = []; + const form = mountInParent({ data, schema, uischema, renderers }); + expect(form.core().uischema).toBe(uischema); + + form.state.uischema = newUischema; + await flushPromises(); + + expect(form.core().uischema).toBe(newUischema); + }); + + it('adds no reactivity to the renderers and the cells', async () => { + const data = { number: 5.5 }; + const stub = { name: 'Stub', template: '
' }; + const renderers: JsonFormsRendererRegistryEntry[] = [ + { tester: () => -1, renderer: stub }, + ]; + const cells: JsonFormsCellRendererRegistryEntry[] = [ + { tester: () => -1, cell: stub }, + ]; + const newRenderers: JsonFormsRendererRegistryEntry[] = [ + { tester: () => 1, renderer: stub }, + ]; + const form = mountInParent({ data, renderers, cells }); + + // A reactive renderer entry gives the Vue warning for a reactive + // component, thus an application would need `markRaw` for its set. + expect(form.jsonforms().renderers).toBe(renderers); + expect(form.jsonforms().renderers[0].renderer).toBe(stub); + expect(isReactive(form.jsonforms().renderers)).toBe(false); + expect(form.jsonforms().cells).toBe(cells); + expect(isReactive(form.jsonforms().cells)).toBe(false); + + form.state.renderers = newRenderers; + await flushPromises(); + + expect(form.jsonforms().renderers).toBe(newRenderers); + expect(isReactive(form.jsonforms().renderers)).toBe(false); + }); + + it('adds no reactivity to ajv', () => { + const data = { number: 5.5 }; + const ajv = createAjv(); + const renderers: JsonFormsUISchemaRegistryEntry[] = []; + const core = mountInParent({ data, ajv, renderers }).core(); + + // Ajv cannot compile a schema through a proxy of itself. + expect(core.ajv).toBe(ajv); + expect(isReactive(core.ajv)).toBe(false); + }); + + it('unwraps an ajv that comes from reactive state', () => { + const data = { number: 5.5 }; + const ajv = createAjv(); + const renderers: JsonFormsUISchemaRegistryEntry[] = []; + // An application that keeps `ajv` in `reactive()` gives a proxy of it. + const core = mountInParent({ data, ajv: reactive(ajv), renderers }).core(); + + // Ajv cannot compile a schema through a proxy of itself. + expect(core.ajv).toBe(ajv); + expect(core.errors).toHaveLength(0); + }); + + it('unwraps a data, schema and ui schema that come from reactive state', () => { + const data = { number: 5.5 }; + const schema = { + type: 'object', + properties: { number: { type: 'number' } }, + }; + const uischema = { + type: 'Control', + scope: '#/properties/number', + }; + const renderers: JsonFormsUISchemaRegistryEntry[] = []; + // An application that keeps these values in `reactive()` gives proxies of + // them. + const state = reactive({ data, schema, uischema }); + const core = mountInParent({ ...state, renderers }).core(); + + expect(core.data).toBe(data); + expect(core.schema).toBe(schema); + expect(core.uischema).toBe(uischema); + }); + + it('unwraps a new data, schema and ui schema that come from reactive state', async () => { + const renderers: JsonFormsUISchemaRegistryEntry[] = []; + const form = mountInParent({ data: { number: 5.5 }, renderers }); + const data = { number: 6 }; + const schema = { + type: 'object', + properties: { number: { type: 'number' } }, + }; + const uischema = { + type: 'Control', + scope: '#/properties/number', + }; + const state = reactive({ data, schema, uischema }); + form.state.data = state.data; + form.state.schema = state.schema; + form.state.uischema = state.uischema; + await nextTick(); + + const core = form.core(); + expect(core.data).toBe(data); + expect(core.schema).toBe(schema); + expect(core.uischema).toBe(uischema); + }); + + it('adds no reactivity to a generated ajv', () => { + const data = { number: 5.5 }; + const renderers: JsonFormsUISchemaRegistryEntry[] = []; + const core = mountInParent({ data, renderers }).core(); + + expect(core.ajv).toBeDefined(); + expect(isReactive(core.ajv)).toBe(false); + }); + + it('leaves the objects of the application untouched', async () => { + const data = { number: 5.5 }; + const schema = { + type: 'object', + properties: { number: { type: 'number' } as Record }, + }; + const state = reactive({ schema }); + let deepChanges = 0; + watch( + () => state.schema, + () => { + deepChanges++; + }, + { deep: true } + ); + const renderers: JsonFormsUISchemaRegistryEntry[] = []; + mountInParent({ data, schema: state.schema, renderers }); + + // JSON Forms keeps its own state shallow. It writes no flag on the object + // of the application, thus a deep watcher of the application still runs. + expect((schema as Record).__v_skip).toBeUndefined(); + state.schema.properties.number.title = 'Number'; + await nextTick(); + expect(deepChanges).toBe(1); + }); });