@dhmk/atom
Advanced tools
Comparing version 0.0.1 to 1.0.0
@@ -26,6 +26,2 @@ export declare class _Atom<T> { | ||
} | ||
export declare const isAtom: (x: any) => any; | ||
export declare const isValue: (x: any) => x is Atom<any>; | ||
export declare const isComputed: (x: any) => x is Atom<any>; | ||
export declare const isObserve: (x: any) => x is Observer; | ||
export declare function atom<T>(fn: () => T, options?: AtomOptions<T>): Atom<T>; | ||
@@ -48,4 +44,5 @@ export declare function atom<T>(x: T, options?: AtomOptions<T>): WritableAtom<T>; | ||
declare type ValueNotChanged = false; | ||
declare type Setter<T> = (value: T) => ValueChanged | ValueNotChanged; | ||
export interface WritableAtom<T> extends Atom<T> { | ||
set(value: T): ValueChanged | ValueNotChanged; | ||
set: Setter<T>; | ||
} | ||
@@ -62,2 +59,3 @@ export interface Observer { | ||
onBecomeUnobserved: () => void; | ||
setter: (originalSetter: Setter<T>) => Setter<T>; | ||
}>>; | ||
@@ -64,0 +62,0 @@ export declare type ObserverOptions = Readonly<Partial<{ |
@@ -262,17 +262,17 @@ // global state | ||
export { _Atom }; | ||
export var isAtom = function (x) { return x && x.atom instanceof _Atom; }; | ||
export var isValue = function (x) { return isAtom(x) && !x.atom.fn; }; | ||
export var isComputed = function (x) { | ||
return isAtom(x) && !!x.atom.fn && !x.atom.isSideEffect; | ||
}; | ||
export var isObserve = function (x) { | ||
return isAtom(x) && !!x.atom.fn && x.atom.isSideEffect; | ||
}; | ||
// export const isAtom = (x: any) => x && x.atom instanceof _Atom; | ||
// export const isValue = (x: any): x is Atom<any> => isAtom(x) && !x.atom.fn; | ||
// export const isComputed = (x: any): x is Atom<any> => | ||
// isAtom(x) && !!x.atom.fn && !x.atom.isSideEffect; | ||
// export const isObserve = (x: any): x is Observer => | ||
// isAtom(x) && !!x.atom.fn && x.atom.isSideEffect; | ||
var id = function (x) { return x; }; | ||
export function atom(arg, opts) { | ||
var a = typeof arg === "function" | ||
var isComputed = typeof arg === "function"; | ||
var a = isComputed | ||
? new _Atom(undefined, arg, false, opts) | ||
: new _Atom(arg, undefined, false, opts); | ||
var self = a.get.bind(a); | ||
if (a.set) | ||
self.set = a.set.bind(a); | ||
if (!isComputed) | ||
self.set = ((opts === null || opts === void 0 ? void 0 : opts.setter) || id)(a.set.bind(a)); | ||
self.get = self.toString = self.toJSON = self.valueOf = self; | ||
@@ -279,0 +279,0 @@ self.atom = a; // for is...() checks |
@@ -1,2 +0,2 @@ | ||
import { atom, isAtom, observe, runInAction } from "./atom"; | ||
import { atom, observe, runInAction } from "./atom"; | ||
describe("atom", function () { | ||
@@ -6,4 +6,2 @@ test("basic", function () { | ||
var b = atom(function () { return a() + 1; }); | ||
expect(isAtom(a)).toEqual(true); | ||
expect(isAtom(b)).toEqual(true); | ||
expect(a()).toEqual(1); | ||
@@ -10,0 +8,0 @@ expect(b()).toEqual(2); |
@@ -1,2 +0,2 @@ | ||
import { WritableAtom, Atom, NotAtom } from "./atom"; | ||
import { WritableAtom, Atom, NotAtom, AtomOptions } from "./atom"; | ||
export interface ObjectAtom<T> extends WritableAtom<T> { | ||
@@ -6,3 +6,3 @@ merge(arg: Partial<T>): void; | ||
} | ||
export declare function objectAtom<T extends object>(value: T): ObjectAtom<T>; | ||
export declare function objectAtom<T extends object>(value: T, options?: AtomOptions<T>): ObjectAtom<T>; | ||
export interface ArrayAtom<T> extends WritableAtom<ReadonlyArray<T>> { | ||
@@ -16,3 +16,3 @@ set(value: ReadonlyArray<T>): boolean; | ||
} | ||
export declare function arrayAtom<T>(value?: ReadonlyArray<T>): ArrayAtom<T>; | ||
export declare function arrayAtom<T>(value?: ReadonlyArray<T>, options?: AtomOptions<ReadonlyArray<T>>): ArrayAtom<T>; | ||
export interface MapAtom<K, V> extends Atom<ReadonlyMap<K, V>> { | ||
@@ -24,3 +24,3 @@ set(value: ReadonlyMap<K, V>): boolean; | ||
} | ||
export declare function mapAtom<K, V>(value?: ReadonlyMap<K, V>): MapAtom<K, V>; | ||
export declare function mapAtom<K, V>(value?: ReadonlyMap<K, V>, options?: AtomOptions<ReadonlyMap<K, V>>): MapAtom<K, V>; | ||
export interface SetAtom<T> extends WritableAtom<ReadonlySet<T>> { | ||
@@ -31,4 +31,4 @@ add(v: T): void; | ||
} | ||
export declare function setAtom<T>(value?: ReadonlySet<T>): SetAtom<T>; | ||
export declare function asyncAtom<T = any>(fn: (prev: T) => Promise<T>, initial: T): Atom<T>; | ||
export declare function setAtom<T>(value?: ReadonlySet<T>, options?: AtomOptions<ReadonlySet<T>>): SetAtom<T>; | ||
export declare function asyncAtom<T = any>(fn: (prev: T) => Promise<T>, initial: T, options?: AtomOptions<T>): Atom<T>; | ||
export declare function asyncAtom<T = any>(fn: (prev?: T) => Promise<T>): Atom<T | undefined>; | ||
@@ -39,2 +39,6 @@ export declare function debouncedEvents(onBO: Function, onBUO: Function): { | ||
}; | ||
export declare const keepAlive: (a: Atom<any>) => Function; | ||
export declare const keepAlive: <T>(a: Atom<T>) => (() => T) & { | ||
readonly __$$unique_name$$__: "__$$unique_name$$__"; | ||
} & { | ||
dispose(): any; | ||
}; |
@@ -8,4 +8,4 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from) { | ||
import { atom, atTransactionEnd, observe, } from "./atom"; | ||
export function objectAtom(value) { | ||
var a = atom(value); | ||
export function objectAtom(value, options) { | ||
var a = atom(value, options); | ||
a.merge = function (what) { | ||
@@ -16,5 +16,5 @@ a.set(object.merge(a(), what)); | ||
} | ||
export function arrayAtom(value) { | ||
export function arrayAtom(value, options) { | ||
if (value === void 0) { value = []; } | ||
var a = atom(value); | ||
var a = atom(value, options); | ||
var _set = a.set; | ||
@@ -51,5 +51,5 @@ a.set = function () { | ||
} | ||
export function mapAtom(value) { | ||
export function mapAtom(value, options) { | ||
if (value === void 0) { value = new Map(); } | ||
var a = atom(value); | ||
var a = atom(value, options); | ||
var _set = a.set; | ||
@@ -72,5 +72,5 @@ a.set = function () { | ||
} | ||
export function setAtom(value) { | ||
export function setAtom(value, options) { | ||
if (value === void 0) { value = new Set(); } | ||
var a = atom(value); | ||
var a = atom(value, options); | ||
a.add = function (v) { return a.set(set.add(a(), v)); }; | ||
@@ -81,4 +81,4 @@ a.delete = function (v) { return a.set(set.delete(a(), v)); }; | ||
} | ||
export function asyncAtom(fn, initial) { | ||
var a = atom(initial); | ||
export function asyncAtom(fn, initial, options) { | ||
var a = atom(initial, options); | ||
var c = { cancelled: false }; | ||
@@ -117,3 +117,7 @@ return function () { | ||
} | ||
export var keepAlive = function (a) { return observe(function () { return a(); }); }; | ||
export var keepAlive = function (a) { | ||
var self = function () { return a(); }; | ||
self.dispose = observe(self); | ||
return self; | ||
}; | ||
// const mappedItems = atom(cacheMap(() => items, x => y)); | ||
@@ -120,0 +124,0 @@ // export function cacheMap(getArr, mapItem) { |
@@ -26,6 +26,2 @@ export declare class _Atom<T> { | ||
} | ||
export declare const isAtom: (x: any) => any; | ||
export declare const isValue: (x: any) => x is Atom<any>; | ||
export declare const isComputed: (x: any) => x is Atom<any>; | ||
export declare const isObserve: (x: any) => x is Observer; | ||
export declare function atom<T>(fn: () => T, options?: AtomOptions<T>): Atom<T>; | ||
@@ -48,4 +44,5 @@ export declare function atom<T>(x: T, options?: AtomOptions<T>): WritableAtom<T>; | ||
declare type ValueNotChanged = false; | ||
declare type Setter<T> = (value: T) => ValueChanged | ValueNotChanged; | ||
export interface WritableAtom<T> extends Atom<T> { | ||
set(value: T): ValueChanged | ValueNotChanged; | ||
set: Setter<T>; | ||
} | ||
@@ -62,2 +59,3 @@ export interface Observer { | ||
onBecomeUnobserved: () => void; | ||
setter: (originalSetter: Setter<T>) => Setter<T>; | ||
}>>; | ||
@@ -64,0 +62,0 @@ export declare type ObserverOptions = Readonly<Partial<{ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.asAtom = exports.deepReadonly = exports.atTransactionEnd = exports.runInAction = exports.untracked = exports.observe = exports.atom = exports.isObserve = exports.isComputed = exports.isValue = exports.isAtom = exports._Atom = void 0; | ||
exports.asAtom = exports.deepReadonly = exports.atTransactionEnd = exports.runInAction = exports.untracked = exports.observe = exports.atom = exports._Atom = void 0; | ||
// global state | ||
@@ -265,21 +265,17 @@ var g_pendingAtoms = []; | ||
exports._Atom = _Atom; | ||
var isAtom = function (x) { return x && x.atom instanceof _Atom; }; | ||
exports.isAtom = isAtom; | ||
var isValue = function (x) { return exports.isAtom(x) && !x.atom.fn; }; | ||
exports.isValue = isValue; | ||
var isComputed = function (x) { | ||
return exports.isAtom(x) && !!x.atom.fn && !x.atom.isSideEffect; | ||
}; | ||
exports.isComputed = isComputed; | ||
var isObserve = function (x) { | ||
return exports.isAtom(x) && !!x.atom.fn && x.atom.isSideEffect; | ||
}; | ||
exports.isObserve = isObserve; | ||
// export const isAtom = (x: any) => x && x.atom instanceof _Atom; | ||
// export const isValue = (x: any): x is Atom<any> => isAtom(x) && !x.atom.fn; | ||
// export const isComputed = (x: any): x is Atom<any> => | ||
// isAtom(x) && !!x.atom.fn && !x.atom.isSideEffect; | ||
// export const isObserve = (x: any): x is Observer => | ||
// isAtom(x) && !!x.atom.fn && x.atom.isSideEffect; | ||
var id = function (x) { return x; }; | ||
function atom(arg, opts) { | ||
var a = typeof arg === "function" | ||
var isComputed = typeof arg === "function"; | ||
var a = isComputed | ||
? new _Atom(undefined, arg, false, opts) | ||
: new _Atom(arg, undefined, false, opts); | ||
var self = a.get.bind(a); | ||
if (a.set) | ||
self.set = a.set.bind(a); | ||
if (!isComputed) | ||
self.set = ((opts === null || opts === void 0 ? void 0 : opts.setter) || id)(a.set.bind(a)); | ||
self.get = self.toString = self.toJSON = self.valueOf = self; | ||
@@ -286,0 +282,0 @@ self.atom = a; // for is...() checks |
@@ -8,4 +8,2 @@ "use strict"; | ||
var b = atom_1.atom(function () { return a() + 1; }); | ||
expect(atom_1.isAtom(a)).toEqual(true); | ||
expect(atom_1.isAtom(b)).toEqual(true); | ||
expect(a()).toEqual(1); | ||
@@ -12,0 +10,0 @@ expect(b()).toEqual(2); |
@@ -1,2 +0,2 @@ | ||
import { WritableAtom, Atom, NotAtom } from "./atom"; | ||
import { WritableAtom, Atom, NotAtom, AtomOptions } from "./atom"; | ||
export interface ObjectAtom<T> extends WritableAtom<T> { | ||
@@ -6,3 +6,3 @@ merge(arg: Partial<T>): void; | ||
} | ||
export declare function objectAtom<T extends object>(value: T): ObjectAtom<T>; | ||
export declare function objectAtom<T extends object>(value: T, options?: AtomOptions<T>): ObjectAtom<T>; | ||
export interface ArrayAtom<T> extends WritableAtom<ReadonlyArray<T>> { | ||
@@ -16,3 +16,3 @@ set(value: ReadonlyArray<T>): boolean; | ||
} | ||
export declare function arrayAtom<T>(value?: ReadonlyArray<T>): ArrayAtom<T>; | ||
export declare function arrayAtom<T>(value?: ReadonlyArray<T>, options?: AtomOptions<ReadonlyArray<T>>): ArrayAtom<T>; | ||
export interface MapAtom<K, V> extends Atom<ReadonlyMap<K, V>> { | ||
@@ -24,3 +24,3 @@ set(value: ReadonlyMap<K, V>): boolean; | ||
} | ||
export declare function mapAtom<K, V>(value?: ReadonlyMap<K, V>): MapAtom<K, V>; | ||
export declare function mapAtom<K, V>(value?: ReadonlyMap<K, V>, options?: AtomOptions<ReadonlyMap<K, V>>): MapAtom<K, V>; | ||
export interface SetAtom<T> extends WritableAtom<ReadonlySet<T>> { | ||
@@ -31,4 +31,4 @@ add(v: T): void; | ||
} | ||
export declare function setAtom<T>(value?: ReadonlySet<T>): SetAtom<T>; | ||
export declare function asyncAtom<T = any>(fn: (prev: T) => Promise<T>, initial: T): Atom<T>; | ||
export declare function setAtom<T>(value?: ReadonlySet<T>, options?: AtomOptions<ReadonlySet<T>>): SetAtom<T>; | ||
export declare function asyncAtom<T = any>(fn: (prev: T) => Promise<T>, initial: T, options?: AtomOptions<T>): Atom<T>; | ||
export declare function asyncAtom<T = any>(fn: (prev?: T) => Promise<T>): Atom<T | undefined>; | ||
@@ -39,2 +39,6 @@ export declare function debouncedEvents(onBO: Function, onBUO: Function): { | ||
}; | ||
export declare const keepAlive: (a: Atom<any>) => Function; | ||
export declare const keepAlive: <T>(a: Atom<T>) => (() => T) & { | ||
readonly __$$unique_name$$__: "__$$unique_name$$__"; | ||
} & { | ||
dispose(): any; | ||
}; |
@@ -11,4 +11,4 @@ "use strict"; | ||
var atom_1 = require("./atom"); | ||
function objectAtom(value) { | ||
var a = atom_1.atom(value); | ||
function objectAtom(value, options) { | ||
var a = atom_1.atom(value, options); | ||
a.merge = function (what) { | ||
@@ -20,5 +20,5 @@ a.set(utils_1.object.merge(a(), what)); | ||
exports.objectAtom = objectAtom; | ||
function arrayAtom(value) { | ||
function arrayAtom(value, options) { | ||
if (value === void 0) { value = []; } | ||
var a = atom_1.atom(value); | ||
var a = atom_1.atom(value, options); | ||
var _set = a.set; | ||
@@ -56,5 +56,5 @@ a.set = function () { | ||
exports.arrayAtom = arrayAtom; | ||
function mapAtom(value) { | ||
function mapAtom(value, options) { | ||
if (value === void 0) { value = new Map(); } | ||
var a = atom_1.atom(value); | ||
var a = atom_1.atom(value, options); | ||
var _set = a.set; | ||
@@ -78,5 +78,5 @@ a.set = function () { | ||
exports.mapAtom = mapAtom; | ||
function setAtom(value) { | ||
function setAtom(value, options) { | ||
if (value === void 0) { value = new Set(); } | ||
var a = atom_1.atom(value); | ||
var a = atom_1.atom(value, options); | ||
a.add = function (v) { return a.set(utils_1.set.add(a(), v)); }; | ||
@@ -88,4 +88,4 @@ a.delete = function (v) { return a.set(utils_1.set.delete(a(), v)); }; | ||
exports.setAtom = setAtom; | ||
function asyncAtom(fn, initial) { | ||
var a = atom_1.atom(initial); | ||
function asyncAtom(fn, initial, options) { | ||
var a = atom_1.atom(initial, options); | ||
var c = { cancelled: false }; | ||
@@ -126,3 +126,7 @@ return function () { | ||
exports.debouncedEvents = debouncedEvents; | ||
var keepAlive = function (a) { return atom_1.observe(function () { return a(); }); }; | ||
var keepAlive = function (a) { | ||
var self = function () { return a(); }; | ||
self.dispose = atom_1.observe(self); | ||
return self; | ||
}; | ||
exports.keepAlive = keepAlive; | ||
@@ -129,0 +133,0 @@ // const mappedItems = atom(cacheMap(() => items, x => y)); |
{ | ||
"name": "@dhmk/atom", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "Lightweight mobx-like observable values, computed values and side-effects", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -27,3 +27,9 @@ # @dhmk-atom | ||
onBecomeUnobserved: () => void; | ||
setter: (originalSetter: Setter<T>) => Setter<T>; | ||
}; | ||
type Setter<T> = (value: T) => ValueChanged | ValueNotChanged; | ||
type ValueChanged = true; | ||
type ValueNotChanged = false; | ||
``` | ||
@@ -86,3 +92,3 @@ | ||
Runs `fn` in transaction. Returns `fn` result. Doesn't apply `untracked`. | ||
Runs `fn` in transaction. Returns `fn` result. Also applies `untracked`. | ||
@@ -96,1 +102,19 @@ ### `runInFlow(flow)` | ||
Runs `fn` at the end of the outermost transaction or immediately if is not in transaction. | ||
## Helpers | ||
### `keepAlive(computed: Atom<T>): Atom<T> & { dispose() }` | ||
Similar to MobX, if a computed atom isn't observed by anyone, its value is recomputed on every access. This function is a shorcut to `observe(computed)`. | ||
### `objectAtom()` | ||
### `arrayAtom()` | ||
### `mapAtom()` | ||
### `setAtom()` | ||
### `asyncAtom()` | ||
### `debouncedEvents()` |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
64725
1714
1
118