@bunt/unit
Advanced tools
Comparing version 0.13.0 to 0.14.0
@@ -6,2 +6,18 @@ # Change Log | ||
# [0.14.0](https://github.com/izatop/bunt/compare/v0.13.0...v0.14.0) (2021-01-19) | ||
### Bug Fixes | ||
* organize tests ([32a2f23](https://github.com/izatop/bunt/commit/32a2f232afc63340f478316d2f7fa52a75860cc6)) | ||
### Features | ||
* ws package ([0d39ce1](https://github.com/izatop/bunt/commit/0d39ce1b4631839b8dd14446e2f44e06b3ec4f6d)) | ||
# [0.13.0](https://github.com/izatop/bunt/compare/v0.11.3...v0.13.0) (2021-01-12) | ||
@@ -8,0 +24,0 @@ |
@@ -1,3 +0,4 @@ | ||
import { IContext, Promisify } from "./interfaces"; | ||
export declare abstract class Action<C extends IContext = Record<any, any>, S extends Record<string, any> | null = null, T extends any = any> { | ||
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, T extends any = any, SS extends any = any> { | ||
readonly state: S; | ||
@@ -7,3 +8,4 @@ protected readonly context: C; | ||
get name(): string; | ||
getShadowState(): SS; | ||
abstract run(): Promisify<T>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Action = void 0; | ||
const util_1 = require("@bunt/util"); | ||
const Context_1 = require("./Context"); | ||
class Action { | ||
@@ -12,4 +14,9 @@ constructor(context, state) { | ||
} | ||
getShadowState() { | ||
const shadowState = Context_1.ShadowState.get(this.state); | ||
util_1.assert(shadowState, "Shadow state should be defined"); | ||
return shadowState; | ||
} | ||
} | ||
exports.Action = Action; | ||
//# sourceMappingURL=Action.js.map |
export * from "./Context"; | ||
export * from "./interfaces"; | ||
export * from "./ShadowState"; |
@@ -15,2 +15,3 @@ "use strict"; | ||
__exportStar(require("./interfaces"), exports); | ||
__exportStar(require("./ShadowState"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,2 @@ | ||
import { Promisify } from "@bunt/util"; | ||
import { Action } from "./Action"; | ||
@@ -5,6 +6,8 @@ import { Context, MatchContext } from "./Context"; | ||
} | ||
export declare type Promisify<T> = T | Promise<T>; | ||
export declare type ContextArg<C extends Context> = (() => Promisify<C>) | Promisify<C>; | ||
export declare type AllowState = Record<string, any> | null; | ||
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; | ||
prototype: A; | ||
}; | ||
export declare type ActionCtor<A extends Action<any, any>> = { | ||
@@ -14,6 +17,2 @@ new (context: ActionCtx<A>, state: any): A; | ||
}; | ||
export declare type ActionStateCtor<S extends AllowState, A extends Action<any, S>> = { | ||
new (context: ActionCtx<A>, state: null): A; | ||
prototype: A; | ||
}; | ||
export declare type ActionCtx<A> = A extends Action<infer T, any> ? T : never; | ||
@@ -28,1 +27,2 @@ export declare type ActionState<A> = A extends Action<any, infer T> ? T : never; | ||
} | ||
export declare type StateType = Record<string, any>; |
@@ -5,5 +5,5 @@ export declare type HeartbeatExecutor<T> = (resolve: (value: Error | T) => void) => void; | ||
constructor(executor: HeartbeatExecutor<T>); | ||
get beats(): boolean; | ||
static of<T = unknown>(target: unknown, executor: HeartbeatExecutor<T>): Heartbeat<T>; | ||
get beats(): boolean; | ||
waitUntilStop(): Promise<T | Error>; | ||
} |
@@ -30,2 +30,5 @@ "use strict"; | ||
} | ||
get beats() { | ||
return __classPrivateFieldGet(this, _beats); | ||
} | ||
static of(target, executor) { | ||
@@ -39,5 +42,2 @@ var _a; | ||
} | ||
get beats() { | ||
return __classPrivateFieldGet(this, _beats); | ||
} | ||
waitUntilStop() { | ||
@@ -44,0 +44,0 @@ return __classPrivateFieldGet(this, _defer); |
@@ -1,2 +0,2 @@ | ||
import { Promisify } from "../interfaces"; | ||
import { Promisify } from "@bunt/util"; | ||
import { Heartbeat } from "./Heartbeat"; | ||
@@ -3,0 +3,0 @@ import { DisposableSync } from "./index"; |
@@ -1,3 +0,2 @@ | ||
import { Logger } from "@bunt/util"; | ||
import { Promisify } from "../interfaces"; | ||
import { Logger, Promisify } from "@bunt/util"; | ||
import { Heartbeat } from "./Heartbeat"; | ||
@@ -14,5 +13,5 @@ import { Disposable, DisposableFn, IRunnable } from "./interfaces"; | ||
private constructor(); | ||
static on(event: "release", callback: DisposableFn): void; | ||
static get runtime(): Runtime; | ||
get online(): boolean; | ||
static on(event: "release", callback: DisposableFn): void; | ||
static isDebugEnable(): boolean; | ||
@@ -19,0 +18,0 @@ static isProduction(): boolean; |
@@ -40,5 +40,2 @@ "use strict"; | ||
} | ||
static on(event, callback) { | ||
this.runtime.disposable.push(callback); | ||
} | ||
static get runtime() { | ||
@@ -50,2 +47,5 @@ return this[RuntimeRef]; | ||
} | ||
static on(event, callback) { | ||
this.runtime.disposable.push(callback); | ||
} | ||
static isDebugEnable() { | ||
@@ -52,0 +52,0 @@ return DEBUG; |
import { Logger } from "@bunt/util"; | ||
import { Action } from "./Action"; | ||
import { ApplyContext, Context } from "./Context"; | ||
import { ActionCtor, ActionReturn, ActionStateArgs, ContextArg, IContext, UnitAction } from "./interfaces"; | ||
import { ActionContextCtor, ActionReturn, ActionStateArgs, ContextArg, IContext } from "./interfaces"; | ||
import { IDisposable } from "./Runtime"; | ||
export declare class Unit<C extends IContext = IContext> implements IDisposable { | ||
readonly logger: Logger; | ||
protected readonly context: C; | ||
private readonly registry; | ||
protected constructor(context: C, actions?: UnitAction<C, any>[]); | ||
static factory<A, C extends Context>(context: ContextArg<C>, actions?: UnitAction<C, A>[]): Promise<Unit<C>>; | ||
add<A>(...actions: UnitAction<C, A>[]): ActionCtor<Action>[]; | ||
remove<A>(...actions: UnitAction<C, A>[]): UnitAction<C, A>[]; | ||
has<A>(action: UnitAction<C, A>): boolean; | ||
getContext(): Promise<ApplyContext<C>>; | ||
run<A extends Action<any, any>>(ctor: UnitAction<C, ActionCtor<A>>, ...args: ActionStateArgs<A>): Promise<ActionReturn<A>>; | ||
#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 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>>; | ||
dispose(): IDisposable[]; | ||
} | ||
export declare function unit<A, C extends Context>(context: ContextArg<C>, actions?: UnitAction<C, A>[]): Promise<Unit<C>>; | ||
export declare function unit<C extends Context>(context: ContextArg<C>, actions?: ActionContextCtor<ApplyContext<C>>[]): Promise<Unit<ApplyContext<C>>>; |
@@ -8,2 +8,16 @@ "use strict"; | ||
}; | ||
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) { | ||
if (!privateMap.has(receiver)) { | ||
throw new TypeError("attempted to set private field on non-instance"); | ||
} | ||
privateMap.set(receiver, value); | ||
return value; | ||
}; | ||
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 _context, _registry; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -15,12 +29,22 @@ exports.unit = exports.Unit = void 0; | ||
constructor(context, actions = []) { | ||
this.registry = new WeakSet(); | ||
this.context = context; | ||
_context.set(this, void 0); | ||
_registry.set(this, new WeakSet()); | ||
__classPrivateFieldSet(this, _context, context); | ||
this.add(...actions); | ||
} | ||
get context() { | ||
return __classPrivateFieldGet(this, _context); | ||
} | ||
static from(context, actions = []) { | ||
return new this(context, actions); | ||
} | ||
static async factory(context, actions = []) { | ||
util_1.assert(util_1.isFunction(context) || util_1.isInstanceOf(context, Context_1.Context), `Wrong context type`); | ||
return new this(await this.getContext(context), actions); | ||
} | ||
static async getContext(context) { | ||
if (util_1.isFunction(context)) { | ||
return new this(await context(), actions); | ||
return this.getContext(await context()); | ||
} | ||
return new this(await context, actions); | ||
util_1.assert(util_1.isInstanceOf(context, Context_1.Context), `Wrong context type`); | ||
return Context_1.Context.apply(await context); | ||
} | ||
@@ -30,5 +54,5 @@ add(...actions) { | ||
for (const ctor of actions) { | ||
util_1.fails(util_1.isUndefined(ctor), "Arg isn't defined"); | ||
if (!this.registry.has(ctor)) { | ||
this.registry.add(ctor); | ||
util_1.fails(util_1.isUndefined(ctor), "Wrong the Action type"); | ||
if (!__classPrivateFieldGet(this, _registry).has(ctor)) { | ||
__classPrivateFieldGet(this, _registry).add(ctor); | ||
added.push(ctor); | ||
@@ -42,5 +66,5 @@ } | ||
for (const ctor of actions) { | ||
util_1.fails(util_1.isUndefined(ctor), "Argument isn't a class constructor"); | ||
if (this.registry.has(ctor)) { | ||
this.registry.delete(ctor); | ||
util_1.fails(util_1.isUndefined(ctor), "Wrong the Action type"); | ||
if (__classPrivateFieldGet(this, _registry).has(ctor)) { | ||
__classPrivateFieldGet(this, _registry).delete(ctor); | ||
removed.push(ctor); | ||
@@ -52,16 +76,17 @@ } | ||
has(action) { | ||
return this.registry.has(action); | ||
return __classPrivateFieldGet(this, _registry).has(action); | ||
} | ||
/** | ||
* @deprecated see context | ||
*/ | ||
getContext() { | ||
return Context_1.Context.apply(this.context); | ||
return Promise.resolve(__classPrivateFieldGet(this, _context)); | ||
} | ||
async run(ctor, ...args) { | ||
const finish = this.logger.perf("action", { action: ctor.name }); | ||
util_1.assert(util_1.isClass(ctor), "First argument isn't a class constructor"); | ||
util_1.assert(this.registry.has(ctor), `Unknown 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 context = await this.getContext(); | ||
const action = new ctor(context, state); | ||
return Promise.resolve(action.run()) | ||
.finally(finish); | ||
const action = new ctor(__classPrivateFieldGet(this, _context), state); | ||
return Promise.resolve(action.run()).finally(finish); | ||
} | ||
@@ -72,2 +97,3 @@ dispose() { | ||
} | ||
_context = new WeakMap(), _registry = new WeakMap(); | ||
__decorate([ | ||
@@ -74,0 +100,0 @@ util_1.logger |
{ | ||
"name": "@bunt/unit", | ||
"version": "0.13.0", | ||
"version": "0.14.0", | ||
"keywords": [ | ||
@@ -29,6 +29,6 @@ "typescript" | ||
"dependencies": { | ||
"@bunt/util": "^0.13.0" | ||
"@bunt/util": "^0.14.0" | ||
}, | ||
"license": "MIT", | ||
"gitHead": "a5085f760c6657c29d525003cfaf8b8387c35e5f" | ||
"gitHead": "e085b193f8700ecfc8651cd7e85d8068d023b652" | ||
} |
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
61046
101
60
804
+ Added@bunt/util@0.14.9(transitive)
- Removed@bunt/util@0.13.0(transitive)
Updated@bunt/util@^0.14.0