Skip to content
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

fix(Form): memory leak #1185

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/runtime/components/forms/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</template>

<script lang="ts">
import { provide, ref, type PropType, defineComponent } from 'vue'
import { provide, ref, type PropType, defineComponent, onUnmounted, onMounted } from 'vue'
import { useEventBus } from '@vueuse/core'
import type { ZodSchema } from 'zod'
import type { ValidationError as JoiError, Schema as JoiSchema } from 'joi'
Expand Down Expand Up @@ -51,10 +51,16 @@ export default defineComponent({
setup (props, { expose, emit }) {
const bus = useEventBus<FormEvent>(`form-${uid()}`)

bus.on(async (event) => {
if (event.type !== 'submit' && props.validateOn?.includes(event.type)) {
await validate(event.path, { silent: true })
}
onMounted(() => {
bus.on(async (event) => {
if (event.type !== 'submit' && props.validateOn?.includes(event.type)) {
await validate(event.path, { silent: true })
}
})
})

onUnmounted(() => {
bus.reset()
})

const errors = ref<FormError[]>([])
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/utils/uid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let _id = 0

export function uid () {
return `nuid-${_id++}`
_id = (_id + 1) % Number.MAX_SAFE_INTEGER
return `nuid-${_id}`
}