Comparing version 0.5.4 to 0.6.0-0
@@ -8,2 +8,6 @@ # Changelog | ||
## [Unreleased] | ||
- **Changed:** The library is now built and bundled using the excellent [microbundle](https://github.com/developit/microbundle). | ||
## [0.5.4] - 2021-03-20 | ||
@@ -10,0 +14,0 @@ |
@@ -55,9 +55,1 @@ /** | ||
export declare const makeStore: <T extends State>(initialState: T) => Store<T>; | ||
/** | ||
* Provides reactive read access to a Statery store. Returns a proxy object that | ||
* provides direct access to the store's state and makes sure that the React component | ||
* it was invoked from automaticaly re-renders when any of the data it uses is updated. | ||
* | ||
* @param store The Statery store to access. | ||
*/ | ||
export declare const useStore: <T extends State>(store: Store<T>) => T; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");exports.makeStore=e=>{let t=e;const s=new Set;return{get state(){return t},set:e=>{const r=(e=>Object.keys(e).reduce(((s,r)=>(e[r]!==t[r]&&(s[r]=e[r]),s)),{}))(e instanceof Function?e(t):e);if(Object.keys(r).length>0){const e=t;t=Object.assign(Object.assign({},t),r);for(const t of s)t(r,e)}return t},subscribe:e=>{s.add(e)},unsubscribe:e=>{s.delete(e)}}},exports.useStore=t=>{const[,s]=e.useState(0),r=e.useRef(new Set).current;return e.useEffect((()=>{const e=e=>{Object.keys(e).find((e=>r.has(e)))&&s((e=>e+1))};return t.subscribe(e),()=>{t.unsubscribe(e)}}),[t]),new Proxy({},{get:(e,s)=>(r.add(s),t.state[s])})}; | ||
function t(){return(t=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t}).apply(this,arguments)}function r(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}exports.makeStore=function(e){var n=e,o=new Set;return{get state(){return n},set:function(e){var a=function(t){return Object.keys(t).reduce(function(r,e){return t[e]!==n[e]&&(r[e]=t[e]),r},{})}(e instanceof Function?e(n):e);if(Object.keys(a).length>0){var i=n;n=t({},n,a);for(var u,c=function(t,e){var n;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return r(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(t,e):void 0}}(t))){n&&(t=n);var o=0;return function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(n=t[Symbol.iterator]()).next.bind(n)}(o);!(u=c()).done;)(0,u.value)(a,i)}return n},subscribe:function(t){o.add(t)},unsubscribe:function(t){o.delete(t)}}}; | ||
//# sourceMappingURL=index.js.map |
@@ -17,5 +17,22 @@ { | ||
"sideEffects": false, | ||
"version": "0.5.4", | ||
"version": "0.6.0-0", | ||
"src": "src/index.ts", | ||
"main": "dist/index.js", | ||
"module": "dist/index.esm.js", | ||
"module": "dist/index.modern.js", | ||
"unpkg": "dist/index.umd.js", | ||
"exports": { | ||
".": "./dist/index.modern.js", | ||
"./react": "./dist/react.modern.js", | ||
"./preact": "./dist/preact.modern.js" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"react": [ | ||
"dist/react" | ||
], | ||
"preact": [ | ||
"dist/preact" | ||
] | ||
} | ||
}, | ||
"types": "dist/index.d.ts", | ||
@@ -28,6 +45,7 @@ "files": [ | ||
"clean": "rimraf dist", | ||
"dev": "yarn clean && rollup -c -w", | ||
"build": "yarn clean && rollup -c", | ||
"dev": "microbundle watch src/*.ts --external react,preact", | ||
"build": "microbundle build src/*.ts --external react,preact", | ||
"test": "jest", | ||
"docs": "typedoc src/index.ts" | ||
"docs": "typedoc src/index.ts", | ||
"prepublishOnly": "yarn test && yarn build" | ||
}, | ||
@@ -40,8 +58,7 @@ "devDependencies": { | ||
"jest": "^26.6.3", | ||
"microbundle": "^0.13.0", | ||
"preact": "^10.5.13", | ||
"react": "^17.0.1", | ||
"react-dom": "^17.0.1", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.35.1", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"rollup-plugin-typescript2": "^0.29.0", | ||
"ts-jest": "^26.4.4", | ||
@@ -51,6 +68,3 @@ "tslib": "^2.0.3", | ||
"typescript": "^4.1.3" | ||
}, | ||
"peerDependencies": { | ||
"react": ">=16.8" | ||
} | ||
} |
@@ -1,3 +0,1 @@ | ||
import { useEffect, useRef, useState } from "react" | ||
/* | ||
@@ -93,2 +91,6 @@ | ||
/** | ||
* Reduces a set of incoming changes to those that actually _are_ changes from the | ||
* current state. | ||
*/ | ||
const getActualChanges = (updates: Partial<T>) => | ||
@@ -132,57 +134,1 @@ Object.keys(updates).reduce<Partial<T>>((changes, prop: keyof Partial<T>) => { | ||
} | ||
/* | ||
▄█ █▄ ▄██████▄ ▄██████▄ ▄█ ▄█▄ ▄████████ | ||
███ ███ ███ ███ ███ ███ ███ ▄███▀ ███ ███ | ||
███ ███ ███ ███ ███ ███ ███▐██▀ ███ █▀ | ||
▄███▄▄▄▄███▄▄ ███ ███ ███ ███ ▄█████▀ ███ | ||
▀▀███▀▀▀▀███▀ ███ ███ ███ ███ ▀▀█████▄ ▀███████████ | ||
███ ███ ███ ███ ███ ███ ███▐██▄ ███ | ||
███ ███ ███ ███ ███ ███ ███ ▀███▄ ▄█ ███ | ||
███ █▀ ▀██████▀ ▀██████▀ ███ ▀█▀ ▄████████▀ | ||
▀ | ||
*/ | ||
/** | ||
* Provides reactive read access to a Statery store. Returns a proxy object that | ||
* provides direct access to the store's state and makes sure that the React component | ||
* it was invoked from automaticaly re-renders when any of the data it uses is updated. | ||
* | ||
* @param store The Statery store to access. | ||
*/ | ||
export const useStore = <T extends State>(store: Store<T>): T => { | ||
/* A cheap version state that we will bump in order to re-render the component. */ | ||
const [, setVersion] = useState(0) | ||
/* A set containing all props that we're interested in. */ | ||
const subscribedProps = useRef(new Set<keyof T>()).current | ||
/* Subscribe to changes in the store. */ | ||
useEffect(() => { | ||
const listener: Listener<T> = (updates: Partial<T>) => { | ||
/* If there is at least one prop being updated that we're interested in, | ||
bump our local version. */ | ||
if (Object.keys(updates).find((prop) => subscribedProps.has(prop))) { | ||
setVersion((v) => v + 1) | ||
} | ||
} | ||
/* Mount & unmount the listener */ | ||
store.subscribe(listener) | ||
return () => void store.unsubscribe(listener) | ||
}, [store]) | ||
return new Proxy<Record<any, any>>( | ||
{}, | ||
{ | ||
get: (_, prop: string) => { | ||
/* Add the prop we're interested in to the list of props */ | ||
subscribedProps.add(prop) | ||
/* Return the current value of the property. */ | ||
return store.state[prop] | ||
} | ||
} | ||
) | ||
} |
{ | ||
"compilerOptions": { | ||
"target": "es2015", | ||
"module": "es2015", | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"strict": true, | ||
"jsx": "react-jsx", | ||
"jsx": "react", | ||
"allowSyntheticDefaultImports": true, | ||
@@ -10,0 +10,0 @@ "esModuleInterop": true |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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 2 instances 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
1032062
0
14
51
3744
1
5