jotai-optics
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -5,4 +5,9 @@ # Change Log | ||
## [0.2.1] - 2023-01-29 | ||
### Changed | ||
- refactor: simplify memoize | ||
- fix: accept 'undefined' as valid value for Prisms, Lens, ISO and Equivalence #5 | ||
## [0.2.0] - 2022-11-15 | ||
### Added | ||
- Move back from the official Jotai library |
@@ -1,2 +0,2 @@ | ||
import{atom as t}from"jotai";import*as e from"optics-ts";const o=new WeakMap;function r(r,s){return((a,c,i)=>{o.has(c)||o.set(c,new WeakMap);const p=o.get(c);return p.has(i)||p.set(i,(()=>{const o=s(e.optic());return t(t=>n(o,t(r)),(t,n,s)=>{const a="function"==typeof s?e.modify(o)(s):e.set(o)(s);return n(r,a(t(r)))})})()),p.get(i)})(0,r,s)}const n=(t,o)=>"Traversal"===t._tag?e.collect(t)(o):"Prism"===t._tag?e.preview(t)(o):e.get(t)(o);export{r as focusAtom}; | ||
import{atom as t}from"jotai";import*as e from"optics-ts";const o=(t,e,o)=>(e.has(o)?e:e.set(o,t())).get(o),r=new WeakMap;function n(n,a){return((c,i,p)=>{const f=o(()=>new WeakMap,r,i);return o(()=>{const o=a(e.optic());return t(t=>s(o,t(n)),(t,r,s)=>{const a="function"==typeof s?e.modify(o)(s):e.set(o)(s);return r(n,a(t(n)))})},f,p)})(0,n,a)}const s=(t,o)=>"Traversal"===t._tag?e.collect(t)(o):"Prism"===t._tag?e.preview(t)(o):e.get(t)(o);export{n as focusAtom}; | ||
//# sourceMappingURL=index.modern.mjs.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("jotai"),require("optics-ts")):"function"==typeof define&&define.amd?define(["exports","jotai","optics-ts"],t):t((e||self).jotaiOptics={},e.jotai,e.opticsTs)}(this,function(e,t,n){function o(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var o=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,o.get?o:{enumerable:!0,get:function(){return e[n]}})}}),t.default=e,t}var r=/*#__PURE__*/o(n),i=new WeakMap;e.focusAtom=function(e,n){return function(o,f,u){i.has(f)||i.set(f,new WeakMap);var a,c=i.get(f);return c.has(u)||c.set(u,(a=n(r.optic()),t.atom(function(t){return function(e,t){return"Traversal"===e._tag?r.collect(e)(t):"Prism"===e._tag?r.preview(e)(t):r.get(e)(t)}(a,t(e))},function(t,n,o){var i="function"==typeof o?r.modify(a)(o):r.set(a)(o);return n(e,i(t(e)))}))),c.get(u)}(0,e,n)}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("jotai"),require("optics-ts")):"function"==typeof define&&define.amd?define(["exports","jotai","optics-ts"],t):t((e||self).jotaiOptics={},e.jotai,e.opticsTs)}(this,function(e,t,n){function r(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}}),t.default=e,t}var o=/*#__PURE__*/r(n),i=function(e,t,n){return(t.has(n)?t:t.set(n,e())).get(n)},u=new WeakMap;e.focusAtom=function(e,n){return r=function(){var r=n(o.optic());return t.atom(function(t){return function(e,t){return"Traversal"===e._tag?o.collect(e)(t):"Prism"===e._tag?o.preview(e)(t):o.get(e)(t)}(r,t(e))},function(t,n,i){var u="function"==typeof i?o.modify(r)(i):o.set(r)(i);return n(e,u(t(e)))})},f=n,c=i(function(){return new WeakMap},u,e),i(r,c,f);var r,f,c}}); | ||
//# sourceMappingURL=index.umd.js.map |
import type { SetStateAction, WritableAtom } from 'jotai'; | ||
import * as O from 'optics-ts'; | ||
declare type NonFunction<T> = [T] extends [(...args: any[]) => any] ? never : T; | ||
type NonFunction<T> = [T] extends [(...args: any[]) => any] ? never : T; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A>): WritableAtom<A | undefined, SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Traversal<S, any, A>): WritableAtom<A[], SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Prism<S, any, A>): WritableAtom<A | undefined, SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Traversal<S, any, A>): WritableAtom<A[], SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A>): WritableAtom<A | undefined, SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Traversal<S, any, A>): WritableAtom<A[], SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Prism<S, any, A>): WritableAtom<A | undefined, SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Traversal<S, any, A>): WritableAtom<A[], SetStateAction<A>, R>; | ||
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, SetStateAction<A>, R>; | ||
export {}; |
{ | ||
"name": "jotai-optics", | ||
"description": "👻💎", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"author": "merisbahti", | ||
"contributors": [ | ||
"Daishi Kato (https://github.com/dai-shi)", | ||
"Arjun Vegda (https://github.com/arjunvegda)" | ||
], | ||
"repository": { | ||
@@ -51,31 +55,32 @@ "type": "git", | ||
"@testing-library/react": "^13.4.0", | ||
"@types/jest": "^29.2.3", | ||
"@types/react": "^18.0.25", | ||
"@types/react-dom": "^18.0.9", | ||
"@typescript-eslint/eslint-plugin": "^5.43.0", | ||
"@typescript-eslint/parser": "^5.43.0", | ||
"eslint": "^8.27.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"@types/jest": "^29.4.0", | ||
"@types/react": "^18.0.27", | ||
"@types/react-dom": "^18.0.10", | ||
"@typescript-eslint/eslint-plugin": "^5.49.0", | ||
"@typescript-eslint/parser": "^5.49.0", | ||
"eslint": "^8.33.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
"eslint-import-resolver-alias": "^1.1.2", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-jest": "^27.1.5", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-jest": "^27.2.1", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint-plugin-react": "^7.31.10", | ||
"eslint-plugin-react": "^7.32.1", | ||
"eslint-plugin-react-hooks": "^4.6.0", | ||
"expect-type": "^0.15.0", | ||
"html-webpack-plugin": "^5.5.0", | ||
"jest": "^29.3.1", | ||
"jest-environment-jsdom": "^29.3.1", | ||
"jotai": "^1.9.2", | ||
"jest": "^29.4.1", | ||
"jest-environment-jsdom": "^29.4.1", | ||
"jotai": "^1.13.1", | ||
"microbundle": "^0.15.1", | ||
"npm-run-all": "^4.1.5", | ||
"optics-ts": "^2.4.0", | ||
"prettier": "^2.7.1", | ||
"prettier": "^2.8.3", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"react-error-boundary": "^3.1.4", | ||
"ts-jest": "^29.0.3", | ||
"ts-loader": "^9.4.1", | ||
"typescript": "^4.8.4", | ||
"ts-jest": "^29.0.5", | ||
"ts-loader": "^9.4.2", | ||
"typescript": "^4.9.4", | ||
"webpack": "^5.75.0", | ||
"webpack-cli": "^4.10.0", | ||
"webpack-cli": "^5.0.1", | ||
"webpack-dev-server": "^4.11.1" | ||
@@ -82,0 +87,0 @@ }, |
@@ -5,12 +5,8 @@ import { atom } from 'jotai' | ||
const getCached = <T>(c: () => T, m: WeakMap<object, T>, k: object): T => | ||
(m.has(k) ? m : m.set(k, c())).get(k) as T | ||
const cache1 = new WeakMap() | ||
const memoize = <T>(create: () => T, dep1: object, dep2: object): T => { | ||
if (!cache1.has(dep1)) { | ||
cache1.set(dep1, new WeakMap()) | ||
} | ||
const cache2 = cache1.get(dep1) | ||
if (!cache2.has(dep2)) { | ||
cache2.set(dep2, create()) | ||
} | ||
return cache2.get(dep2) | ||
const memo2 = <T>(create: () => T, dep1: object, dep2: object): T => { | ||
const cache2 = getCached(() => new WeakMap(), cache1, dep1) | ||
return getCached(create, cache2, dep2) | ||
} | ||
@@ -23,2 +19,4 @@ | ||
// Pattern 1: Promise | ||
export function focusAtom<S, A, R extends void | Promise<void>>( | ||
@@ -41,3 +39,24 @@ baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>, | ||
// Pattern 2: Promise with undefined type | ||
export function focusAtom<S, A, R extends void | Promise<void>>( | ||
baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>, | ||
callback: (optic: O.OpticFor<S | undefined>) => O.Prism<S, any, A> | ||
): WritableAtom<A | undefined, SetStateAction<A>, R> | ||
export function focusAtom<S, A, R extends void | Promise<void>>( | ||
baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>, | ||
callback: (optic: O.OpticFor<S | undefined>) => O.Traversal<S, any, A> | ||
): WritableAtom<A[], SetStateAction<A>, R> | ||
export function focusAtom<S, A, R extends void | Promise<void>>( | ||
baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>, | ||
callback: ( | ||
optic: O.OpticFor<S | undefined> | ||
) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A> | ||
): WritableAtom<A, SetStateAction<A>, R> | ||
// Pattern 3: Default | ||
export function focusAtom<S, A, R extends void | Promise<void>>( | ||
baseAtom: WritableAtom<S, NonFunction<S>, R>, | ||
@@ -59,3 +78,24 @@ callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A> | ||
// Pattern 4: Default with undefined type | ||
export function focusAtom<S, A, R extends void | Promise<void>>( | ||
baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>, | ||
callback: (optic: O.OpticFor<S | undefined>) => O.Prism<S, any, A> | ||
): WritableAtom<A | undefined, SetStateAction<A>, R> | ||
export function focusAtom<S, A, R extends void | Promise<void>>( | ||
baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>, | ||
callback: (optic: O.OpticFor<S | undefined>) => O.Traversal<S, any, A> | ||
): WritableAtom<A[], SetStateAction<A>, R> | ||
export function focusAtom<S, A, R extends void | Promise<void>>( | ||
baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>, | ||
callback: ( | ||
optic: O.OpticFor<S | undefined> | ||
) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A> | ||
): WritableAtom<A, SetStateAction<A>, R> | ||
// Implementation | ||
export function focusAtom<S, A, R extends void | Promise<void>>( | ||
baseAtom: WritableAtom<S, NonFunction<S>, R>, | ||
@@ -71,3 +111,3 @@ callback: ( | ||
) { | ||
return memoize( | ||
return memo2( | ||
() => { | ||
@@ -74,0 +114,0 @@ const focus = callback(O.optic<S>()) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
33322
156
0
32