Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support the ready method to pass in data #4

Open
wants to merge 1 commit into
base: master
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
21 changes: 11 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
export type CallbackFunction = (err?: Error) => void;
export type CallbackFunction = (err?: Error, resolvedData?: any) => void;
export type ReadyFunctionArg = boolean | Error | CallbackFunction | undefined;

export class Ready {
#isReady: boolean;
#readyCallbacks: CallbackFunction[];
#readyArg?: Error = undefined;
#resolved?: any = undefined;

constructor() {
this.#isReady = false;
this.#readyCallbacks = [];
}

ready(flagOrFunction?: ReadyFunctionArg) {
ready(flagOrFunction?: ReadyFunctionArg, resolved?: unknown) {
// register a callback
if (flagOrFunction === undefined || typeof flagOrFunction === 'function') {
return this.#register(flagOrFunction);
}
// emit callbacks
this.#emit(flagOrFunction);
this.#emit(flagOrFunction, resolved);
}

/**
Expand All @@ -28,23 +29,23 @@ export class Ready {
// support `this.ready().then(onready);` and `await this.ready()`;
if (!func) {
return new Promise<void>((resolve, reject) => {
function func(err?: Error) {
function func(err?: Error, data?: any) {
if (err) {
reject(err);
} else {
resolve();
resolve(data);
}
}
if (this.#isReady) {
return func(this.#readyArg);
return func(this.#readyArg, this.#resolved);
}
this.#readyCallbacks.push(func);
});
}

// this.ready(fn)
if (this.#isReady) {
func(this.#readyArg);
func(this.#readyArg, this.#resolved);
} else {
this.#readyCallbacks.push(func);
}
Expand All @@ -55,7 +56,7 @@ export class Ready {
* If the flag is not false, it will be marked as ready. Then the callbacks will be called immediatly when register.
* @param {Boolean|Error} flag - Set a flag whether it had been ready. If the flag is an error, it's also ready, but the callback will be called with argument `error`
*/
#emit(flag: boolean | Error) {
#emit(flag: boolean | Error, resolved?: unknown) {
// this.ready(true);
// this.ready(false);
// this.ready(err);
Expand All @@ -65,7 +66,7 @@ export class Ready {
if (this.#isReady) {
this.#readyCallbacks
.splice(0, Infinity)
.forEach(callback => process.nextTick(() => callback(this.#readyArg)));
.forEach(callback => process.nextTick(() => callback(this.#readyArg, resolved)));
}
}

Expand All @@ -76,7 +77,7 @@ export class Ready {
if (!obj) return;
const ready = new Ready();
// delegate method
obj.ready = (flagOrFunction: any) => ready.ready(flagOrFunction);
obj.ready = (flagOrFunction: any, resolved?: unknown) => ready.ready(flagOrFunction, resolved);
}
}

Expand Down