Skip to content

Commit e64b382

Browse files
fix: avoid circular dependency b/w utils.ts and atom.ts
1 parent 15aca7c commit e64b382

File tree

12 files changed

+45
-41
lines changed

12 files changed

+45
-41
lines changed

docs/classes/atom.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> of</h3>
113113
<li class="tsd-description">
114114
<aside class="tsd-sources">
115115
<ul>
116-
<li>Defined in atom.ts:29</li>
116+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/atom.ts#L29">atom.ts:29</a></li>
117117
</ul>
118118
</aside>
119119
<div class="tsd-comment tsd-typography">

docs/globals.html

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ <h3>Atom<wbr>State</h3>
105105
<div class="tsd-signature tsd-kind-icon">Atom<wbr>State<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">AtomState&lt;A&gt;</span></div>
106106
<aside class="tsd-sources">
107107
<ul>
108-
<li>Defined in internal-types.ts:32</li>
108+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/internal-types.ts#L32">internal-types.ts:32</a></li>
109109
</ul>
110110
</aside>
111111
<div class="tsd-comment tsd-typography">
@@ -128,7 +128,7 @@ <h3>Deep<wbr>Immutable</h3>
128128
<div class="tsd-signature tsd-kind-icon">Deep<wbr>Immutable<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">T</span><span class="tsd-signature-symbol"> &amp; </span><span class="tsd-signature-type">T extends Primitive ? T : T extends (infer U)[] ? DeepImmutableArray&lt;U&gt; : T extends Map&lt;infer K, infer V&gt; ? DeepImmutableMap&lt;K, V&gt; : DeepImmutableObject&lt;T&gt;</span></div>
129129
<aside class="tsd-sources">
130130
<ul>
131-
<li>Defined in internal-types.ts:49</li>
131+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/internal-types.ts#L49">internal-types.ts:49</a></li>
132132
</ul>
133133
</aside>
134134
<div class="tsd-comment tsd-typography">
@@ -150,7 +150,7 @@ <h3>add<wbr>Change<wbr>Handler</h3>
150150
<li class="tsd-description">
151151
<aside class="tsd-sources">
152152
<ul>
153-
<li>Defined in changeHandler.ts:29</li>
153+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/changeHandler.ts#L29">changeHandler.ts:29</a></li>
154154
</ul>
155155
</aside>
156156
<div class="tsd-comment tsd-typography">
@@ -231,7 +231,7 @@ <h3>deref</h3>
231231
<li class="tsd-description">
232232
<aside class="tsd-sources">
233233
<ul>
234-
<li>Defined in deref.ts:22</li>
234+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/deref.ts#L22">deref.ts:22</a></li>
235235
</ul>
236236
</aside>
237237
<div class="tsd-comment tsd-typography">
@@ -279,7 +279,7 @@ <h3>get<wbr>Validator</h3>
279279
<li class="tsd-description">
280280
<aside class="tsd-sources">
281281
<ul>
282-
<li>Defined in getValidator.ts:24</li>
282+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/getValidator.ts#L24">getValidator.ts:24</a></li>
283283
</ul>
284284
</aside>
285285
<div class="tsd-comment tsd-typography">
@@ -327,7 +327,7 @@ <h3>remove<wbr>Change<wbr>Handler</h3>
327327
<li class="tsd-description">
328328
<aside class="tsd-sources">
329329
<ul>
330-
<li>Defined in changeHandler.ts:65</li>
330+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/changeHandler.ts#L65">changeHandler.ts:65</a></li>
331331
</ul>
332332
</aside>
333333
<div class="tsd-comment tsd-typography">
@@ -389,7 +389,7 @@ <h3>set</h3>
389389
<li class="tsd-description">
390390
<aside class="tsd-sources">
391391
<ul>
392-
<li>Defined in set.ts:28</li>
392+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/set.ts#L29">set.ts:29</a></li>
393393
</ul>
394394
</aside>
395395
<div class="tsd-comment tsd-typography">
@@ -447,7 +447,7 @@ <h3>set<wbr>Validator</h3>
447447
<li class="tsd-description">
448448
<aside class="tsd-sources">
449449
<ul>
450-
<li>Defined in setValidator.ts:30</li>
450+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/setValidator.ts#L30">setValidator.ts:30</a></li>
451451
</ul>
452452
</aside>
453453
<div class="tsd-comment tsd-typography">
@@ -505,7 +505,7 @@ <h3>swap</h3>
505505
<li class="tsd-description">
506506
<aside class="tsd-sources">
507507
<ul>
508-
<li>Defined in swap.ts:25</li>
508+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/swap.ts#L27">swap.ts:27</a></li>
509509
</ul>
510510
</aside>
511511
<div class="tsd-comment tsd-typography">
@@ -517,6 +517,7 @@ <h3>swap</h3>
517517
<dt>example</dt>
518518
<dd><pre><code class="language-jsx">
519519
<span class="hljs-keyword">import</span> {Atom, swap} <span class="hljs-keyword">from</span> <span class="hljs-string">'@libre/atom'</span>
520+
<span class="hljs-keyword">import</span> {prettyPrint} <span class="hljs-keyword">from</span> <span class="hljs-string">'./prettyPrint'</span>
520521

