@dhmk/utils
Advanced tools
Comparing version 4.2.2 to 4.3.0
@@ -0,5 +1,23 @@ | ||
export declare type Unsubscribe = () => void; | ||
export declare type Listener<T> = (x: T) => void; | ||
export declare function signal<T = void>(): { | ||
export interface Event<T> { | ||
subscribe(fn: Listener<T>): Unsubscribe; | ||
} | ||
export interface Signal<T> extends Event<T> { | ||
(x: T): void; | ||
subscribe(fn: Listener<T>): () => void; | ||
event(): Event<T>; | ||
} | ||
export declare function signal<T = void>({ listeners, emit, subscribe, }?: { | ||
listeners?: Set<Listener<T>> | undefined; | ||
emit?: ((fn: (x: T) => void) => (x: T) => void) | undefined; | ||
subscribe?: ((fn: (x: Listener<T>) => Unsubscribe) => (x: Listener<T>) => Unsubscribe) | undefined; | ||
}): Signal<T>; | ||
declare type Signals<T> = { | ||
[P in keyof T]: T[P] extends Signal<infer S> ? Signal<S> : Signals<T[P]>; | ||
}; | ||
declare type Events<T> = { | ||
[P in keyof T]: T[P] extends Signal<infer S> ? Event<S> : Events<T[P]>; | ||
}; | ||
export declare function asEvents<T extends Signals<T>>(x: T): Events<T>; | ||
export declare function once<T>(ev: Event<T>, onValue?: Listener<T>): Promise<T>; | ||
export {}; |
@@ -1,32 +0,41 @@ | ||
export function signal() { | ||
var subs = new Set(); | ||
var self = function (x) { return subs.forEach(function (s) { return s(x); }); }; | ||
self.subscribe = function (fn) { | ||
subs.add(fn); | ||
return function () { | ||
subs.delete(fn); | ||
}; | ||
}; | ||
export function signal(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.listeners, listeners = _c === void 0 ? new Set() : _c, _d = _b.emit, emit = _d === void 0 ? function (fn) { return fn; } : _d, _e = _b.subscribe, subscribe = _e === void 0 ? function (fn) { return fn; } : _e; | ||
var self = emit(function (x) { | ||
var errors = []; | ||
listeners.forEach(function (fn) { | ||
try { | ||
fn(x); | ||
} | ||
catch (e) { | ||
errors.push(e); | ||
} | ||
}); | ||
if (errors.length) | ||
throw new AggregateError(errors); | ||
}); | ||
self.subscribe = subscribe(function (fn) { | ||
listeners.add(fn); | ||
return function () { return listeners.delete(fn); }; | ||
}); | ||
self.event = function () { return self; }; | ||
return self; | ||
} | ||
// todo: signal improvement (multi args + customizable) | ||
// export type Listener<A extends any[]> = (...args: A) => void; | ||
// export type Signal<A extends any[]> = { | ||
// subscribe(fn: Listener<A>): () => void; | ||
// }; | ||
// export function signal<A extends any[] = []>({ | ||
// listeners = new Set<any>(), | ||
// emit = (fn: (...args: A) => void) => fn, | ||
// subscribe = (fn: (fn: Listener<A>) => () => void) => fn, | ||
// } = {}) { | ||
// const self: any = emit((...args: A) => | ||
// listeners.forEach((s) => s(...args)) | ||
// ); | ||
// self.subscribe = subscribe((fn: Listener<A>) => { | ||
// listeners.add(fn); | ||
// return () => { | ||
// listeners.delete(fn); | ||
// }; | ||
// }); | ||
// return self as ((...args: A) => void) & Signal<A>; | ||
// } | ||
export function asEvents(x) { | ||
return x; | ||
} | ||
export function once(ev, onValue) { | ||
return new Promise(function (res, rej) { | ||
var unsub = ev.subscribe(function (x) { | ||
try { | ||
onValue === null || onValue === void 0 ? void 0 : onValue(x); | ||
res(x); | ||
} | ||
catch (e) { | ||
rej(e); | ||
} | ||
finally { | ||
unsub(); | ||
} | ||
}); | ||
}); | ||
} |
@@ -43,2 +43,3 @@ import { StrKeyOf, DeepReadonly, DeepPartial } from "./types"; | ||
export declare function diff<T>(a: ReadonlyArray<T>, b: ReadonlyArray<T>, { getId, enter, exit, update }: DiffOptions<T>): void; | ||
export declare function overwrite<T extends object>(x: T, fn: (x: T) => Partial<T>): T; | ||
export {}; |
@@ -143,1 +143,6 @@ var __assign = (this && this.__assign) || function () { | ||
} | ||
export function overwrite(x, fn) { | ||
var snapshot = Object.defineProperties({}, Object.getOwnPropertyDescriptors(x)); | ||
var newProps = Object.getOwnPropertyDescriptors(fn(snapshot)); | ||
return Object.defineProperties(x, newProps); | ||
} |
@@ -0,5 +1,23 @@ | ||
export declare type Unsubscribe = () => void; | ||
export declare type Listener<T> = (x: T) => void; | ||
export declare function signal<T = void>(): { | ||
export interface Event<T> { | ||
subscribe(fn: Listener<T>): Unsubscribe; | ||
} | ||
export interface Signal<T> extends Event<T> { | ||
(x: T): void; | ||
subscribe(fn: Listener<T>): () => void; | ||
event(): Event<T>; | ||
} | ||
export declare function signal<T = void>({ listeners, emit, subscribe, }?: { | ||
listeners?: Set<Listener<T>> | undefined; | ||
emit?: ((fn: (x: T) => void) => (x: T) => void) | undefined; | ||
subscribe?: ((fn: (x: Listener<T>) => Unsubscribe) => (x: Listener<T>) => Unsubscribe) | undefined; | ||
}): Signal<T>; | ||
declare type Signals<T> = { | ||
[P in keyof T]: T[P] extends Signal<infer S> ? Signal<S> : Signals<T[P]>; | ||
}; | ||
declare type Events<T> = { | ||
[P in keyof T]: T[P] extends Signal<infer S> ? Event<S> : Events<T[P]>; | ||
}; | ||
export declare function asEvents<T extends Signals<T>>(x: T): Events<T>; | ||
export declare function once<T>(ev: Event<T>, onValue?: Listener<T>): Promise<T>; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.signal = void 0; | ||
function signal() { | ||
var subs = new Set(); | ||
var self = function (x) { return subs.forEach(function (s) { return s(x); }); }; | ||
self.subscribe = function (fn) { | ||
subs.add(fn); | ||
return function () { | ||
subs.delete(fn); | ||
}; | ||
}; | ||
exports.once = exports.asEvents = exports.signal = void 0; | ||
function signal(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.listeners, listeners = _c === void 0 ? new Set() : _c, _d = _b.emit, emit = _d === void 0 ? function (fn) { return fn; } : _d, _e = _b.subscribe, subscribe = _e === void 0 ? function (fn) { return fn; } : _e; | ||
var self = emit(function (x) { | ||
var errors = []; | ||
listeners.forEach(function (fn) { | ||
try { | ||
fn(x); | ||
} | ||
catch (e) { | ||
errors.push(e); | ||
} | ||
}); | ||
if (errors.length) | ||
throw new AggregateError(errors); | ||
}); | ||
self.subscribe = subscribe(function (fn) { | ||
listeners.add(fn); | ||
return function () { return listeners.delete(fn); }; | ||
}); | ||
self.event = function () { return self; }; | ||
return self; | ||
} | ||
exports.signal = signal; | ||
// todo: signal improvement (multi args + customizable) | ||
// export type Listener<A extends any[]> = (...args: A) => void; | ||
// export type Signal<A extends any[]> = { | ||
// subscribe(fn: Listener<A>): () => void; | ||
// }; | ||
// export function signal<A extends any[] = []>({ | ||
// listeners = new Set<any>(), | ||
// emit = (fn: (...args: A) => void) => fn, | ||
// subscribe = (fn: (fn: Listener<A>) => () => void) => fn, | ||
// } = {}) { | ||
// const self: any = emit((...args: A) => | ||
// listeners.forEach((s) => s(...args)) | ||
// ); | ||
// self.subscribe = subscribe((fn: Listener<A>) => { | ||
// listeners.add(fn); | ||
// return () => { | ||
// listeners.delete(fn); | ||
// }; | ||
// }); | ||
// return self as ((...args: A) => void) & Signal<A>; | ||
// } | ||
function asEvents(x) { | ||
return x; | ||
} | ||
exports.asEvents = asEvents; | ||
function once(ev, onValue) { | ||
return new Promise(function (res, rej) { | ||
var unsub = ev.subscribe(function (x) { | ||
try { | ||
onValue === null || onValue === void 0 ? void 0 : onValue(x); | ||
res(x); | ||
} | ||
catch (e) { | ||
rej(e); | ||
} | ||
finally { | ||
unsub(); | ||
} | ||
}); | ||
}); | ||
} | ||
exports.once = once; |
@@ -43,2 +43,3 @@ import { StrKeyOf, DeepReadonly, DeepPartial } from "./types"; | ||
export declare function diff<T>(a: ReadonlyArray<T>, b: ReadonlyArray<T>, { getId, enter, exit, update }: DiffOptions<T>): void; | ||
export declare function overwrite<T extends object>(x: T, fn: (x: T) => Partial<T>): T; | ||
export {}; |
@@ -23,3 +23,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.diff = exports.flatMap = exports.isPlainObject = exports.assert = exports.namespace = exports.join = exports.mergeDeep = exports.merge = exports.deepReadonly = exports.call = exports.createFactoryAs = exports.createFactory = exports.getter = exports.objectTransform = exports.objectMap = exports.objectFrom = exports.shallowEqual = exports.arraysEqual = exports.range = exports.id = exports.noop = void 0; | ||
exports.overwrite = exports.diff = exports.flatMap = exports.isPlainObject = exports.assert = exports.namespace = exports.join = exports.mergeDeep = exports.merge = exports.deepReadonly = exports.call = exports.createFactoryAs = exports.createFactory = exports.getter = exports.objectTransform = exports.objectMap = exports.objectFrom = exports.shallowEqual = exports.arraysEqual = exports.range = exports.id = exports.noop = void 0; | ||
var noop = function () { }; | ||
@@ -168,1 +168,7 @@ exports.noop = noop; | ||
exports.diff = diff; | ||
function overwrite(x, fn) { | ||
var snapshot = Object.defineProperties({}, Object.getOwnPropertyDescriptors(x)); | ||
var newProps = Object.getOwnPropertyDescriptors(fn(snapshot)); | ||
return Object.defineProperties(x, newProps); | ||
} | ||
exports.overwrite = overwrite; |
{ | ||
"name": "@dhmk/utils", | ||
"version": "4.2.2", | ||
"version": "4.3.0", | ||
"description": "A collection of frequently used functions and primitives", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
125109
2890