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

Copy past and future states only if necessary #112

Merged
merged 1 commit into from
Jun 10, 2023
Merged
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
1 change: 0 additions & 1 deletion __tests__/react.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('React Re-renders when state changes', () => {
});
});


// React Code from examples/web/pages/reactive.tsx
import { TemporalState, temporal } from '../src';
import { StoreApi, useStore, create } from 'zustand';
Expand Down
16 changes: 8 additions & 8 deletions src/temporal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export const createVanillaTemporal = <TState>(
pastStates: options?.pastStates || [],
futureStates: options?.futureStates || [],
undo: (steps = 1) => {
// Fastest way to clone an array on Chromium. Needed to create a new array reference
const pastStates = get().pastStates.slice();
const futureStates = get().futureStates.slice();
if (pastStates.length) {
if (get().pastStates.length) {
// Fastest way to clone an array on Chromium. Needed to create a new array reference
const pastStates = get().pastStates.slice();
const futureStates = get().futureStates.slice();
// Based on the steps, get values from the pastStates array and push them to the futureStates array
while (steps--) {
const pastState = pastStates.pop();
Expand All @@ -30,10 +30,10 @@ export const createVanillaTemporal = <TState>(
}
},
redo: (steps = 1) => {
// Fastest way to clone an array on Chromium. Needed to create a new array reference
const pastStates = get().pastStates.slice();
const futureStates = get().futureStates.slice();
if (futureStates.length) {
if (get().futureStates.length) {
// Fastest way to clone an array on Chromium. Needed to create a new array reference
const pastStates = get().pastStates.slice();
const futureStates = get().futureStates.slice();
// https://stackoverflow.com/questions/5349425/whats-the-fastest-way-to-loop-through-an-array-in-javascript
// https://stackoverflow.com/a/10993837/9931154
// Based on the steps, get values from the futureStates array and push them to the pastStates array
Expand Down