Skip to content

Commit f68448e

Browse files
Copy past and future states only if necessary (#112)
1 parent 1a171a4 commit f68448e

2 files changed

Lines changed: 8 additions & 9 deletions

File tree

__tests__/react.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ describe('React Re-renders when state changes', () => {
4444
});
4545
});
4646

47-
4847
// React Code from examples/web/pages/reactive.tsx
4948
import { TemporalState, temporal } from '../src';
5049
import { StoreApi, useStore, create } from 'zustand';

src/temporal.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export const createVanillaTemporal = <TState>(
1414
pastStates: options?.pastStates || [],
1515
futureStates: options?.futureStates || [],
1616
undo: (steps = 1) => {
17-
// Fastest way to clone an array on Chromium. Needed to create a new array reference
18-
const pastStates = get().pastStates.slice();
19-
const futureStates = get().futureStates.slice();
20-
if (pastStates.length) {
17+
if (get().pastStates.length) {
18+
// Fastest way to clone an array on Chromium. Needed to create a new array reference
19+
const pastStates = get().pastStates.slice();
20+
const futureStates = get().futureStates.slice();
2121
// Based on the steps, get values from the pastStates array and push them to the futureStates array
2222
while (steps--) {
2323
const pastState = pastStates.pop();
@@ -30,10 +30,10 @@ export const createVanillaTemporal = <TState>(
3030
}
3131
},
3232
redo: (steps = 1) => {
33-
// Fastest way to clone an array on Chromium. Needed to create a new array reference
34-
const pastStates = get().pastStates.slice();
35-
const futureStates = get().futureStates.slice();
36-
if (futureStates.length) {
33+
if (get().futureStates.length) {
34+
// Fastest way to clone an array on Chromium. Needed to create a new array reference
35+
const pastStates = get().pastStates.slice();
36+
const futureStates = get().futureStates.slice();
3737
// https://stackoverflow.com/questions/5349425/whats-the-fastest-way-to-loop-through-an-array-in-javascript
3838
// https://stackoverflow.com/a/10993837/9931154
3939
// Based on the steps, get values from the futureStates array and push them to the pastStates array

0 commit comments

Comments
 (0)