@litert/core
Advanced tools
Comparing version 1.0.1 to 1.0.2
# Changes Logs | ||
## v1.0.2 | ||
- Added `module` field for error objects and constructors, hubs. | ||
- Improved the `Async.nextTick` and `Async.sleep` method. | ||
- Improved the documents. | ||
## v1.0.1 | ||
@@ -4,0 +10,0 @@ |
@@ -40,2 +40,6 @@ /** | ||
metadata: M; | ||
/** | ||
* The name of module thats emit this error. | ||
*/ | ||
module: string; | ||
} | ||
@@ -79,2 +83,6 @@ /** | ||
/** | ||
* The name of module thats emit this error. | ||
*/ | ||
readonly module: string; | ||
/** | ||
* Get the calling-stack as a string array. | ||
@@ -116,2 +124,6 @@ */ | ||
readonly code: number; | ||
/** | ||
* The name of module thats emit this error. | ||
*/ | ||
readonly module: string; | ||
} | ||
@@ -125,2 +137,6 @@ /** | ||
/** | ||
* The name of module thats emit this error. | ||
*/ | ||
readonly module: string; | ||
/** | ||
* Define a new error type. | ||
@@ -156,5 +172,9 @@ * | ||
/** | ||
* The default name for module of errors, if omitted. | ||
*/ | ||
export declare const DEFAULT_ERROR_HUB_MODULE = "unknown"; | ||
/** | ||
* Create a new error hub that has a standalone namespace of error types. | ||
*/ | ||
export declare function createErrorHub<M extends {}>(): IErrorHub<M>; | ||
export declare function createErrorHub<M extends {}>(moduleName?: string): IErrorHub<M>; | ||
/** | ||
@@ -161,0 +181,0 @@ * Get the default hub of errors. |
@@ -41,2 +41,7 @@ "use strict"; | ||
"value": metadata || {} | ||
}, | ||
"module": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": moduleName | ||
} | ||
@@ -55,3 +60,3 @@ }); | ||
} | ||
let ret = Function("code", "name", "message", "metadata", THE_CONSTRUCTOR); | ||
let ret = Function("code", "name", "message", "metadata", "moduleName", THE_CONSTRUCTOR); | ||
ret.prototype.getStackAsArray = function () { | ||
@@ -63,11 +68,13 @@ return this.stack.split(/\n\s+at /).slice(1); | ||
"code": this.code, | ||
"name": this.name, | ||
"message": this.message, | ||
"metadata": this.metadata, | ||
"module": this.module, | ||
"name": this.name, | ||
"stack": this.getStackAsArray() | ||
} : { | ||
"code": this.code, | ||
"name": this.name, | ||
"message": this.message, | ||
"metadata": this.metadata | ||
"metadata": this.metadata, | ||
"module": this.module, | ||
"name": this.name | ||
}; | ||
@@ -84,7 +91,8 @@ }; | ||
class ErrorHub { | ||
constructor() { | ||
constructor(moduleName) { | ||
this._counter = 0; | ||
this._errors = {}; | ||
this._module = moduleName; | ||
this._baseError = (Function("BaseError", `class __ extends BaseError { | ||
constructor(code, name, message, metadata) { | ||
constructor(code, name, message, metadata, moduleName) { | ||
super( | ||
@@ -94,8 +102,12 @@ code, | ||
message, | ||
metadata | ||
metadata, | ||
moduleName | ||
); | ||
}; | ||
} | ||
return __;`))(BaseError); | ||
return __;`))(BaseError, this._module); | ||
} | ||
get module() { | ||
return this._module; | ||
} | ||
is(e, id) { | ||
@@ -146,3 +158,4 @@ if (typeof id !== "undefined") { | ||
opts.message || ${JSON.stringify(message)}, | ||
opts.metadata | ||
opts.metadata, | ||
${JSON.stringify(this._module)} | ||
); | ||
@@ -167,3 +180,8 @@ }; | ||
"value": ${JSON.stringify(message)} | ||
} | ||
}, | ||
"module": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": ${JSON.stringify(this._module)} | ||
}, | ||
}); | ||
@@ -178,9 +196,13 @@ | ||
/** | ||
* The default name for module of errors, if omitted. | ||
*/ | ||
exports.DEFAULT_ERROR_HUB_MODULE = "unknown"; | ||
/** | ||
* Create a new error hub that has a standalone namespace of error types. | ||
*/ | ||
function createErrorHub() { | ||
return new ErrorHub(); | ||
function createErrorHub(moduleName = exports.DEFAULT_ERROR_HUB_MODULE) { | ||
return new ErrorHub(moduleName); | ||
} | ||
exports.createErrorHub = createErrorHub; | ||
DEFAULT_HUB = createErrorHub(); | ||
DEFAULT_HUB = createErrorHub("@litert/core"); | ||
DEFAULT_HUB.define(null, "INVALID_ERROR_NAME", `Invalid name for error definition.`); | ||
@@ -187,0 +209,0 @@ DEFAULT_HUB.define(null, "INVALID_ERROR_CODE", `Invalid code for error definition.`); |
@@ -31,7 +31,7 @@ /** | ||
*/ | ||
export declare function sleep(ms: number, ...args: any[]): Promise<any[]>; | ||
export declare function sleep<T extends any[]>(ms: number, ...args: T): Promise<T>; | ||
/** | ||
* Call the function after 0ms. | ||
* Call the function after 0ms, which means in next tick. | ||
*/ | ||
export declare const nextTick: (fn: Function, ...args: any[]) => void; | ||
export declare const nextTick: <T extends any[]>(fn: (...args: T) => void, ...args: T) => void; | ||
/** | ||
@@ -38,0 +38,0 @@ * Wait for multi-tasks, and get all results, whatever succeed or failed. |
@@ -31,3 +31,3 @@ "use strict"; | ||
/** | ||
* Call the function after 0ms. | ||
* Call the function after 0ms, which means in next tick. | ||
*/ | ||
@@ -34,0 +34,0 @@ exports.nextTick = typeof setImmediate === "function" ? |
@@ -28,3 +28,4 @@ "use strict"; | ||
function isDomain(domain) { | ||
return domain.length < DOMAIN_MAX_LENGTH && | ||
return domain.length > 0 && | ||
domain.length < DOMAIN_MAX_LENGTH && | ||
DOMAIN_REGEXP.test(domain); | ||
@@ -31,0 +32,0 @@ } |
{ | ||
"name": "@litert/core", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "The core of LiteRT.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -46,2 +46,6 @@ "use strict"; | ||
} | ||
console.log(`Hub[default] Module: ${errors.module}`); | ||
console.log(`Hub[myErrors] Module: ${myErrors.module}`); | ||
console.log(`Error[MY_TEST_ERROR2] Module: ${MY_TEST_ERROR2.module}`); | ||
console.log(`Error[INVALID_ERROR_NAME] Module: ${errors.get("INVALID_ERROR_NAME").module}`); | ||
//# sourceMappingURL=01-errors.js.map |
@@ -22,5 +22,8 @@ "use strict"; | ||
console.log(new Date().toISOString()); | ||
await Core.Async.sleep(1000); | ||
const result = await Core.Async.sleep(1000, "cc", 123, true); | ||
console.log(new Date().toISOString()); | ||
for (let i = 0; i < result.length; i++) { | ||
console.info(`result[${i}] = ${JSON.stringify(result[i])}`); | ||
} | ||
})(); | ||
//# sourceMappingURL=02-async.sleep.js.map |
@@ -46,2 +46,7 @@ /** | ||
metadata: M; | ||
/** | ||
* The name of module thats emit this error. | ||
*/ | ||
module: string; | ||
} | ||
@@ -94,2 +99,7 @@ | ||
/** | ||
* The name of module thats emit this error. | ||
*/ | ||
readonly module: string; | ||
/** | ||
* Get the calling-stack as a string array. | ||
@@ -118,3 +128,4 @@ */ | ||
message: string, | ||
metadata?: DefaultMetadataType | ||
metadata?: DefaultMetadataType, | ||
moduleName?: string | ||
): IError<DefaultMetadataType>; | ||
@@ -156,2 +167,8 @@ | ||
readonly code: number; | ||
/** | ||
* The name of module thats emit this error. | ||
*/ | ||
readonly module: string; | ||
} | ||
@@ -183,2 +200,7 @@ | ||
"value": metadata || {} | ||
}, | ||
"module": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": moduleName | ||
} | ||
@@ -202,3 +224,3 @@ }); | ||
let ret = Function( | ||
"code", "name", "message", "metadata", THE_CONSTRUCTOR | ||
"code", "name", "message", "metadata", "moduleName", THE_CONSTRUCTOR | ||
); | ||
@@ -218,11 +240,13 @@ | ||
"code": this.code, | ||
"name": this.name, | ||
"message": this.message, | ||
"metadata": this.metadata, | ||
"module": this.module, | ||
"name": this.name, | ||
"stack": this.getStackAsArray() | ||
} : { | ||
"code": this.code, | ||
"name": this.name, | ||
"message": this.message, | ||
"metadata": this.metadata | ||
"metadata": this.metadata, | ||
"module": this.module, | ||
"name": this.name | ||
}; | ||
@@ -249,2 +273,7 @@ }; | ||
/** | ||
* The name of module thats emit this error. | ||
*/ | ||
readonly module: string; | ||
/** | ||
* Define a new error type. | ||
@@ -300,4 +329,6 @@ * | ||
public constructor() { | ||
private _module: string; | ||
public constructor(moduleName: string) { | ||
this._counter = 0; | ||
@@ -307,5 +338,7 @@ | ||
this._module = moduleName; | ||
this._baseError = (Function( | ||
"BaseError", `class __ extends BaseError { | ||
constructor(code, name, message, metadata) { | ||
constructor(code, name, message, metadata, moduleName) { | ||
super( | ||
@@ -315,3 +348,4 @@ code, | ||
message, | ||
metadata | ||
metadata, | ||
moduleName | ||
); | ||
@@ -321,5 +355,10 @@ }; | ||
return __;` | ||
))(BaseError) as any; | ||
))(BaseError, this._module) as any; | ||
} | ||
public get module(): string { | ||
return this._module; | ||
} | ||
public is(e: any, id?: string | number): e is IError<M> { | ||
@@ -394,3 +433,4 @@ | ||
opts.message || ${JSON.stringify(message)}, | ||
opts.metadata | ||
opts.metadata, | ||
${JSON.stringify(this._module)} | ||
); | ||
@@ -415,3 +455,8 @@ }; | ||
"value": ${JSON.stringify(message)} | ||
} | ||
}, | ||
"module": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": ${JSON.stringify(this._module)} | ||
}, | ||
}); | ||
@@ -432,10 +477,17 @@ | ||
/** | ||
* The default name for module of errors, if omitted. | ||
*/ | ||
export const DEFAULT_ERROR_HUB_MODULE = "unknown"; | ||
/** | ||
* Create a new error hub that has a standalone namespace of error types. | ||
*/ | ||
export function createErrorHub<M extends {}>(): IErrorHub<M> { | ||
export function createErrorHub<M extends {}>( | ||
moduleName: string = DEFAULT_ERROR_HUB_MODULE | ||
): IErrorHub<M> { | ||
return new ErrorHub<M>(); | ||
return new ErrorHub<M>(moduleName); | ||
} | ||
DEFAULT_HUB = createErrorHub<DefaultMetadataType>(); | ||
DEFAULT_HUB = createErrorHub<DefaultMetadataType>("@litert/core"); | ||
@@ -442,0 +494,0 @@ DEFAULT_HUB.define( |
@@ -38,8 +38,8 @@ /** | ||
*/ | ||
export function sleep( | ||
export function sleep<T extends any[]>( | ||
ms: number, | ||
...args: any[] | ||
): Promise<any[]> { | ||
...args: T | ||
): Promise<T> { | ||
return new Promise<any[]>((resolve) => setTimeout(resolve, ms, args)); | ||
return new Promise<T>((resolve) => setTimeout(resolve, ms, args)); | ||
} | ||
@@ -50,7 +50,7 @@ | ||
/** | ||
* Call the function after 0ms. | ||
* Call the function after 0ms, which means in next tick. | ||
*/ | ||
export const nextTick: ( | ||
fn: Function, | ||
...args: any[] | ||
export const nextTick: <T extends any[]>( | ||
fn: (...args: T) => void, | ||
...args: T | ||
) => void = typeof setImmediate === "function" ? | ||
@@ -57,0 +57,0 @@ setImmediate : function(fn: Function, ...args: any[]): void { |
@@ -32,3 +32,4 @@ /** | ||
return domain.length < DOMAIN_MAX_LENGTH && | ||
return domain.length > 0 && | ||
domain.length < DOMAIN_MAX_LENGTH && | ||
DOMAIN_REGEXP.test(domain); | ||
@@ -35,0 +36,0 @@ } |
@@ -75,1 +75,9 @@ /** | ||
} | ||
console.log(`Hub[default] Module: ${errors.module}`); | ||
console.log(`Hub[myErrors] Module: ${myErrors.module}`); | ||
console.log(`Error[MY_TEST_ERROR2] Module: ${MY_TEST_ERROR2.module}`); | ||
console.log(`Error[INVALID_ERROR_NAME] Module: ${errors.get("INVALID_ERROR_NAME").module}`); |
@@ -25,6 +25,11 @@ /** | ||
await Core.Async.sleep(1000); | ||
const result = await Core.Async.sleep(1000, "cc", 123, true); | ||
console.log(new Date().toISOString()); | ||
for (let i = 0; i < result.length; i++) { | ||
console.info(`result[${i}] = ${JSON.stringify(result[i])}`); | ||
} | ||
})(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107256
69
2286