diff --git a/README.md b/README.md index f4db5e8..9ba3790 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ Charm = "littensy/charm@VERSION" #### `map(atom, mapper)` -#### `unwrap(atom)` +#### `peek(atom)` ### React diff --git a/src/__tests__/unwrap.spec.luau b/src/__tests__/unwrap.spec.luau index a8e4253..0ad8146 100644 --- a/src/__tests__/unwrap.spec.luau +++ b/src/__tests__/unwrap.spec.luau @@ -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) diff --git a/src/atom.luau b/src/atom.luau index 60dd945..9c258d2 100644 --- a/src/atom.luau +++ b/src/atom.luau @@ -1,8 +1,8 @@ +local peek = require(script.Parent.peek) local store = require(script.Parent.store) local types = require(script.Parent.types) type Atom = types.Atom type AtomOptions = types.AtomOptions -local unwrap = require(script.Parent.unwrap) local function atom(state: T, options: AtomOptions?): Atom local equals = options and options.equals @@ -17,7 +17,7 @@ local function atom(state: T, options: AtomOptions?): Atom 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 diff --git a/src/index.d.ts b/src/index.d.ts index b006d20..a0055a1 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -28,7 +28,7 @@ declare namespace Charm { function effect(callback: () => void): Cleanup; - function unwrap(molecule: State | ((...args: Args) => State), ...args: Args): State; + function peek(molecule: State | ((...args: Args) => State), ...args: Args): State; function isAtom(value: unknown): value is Atom; diff --git a/src/init.luau b/src/init.luau index 9955754..1c42892 100644 --- a/src/init.luau +++ b/src/init.luau @@ -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 = types.Atom export type Molecule = types.Molecule @@ -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, } diff --git a/src/unwrap.luau b/src/peek.luau similarity index 75% rename from src/unwrap.luau rename to src/peek.luau index 63c6b52..bceed95 100644 --- a/src/unwrap.luau +++ b/src/peek.luau @@ -1,6 +1,6 @@ local store = require(script.Parent.store) -local function unwrap(callback: ((U...) -> T) | T, ...: U...): T +local function peek(callback: ((U...) -> T) | T, ...: U...): T if type(callback) ~= "function" then return callback end @@ -16,4 +16,4 @@ local function unwrap(callback: ((U...) -> T) | T, ...: U...): T return result end -return unwrap +return peek