Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add presentMut lens for always replacing the current history item instead of appending #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Undo-Redo history. Basic features:
* [`H.init({[maxCount, pushEquals, replacePeriod]}, value) ~> history`](#H-init) <small><sup>v0.1.0</sup></small>
* [Present](#present)
* [`H.present ~> valueLens`](#H-present) <small><sup>v0.2.0</sup></small>
* [`H.presentReplace ~> valueLens`](#H-presentReplace) <small><sup>v1.3.0</sup></small>
* [Undo](#undo)
* [`H.undoForget(history) ~> history`](#H-undoForget) <small><sup>v0.1.0</sup></small>
* [`H.undoIndex ~> numberLens`](#H-undoIndex) <small><sup>v0.2.0</sup></small>
Expand Down Expand Up @@ -285,6 +286,26 @@ thru(
Note that modifications through `H.present` are not referentially transparent
operations, because setting through `H.present` takes a timestamp underneath.

#### <a id="H-presentReplace"></a> [≡](#contents) [▶](https://calmm-js.github.io/partial.lenses.history/index.html#H-presentReplace) [`H.presentReplace ~> valueLens`](#H-presentReplace) <small><sup>v1.3.0</sup></small>

`H.presentReplace` is a
[lens](https://github.com/calmm-js/partial.lenses/#partial-lenses) that focuses
on the present value of history. When read it behaves exactly as [`H.present`](#H-present). When written it does *not* create a new history entry but instead replaces the current one. This might be useful for applying multiple changes in squence without polluting the history.

For example:

```js
thru(
H.init({}, 42),
L.modify(H.presentReplace, x => -x),
L.get(H.undoIndex)
)
// 0
```

Note that modifications through `H.presentReplace` are not referentially transparent
operations, because setting through `H.presentReplace` takes a timestamp underneath.

### <a id="undo"></a> [≡](#contents) [▶](https://calmm-js.github.io/partial.lenses.history/index.html#undo) [Undo](#undo)

#### <a id="H-undoForget"></a> [≡](#contents) [▶](https://calmm-js.github.io/partial.lenses.history/index.html#H-undoForget) [`H.undoForget(history) ~> history`](#H-undoForget) <small><sup>v0.1.0</sup></small>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "partial.lenses.history",
"version": "1.2.0",
"version": "1.3.0",
"description": "Partial Lenses History is a JavaScript library for Undo-Redo",
"module": "dist/partial.lenses.history.es.js",
"main": "dist/partial.lenses.history.cjs.js",
Expand Down
24 changes: 24 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ function setPresentU(value, history) {
)
}

function setPresentReplaceU(value, history) {
const v = history.v
const i = history.i
const c = history.c
if (c.e) {
if (I.acyclicEqualsU(S.nth(i, v), value)) {
return history
}
}
const t = history.t
const now = Date.now()
const j0 = Math.max(0, i - c.m)
return construct(
i - j0,
S.append(now, S.slice(j0, i, t)),
S.append(value, S.slice(j0, i, v)),
c
)
}

const setIndexU = (index, history) =>
construct(
Math.max(0, Math.min(index, indexMax(history))),
Expand Down Expand Up @@ -69,6 +89,10 @@ export const present = L.lens(function present(history) {
return S.nth(history.i, history.v)
}, setPresentU)

export const presentReplace = L.lens(function presentReplace(history) {
return S.nth(history.i, history.v)
}, setPresentReplaceU)

// Undo

export {index as undoIndex}
Expand Down
1 change: 1 addition & 0 deletions src/partial.lenses.history.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const init = C(
// Present

export const present = C(H.present, lens(history, V.any))
export const presentReplace = C(H.presentReplace, lens(history, V.any))

// Undo

Expand Down
13 changes: 13 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ describe('History', () => {
)
})

testEq([1, 2, 5, 6], async () => {
let h = H.init({pushEquals: true}, 1)
h = L.set(H.present, 2, h)
h = L.set(H.present, 3, h)
h = L.set(H.presentReplace, 4, h)
h = L.set(H.presentReplace, 5, h)
h = L.set(H.present, 6, h)
return R.map(
i => L.get(H.present, L.set(H.index, i, h)),
R.range(0, H.count(h))
)
})

testEq(16000, () => {
let h = H.init({maxCount: 5000}, 1)
for (let i = 2; i < 20000; ++i) h = L.set(H.present, i, h)
Expand Down