Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bunt/unit

Package Overview
Dependencies
Maintainers
1
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bunt/unit - npm Package Compare versions

Comparing version 0.17.0 to 0.18.0

dist/Application/Disposable.d.ts

11

CHANGELOG.md

@@ -6,2 +6,13 @@ # Change Log

# [0.18.0](https://github.com/izatop/bunt/compare/v0.17.0...v0.18.0) (2021-05-24)
### Features
* refactor ([f857e9f](https://github.com/izatop/bunt/commit/f857e9f2734ef435d96c894bea9cca09839a86b2))
# [0.17.0](https://github.com/izatop/bunt/compare/v0.16.0...v0.17.0) (2021-05-22)

@@ -8,0 +19,0 @@

5

dist/Action.d.ts
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> {
export declare abstract class Action<C extends IContext = Record<any, any>, S extends StateType | null = null, R extends any = any> {
readonly state: S;

@@ -8,4 +8,3 @@ protected readonly context: C;

get name(): string;
getShadowState(): SS;
abstract run(): Promisify<T>;
abstract run(): Promisify<R>;
}
"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 {

@@ -14,9 +12,4 @@ 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

1

dist/index.d.ts

@@ -7,1 +7,2 @@ export * from "./interfaces";

export * from "./Runtime";
export * from "./Application";

@@ -19,2 +19,3 @@ "use strict";

__exportStar(require("./Runtime"), exports);
__exportStar(require("./Application"), exports);
//# sourceMappingURL=index.js.map

@@ -26,1 +26,4 @@ import { Promisify } from "@bunt/util";

export declare type StateType = Record<string, any>;
export interface IShadowState<T> {
getShadowState(): T;
}

@@ -1,8 +0,16 @@

export declare type HeartbeatExecutor<T> = (resolve: (value: Error | T) => void) => void;
export declare class Heartbeat<T = unknown> {
import { HeartbeatDisposer } from "./interfaces";
export declare class Heartbeat {
#private;
constructor(executor: HeartbeatExecutor<T>);
constructor(disposer?: HeartbeatDisposer);
get beats(): boolean;
static of<T = unknown>(target: unknown, executor: HeartbeatExecutor<T>): Heartbeat<T>;
waitUntilStop(): Promise<T | Error>;
/**
* Always getting an unique Heartbeat of the target
*
* @param target
* @param disposer
*/
static create(target: unknown, disposer?: HeartbeatDisposer): Heartbeat;
static destroy(target: unknown): void;
destroy(error?: Error): void;
watch(): Promise<void>;
}

@@ -15,16 +15,15 @@ "use strict";

};
var _beats, _defer;
var _beats, _pending;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Heartbeat = void 0;
const of = new WeakMap();
const util_1 = require("@bunt/util");
const registry = new WeakMap();
class Heartbeat {
constructor(executor) {
constructor(disposer) {
_beats.set(this, true);
_defer.set(this, void 0);
__classPrivateFieldSet(this, _defer, new Promise((resolve) => {
executor((error) => {
__classPrivateFieldSet(this, _beats, false);
resolve(error);
});
}));
_pending.set(this, void 0);
__classPrivateFieldSet(this, _pending, util_1.AsyncState.acquire());
if (disposer) {
disposer((error) => this.destroy(error));
}
}

@@ -34,16 +33,32 @@ get beats() {

}
static of(target, executor) {
/**
* Always getting an unique Heartbeat of the target
*
* @param target
* @param disposer
*/
static create(target, disposer) {
var _a;
const heartbeat = (_a = of.get(target)) !== null && _a !== void 0 ? _a : new Heartbeat(executor);
if (!of.has(target)) {
of.set(target, heartbeat);
const heartbeat = (_a = registry.get(target)) !== null && _a !== void 0 ? _a : new Heartbeat(disposer);
if (!registry.has(target)) {
registry.set(target, heartbeat);
}
return heartbeat;
}
waitUntilStop() {
return __classPrivateFieldGet(this, _defer);
static destroy(target) {
const heartbeat = registry.get(target);
heartbeat === null || heartbeat === void 0 ? void 0 : heartbeat.destroy();
}
destroy(error) {
if (error) {
return util_1.AsyncState.reject(__classPrivateFieldGet(this, _pending), error);
}
util_1.AsyncState.resolve(__classPrivateFieldGet(this, _pending));
}
watch() {
return __classPrivateFieldGet(this, _pending);
}
}
exports.Heartbeat = Heartbeat;
_beats = new WeakMap(), _defer = new WeakMap();
_beats = new WeakMap(), _pending = new WeakMap();
//# sourceMappingURL=Heartbeat.js.map
export * from "./Runtime";
export * from "./Heartbeat";
export * from "./Disposer";
export * from "./EnvReader";
export * from "./internal";
export * from "./interfaces";

@@ -15,3 +15,2 @@ "use strict";

__exportStar(require("./Heartbeat"), exports);
__exportStar(require("./Disposer"), exports);
__exportStar(require("./EnvReader"), exports);

@@ -18,0 +17,0 @@ __exportStar(require("./internal"), exports);

import { Promisify } from "@bunt/util";
import { Heartbeat } from "./Heartbeat";
import { DisposableSync } from "./index";
export interface IRunnable<TState = unknown> {
getHeartbeat(): Heartbeat<TState>;
export interface IRunnable {
getHeartbeat(): Heartbeat;
}
export interface IDestroyable<TState = unknown> {
destroy(): Promisify<void> | Heartbeat<TState>;
export interface IDisposable {
dispose(): Promise<void>;
}
export interface IDisposableSync {
[DisposableSync]: true;
export interface IDisposedHistory {
error?: Error;
timeout: number;
target: string;
date: Date;
}
export interface IDisposable {
dispose(): Promisify<Disposable | Disposable[] | void>;
}
export declare type HeartbeatDisposer = (resolve: (error?: Error) => any) => Promisify<any>;
export declare type DisposableFn = () => Promisify<void>;
export declare type Disposable = DisposableFn | IDisposable | IDisposableSync & DisposableFn | IDisposable & IDisposableSync;
export declare type DisposableType = DisposableFn | IDisposable;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
//# sourceMappingURL=interfaces.js.map
/// <reference types="node" />
import { Promisify } from "@bunt/util";
import { IDisposable, IRunnable } from "./interfaces";
import { Runtime } from "./Runtime";
export declare const DisposableSync: unique symbol;
export declare const Signals: NodeJS.Signals[];
export declare function isDisposable(candidate: unknown): candidate is IDisposable;
export declare function isDisposableSync(candidate: unknown): candidate is IDisposable;
export declare function isRunnable(candidate: unknown): candidate is IRunnable;
export declare function main(fn: (runtime: Runtime) => any, before?: () => any): void;
export declare function main(...chain: ((runtime: Runtime) => Promisify<unknown>)[]): void;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.main = exports.isRunnable = exports.isDisposableSync = exports.isDisposable = exports.Signals = exports.DisposableSync = void 0;
exports.main = exports.isRunnable = exports.isDisposable = exports.Signals = void 0;
const util_1 = require("@bunt/util");
const Runtime_1 = require("./Runtime");
exports.DisposableSync = Symbol();
exports.Signals = ["SIGINT", "SIGQUIT", "SIGTERM"];

@@ -12,6 +11,2 @@ function isDisposable(candidate) {

exports.isDisposable = isDisposable;
function isDisposableSync(candidate) {
return util_1.isObject(candidate) && "dispose" in candidate && exports.DisposableSync in candidate;
}
exports.isDisposableSync = isDisposableSync;
function isRunnable(candidate) {

@@ -21,6 +16,5 @@ return util_1.isObject(candidate) && "getHeartbeat" in candidate;

exports.isRunnable = isRunnable;
function main(fn, before) {
function main(...chain) {
process.nextTick(async () => {
Runtime_1.Runtime.initialize(before);
Runtime_1.Runtime.run(fn);
await Runtime_1.Runtime.run(...chain);
});

@@ -27,0 +21,0 @@ }

import { Logger, Promisify } from "@bunt/util";
import { Heartbeat } from "./Heartbeat";
import { Disposable, DisposableFn, IRunnable } from "./interfaces";
declare const RuntimeRef: unique symbol;
export declare class Runtime {
import { DisposableFn, IDisposable } from "./interfaces";
export declare class Runtime implements IDisposable {
#private;
private static [RuntimeRef];
readonly logger: Logger;
protected readonly queue: Heartbeat[];
protected readonly disposable: Disposable[];
private readonly created;
private constructor();
static get runtime(): Runtime;
get online(): boolean;

@@ -20,8 +16,7 @@ static on(event: "release", callback: DisposableFn): void;

static isTest(): boolean;
static initialize(before?: () => any): void;
static run(fn: (runtime: Runtime) => Promisify<void | IRunnable>): Promise<void>;
run(fn: (runtime: Runtime) => Promisify<void | IRunnable>): Promise<void>;
accept(item: unknown): void;
static run(...chain: ((runtime: Runtime) => Promisify<unknown>)[]): Promise<void>;
dispose(): Promise<void>;
accept(result: unknown): Promise<void>;
private run;
private release;
}
export {};

@@ -19,5 +19,5 @@ "use strict";

const util_1 = require("@bunt/util");
const Disposer_1 = require("./Disposer");
const Application_1 = require("../Application");
const internal_1 = require("./internal");
const RuntimeRef = Symbol();
const ref = new util_1.SingleRef();
const DEBUG = !!process.env.DEBUG;

@@ -28,3 +28,2 @@ const ENV = process.env.NODE_ENV || "production";

this.queue = [];
this.disposable = [];
_disposed.set(this, false);

@@ -35,3 +34,2 @@ this.logger = util_1.Logger.factory(this);

this.accept(this.logger);
this.accept(() => new Promise((resolve) => process.nextTick(resolve)));
// @TODO Send an event when a signal has been received.

@@ -43,5 +41,2 @@ for (const signal of internal_1.Signals) {

}
static get runtime() {
return this[RuntimeRef];
}
get online() {

@@ -51,3 +46,3 @@ return !__classPrivateFieldGet(this, _disposed);

static on(event, callback) {
this.runtime.disposable.push(callback);
Application_1.Disposable.attach(ref.ensure(), callback);
}

@@ -66,22 +61,28 @@ static isDebugEnable() {

}
static initialize(before) {
if (this[RuntimeRef]) {
static run(...chain) {
const runtime = ref.create(() => new Runtime());
return runtime.run(...chain);
}
async dispose() {
return;
}
async accept(result) {
const done = await result;
if (util_1.isUndefined(done) || util_1.isNull(done)) {
return;
}
try {
before && before();
if (internal_1.isDisposable(done)) {
Application_1.Disposable.attach(this, done);
}
finally {
this[RuntimeRef] = new this();
if (internal_1.isRunnable(done)) {
this.queue.push(done.getHeartbeat());
}
}
static run(fn) {
this.initialize();
return this.runtime.run(fn);
}
async run(fn) {
async run(...chain) {
const finish = this.logger.perf("run");
try {
this.accept(await fn(this));
await Promise.allSettled(this.queue.map((hb) => hb.waitUntilStop()));
for (const callback of chain) {
await this.accept(callback(this));
}
await Promise.allSettled(this.queue.map((hb) => hb.watch()));
}

@@ -98,13 +99,2 @@ catch (error) {

}
accept(item) {
if (util_1.isUndefined(item) || util_1.isNull(item)) {
return;
}
if (internal_1.isDisposable(item)) {
this.disposable.push(item);
}
if (internal_1.isRunnable(item)) {
this.queue.push(item.getHeartbeat());
}
}
async release() {

@@ -114,4 +104,4 @@ this.logger.info("release");

__classPrivateFieldSet(this, _disposed, true);
await Disposer_1.Disposer.dispose(this.disposable);
process.exit();
this.dispose();
process.exit(0);
}

@@ -118,0 +108,0 @@ }

@@ -8,4 +8,4 @@ import { Ctor } from "@bunt/util";

static resolve(): PropertyDecorator;
protected static getPrototypes(target: unknown, base?: Ctor<any> | null): Generator<any>;
protected static getPrototypes(target: unknown, base?: Ctor | null): Generator<any>;
abstract resolve(): Promise<T>;
}

@@ -5,4 +5,3 @@ import { Logger } from "@bunt/util";

import { ActionContextCtor, ActionReturn, ActionStateArgs, ContextArg, IContext } from "./interfaces";
import { IDisposable } from "./Runtime";
export declare class Unit<C extends IContext = IContext> implements IDisposable {
export declare class Unit<C extends IContext = IContext> {
#private;

@@ -23,4 +22,3 @@ protected readonly logger: Logger;

run<A extends Action<C, any>>(ctor: ActionContextCtor<C, A>, ...args: ActionStateArgs<A>): Promise<ActionReturn<A>>;
dispose(): IDisposable[];
}
export declare function unit<C extends Context>(context: ContextArg<C>, actions?: ActionContextCtor<ApplyContext<C>>[]): Promise<Unit<ApplyContext<C>>>;

@@ -88,5 +88,2 @@ "use strict";

}
dispose() {
return [...Context_1.Context.disposables.values()];
}
}

@@ -93,0 +90,0 @@ _context = new WeakMap(), _registry = new WeakMap();

{
"name": "@bunt/unit",
"version": "0.17.0",
"version": "0.18.0",
"keywords": [

@@ -29,6 +29,6 @@ "typescript"

"dependencies": {
"@bunt/util": "^0.17.0"
"@bunt/util": "^0.18.0"
},
"license": "MIT",
"gitHead": "0076a18157eaa35485b784bae95fcd5ff6852d72"
"gitHead": "21e4f7bc6fce2bdcb796b33dcccc9b3cdda6a8e3"
}

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc