-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): consolidate options validation and cleanup into core
- Loading branch information
Showing
9 changed files
with
173 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** @type {Set<() => void} */ | ||
const cleanupTasks = new Set() | ||
|
||
/** Register later cleanup task */ | ||
const addCleanupTask = (onCleanup) => { | ||
cleanupTasks.add(onCleanup) | ||
return onCleanup | ||
} | ||
|
||
/** Remove a cleanup task without running it. */ | ||
const removeCleanupTask = (onCleanup) => { | ||
cleanupTasks.delete(onCleanup) | ||
} | ||
|
||
/** Clean up all components and elements added to the document. */ | ||
const cleanup = () => { | ||
for (const handleCleanup of cleanupTasks.values()) { | ||
handleCleanup() | ||
} | ||
|
||
cleanupTasks.clear() | ||
} | ||
|
||
export { addCleanupTask, cleanup, removeCleanupTask } |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { tick } from 'svelte' | ||
|
||
import * as MountLegacy from './mount-legacy.js' | ||
import * as MountModern from './mount-modern.svelte.js' | ||
|
||
const mountComponent = MountModern.IS_MODERN_SVELTE | ||
? MountModern.mount | ||
: MountLegacy.mount | ||
|
||
/** | ||
* Render a Svelte component into the document. | ||
* | ||
* @template {import('./types.js').Component} C | ||
* @param {import('./types.js').ComponentType<C>} Component | ||
* @param {import('./types.js').MountOptions<C>} options | ||
* @returns {{ | ||
* component: C | ||
* unmount: () => void | ||
* rerender: (props: Partial<import('./types.js').Props<C>>) => Promise<void> | ||
* }} | ||
*/ | ||
const mount = (Component, options = {}) => { | ||
const { component, unmount, rerender } = mountComponent(Component, options) | ||
|
||
return { | ||
component, | ||
unmount, | ||
rerender: async (props) => { | ||
rerender(props) | ||
// Await the next tick for Svelte 4, which cannot flush changes synchronously | ||
await tick() | ||
}, | ||
} | ||
} | ||
|
||
export { mount } |
This file contains 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 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 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
Oops, something went wrong.