Skip to content

Commit

Permalink
refactor(atom): update INotify impls, update History
Browse files Browse the repository at this point in the history
- extract EVENT_XXX constants from History
- add INotify generics
  • Loading branch information
postspectacular committed Jul 25, 2023
1 parent af8ffb3 commit 1ad68f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
14 changes: 12 additions & 2 deletions packages/atom/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type {
EVENT_ALL,
FnO,
IClear,
IDeref,
IID,
INotify,
IRelease,
IWatch,
OptPathVal,
Expand Down Expand Up @@ -135,7 +135,7 @@ export interface CursorOpts<T> {
id: string;
}

export interface IHistory<T> extends IAtom<T>, IClear, INotify {
export interface IHistory<T> extends IAtom<T>, IClear {
canUndo(): boolean;
canRedo(): boolean;

Expand All @@ -144,3 +144,13 @@ export interface IHistory<T> extends IAtom<T>, IClear, INotify {

record(): void;
}

export const EVENT_UNDO = "undo";
export const EVENT_REDO = "redo";
export const EVENT_RECORD = "record";

export type HistoryEventType =
| typeof EVENT_RECORD
| typeof EVENT_UNDO
| typeof EVENT_REDO
| typeof EVENT_ALL;
29 changes: 17 additions & 12 deletions packages/atom/src/history.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
DeepPath,
Event,
INotify,
Listener,
OptPathVal,
Path,
Expand All @@ -22,7 +23,15 @@ import { equiv } from "@thi.ng/equiv";
import { defGetterUnsafe } from "@thi.ng/paths/getter";
import { setInUnsafe } from "@thi.ng/paths/set-in";
import { updateInUnsafe } from "@thi.ng/paths/update-in";
import type { IAtom, IHistory, SwapFn } from "./api.js";
import {
EVENT_RECORD,
EVENT_REDO,
EVENT_UNDO,
type HistoryEventType,
type IAtom,
type IHistory,
type SwapFn,
} from "./api.js";

export const defHistory = <T>(
state: IAtom<T>,
Expand All @@ -44,11 +53,7 @@ export const defHistory = <T>(
* {@link History.redo} and {@link History.record}.
*/
@INotifyMixin
export class History<T> implements IHistory<T> {
static readonly EVENT_UNDO = "undo";
static readonly EVENT_REDO = "redo";
static readonly EVENT_RECORD = "record";

export class History<T> implements IHistory<T>, INotify<HistoryEventType> {
state: IAtom<T>;
maxLen: number;
changed: Predicate2<T>;
Expand Down Expand Up @@ -113,7 +118,7 @@ export class History<T> implements IHistory<T> {
const prev = this.state.deref();
this.future.push(prev);
const curr = this.state.reset(this.history.pop()!);
this.notify({ id: History.EVENT_UNDO, value: { prev, curr } });
this.notify({ id: EVENT_UNDO, value: { prev, curr } });
return curr;
}
}
Expand All @@ -139,7 +144,7 @@ export class History<T> implements IHistory<T> {
const prev = this.state.deref();
this.history.push(prev);
const curr = this.state.reset(this.future.pop()!);
this.notify({ id: History.EVENT_REDO, value: { prev, curr } });
this.notify({ id: EVENT_REDO, value: { prev, curr } });
return curr;
}
}
Expand Down Expand Up @@ -317,7 +322,7 @@ export class History<T> implements IHistory<T> {
history.shift();
}
history.push(state!);
this.notify({ id: History.EVENT_RECORD, value: state });
this.notify({ id: EVENT_RECORD, value: state });
this.future.length = 0;
}
}
Expand Down Expand Up @@ -369,13 +374,13 @@ export class History<T> implements IHistory<T> {

/** {@inheritDoc @thi.ng/api#INotify.addListener} */
// @ts-ignore: mixin
addListener(id: string, fn: Listener, scope?: any): boolean {}
addListener(id: HistoryEventType, fn: Listener, scope?: any): boolean {}

/** {@inheritDoc @thi.ng/api#INotify.removeListener} */
// @ts-ignore: mixin
removeListener(id: string, fn: Listener, scope?: any): boolean {}
removeListener(id: HistoryEventType, fn: Listener, scope?: any): boolean {}

/** {@inheritDoc @thi.ng/api#INotify.notify} */
// @ts-ignore: mixin
notify(e: Event): boolean {}
notify(e: Event<HistoryEventType>): boolean {}
}

0 comments on commit 1ad68f6

Please sign in to comment.