refactor: rename unwrap to peek

This commit is contained in:
Richard 2024-04-23 20:35:42 -07:00
parent 1e7e20902a
commit 047be71eaa
6 changed files with 16 additions and 16 deletions

View file

@ -61,7 +61,7 @@ Charm = "littensy/charm@VERSION"
#### `map(atom, mapper)`
#### `unwrap(atom)`
#### `peek(atom)`
### React

View file

@ -1,12 +1,12 @@
local atom = require(script.Parent.Parent.atom)
local peek = require(script.Parent.Parent.peek)
local store = require(script.Parent.Parent.store)
local unwrap = require(script.Parent.Parent.unwrap)
return function()
it("calls an atom without capturing it", function()
local a, b, c = atom(1), atom(1), atom(1)
local captured, result = store.capture(function()
return a() + unwrap(b) + c()
return a() + peek(b) + c()
end)
expect(captured[a]).to.be.ok()
expect(captured[b]).to.never.be.ok()
@ -15,8 +15,8 @@ return function()
end)
it("passes through arguments", function()
expect(unwrap(1)).to.equal(1)
expect(unwrap(function()
expect(peek(1)).to.equal(1)
expect(peek(function()
return 1
end)).to.equal(1)
end)

View file

@ -1,8 +1,8 @@
local peek = require(script.Parent.peek)
local store = require(script.Parent.store)
local types = require(script.Parent.types)
type Atom<T> = types.Atom<T>
type AtomOptions<T> = types.AtomOptions<T>
local unwrap = require(script.Parent.unwrap)
local function atom<T>(state: T, options: AtomOptions<T>?): Atom<T>
local equals = options and options.equals
@ -17,7 +17,7 @@ local function atom<T>(state: T, options: AtomOptions<T>?): Atom<T>
end
-- Atoms called in a function should not be a part of the dependency tree
local nextState = unwrap(..., state)
local nextState = peek(..., state)
if state ~= nextState and not (equals and equals(state, nextState)) then
state = nextState

2
src/index.d.ts vendored
View file

@ -28,7 +28,7 @@ declare namespace Charm {
function effect(callback: () => void): Cleanup;
function unwrap<State, Args extends unknown[]>(molecule: State | ((...args: Args) => State), ...args: Args): State;
function peek<State, Args extends unknown[]>(molecule: State | ((...args: Args) => State), ...args: Args): State;
function isAtom(value: unknown): value is Atom<any>;

View file

@ -5,6 +5,7 @@ local derive = require(script.derive)
local effect = require(script.effect)
local map = require(script.map)
local observe = require(script.observe)
local peek = require(script.peek)
local useAtom = require(script.react.useAtom)
local useAtomState = require(script.react.useAtomState)
local useSetAtom = require(script.react.useSetAtom)
@ -12,7 +13,6 @@ local store = require(script.store)
local subscribe = require(script.subscribe)
local sync = require(script.sync)
local types = require(script.types)
local unwrap = require(script.unwrap)
export type Atom<State> = types.Atom<State>
export type Molecule<State> = types.Molecule<State>
@ -20,18 +20,18 @@ export type SyncPayload = types.SyncPayload
return {
atom = atom,
batch = store.batch,
capture = store.capture,
notify = store.notify,
isAtom = store.isAtom,
derive = derive,
effect = effect,
map = map,
observe = observe,
peek = peek,
useAtom = useAtom,
useAtomState = useAtomState,
useSetAtom = useSetAtom,
batch = store.batch,
capture = store.capture,
notify = store.notify,
isAtom = store.isAtom,
subscribe = subscribe,
sync = sync,
unwrap = unwrap,
}

View file

@ -1,6 +1,6 @@
local store = require(script.Parent.store)
local function unwrap<T, U...>(callback: ((U...) -> T) | T, ...: U...): T
local function peek<T, U...>(callback: ((U...) -> T) | T, ...: U...): T
if type(callback) ~= "function" then
return callback
end
@ -16,4 +16,4 @@ local function unwrap<T, U...>(callback: ((U...) -> T) | T, ...: U...): T
return result
end
return unwrap
return peek