StarLight는 앱 안에서 일어나는 일을 코루틴 SharedFlow로 흘려보내요. 플러그인은 EventHandler로 원하는 이벤트를 구독할 수 있어요.

참고

이 페이지의 이벤트는 앱 이벤트예요. 스크립트 함수를 부르는 프로젝트 이벤트와는 다른 개념이에요.

구독

Kotlin
inline fun <reified T : Event> EventHandler.on(
    scope: CoroutineScope = EventHandler,
    noinline callback: suspend T.() -> Unit,
): Job
Kotlin
private var scope: CoroutineScope? = null

override fun onEnable() {
    val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default).also { scope = it }

    EventHandler.on<Events.Message.Create>(scope) {
        if (message.message == "/ping")
            message.room.send("pong")
    }
}

override fun onDisable() {
    scope?.cancel() // 구독도 함께 취소돼요.
}
  • 콜백은 이벤트 객체를 수신 객체(this)로 받아요. 위 예시의 message는 Events.Message.Create.message예요.
  • 콜백은 이벤트마다 새 코루틴에서 실행되고, 콜백에서 던진 예외는 로그로만 남아요.
  • 플러그인 전용 스코프를 넘기세요. 기본값인 EventHandler 스코프로 구독하면 플러그인을 꺼도 구독이 남아요.
  • 위젯 안에서는 WidgetScope.on<T> { }를 쓰면 화면이 보이는 동안만 구독해요.

발생시키기

Kotlin
suspend fun EventHandler.fireEvent(event: Event)
fun EventHandler.fireEventWithScope(event: Event, scope: CoroutineScope = EventHandler): Job

Event를 구현한 클래스를 직접 만들어 플러그인끼리 주고받을 수도 있어요. Event는 CoroutineScope를 상속하므로, 기본 이벤트들처럼 CoroutineScope by eventHandlerScope()로 위임하면 돼요.

Kotlin
class AlarmRang(
    val label: String,
    val coroutineScope: CoroutineScope = eventHandlerScope(),
) : Event, CoroutineScope by coroutineScope

EventHandler.fireEventWithScope(AlarmRang("기상"))

경고

Events.Locale.Update처럼 앱이 관리하는 이벤트를 플러그인이 발생시키면 앱 상태가 꼬일 수 있어요. 기본 이벤트는 구독만 하세요.

EventHandler.eventFlow로 모든 이벤트가 흐르는 SharedFlow<Event>에 직접 접근할 수도 있어요.

이벤트 목록

모든 이벤트는 dev.mooner.starlight.plugincore.event.Events 안에 있어요.

메시지

이벤트속성발생 시점
Events.Message.Createmessage: Message메신저 메시지를 받았을 때

프로젝트

이벤트속성발생 시점
Events.Project.Createproject: Project프로젝트가 만들어졌을 때
Events.Project.Compileproject: Project프로젝트가 컴파일됐을 때
Events.Project.InfoUpdateproject: Project프로젝트 정보(허용 이벤트 등)가 바뀌었을 때
Events.Project.DeleteprojectName: String, projectId: ProjectID프로젝트가 삭제됐을 때

알림

이벤트속성발생 시점
Events.Notification.PoststatusBarNotification, context기기에 알림이 올라왔을 때
Events.Notification.Dismisssbn, rankingMap, reason알림이 사라졌을 때
Events.Notification.ClicknotificationId: Int알림을 눌렀을 때

그 밖의 이벤트

이벤트속성발생 시점
Events.Log.Createlog: LogData로그가 남았을 때
Events.Config.GlobalConfigUpdate—앱 전역 설정이 바뀌었을 때
Events.Scheme.SchemeOpenEventparams: Map<String, String>starlight://action/… 링크가 열렸을 때
Events.Locale.Updatelocale: Locale앱 언어가 바뀌었을 때

모든 이벤트에는 coroutineScope 속성도 있어요.

메시지 객체

Events.Message.Create.message의 형식이에요.

Kotlin
data class Message(
    val message     : String,      // 본문
    val image       : Bitmap?,     // 이미지 메시지일 때
    val sender      : ChatSender,
    val room        : ChatRoom,
    val packageName : String,      // 메신저 앱 패키지 이름
    val hasMention  : Boolean,
    val chatLogId   : Long,
)
ChatRoom설명
id, name방 ID와 이름이에요.
isGroupChat단체방인지 알려 줘요.
isDebugRoom앱의 디버그룸에서 온 메시지인지 알려 줘요.
send(message): Boolean그 방에 답장을 보내요.
markAsRead(): Boolean그 방의 메시지를 읽음으로 표시해요.
ChatSender설명
name보낸 사람 이름이에요.
id보낸 사람 ID예요. Android 9(P) 이상에서는 null이에요.
profileBitmap, profileBase64, profileHash프로필 이미지와 그 Base64 문자열, 해시예요.