Skip to content

Commit

Permalink
feat: export setLogLevel on webmscore web worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Xmader committed Jan 24, 2023
1 parent 4921a6f commit 568e753
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions web-public/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class WebMscore {
* - 0: Off
* - 1: Normal (`ERRR` or `WARN` or `INFO`)
* - 2: Debug (`DEBG`)
* @returns {Promise<void>}
*/
static async setLogLevel(level) {
_hasLogLevelSet = true
Expand Down
41 changes: 33 additions & 8 deletions web-public/src/worker-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class WorkerError extends Error {
}
}

/**
* Set the log level when the instance is created
* default: 0 (Off)
* @see WebMscore.setLogLevel
*/
let _logLevel = 0

/**
* Use webmscore as a web worker
* @implements {import('./index').default}
Expand Down Expand Up @@ -65,6 +72,18 @@ class WebMscoreW {
return -1
}

/**
* Set log level
* @param {0 | 1 | 2} level - See https://github.com/LibreScore/webmscore/blob/v1.0.0/src/framework/global/thirdparty/haw_logger/logger/log_base.h#L30-L33
* - 0: Off
* - 1: Normal (`ERRR` or `WARN` or `INFO`)
* - 2: Debug (`DEBG`)
* @returns {Promise<void>}
*/
static async setLogLevel(level) {
_logLevel = level
}

/**
* Load score data
* @param {import('../schemas').InputFileFormat} format
Expand All @@ -78,6 +97,7 @@ class WebMscoreW {
fonts,
instance.rpc('ready')
])
await instance.rpc('setLogLevel', [_logLevel]) // default 0 (Off)
await instance.rpc('load', [format, data, _fonts, doLayout], [data.buffer, ..._fonts.map(f => f.buffer)])
return instance
}
Expand All @@ -86,11 +106,11 @@ class WebMscoreW {
* Communicate with the worker thread with JSON-RPC
* @private
* @typedef {{ id: number; result?: any; error?: any; }} RPCRes
* @param {keyof import('./index').default | '_synthAudio' | 'processSynth' | 'processSynthBatch' | 'load' | 'ready'} method
* @param {keyof import('./index').default | '_synthAudio' | 'processSynth' | 'processSynthBatch' | 'load' | 'ready' | 'setLogLevel'} method
* @param {any[]} params
* @param {Transferable[]} transfer
*/
async rpc(method, params = [], transfer = undefined) {
async rpc(method, params = [], transfer = []) {
const id = Math.random()

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -120,12 +140,16 @@ class WebMscoreW {
* if no excerpts, generate excerpts from existing instrument parts
*
* @param {number} id `-1` means the full score
* @returns {Promise<void>}
*/
async setExcerptId(id) {
setExcerptId(id) {
return this.rpc('setExcerptId', [id])
}

async getExcerptId() {
/**
* @returns {Promise<number>}
*/
getExcerptId() {
return this.rpc('getExcerptId')
}

Expand Down Expand Up @@ -206,7 +230,7 @@ class WebMscoreW {
* @param {'mscz' | 'mscx'} format
* @returns {Promise<Uint8Array>}
*/
async saveMsc(format = 'mscz') {
saveMsc(format = 'mscz') {
return this.rpc('saveMsc', [format])
}

Expand All @@ -227,7 +251,7 @@ class WebMscoreW {
* @param {boolean} transparent
* @returns {Promise<Uint8Array>}
*/
async savePng(pageNumber = 0, drawPageBackground = false, transparent = true) {
savePng(pageNumber = 0, drawPageBackground = false, transparent = true) {
return this.rpc('savePng', [pageNumber, drawPageBackground, transparent])
}

Expand All @@ -252,9 +276,10 @@ class WebMscoreW {
/**
* Set the soundfont (sf2/sf3) data
* @param {Uint8Array} data
* @returns {Promise<void>}
*/
async setSoundFont(data) {
await this.rpc('setSoundFont', [data], [data.buffer])
setSoundFont(data) {
return this.rpc('setSoundFont', [data], [data.buffer])
}

/**
Expand Down
7 changes: 6 additions & 1 deletion web-public/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import WebMscore from './index.js'
let score

/**
* @typedef {{ id: number; method: Exclude<keyof import('./index').default, 'scoreptr' | 'excerptId'> | 'load' | 'ready'; params: any[]; }} RPCReq
* @typedef {{ id: number; method: Exclude<keyof import('./index').default, 'scoreptr' | 'excerptId'> | 'load' | 'ready' | 'setLogLevel'; params: any[]; }} RPCReq
* @typedef {{ id: number; result?: any; error?: any; }} RPCRes
* @param {number} id
* @param {any} result
Expand Down Expand Up @@ -73,6 +73,11 @@ self.onmessage = async (e) => {
rpcRes(id, 'done')
break;

case 'setLogLevel':
await WebMscore.setLogLevel.apply(undefined, params)
rpcRes(id, 'done')
break

default:
if (!score) { rpcErr(id, new Error('Score not loaded')) }
const result = await score[method].apply(score, params)
Expand Down

0 comments on commit 568e753

Please sign in to comment.