feat(engine): Add runOnInit method to getContext.

This commit is contained in:
Gervwyk 2022-04-20 08:29:56 +02:00
parent 8c67b986c3
commit 2339a3a77d
2 changed files with 28 additions and 23 deletions

View File

@ -93,6 +93,26 @@ function getContext({ config, lowdefy, development = false }) {
_internal.update = () => {
_internal.RootBlocks.update();
};
_internal.runOnInit = async (progress) => {
progress();
if (!_internal.State.initialized && !_internal.onInitDone) {
await _internal.RootBlocks.areas.root.blocks[0].triggerEvent({
name: 'onInit',
progress,
});
_internal.State.freezeState();
_internal.onInitDone = true;
}
};
_internal.runOnInitAsync = async (progress) => {
if (!_internal.State.initialized && !_internal.onInitAsyncDone) {
await _internal.RootBlocks.areas.root.blocks[0].triggerEvent({
name: 'onInitAsync',
progress,
});
_internal.onInitAsyncDone = true;
}
};
lowdefy.contexts[id] = ctx;
return ctx;
}

View File

@ -24,34 +24,19 @@ const Context = ({ children, config, lowdefy, progress }) => {
return (
<MountEvents
context={context}
ename="onInit"
triggerEvent={async () => {
progress.dispatch({
type: 'increment',
});
if (!context._internal.State.initialized) {
await context._internal.RootBlocks.areas.root.blocks[0].triggerEvent({
name: 'onInit',
progress: () => {
progress.dispatch({
type: 'increment',
});
},
await context._internal.runOnInit(() => {
progress.dispatch({
type: 'increment',
});
context._internal.State.freezeState();
}
});
}}
triggerEventAsync={() => {
if (!context._internal.State.initialized) {
context._internal.RootBlocks.areas.root.blocks[0].triggerEvent({
name: 'onInitAsync',
progress: () => {
progress.dispatch({
type: 'increment',
});
},
context._internal.runOnInitAsync(() => {
progress.dispatch({
type: 'increment',
});
}
});
}}
>
{(loadingOnInit) => {