Skip to content

Commit

Permalink
return null when labels.len() <= 2
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-ayf committed May 25, 2024
1 parent d33a735 commit ec1af49
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SyrinxStream extends Readable {
* @param {(error?: Error | null) => void} callback
*/
async _construct(callback) {
/** @type {[import("./native").PreparedSynthesizer, null] | [null, Error]} */
/** @type {[import("./native").PreparedSynthesizer | null, null] | [null, Error]} */
const [synthesizer, error] = await this._syrinx
.prepare(this._inputText, this._option)
.then(
Expand Down
2 changes: 1 addition & 1 deletion lib/native.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export interface SyrinxConfig {
export class Syrinx {
static fromConfig(config: SyrinxConfig): Syrinx
static fromConfigAsync(config: SyrinxConfig): Promise<Syrinx>
prepare(inputText: string, option: SynthesisOption): Promise<PreparedSynthesizer>
prepare(inputText: string, option: SynthesisOption): Promise<PreparedSynthesizer | null>
}
export class PreparedSynthesizer {
synthesize(push: (...args: [err: null, frame: Buffer] | [err: Error, frame: null]) => void): Promise<void>
Expand Down
14 changes: 9 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Syrinx {
AsyncTask::new(FromConfigTask { config })
}

#[napi(ts_return_type = "Promise<PreparedSynthesizer>")]
#[napi(ts_return_type = "Promise<PreparedSynthesizer | null>")]
pub fn prepare(&self, input_text: String, option: SynthesisOption) -> AsyncTask<PrepareTask> {
let worker = self.0.clone();
AsyncTask::new(PrepareTask {
Expand Down Expand Up @@ -121,8 +121,8 @@ pub struct PrepareTask {
}

impl Task for PrepareTask {
type Output = PreparedSynthesizer;
type JsValue = PreparedSynthesizer;
type Output = Option<PreparedSynthesizer>;
type JsValue = Option<PreparedSynthesizer>;

fn compute(&mut self) -> napi::Result<Self::Output> {
self
Expand All @@ -136,6 +136,10 @@ impl Task for PrepareTask {
.extract_fullcontext(&self.input_text)
.map_err(|err| Error::new(Status::InvalidArg, err))?;

if labels.len() <= 2 {
return Ok(None);
}

let generator = self
.worker
.jbonsai
Expand All @@ -144,10 +148,10 @@ impl Task for PrepareTask {
let encoder: Box<dyn Encoder> =
Encoder::new(&self.worker.jbonsai.condition, &self.worker.encoder_config)?;

Ok(PreparedSynthesizer {
Ok(Some(PreparedSynthesizer {
generator: Some(Box::new(generator)),
encoder: Some(encoder),
})
}))
}

fn resolve(&mut self, _: Env, output: Self::Output) -> napi::Result<Self::JsValue> {
Expand Down

0 comments on commit ec1af49

Please sign in to comment.