@bunt/unit
Advanced tools
Comparing version 0.18.3 to 0.19.0
@@ -6,2 +6,18 @@ # Change Log | ||
# [0.19.0](https://github.com/izatop/bunt/compare/v0.18.3...v0.19.0) (2021-05-26) | ||
### Bug Fixes | ||
* tests, disposable ([99ead5a](https://github.com/izatop/bunt/commit/99ead5a2b518bb6160687afcf1d12311b38d1c44)) | ||
### Features | ||
* update unit types ([6a7124c](https://github.com/izatop/bunt/commit/6a7124cb8074892a0d4ce4e86d551d34289b42ee)) | ||
## [0.18.3](https://github.com/izatop/bunt/compare/v0.18.2...v0.18.3) (2021-05-25) | ||
@@ -8,0 +24,0 @@ |
import { Promisify } from "@bunt/util"; | ||
import { IContext, StateType } from "./interfaces"; | ||
export declare abstract class Action<C extends IContext = Record<any, any>, S extends StateType | null = null, R extends any = any> { | ||
import { ApplyContext, Context } from "./Context"; | ||
import { StateType } from "./interfaces"; | ||
export declare abstract class Action<C extends Context, S extends StateType = null, R = unknown> { | ||
readonly state: S; | ||
protected readonly context: C; | ||
constructor(context: C, state: S); | ||
protected readonly context: ApplyContext<C>; | ||
constructor(context: ApplyContext<C>, state: S); | ||
get name(): string; | ||
abstract run(): Promisify<R>; | ||
} |
@@ -5,3 +5,4 @@ import { Logger } from "@bunt/util"; | ||
import { ApplyContext, ResolveService } from "./interfaces"; | ||
export declare class Context { | ||
export declare class Context implements IDisposable { | ||
#private; | ||
static logger: Logger; | ||
@@ -12,2 +13,3 @@ static disposables: Set<IDisposable>; | ||
protected static getResolvedContext<C extends Context>(context: C): Promise<ApplyContext<C>>; | ||
dispose(): Promise<void>; | ||
} |
"use strict"; | ||
var _IsContext; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -9,2 +10,5 @@ exports.Context = void 0; | ||
class Context { | ||
constructor() { | ||
_IsContext.set(this, void 0); | ||
} | ||
static async resolve(value) { | ||
@@ -55,6 +59,10 @@ if (Service_1.isService(value)) { | ||
} | ||
dispose() { | ||
return Promise.resolve(undefined); | ||
} | ||
} | ||
exports.Context = Context; | ||
_IsContext = new WeakMap(); | ||
Context.logger = util_1.Logger.factory(Context); | ||
Context.disposables = new Set(); | ||
//# sourceMappingURL=Context.js.map |
import { IServiceResolver } from "../interfaces"; | ||
import { Context } from "./Context"; | ||
export declare type ResolveService<T extends any> = T extends IServiceResolver<infer S> ? Promise<S> : T; | ||
export declare type ApplyContext<C extends Context> = { | ||
[K in keyof C]: C[K] extends IServiceResolver<infer M> ? M : C[K]; | ||
export declare type ApplyContext<C extends Context> = C & { | ||
[K in keyof C]: C[K] extends IServiceResolver<infer S> ? S : C[K]; | ||
}; | ||
export declare type Ex<C, K> = K extends keyof C ? C[K] : never; | ||
export declare type MatchContext<C extends any, T> = { | ||
[K in keyof T]: Ex<C, K> extends T[K] ? T[K] : Ex<C, K> extends IServiceResolver<infer M> ? M extends T[K] ? T[K] : ["The IServiceResolver<T> doesn't match context", Ex<C, K>, T[K]] : ["The input type doesn't match context", Ex<C, K>, T[K]]; | ||
}; |
import { StateType } from "../interfaces"; | ||
export declare class ShadowState { | ||
static get<SS>(state: StateType | any): SS; | ||
static get<SS>(state: StateType): SS; | ||
static set<SS>(state: StateType, shadowState: SS): void; | ||
} |
@@ -7,2 +7,2 @@ export * from "./interfaces"; | ||
export * from "./Runtime"; | ||
export * from "./Application"; | ||
export * from "./Dispose"; |
@@ -19,3 +19,3 @@ "use strict"; | ||
__exportStar(require("./Runtime"), exports); | ||
__exportStar(require("./Application"), exports); | ||
__exportStar(require("./Dispose"), exports); | ||
//# sourceMappingURL=index.js.map |
import { Promisify } from "@bunt/util"; | ||
import { Action } from "./Action"; | ||
import { Context, MatchContext } from "./Context"; | ||
export interface IContext { | ||
} | ||
import { ApplyContext, Context } from "./Context"; | ||
export declare type ActionAny<C extends Context = Context, S extends StateType | null = any, R = any> = Action<C, S, R>; | ||
export declare type ContextArg<C extends Context> = (() => Promisify<C>) | Promisify<C>; | ||
export declare type ActionStateArgs<A> = ActionState<A> extends null ? [] : [ActionState<A>]; | ||
export declare type ActionContextCtor<C extends IContext, A extends Action<C, any> = Action<C, any>> = { | ||
new (context: C, state: any): A; | ||
export declare type ActionCtor<C extends Context, S extends StateType | null = any, R = unknown, A extends Action<C, S, R> = Action<C, S, R>> = { | ||
new (context: ApplyContext<C>, state: S): A; | ||
prototype: A; | ||
}; | ||
export declare type ActionCtor<A extends Action<any, any>> = { | ||
new (context: ActionCtx<A>, state: any): A; | ||
prototype: A; | ||
}; | ||
export declare type ActionCtx<A> = A extends Action<infer T, any> ? T : never; | ||
export declare type ActionState<A> = A extends Action<any, infer T> ? T : never; | ||
export declare type ActionReturn<A> = A extends Action<any, any, infer T> ? T : never; | ||
export declare type ActionContext<A> = ActionCtx<A>; | ||
export declare type UnitAction<C, T> = T extends ActionCtor<infer A> ? A extends Action<infer AC, any> ? MatchContext<C, AC> extends AC ? T : never : never : never; | ||
export declare type ActionContext<A> = A extends ActionAny<infer T> ? T : never; | ||
export declare type ActionState<A> = A extends ActionAny<any, infer T> ? T : never; | ||
export declare type ActionReturn<A> = A extends ActionAny<any, any, infer T> ? T : never; | ||
export declare type ResolvableValue<T> = Promise<T> | (() => T | Promise<T>); | ||
@@ -25,5 +17,5 @@ export interface IServiceResolver<T> { | ||
} | ||
export declare type StateType = Record<string, any>; | ||
export declare type StateType = Record<string, any> | null; | ||
export interface IShadowState<T> { | ||
getShadowState(): T; | ||
} |
@@ -15,3 +15,3 @@ import { Logger } from "@bunt/util"; | ||
static create(target: IRunnable, disposer?: HeartbeatDisposer): Heartbeat; | ||
static watch(runnable: IRunnable): Promise<void>; | ||
static watch(runnable: IRunnable | unknown): Promise<void>; | ||
static destroy(target: IRunnable): void; | ||
@@ -18,0 +18,0 @@ destroy(error?: Error): void; |
@@ -21,10 +21,10 @@ "use strict"; | ||
}; | ||
var _beats, _pending; | ||
var _pending; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Heartbeat = void 0; | ||
const util_1 = require("@bunt/util"); | ||
const internal_1 = require("./internal"); | ||
const registry = new WeakMap(); | ||
class Heartbeat { | ||
constructor(label, disposer) { | ||
_beats.set(this, true); | ||
_pending.set(this, void 0); | ||
@@ -38,3 +38,3 @@ this.logger.debug("create", { label }); | ||
get beats() { | ||
return __classPrivateFieldGet(this, _beats); | ||
return !util_1.AsyncState.isReleased(__classPrivateFieldGet(this, _pending)); | ||
} | ||
@@ -56,4 +56,6 @@ /** | ||
static async watch(runnable) { | ||
const heartbeat = runnable.getHeartbeat(); | ||
return heartbeat.watch(); | ||
if (internal_1.isRunnable(runnable)) { | ||
const heartbeat = runnable.getHeartbeat(); | ||
return heartbeat.watch(); | ||
} | ||
} | ||
@@ -65,2 +67,5 @@ static destroy(target) { | ||
destroy(error) { | ||
if (!this.beats) { | ||
return; | ||
} | ||
if (error) { | ||
@@ -75,3 +80,3 @@ return util_1.AsyncState.reject(__classPrivateFieldGet(this, _pending), error); | ||
} | ||
_beats = new WeakMap(), _pending = new WeakMap(); | ||
_pending = new WeakMap(); | ||
__decorate([ | ||
@@ -78,0 +83,0 @@ util_1.logger |
@@ -11,2 +11,3 @@ import { Promisify } from "@bunt/util"; | ||
error?: Error; | ||
label: string; | ||
timeout: number; | ||
@@ -13,0 +14,0 @@ target: string; |
@@ -6,4 +6,4 @@ /// <reference types="node" /> | ||
export declare const Signals: NodeJS.Signals[]; | ||
export declare function isDisposable(candidate: unknown): candidate is IDisposable; | ||
export declare function isRunnable(candidate: unknown): candidate is IRunnable; | ||
export declare function isDisposable(target: unknown): target is IDisposable; | ||
export declare function isRunnable(target: unknown): target is IRunnable; | ||
export declare function main(...chain: ((runtime: Runtime) => Promisify<unknown>)[]): void; |
@@ -7,8 +7,8 @@ "use strict"; | ||
exports.Signals = ["SIGINT", "SIGQUIT", "SIGTERM"]; | ||
function isDisposable(candidate) { | ||
return util_1.isObject(candidate) && "dispose" in candidate; | ||
function isDisposable(target) { | ||
return util_1.isObject(target) && "dispose" in target; | ||
} | ||
exports.isDisposable = isDisposable; | ||
function isRunnable(candidate) { | ||
return util_1.isObject(candidate) && "getHeartbeat" in candidate; | ||
function isRunnable(target) { | ||
return util_1.isObject(target) && "getHeartbeat" in target; | ||
} | ||
@@ -15,0 +15,0 @@ exports.isRunnable = isRunnable; |
@@ -5,3 +5,2 @@ import { Logger, Promisify } from "@bunt/util"; | ||
export declare class Runtime implements IDisposable { | ||
#private; | ||
readonly logger: Logger; | ||
@@ -11,3 +10,2 @@ protected readonly queue: Heartbeat[]; | ||
private constructor(); | ||
get online(): boolean; | ||
static on(event: "release", callback: DisposableFn): void; | ||
@@ -19,5 +17,5 @@ static isDebugEnable(): boolean; | ||
static run(...chain: ((runtime: Runtime) => Promisify<unknown>)[]): Promise<void>; | ||
accept(result: unknown): Promise<void>; | ||
handle(result: unknown): Promise<void>; | ||
dispose(): Promise<void>; | ||
private run; | ||
} |
@@ -8,13 +8,6 @@ "use strict"; | ||
}; | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) { | ||
if (!privateMap.has(receiver)) { | ||
throw new TypeError("attempted to get private field on non-instance"); | ||
} | ||
return privateMap.get(receiver); | ||
}; | ||
var _disposed; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Runtime = void 0; | ||
const util_1 = require("@bunt/util"); | ||
const Application_1 = require("../Application"); | ||
const Dispose_1 = require("../Dispose"); | ||
const internal_1 = require("./internal"); | ||
@@ -27,3 +20,2 @@ const ref = new util_1.SingleRef(); | ||
this.queue = []; | ||
_disposed.set(this, false); | ||
this.created = new Date(); | ||
@@ -34,10 +26,7 @@ this.logger.info("register", { ENV, DEBUG }); | ||
this.logger.debug("listen", signal); | ||
process.on(signal, async () => this.online && this.dispose()); | ||
process.on(signal, async () => this.dispose()); | ||
} | ||
} | ||
get online() { | ||
return !__classPrivateFieldGet(this, _disposed); | ||
} | ||
static on(event, callback) { | ||
Application_1.Disposable.attach(ref.ensure(), callback); | ||
Dispose_1.Disposable.attach(ref.ensure(), callback); | ||
} | ||
@@ -60,12 +49,12 @@ static isDebugEnable() { | ||
} | ||
async accept(result) { | ||
const done = await result; | ||
if (util_1.isUndefined(done) || util_1.isNull(done)) { | ||
async handle(result) { | ||
const object = await result; | ||
if (util_1.isUndefined(object) || util_1.isNull(object)) { | ||
return; | ||
} | ||
if (internal_1.isDisposable(done)) { | ||
Application_1.Disposable.attach(this, done); | ||
if (internal_1.isRunnable(object)) { | ||
this.queue.push(object.getHeartbeat()); | ||
} | ||
if (internal_1.isRunnable(done)) { | ||
this.queue.push(done.getHeartbeat()); | ||
if (internal_1.isDisposable(object)) { | ||
Dispose_1.Disposable.attach(this, object); | ||
} | ||
@@ -75,3 +64,2 @@ } | ||
this.logger.info("dispose"); | ||
util_1.assert(this.online, "Runtime has been already released"); | ||
process.exit(0); | ||
@@ -83,3 +71,3 @@ } | ||
for (const callback of chain) { | ||
await this.accept(callback(this)); | ||
await this.handle(callback(this)); | ||
} | ||
@@ -94,8 +82,6 @@ await Promise.allSettled(this.queue.map((hb) => hb.watch())); | ||
} | ||
if (this.online) { | ||
process.nextTick(() => Application_1.dispose(this)); | ||
} | ||
await Dispose_1.dispose(this); | ||
await Dispose_1.Disposable.disposeAll(); | ||
} | ||
} | ||
_disposed = new WeakMap(); | ||
__decorate([ | ||
@@ -102,0 +88,0 @@ util_1.logger |
import { Logger } from "@bunt/util"; | ||
import { Action } from "./Action"; | ||
import { ApplyContext, Context } from "./Context"; | ||
import { ActionContextCtor, ActionReturn, ActionStateArgs, ContextArg, IContext } from "./interfaces"; | ||
export declare class Unit<C extends IContext = IContext> { | ||
import { ActionCtor, ActionReturn, ActionState, ContextArg, StateType } from "./interfaces"; | ||
export declare class Unit<C extends Context> { | ||
#private; | ||
protected readonly logger: Logger; | ||
protected constructor(context: C, actions?: ActionContextCtor<C>[]); | ||
get context(): C; | ||
static from<C extends IContext>(context: C, actions?: ActionContextCtor<C>[]): Unit<C>; | ||
static factory<C extends Context>(context: ContextArg<C>, actions?: ActionContextCtor<ApplyContext<C>>[]): Promise<Unit<ApplyContext<C>>>; | ||
protected constructor(context: ApplyContext<C>, actions?: ActionCtor<C>[]); | ||
get context(): ApplyContext<C>; | ||
static from<C extends Context>(context: ApplyContext<C>, actions?: ActionCtor<C>[]): Unit<C>; | ||
static factory<C extends Context>(context: ContextArg<C>, actions?: ActionCtor<C>[]): Promise<Unit<C>>; | ||
protected static getContext<C extends Context>(context: ContextArg<C>): Promise<ApplyContext<C>>; | ||
add(...actions: ActionContextCtor<C>[]): ActionContextCtor<C>[]; | ||
remove(...actions: ActionContextCtor<C>[]): ActionContextCtor<C>[]; | ||
has(action: ActionContextCtor<C>): boolean; | ||
/** | ||
* @deprecated see context | ||
*/ | ||
getContext(): Promise<C>; | ||
run<A extends Action<C, any>>(ctor: ActionContextCtor<C, A>, ...args: ActionStateArgs<A>): Promise<ActionReturn<A>>; | ||
add(...actions: ActionCtor<C>[]): ActionCtor<C>[]; | ||
remove(...actions: ActionCtor<C>[]): ActionCtor<C>[]; | ||
has(action: ActionCtor<C>): boolean; | ||
run<A extends Action<C, S, R>, S extends StateType | null, R = unknown>(ctor: ActionCtor<C, S, R, A>, state: ActionState<A>): Promise<ActionReturn<Action<C, S, R>>>; | ||
} | ||
export declare function unit<C extends Context>(context: ContextArg<C>, actions?: ActionContextCtor<ApplyContext<C>>[]): Promise<Unit<ApplyContext<C>>>; | ||
export declare function unit<C extends Context>(context: ContextArg<C>, actions?: ActionCtor<C>[]): Promise<Unit<C>>; |
@@ -74,13 +74,6 @@ "use strict"; | ||
} | ||
/** | ||
* @deprecated see context | ||
*/ | ||
getContext() { | ||
return Promise.resolve(__classPrivateFieldGet(this, _context)); | ||
} | ||
async run(ctor, ...args) { | ||
async run(ctor, state) { | ||
const finish = this.logger.perf("action", { action: ctor.name }); | ||
util_1.assert(util_1.isClass(ctor), "Wrong the Action type"); | ||
util_1.assert(__classPrivateFieldGet(this, _registry).has(ctor), `Unknown action ${ctor.name}`); | ||
const [state = null] = args; | ||
const action = new ctor(__classPrivateFieldGet(this, _context), state); | ||
@@ -87,0 +80,0 @@ return Promise.resolve(action.run()).finally(finish); |
{ | ||
"name": "@bunt/unit", | ||
"version": "0.18.3", | ||
"version": "0.19.0", | ||
"keywords": [ | ||
@@ -29,6 +29,6 @@ "typescript" | ||
"dependencies": { | ||
"@bunt/util": "^0.18.2" | ||
"@bunt/util": "^0.19.0" | ||
}, | ||
"license": "MIT", | ||
"gitHead": "55fa5286572395dbf967f7770a06cdeda179d7d9" | ||
"gitHead": "8e62b6bb684e9b7cc47b50f0be41c04020e1f9e7" | ||
} |
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
75332
973
+ Added@bunt/util@0.19.18(transitive)
- Removed@bunt/util@0.18.2(transitive)
Updated@bunt/util@^0.19.0