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)` #### `map(atom, mapper)`
#### `unwrap(atom)` #### `peek(atom)`
### React ### React

View file

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

View file

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

2
src/index.d.ts vendored
View file

@ -28,7 +28,7 @@ declare namespace Charm {
function effect(callback: () => void): Cleanup; 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>; 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 effect = require(script.effect)
local map = require(script.map) local map = require(script.map)
local observe = require(script.observe) local observe = require(script.observe)
local peek = require(script.peek)
local useAtom = require(script.react.useAtom) local useAtom = require(script.react.useAtom)
local useAtomState = require(script.react.useAtomState) local useAtomState = require(script.react.useAtomState)
local useSetAtom = require(script.react.useSetAtom) local useSetAtom = require(script.react.useSetAtom)
@ -12,7 +13,6 @@ local store = require(script.store)
local subscribe = require(script.subscribe) local subscribe = require(script.subscribe)
local sync = require(script.sync) local sync = require(script.sync)
local types = require(script.types) local types = require(script.types)
local unwrap = require(script.unwrap)
export type Atom<State> = types.Atom<State> export type Atom<State> = types.Atom<State>
export type Molecule<State> = types.Molecule<State> export type Molecule<State> = types.Molecule<State>
@ -20,18 +20,18 @@ export type SyncPayload = types.SyncPayload
return { return {
atom = atom, atom = atom,
batch = store.batch,
capture = store.capture,
notify = store.notify,
isAtom = store.isAtom,
derive = derive, derive = derive,
effect = effect, effect = effect,
map = map, map = map,
observe = observe, observe = observe,
peek = peek,
useAtom = useAtom, useAtom = useAtom,
useAtomState = useAtomState, useAtomState = useAtomState,
useSetAtom = useSetAtom, useSetAtom = useSetAtom,
batch = store.batch,
capture = store.capture,
notify = store.notify,
isAtom = store.isAtom,
subscribe = subscribe, subscribe = subscribe,
sync = sync, sync = sync,
unwrap = unwrap,
} }

View file

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