getndazn / kopytko-framework

A modern Roku's Brightscript framework based on JS world's solutions
MIT License
19 stars 6 forks source link

fix: not calling state update callbacks if kopytko is not mounted #27

Closed RadoslawZambrowski closed 2 years ago

RadoslawZambrowski commented 2 years ago

What did you implement:

Fixes the problem when setState is called before initKopytko. Currently, update state callbacks are executed even if a component is not mounted and that may result in an app crash if the render function tries to access uninitialized fields.

How did you implement it:

KopytkoUpdater.enqueueStateUpdate additionally checks if a component is mounted before scheduling state update callbacks.

How can we verify it:

Usage:

kptk = CreateObject("roSGNode", "Kptk")
kptk.callFunc("setState")

Kptk.component.xml

<?xml version="1.0" encoding="utf-8" ?>
<component name="Kptk" extends="KopytkoGroup">
    <interface>
        <function name="setState" />
    </interface>

    <script type="text/brightscript" uri="Kptk.component.brs" />
    <script type="text/brightscript" uri="Kptk.template.brs" />
</component>

Kptk.component.brs

sub constructor()
    m.theme = m.global.theme
end sub

Kptk.template.brs

function render() as Object
    return [
        {
            name: "Label"
            props: {
                id: "label"
                color: m.theme.textColor
            }
        }
    ]
end function

In this example, calling setState will result in an app crash because render refers m.theme which is invalid before initKopytko is called.

Todos:

Is this ready for review?: YES Is it a breaking change?: NO

github-actions[bot] commented 2 years ago

:tada: This PR is included in version 1.0.6 :tada:

The release is available on:

Your semantic-release bot :package::rocket: