package ru.dbotthepony.kstarbound.client.gl.properties

import ru.dbotthepony.kstarbound.client.StarboundClient
import ru.dbotthepony.kstarbound.client.gl.checkForGLError
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class GLGenericTracker<T>(private var value: T, private val callback: (T) -> Unit) : ReadWriteProperty<StarboundClient, T> {
	override fun getValue(thisRef: StarboundClient, property: KProperty<*>): T {
		return value
	}

	override fun setValue(thisRef: StarboundClient, property: KProperty<*>, value: T) {
		thisRef.ensureSameThread()

		if (value == this.value)
			return

		callback.invoke(value)
		checkForGLError()
		this.value = value
	}
}