Skip to content

Commit 7a670e3

Browse files
committed
feat(sdk): add PlayEvent types and songloft.events API (songloft-org/songloft#164)
Add SongloftEvents interface with onPlayEvent/offPlayEvent for dynamic play event subscription. Add PlayEvent and PlayEventSong types.
1 parent 83a833e commit 7a670e3

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

packages/plugin-sdk/src/global.d.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,43 @@ export interface SongloftCommand {
304304
exists(filename: string): Promise<boolean>;
305305
}
306306

307+
// ===== 播放事件(songloft.events) =====
308+
309+
/** 播放事件中的歌曲信息 */
310+
export interface PlayEventSong {
311+
id: number;
312+
title: string;
313+
artist: string;
314+
}
315+
316+
/** 播放事件数据 */
317+
export interface PlayEvent {
318+
type: 'play' | 'finish' | 'skip';
319+
song: PlayEventSong;
320+
/** 调用来源标识,如 "songloft-player"(官方客户端)、"miot"(小爱音箱插件)等 */
321+
source: string;
322+
/** Unix 毫秒时间戳 */
323+
timestamp: number;
324+
}
325+
326+
/**
327+
* 事件订阅 API。
328+
*
329+
* 插件通过 `songloft.events.onPlayEvent(fn)` 动态订阅播放事件,
330+
* 通过 `songloft.events.offPlayEvent()` 取消订阅。
331+
* 可在任意时刻调用(onInit、onHTTPRequest、定时器回调等),
332+
* 支持设置页面的开关场景。
333+
*
334+
* 未订阅的插件不会收到广播。插件休眠后订阅自动清除,
335+
* 懒加载恢复时需在 onInit 中重新注册。
336+
*/
337+
export interface SongloftEvents {
338+
/** 订阅播放事件 */
339+
onPlayEvent(handler: (event: PlayEvent) => void | Promise<void>): void;
340+
/** 取消订阅播放事件 */
341+
offPlayEvent(): void;
342+
}
343+
307344
export interface Songloft {
308345
log: SongloftLog;
309346
storage: SongloftStorage;
@@ -314,6 +351,7 @@ export interface Songloft {
314351
jsenv: SongloftJSEnv;
315352
command: SongloftCommand;
316353
fs: SongloftFS;
354+
events: SongloftEvents;
317355
}
318356

319357
// ===== 全局声明 =====
@@ -332,6 +370,11 @@ declare global {
332370
* 再把响应序列化为 HTTP 应答。
333371
*/
334372
function onHTTPRequest(req: HTTPRequest): HTTPResponse | Promise<HTTPResponse>;
373+
/**
374+
* 播放事件回调(通过 songloft.events.onPlayEvent 注册后生效)。
375+
* 客户端播放完一首歌后由后端广播调用。
376+
*/
377+
function onPlayEvent(event: PlayEvent): void | Promise<void>;
335378

336379
// 由 polyfill 注入的标准全局 API(与浏览器/Node 对齐)。
337380
// fetch 是真异步(Go 侧 goroutine 跑 HTTP,JS 侧通过原生 Promise 等待)。

packages/plugin-sdk/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,12 @@ export type {
313313
HTTPResponse,
314314
PluginManifest,
315315
Songloft,
316+
SongloftEvents,
316317
SongloftJSEnv,
317318
SongloftJSEnvCall,
318319
SongloftJSEnvEvent,
319320
SongloftJSEnvResult,
320321
SongloftJSEnvParallelResult,
322+
PlayEvent,
323+
PlayEventSong,
321324
} from './global';

0 commit comments

Comments
 (0)