@bunt/unit
Advanced tools
Comparing version 0.23.0 to 0.24.0
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -6,0 +10,0 @@ if (k2 === undefined) k2 = k; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -6,0 +10,0 @@ if (k2 === undefined) k2 = k; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -6,0 +10,0 @@ if (k2 === undefined) k2 = k; |
@@ -10,2 +10,6 @@ import { Promisify } from "@bunt/util"; | ||
}; | ||
export declare type ActionCtorImport<C extends Context, S extends StateType | null = any, R = unknown, A extends Action<C, S, R> = Action<C, S, R>> = () => Promise<{ | ||
default: ActionCtor<C, S, R, A>; | ||
}>; | ||
export declare type ActionFactory<C extends Context, S extends StateType | null = any, R = unknown, A extends Action<C, S, R> = Action<C, S, R>> = ActionCtor<C, S, R, A> | ActionCtorImport<C, S, R, A>; | ||
export declare type ActionContext<A> = A extends ActionAny<infer T> ? T : never; | ||
@@ -12,0 +16,0 @@ export declare type ActionState<A> = A extends ActionAny<any, infer T> ? T : never; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -6,0 +10,0 @@ if (k2 === undefined) k2 = k; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -6,0 +10,0 @@ if (k2 === undefined) k2 = k; |
import { Logger } from "@bunt/util"; | ||
import { ApplyContext, Context } from "./Context"; | ||
import { Action } from "./Action"; | ||
import { ApplyContext, Context } from "./Context"; | ||
import { ActionCtor, ActionReturn, ActionState, ContextArg, StateType } from "./interfaces"; | ||
import { ActionCtor, ActionFactory, ActionReturn, ActionState, ContextArg, StateType } from "./interfaces"; | ||
export declare class Unit<C extends Context> { | ||
#private; | ||
protected readonly logger: Logger; | ||
protected constructor(context: ApplyContext<C>, actions?: ActionCtor<C>[]); | ||
protected constructor(context: ApplyContext<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>>; | ||
static from<C extends Context>(context: ApplyContext<C>): Unit<C>; | ||
static factory<C extends Context>(context: ContextArg<C>): Promise<Unit<C>>; | ||
protected static getContext<C extends Context>(context: ContextArg<C>): Promise<ApplyContext<C>>; | ||
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>>>; | ||
run<A extends Action<C, S, R>, S extends StateType, R = unknown>(factory: ActionFactory<C, S, R, A>, state: ActionState<A>): Promise<ActionReturn<Action<C, S, R>>>; | ||
static getAction(action: ActionFactory<any>): Promise<ActionCtor<any>>; | ||
private static isActionFactory; | ||
} | ||
export declare function unit<C extends Context>(context: ContextArg<C>, actions?: ActionCtor<C>[]): Promise<Unit<C>>; | ||
export declare function unit<C extends Context>(context: ContextArg<C>): Promise<Unit<C>>; |
@@ -19,3 +19,3 @@ "use strict"; | ||
}; | ||
var _Unit_context, _Unit_registry; | ||
var _Unit_context; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -25,8 +25,7 @@ exports.unit = exports.Unit = void 0; | ||
const Context_1 = require("./Context"); | ||
const Action_1 = require("./Action"); | ||
class Unit { | ||
constructor(context, actions = []) { | ||
constructor(context) { | ||
_Unit_context.set(this, void 0); | ||
_Unit_registry.set(this, new WeakSet()); | ||
__classPrivateFieldSet(this, _Unit_context, context, "f"); | ||
this.add(...actions); | ||
} | ||
@@ -36,7 +35,7 @@ get context() { | ||
} | ||
static from(context, actions = []) { | ||
return new this(context, actions); | ||
static from(context) { | ||
return new this(context); | ||
} | ||
static async factory(context, actions = []) { | ||
return new this(await this.getContext(context), actions); | ||
static async factory(context) { | ||
return new this(await this.getContext(context)); | ||
} | ||
@@ -47,39 +46,25 @@ static async getContext(context) { | ||
} | ||
(0, util_1.assert)((0, util_1.isInstanceOf)(context, Context_1.Context), "Wrong context type"); | ||
return Context_1.Context.apply(await context); | ||
const syncContext = await context; | ||
(0, util_1.assert)((0, util_1.isInstanceOf)(syncContext, Context_1.Context), "Wrong context type"); | ||
return Context_1.Context.apply(syncContext); | ||
} | ||
add(...actions) { | ||
const added = []; | ||
for (const ctor of actions) { | ||
(0, util_1.fails)((0, util_1.isUndefined)(ctor), "Wrong the Action type"); | ||
if (!__classPrivateFieldGet(this, _Unit_registry, "f").has(ctor)) { | ||
__classPrivateFieldGet(this, _Unit_registry, "f").add(ctor); | ||
added.push(ctor); | ||
} | ||
} | ||
return added; | ||
async run(factory, state) { | ||
const ctor = await Unit.getAction(factory); | ||
(0, util_1.assert)(Action_1.Action.isPrototypeOf(ctor), "The 'ctor' hasn't prototype of the Action class"); | ||
const finish = this.logger.perf("run", { action: ctor.name }); | ||
const action = new ctor(__classPrivateFieldGet(this, _Unit_context, "f"), state); | ||
return Promise.resolve(action.run()).finally(finish); | ||
} | ||
remove(...actions) { | ||
const removed = []; | ||
for (const ctor of actions) { | ||
(0, util_1.fails)((0, util_1.isUndefined)(ctor), "Wrong the Action type"); | ||
if (__classPrivateFieldGet(this, _Unit_registry, "f").has(ctor)) { | ||
__classPrivateFieldGet(this, _Unit_registry, "f").delete(ctor); | ||
removed.push(ctor); | ||
} | ||
static async getAction(action) { | ||
if (this.isActionFactory(action)) { | ||
const { default: ctor } = await action(); | ||
return ctor; | ||
} | ||
return removed; | ||
return action; | ||
} | ||
has(action) { | ||
return __classPrivateFieldGet(this, _Unit_registry, "f").has(action); | ||
static isActionFactory(action) { | ||
return !Action_1.Action.isPrototypeOf(action); | ||
} | ||
async run(ctor, state) { | ||
const finish = this.logger.perf("action", { action: ctor.name }); | ||
(0, util_1.assert)((0, util_1.isClass)(ctor), "Wrong the Action type"); | ||
(0, util_1.assert)(__classPrivateFieldGet(this, _Unit_registry, "f").has(ctor), `Unknown action ${ctor.name}`); | ||
const action = new ctor(__classPrivateFieldGet(this, _Unit_context, "f"), state); | ||
return Promise.resolve(action.run()).finally(finish); | ||
} | ||
} | ||
_Unit_context = new WeakMap(), _Unit_registry = new WeakMap(); | ||
_Unit_context = new WeakMap(); | ||
__decorate([ | ||
@@ -89,6 +74,6 @@ util_1.logger | ||
exports.Unit = Unit; | ||
function unit(context, actions = []) { | ||
return Unit.factory(context, actions); | ||
function unit(context) { | ||
return Unit.factory(context); | ||
} | ||
exports.unit = unit; | ||
//# sourceMappingURL=Unit.js.map |
{ | ||
"name": "@bunt/unit", | ||
"version": "0.23.0", | ||
"version": "0.24.0", | ||
"keywords": [ | ||
@@ -29,6 +29,6 @@ "typescript" | ||
"dependencies": { | ||
"@bunt/util": "^0.23.0" | ||
"@bunt/util": "^0.24.0" | ||
}, | ||
"license": "MIT", | ||
"gitHead": "764859d453226d6f229675eb6bcce3bd56c43428" | ||
"gitHead": "870927e58da8dcc164c48be4bb13ddb171c5b1fd" | ||
} |
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
968
61613
+ Added@bunt/util@0.24.53(transitive)
- Removed@bunt/util@0.23.0(transitive)
Updated@bunt/util@^0.24.0