status-desktop/storybook/src/Storybook/HotComponentFromSource.qml
Michał Cieślak dfc5db27d5 feat(Storybook): in-page, state-preserving hot reloading
In comparison to generic page-wise hot reloading, this technique
requires using dedicated component within a storybook page but
provides greater flexibility e.g. preserving component state across
source reloads.

Closes: #7975
2022-10-20 20:34:35 +02:00

54 lines
1.2 KiB
QML

import QtQuick 2.14
import Storybook 1.0
QtObject {
id: root
property string source
property bool rethrowErrors: true
readonly property alias component: d.component
readonly property alias errors: d.errors // QQmlError
onSourceChanged: d.createComponent()
readonly property Connections _d: Connections {
id: d
target: SourceWatcher
property Component component
property var errors: null
function createComponent() {
if (component) {
component.destroy()
component = null
}
try {
component = Qt.createQmlObject(root.source,
this,
"HotComponentFromSource_dynamicSnippet"
)
d.errors = null
} catch (e) {
d.errors = e
if (root.rethrowErrors)
throw e
}
}
function onChanged() {
CacheCleaner.clearComponentCache()
createComponent()
}
}
Component.onCompleted: {
if (root.source)
d.createComponent()
}
}