Skip to content

Commit

Permalink
refactor(atom): update type usage
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 19, 2024
1 parent 6274bf1 commit 1e10e5a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
11 changes: 6 additions & 5 deletions packages/atom/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
IID,
IRelease,
IWatch,
Maybe,
OptPathVal,
Path,
Path0,
Expand Down Expand Up @@ -122,11 +123,11 @@ export interface ISwap<T> {
swapInUnsafe(path: Path, fn: SwapFn<any, any>, ...args: any[]): T;
}

export interface IView<T> extends IDeref<T | undefined>, IID<string>, IRelease {
export interface IView<T> extends IDeref<Maybe<T>>, IID<string>, IRelease {
readonly path: Path;
readonly value: T | undefined;
readonly value: Maybe<T>;

view(): T | undefined;
view(): Maybe<T>;
changed(): boolean;
}

Expand All @@ -139,8 +140,8 @@ export interface IHistory<T> extends IAtom<T>, IClear {
canUndo(): boolean;
canRedo(): boolean;

undo(): T | undefined;
redo(): T | undefined;
undo(): Maybe<T>;
redo(): Maybe<T>;

record(): void;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/atom/src/atom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
DeepPath,
IEquiv,
Maybe,
OptPathVal,
Path,
Path0,
Expand Down Expand Up @@ -32,7 +33,7 @@ export const defAtom = <T>(value: T, valid?: Predicate<T>) =>
@IWatchMixin
export class Atom<T> implements IAtom<T>, IEquiv {
protected _value: T;
protected valid: Predicate<T> | undefined;
protected valid: Maybe<Predicate<T>>;
protected _watches: any;

constructor(val: T, valid?: Predicate<T>) {
Expand Down
3 changes: 2 additions & 1 deletion packages/atom/src/transacted.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
DeepPath,
Fn,
Maybe,
OptPathVal,
Path,
Path0,
Expand Down Expand Up @@ -65,7 +66,7 @@ export const updateAsTransaction = <T>(

export class Transacted<T> implements IAtom<T> {
parent: IAtom<T>;
current: T | undefined;
current: Maybe<T>;
protected id: string;
protected isActive: boolean;
protected _watches: any;
Expand Down
3 changes: 2 additions & 1 deletion packages/atom/src/view.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
DeepPath,
Fn,
Maybe,
OptPathVal,
Path,
Path0,
Expand Down Expand Up @@ -169,7 +170,7 @@ export class View<T> implements IView<T> {
readonly parent: ReadonlyAtom<any>;
readonly path: Path;

protected state: T | undefined;
protected state: Maybe<T>;
protected tx: Fn<any, T>;
protected unprocessed: any;
protected isDirty: boolean;
Expand Down
5 changes: 3 additions & 2 deletions packages/atom/test/view.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Maybe } from "@thi.ng/api";
import { beforeEach, expect, test } from "bun:test";
import {
View,
Expand Down Expand Up @@ -92,7 +93,7 @@ test("can be released", () => {
});

test("is lazy by default", () => {
let x: number | undefined;
let x: Maybe<number>;
v = defView(a, ["b", "c"], (y) => ((x = y), y * 10));
expect(x).toBeUndefined();
expect(v.deref()).toBe(20);
Expand All @@ -103,7 +104,7 @@ test("is lazy by default", () => {
});

test("can be eager", () => {
let x: number | undefined;
let x: Maybe<number>;
v = defView(a, ["b", "c"], (y) => ((x = y), y * 10), false);
expect(x).toBe(2);
expect(v.deref()).toBe(20);
Expand Down

0 comments on commit 1e10e5a

Please sign in to comment.