521522
<span class="hljs-keyword">const</span> stateAtom = Atom.of({ <span class="hljs-attr">count</span>: <span class="hljs-number">0</span> })
522523
<span class="hljs-keyword">const</span> increment = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> swap(stateAtom, (state) =&gt; ({

docs/interfaces/atomconstructoroptions.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ <h3><span class="tsd-flag ts-flagOptional">Optional</span> validator</h3>
115115
<li class="tsd-description">
116116
<aside class="tsd-sources">
117117
<ul>
118-
<li>Defined in internal-types.ts:16</li>
118+
<li>Defined in <a href="https://github.com/libre-org/atom/blob/15aca7c/src/internal-types.ts#L16">internal-types.ts:16</a></li>
119119
</ul>
120120
</aside>
121121
<div class="tsd-comment tsd-typography">

src/atom.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { _getState, _initChangeHandlerDict, _setState, _setValidator, _useNextAtomId } from "./internal-state";
22
import { AtomConstructorOptions, DeepImmutable } from "./internal-types";
3-
import { _prettyPrint } from "./utils";
3+
import { prettyPrint } from "./prettyPrint";
44

55
/**
66
* A data structure useful for providing a controlled, predictable mechanism for mutability.
@@ -37,7 +37,7 @@ const a3 = Atom.of({ count: 0 })
3737
private constructor(state: S, { validator }: AtomConstructorOptions<S> = {}) {
3838
validator = validator || (() => true);
3939
if (!validator(state as DeepImmutable<S>)) {
40-
const errMsg = `Atom initialized with invalid state:\n\n${_prettyPrint(
40+
const errMsg = `Atom initialized with invalid state:\n\n${prettyPrint(
4141
state
4242
)}\n\naccording to validator function:\n${validator}\n\n`;
4343
const err = Error(errMsg);
@@ -53,7 +53,7 @@ const a3 = Atom.of({ count: 0 })
5353
}
5454
/** @ignore */
5555
public toString(): string {
56-
return `Atom<${_prettyPrint(_getState(this))}>`;
56+
return `Atom<${prettyPrint(_getState(this))}>`;
5757
}
5858
/** @ignore */
5959
public inspect(): string {

src/deref.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Atom } from "./atom";
22
import { _getState } from "./internal-state";
33
import { DeepImmutable } from "./internal-types";
4-
import { _throwIfNotAtom } from "./utils";
4+
import { throwIfNotAtom } from "./throwIfNotAtom";
55

66
/**
77
* Dereferences (i.e. "*reads*") the current state of an [[Atom]]. The dereferenced value
@@ -20,6 +20,6 @@ deref(stateAtom) // => { count: 0 }
2020
```
2121
*/
2222
export function deref<S>(atom: Atom<S>): DeepImmutable<S> {
23-
_throwIfNotAtom(atom);
23+
throwIfNotAtom(atom);
2424
return _getState(atom) as DeepImmutable<S>;
2525
}

src/getValidator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Atom } from "./atom";
22
import { _getValidator } from "./internal-state";
33
import { AtomConstructorOptions } from "./internal-types";
44

5-
import { _throwIfNotAtom } from "./utils";
5+
import { throwIfNotAtom } from "./throwIfNotAtom";
66

77
/**
88
* Gets `atom`'s validator function
@@ -22,6 +22,6 @@ validator({ count: 2 }) // => true
2222
*/
2323

2424
export function getValidator<S>(atom: Atom<S>): NonNullable<AtomConstructorOptions<any>["validator"]> {
25-
_throwIfNotAtom(atom);
25+
throwIfNotAtom(atom);
2626
return _getValidator(atom);
2727
}

src/prettyPrint.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @ignore */
2+
export function prettyPrint(val: any): string {
3+
return JSON.stringify(val, null, " ");
4+
}

src/set.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { Atom } from "./atom";
22
import { deref } from "./deref";
33
import { _getValidator, _runChangeHandlers, _setState } from "./internal-state";
44
import { DeepImmutable } from "./internal-types";
5-
import { _prettyPrint, _throwIfNotAtom } from "./utils";
5+
import { prettyPrint } from "./prettyPrint";
6+
import { throwIfNotAtom } from "./throwIfNotAtom";
67

78
/**
89
* Sets `atom`s state to `nextState`.
@@ -26,11 +27,11 @@ deref(atom) // => { count: 100 }
2627
*/
2728

2829
export function set<S>(atom: Atom<S>, nextState: S): void {
29-
_throwIfNotAtom(atom);
30+
throwIfNotAtom(atom);
3031
const validator = _getValidator(atom);
3132
const didValidate = validator(nextState as DeepImmutable<S>);
3233
if (!didValidate) {
33-
const errMsg = `Attempted to set the state of\n\n${atom}\n\nwith:\n\n${_prettyPrint(
34+
const errMsg = `Attempted to set the state of\n\n${atom}\n\nwith:\n\n${prettyPrint(
3435
nextState
3536
)}\n\nbut it did not pass validator:\n${validator}\n\n`;
3637
const err = Error(errMsg);

src/setValidator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Atom } from "./atom";
22
import { _getState, _setValidator } from "./internal-state";
33
import { AtomConstructorOptions } from "./internal-types";
4-
import { _throwIfNotAtom } from "./utils";
4+
import { throwIfNotAtom } from "./throwIfNotAtom";
55

66
/**
77
* Sets the `validator` for `atom`. `validator` must be a pure function of one argument,
@@ -28,7 +28,7 @@ set(atom, {count: 2}) // All good
2828
*/
2929

3030
export function setValidator<S>(atom: Atom<S>, validator: NonNullable<AtomConstructorOptions<any>["validator"]>): void {
31-
_throwIfNotAtom(atom);
31+
throwIfNotAtom(atom);
3232
if (!validator(_getState(atom))) {
3333
const errMsg = `Could not set validator on\n\n${atom}\n\nbecause current state would be invalid according to new validator:\n${validator}\n\n`;
3434
const err = Error(errMsg);

src/swap.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Atom } from "./atom";
22
import { _getState, _getValidator, _runChangeHandlers, _setState } from "./internal-state";
33
import { DeepImmutable } from "./internal-types";
4-
import { _prettyPrint, _throwIfNotAtom } from "./utils";
4+
import { prettyPrint } from "./prettyPrint";
5+
import { throwIfNotAtom } from "./throwIfNotAtom";
56

67
/**
78
* Swaps `atom`'s state with the value returned from applying `updateFn` to `atom`'s
@@ -15,6 +16,7 @@ import { _prettyPrint, _throwIfNotAtom } from "./utils";
1516
* ```jsx
1617
*
1718
*import {Atom, swap} from '@libre/atom'
19+
import {prettyPrint} from './prettyPrint'
1820
*
1921
*const stateAtom = Atom.of({ count: 0 })
2022
*const increment = () => swap(stateAtom, (state) => ({
@@ -23,13 +25,13 @@ import { _prettyPrint, _throwIfNotAtom } from "./utils";
2325
* ```
2426
*/
2527
export function swap<S>(atom: Atom<S>, updateFn: (state: S) => S): void {
26-
_throwIfNotAtom(atom);
28+
throwIfNotAtom(atom);
2729
const prevState = _getState(atom);
2830
const nextState = updateFn(prevState);
2931
const validator = _getValidator(atom);
3032
const didValidate = validator(nextState as DeepImmutable<S>);
3133
if (!didValidate) {
32-
const errMsg = `swap updateFn\n${updateFn}\n\nattempted to swap the state of\n\n${atom}\n\nwith:\n\n${_prettyPrint(
34+
const errMsg = `swap updateFn\n${updateFn}\n\nattempted to swap the state of\n\n${atom}\n\nwith:\n\n${prettyPrint(
3335
nextState
3436
)}\n\nbut it did not pass validator:\n${validator}\n\n`;
3537
const err = Error(errMsg);

0 commit comments

Comments
 (0)