Skip to content
This repository was archived by the owner on Dec 4, 2022. It is now read-only.

Add delay function for Lua scripts #24

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ dependencies {

// ### Core Libraries
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.1")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.1") // For MPP time control

// java only
implementation("org.jsoup:jsoup:1.13.1")
12 changes: 12 additions & 0 deletions src/main/kotlin/app/shosetsu/lib/lua/ShosetsuLuaLib.kt
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package app.shosetsu.lib.lua
import app.shosetsu.lib.*
import app.shosetsu.lib.exceptions.HTTPException
import app.shosetsu.lib.exceptions.MissingExtensionLibrary
import kotlinx.datetime.Clock
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
@@ -242,6 +243,17 @@ class ShosetsuLuaLib : TwoArgFunction() {
fun Log(name: String, arguments: String) {
ShosetsuSharedLib.logger(name, arguments)
}

/**
* Delay by [milliseconds]
*/
fun delay(milliseconds: Long) {
val startTime = Clock.System.now().toEpochMilliseconds()
var currentTime: Long
do {
currentTime = Clock.System.now().toEpochMilliseconds()
} while (startTime + milliseconds > currentTime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timers using infinite loops are inefficient and waste CPU time. We should figure out a better way to make a delay function. Furthermore, I don't know if such a function is even needed right now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage of such a function is to allow scripts to not create network calls too quickly. As this often will lead to website blocking users.

The loop overhead is only 23~ ms on my desktop, and shouldn't be too much more on mobile devices.

Note: Not an infinite loop, as there is a clear end clause.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TechnoJo4 Swapping to use kotlin-corutines for the delay call leads to a 62ms overhead. Currently this is far more performant then the other multi platform method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A 60ms difference is insignificant, especially when waiting for e.g. a rate limit. A web request might take much longer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, Is this acceptable to merge or?

}
}

@Suppress("ClassName")
21 changes: 21 additions & 0 deletions src/test/kotlin/app/shosetsu/lib/LuaLibTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package app.shosetsu.lib

import app.shosetsu.lib.lua.shosetsuGlobals
import org.junit.Test
import kotlin.system.measureTimeMillis

/*
* shosetsu-kotlin-lib
* 02 / 11 / 2021
*/
class LuaLibTest {
@Test
fun delayTest() {
val time = 500L
val script = shosetsuGlobals().load("delay($time)", "delayTest")!!

val resultTime = measureTimeMillis { script.call() }

assert(resultTime > time) { "func 'delay' time < ${time}ms" }
}
}