fun runScheduler()

in gdscript/src/main/kotlin/tscn/toolWindow/TscnScenePreviewWindow.kt [72:131]


    fun runScheduler() {
        val component = toolWindow.component

        val timer = TimerUtil.createNamedTimer("SceneView", 100) { _ ->
            if (!component.isShowing) return@createNamedTimer

            val count = ActivityTracker.getInstance().count
            if (count == activityCount) return@createNamedTimer

            val state = ModalityState.stateForComponent(component)
            if (ModalityState.current().dominates(state)) return@createNamedTimer

            val success = runTask { checkUpdate() }
            if (success) activityCount = count // to check on the next turn
        }

        timer.start()
        Disposer.register(this) {
            timer.stop()
        }

        component.addHierarchyListener { e ->
            if (BitUtil.isSet(e.changeFlags, HierarchyEvent.DISPLAYABILITY_CHANGED.toLong())) {
                val visible = toolWindow.isVisible
                if (visible) {
                    runTask { checkUpdate() }
                    scheduleRebuild()
                } else if (!project.isDisposed) {
                    myFile = null
                    rebuildNow()
                }
            }
        }

        if (component.isShowing) {
            runTask { checkUpdate() }
            scheduleRebuild()
        }

        Disposer.register(toolWindow.contentManager, this)
        coroutineScope.launch {
            rebuildRequests.debounce {
                when (it) {
                    RebuildDelay.NOW -> 0L
                    RebuildDelay.QUEUE -> 100
                }
            }
                .collectLatest {
                    rebuildImpl()
                }
        }

        project.messageBus.connect().subscribe<BulkFileListener>(
            VirtualFileManager.VFS_CHANGES,
            object : BulkFileListener {
                override fun after(events: MutableList<out VFileEvent>) {
                    events.find { it.file == myFile }?.let { scheduleRebuild() }
                }
            })
    }