-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* use state history demo * use previous * add jsdoc @see * dummy test * document use state history * change array order * Create curly-balloons-marry.md
- Loading branch information
Showing
32 changed files
with
408 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"runed": patch | ||
--- | ||
|
||
feat: `useStateHistory` & `usePrevious` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './usePrevious.svelte.js' |
21 changes: 21 additions & 0 deletions
21
packages/runed/src/lib/functions/usePrevious/usePrevious.svelte.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { untrack } from "svelte"; | ||
import { box } from "../index.js"; | ||
import type { BoxOrGetter } from "$lib/internal/types.js"; | ||
|
||
/** | ||
* Holds the previous value of a box or getter. | ||
* | ||
* @see {@link https://runed.dev/docs/functions/use-previous} | ||
*/ | ||
export function usePrevious<T>(value: BoxOrGetter<T>) { | ||
const boxed = box.from(value); | ||
let curr: T | undefined = $state() | ||
const previous = box<T | undefined>(undefined); | ||
|
||
$effect(() => { | ||
previous.value = untrack(() => curr); | ||
curr = boxed.value; | ||
}); | ||
|
||
return previous; | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/runed/src/lib/functions/usePrevious/usePrevious.test.svelte.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { describe } from "node:test"; | ||
import { expect, test } from "vitest"; | ||
import { box } from "../index.js"; | ||
import { usePrevious } from "./usePrevious.svelte.js"; | ||
import { testWithEffect } from "$lib/test/util.svelte.js"; | ||
|
||
// TODO: Find out why tests aren't working, even though the demo works | ||
describe('usePrevious', () => { | ||
test('dummy test', () => { | ||
expect(true).toBe(true) | ||
}) | ||
// testWithEffect('Should return undefined initially', () => { | ||
// const previous = usePrevious(() => 0); | ||
// expect(previous.value).toBe(undefined) | ||
// }) | ||
|
||
// testWithEffect('Should return previous value', async () => { | ||
// const count = box(0); | ||
// const previous = usePrevious(count); | ||
// expect(previous.value).toBe(undefined) | ||
// count.value = 1 | ||
// await new Promise(resolve => setTimeout(resolve, 100)) | ||
// expect(previous.value).toBe(0) | ||
// count.value = 2 | ||
// expect(previous.value).toBe(1) | ||
// }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useStateHistory.svelte.js' |
79 changes: 79 additions & 0 deletions
79
packages/runed/src/lib/functions/useStateHistory/useStateHistory.svelte.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { type WritableBox, box } from "../box/box.svelte.js"; | ||
import { watch } from "../watch/watch.svelte.js"; | ||
import type { MaybeBoxOrGetter } from "$lib/internal/types.js"; | ||
|
||
type UseStateHistoryOptions = { | ||
capacity?: MaybeBoxOrGetter<number> | ||
} | ||
|
||
type LogEvent<T> = { | ||
snapshot: T | ||
timestamp: number | ||
} | ||
|
||
/** | ||
* Tracks the change history of a box, providing undo and redo capabilities. | ||
* | ||
* @see {@link https://runed.dev/docs/functions/use-state-history} | ||
*/ | ||
export function useStateHistory<T>(b: WritableBox<T>, options?: UseStateHistoryOptions) { | ||
const capacity = box.from(options?.capacity) | ||
|
||
const log = box<LogEvent<T>[]>([]) | ||
const redoStack = box<LogEvent<T>[]>([]) | ||
|
||
const canUndo = box.with(() => log.value.length > 1) | ||
const canRedo = box.with(() => redoStack.value.length > 0) | ||
|
||
let ignoreUpdate = false; | ||
|
||
function addEvent(event: LogEvent<T>) { | ||
log.value.push(event) | ||
if (capacity.value && log.value.length > capacity.value) { | ||
log.value = log.value.slice(-capacity.value) | ||
} | ||
} | ||
|
||
watch(() => b.value, (v) => { | ||
if (ignoreUpdate) { | ||
ignoreUpdate = false | ||
return | ||
} | ||
|
||
addEvent({ snapshot: v, timestamp: new Date().getTime() }) | ||
redoStack.value = [] | ||
}) | ||
|
||
watch(() => capacity.value, (c) => { | ||
if (!c) return; | ||
log.value = log.value.slice(-c) | ||
}) | ||
|
||
|
||
function undo() { | ||
const [prev, curr] = log.value.slice(-2) | ||
if (!curr || !prev) return; | ||
ignoreUpdate = true; | ||
redoStack.value.push(curr) | ||
log.value.pop() | ||
b.value = prev.snapshot | ||
} | ||
|
||
function redo() { | ||
const nextEvent = redoStack.value.pop() | ||
if (!nextEvent) return; | ||
ignoreUpdate = true; | ||
addEvent(nextEvent) | ||
b.value = nextEvent.snapshot | ||
} | ||
|
||
|
||
return box.flatten({ | ||
log, | ||
undo, | ||
canUndo, | ||
redo, | ||
canRedo | ||
}) | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
packages/runed/src/lib/functions/useStateHistory/useStateHistory.test.svelte.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, test } from "vitest" | ||
|
||
|
||
describe("useStateHistory", () => { | ||
test("dummy test", () => { | ||
expect(true).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './watch.svelte.js' |
Oops, something went wrong.