<!DOCTYPE html><!-- webkit-test-runner [ IntersectionObserverEnabled=true ] -->
<html>

<head>
    <script src="../resources/testharness.js"></script>
    <script src="../resources/testharnessreport.js"></script>
    <script src="../resources/gc.js"></script>
</head>

<body>
    <div id="container"></div>
    <script>
        async function callbackSurvivesAfterGC() {
            let test = async_test("test: IntersectionObserverCallbacks stay alive if according observers is alive");
            const iterationCount = 10;
            let deletedNodesCount = 0;
            let callbackCountAfterDelete = 0;
            let observers = [];
            await new Promise((resolve, reject) => {
                for (let j = 0; j < iterationCount; ++j) {
                    const div = document.createElement('div');
                    div.state = "toDelete";
                    container.appendChild(div);
                    let observer = new IntersectionObserver(entries => {
                        for (const entry of entries) {
                            if (entry.target.state == "toDelete") {
                                deletedNodesCount++;
                                container.removeChild(entry.target);
                                entry.target.state = "deleted";
                                if (deletedNodesCount == iterationCount)
                                    resolve();
                            } else if (entry.target.state == "new") {
                                entry.target.state = "checked";
                                callbackCountAfterDelete++;
                            }
                        }
                        if (callbackCountAfterDelete == iterationCount)
                            test.done();
                    });
                    observer.observe(div);
                    observers[j] = observer;
                }
            });
            gc();
            for (let j = 0; j < iterationCount; ++j) {
                const div = document.createElement('div');
                div.state = "new";
                container.appendChild(div);
                observers[j].observe(div);
            }
        }
        window.onload = callbackSurvivesAfterGC();
    </script>

</body>

</html>