Skip to content

Commit 90b6881

Browse files
author
Sean Wilson
committed
dispatch "error" events after mounting
1 parent f110e25 commit 90b6881

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/lib/SchemaForm.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<script lang="ts">
22
import 'core-js/actual/structured-clone';
33
import type { JSONSchema7Definition } from "json-schema";
4-
import { createEventDispatcher } from 'svelte';
54
import DownloadOptions, { type DataTransform } from './DowloadOptions';
65
import UISchema from "./UISchema";
76
import JsonSchemaDereferencer from "@json-schema-tools/dereferencer";
87
import Ajv, { type ValidateFunction } from "ajv";
98
import ajvFormats from "ajv-formats";
109
import mergeAllOf from "json-schema-merge-allof";
1110
import Paper, { Title, Subtitle, Content } from '@smui/paper';
11+
import createMountedEventDispatcher from './createMountedEventDispatcher';
1212
import ObjectProps from "./controls/ObjectProps.svelte";
1313
import Control from "./Control.svelte";
1414
import ValidationError from "./ValidationError";
@@ -19,7 +19,7 @@
1919
export let data: { [prop: string]: any } = {};
2020
export let uischema: UISchema = {};
2121
22-
const dispatch = createEventDispatcher();
22+
const dispatch = createMountedEventDispatcher();
2323
/* A bit of a hack - When bulding the static test site, the dereferencer is still behind a
2424
* `.default` property for some reason. I'm guessing it has something to do with how modules are
2525
* imported during the svelte build process. When running in browser, it appears to be imported
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { createEventDispatcher, onMount } from 'svelte';
2+
3+
type EventDispatcher<EventMap extends {} = any> = ReturnType<typeof createEventDispatcher<EventMap>>;
4+
type EventDispatcherhArgs<EventMap extends {} = any> = Parameters<EventDispatcher<EventMap>>;
5+
6+
/**
7+
* Created an event dispatcher with Svelte's `createEventDispatcher` that will queue events until the component is mounted.
8+
*/
9+
export default function createMountedEventDispatcher<EventMap extends {} = any>(): EventDispatcher<EventMap> {
10+
let mounted = false;
11+
const dispatchOnMount: EventDispatcherhArgs<EventMap>[] = [];
12+
const dispatch = createEventDispatcher();
13+
14+
onMount(() => {
15+
mounted = true;
16+
dispatchOnMount.forEach(args => {
17+
dispatch(...args);
18+
});
19+
});
20+
21+
return (...args: EventDispatcherhArgs<EventMap>) => {
22+
if (mounted) {
23+
return dispatch(...args);
24+
}
25+
else {
26+
dispatchOnMount.push(args);
27+
return false;
28+
}
29+
};
30+
}

0 commit comments

Comments
 (0)