Socket
Socket
Sign inDemoInstall

constate

Package Overview
Dependencies
6
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0-alpha.2 to 1.0.0-alpha.3

10

CHANGELOG.md

@@ -5,2 +5,12 @@ # Change Log

<a name="1.0.0-alpha.3"></a>
# [1.0.0-alpha.3](https://github.com/diegohaz/constate/compare/v1.0.0-alpha.2...v1.0.0-alpha.3) (2018-11-25)
### Bug Fixes
* Accept arguments other than objects in `createContext` ([93327a3](https://github.com/diegohaz/constate/commit/93327a3))
<a name="1.0.0-alpha.2"></a>

@@ -7,0 +17,0 @@ # [1.0.0-alpha.2](https://github.com/diegohaz/constate/compare/v1.0.0-alpha.1...v1.0.0-alpha.2) (2018-11-22)

105

dist/constate.cjs.js

@@ -7,22 +7,2 @@ 'use strict';

var hashMap = {};
function hash(string) {
if (hashMap[string] !== undefined) {
return hashMap[string];
}
var _Object$keys = Object.keys(hashMap),
length = _Object$keys.length;
hashMap[string] = 1 << length;
return hashMap[string];
}
function parseUpdater(state, prevState) {
var isUpdater = function isUpdater(a) {
return typeof a === "function";
};
return isUpdater(state) ? state(prevState) : state;
}
function getCurrentOwner() {

@@ -38,3 +18,3 @@ return React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner.current;

var consumers = {};
return function (contextKey, create, inputs) {
return function useContextEffect(contextKey, create, inputs) {
var key = contextKey;

@@ -65,4 +45,4 @@ var consumer = React.useRef(getCurrentOwner());

function createUseContextReducer(context) {
return function (contextKey, reducer, initialState, initialAction) {
function createUseContextReducer(context, hash) {
return function useContextReducer(contextKey, reducer, initialState, initialAction) {
// @ts-ignore

@@ -109,8 +89,8 @@ var _React$useContext = React.useContext(contextKey ? context : EmptyContext, contextKey ? hash(contextKey) : undefined),

function basicStateReducer(state, action) {
return parseUpdater(action, state);
return typeof action === "function" ? action(state) : action;
}
function createUseContextState(context) {
var useContextReducer = createUseContextReducer(context);
return function (contextKey, initialState) {
function createUseContextState(context, hash) {
var useContextReducer = createUseContextReducer(context, hash);
return function useContextState(contextKey, initialState) {
return useContextReducer(contextKey, basicStateReducer, initialState);

@@ -152,5 +132,5 @@ };

if (enabled && devtoolsExtension) {
var changedKey;
if (lastStateSentFromDevtools.current !== state) {
var changedKey;
if (lastStateSentFromDevtools.current !== state) {
for (var key in state) {

@@ -172,28 +152,40 @@ if (prevState.current && state[key] !== prevState.current[key]) {

function defaultCalculateChangedBits(prev, next) {
var changedBits = 0;
for (var contextKey in next) {
if (prev[contextKey] !== next[contextKey]) {
changedBits |= hash(contextKey);
function createHash(skipLength) {
var hashMap = {};
return function hash(key) {
if (hashMap[key] !== undefined) {
return hashMap[key];
}
}
return changedBits;
var _Object$keys = Object.keys(hashMap),
length = _Object$keys.length; // 2, 4, 8, 16...
hashMap[key] = 1 << length % (30 - skipLength) + skipLength;
return hashMap[key];
};
}
function createContext(initialState, calculateChangedBits) {
if (calculateChangedBits === void 0) {
calculateChangedBits = defaultCalculateChangedBits;
}
function createCalculateChangedBits(hash) {
return function calculateChangedBits(prev, next) {
var changedBits = 1;
var Context = React.createContext([initialState, function () {}], calculateChangedBits ? function (_ref, _ref2) {
var prev = _ref[0];
var next = _ref2[0];
return calculateChangedBits(prev, next);
} : undefined);
if (typeof next !== "object" || Array.isArray(next) || next === null) {
return changedBits;
}
var Provider = function Provider(_ref3) {
var children = _ref3.children,
devtools = _ref3.devtools;
for (var contextKey in next) {
if (prev[contextKey] !== next[contextKey]) {
changedBits |= hash(contextKey);
}
}
return changedBits;
};
}
function createProvider(Context, initialState) {
return function Provider(_ref) {
var children = _ref.children,
devtools = _ref.devtools;
var state = React.useState(initialState);

@@ -210,8 +202,17 @@ var value = React.useMemo(function () {

};
}
function createContext(initialState, calculateChangedBits) {
var hash = createHash(1);
var finalCalculateChangedBits = calculateChangedBits === undefined ? createCalculateChangedBits(hash) : calculateChangedBits;
var Context = React.createContext([initialState, function () {}], finalCalculateChangedBits ? function (_ref2, _ref3) {
var prev = _ref2[0];
var next = _ref3[0];
return finalCalculateChangedBits(prev, next);
} : undefined);
return {
Context: Context,
Provider: Provider,
useContextState: createUseContextState(Context),
useContextReducer: createUseContextReducer(Context),
Provider: createProvider(Context, initialState),
useContextState: createUseContextState(Context, hash),
useContextReducer: createUseContextReducer(Context, hash),
unstable_useContextEffect: createUseContextEffect(),

@@ -218,0 +219,0 @@ unstable_useContextLayoutEffect: createUseContextEffect("useLayoutEffect"),

import * as React from 'react';
import { createContext, useState, useMemo, createElement, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, useRef, useMutationEffect, useContext, useReducer, useEffect } from 'react';
import { useState, useMemo, createElement, createContext, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, useRef, useMutationEffect, useContext, useReducer, useEffect } from 'react';
var hashMap = {};
function hash(string) {
if (hashMap[string] !== undefined) {
return hashMap[string];
}
var _Object$keys = Object.keys(hashMap),
length = _Object$keys.length;
hashMap[string] = 1 << length;
return hashMap[string];
}
function parseUpdater(state, prevState) {
var isUpdater = function isUpdater(a) {
return typeof a === "function";
};
return isUpdater(state) ? state(prevState) : state;
}
function getCurrentOwner() {

@@ -34,3 +14,3 @@ return __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner.current;

var consumers = {};
return function (contextKey, create, inputs) {
return function useContextEffect(contextKey, create, inputs) {
var key = contextKey;

@@ -61,4 +41,4 @@ var consumer = useRef(getCurrentOwner());

function createUseContextReducer(context) {
return function (contextKey, reducer, initialState, initialAction) {
function createUseContextReducer(context, hash) {
return function useContextReducer(contextKey, reducer, initialState, initialAction) {
// @ts-ignore

@@ -105,8 +85,8 @@ var _React$useContext = useContext(contextKey ? context : EmptyContext, contextKey ? hash(contextKey) : undefined),

function basicStateReducer(state, action) {
return parseUpdater(action, state);
return typeof action === "function" ? action(state) : action;
}
function createUseContextState(context) {
var useContextReducer = createUseContextReducer(context);
return function (contextKey, initialState) {
function createUseContextState(context, hash) {
var useContextReducer = createUseContextReducer(context, hash);
return function useContextState(contextKey, initialState) {
return useContextReducer(contextKey, basicStateReducer, initialState);

@@ -148,5 +128,5 @@ };

if (enabled && devtoolsExtension) {
var changedKey;
if (lastStateSentFromDevtools.current !== state) {
var changedKey;
if (lastStateSentFromDevtools.current !== state) {
for (var key in state) {

@@ -168,28 +148,40 @@ if (prevState.current && state[key] !== prevState.current[key]) {

function defaultCalculateChangedBits(prev, next) {
var changedBits = 0;
for (var contextKey in next) {
if (prev[contextKey] !== next[contextKey]) {
changedBits |= hash(contextKey);
function createHash(skipLength) {
var hashMap = {};
return function hash(key) {
if (hashMap[key] !== undefined) {
return hashMap[key];
}
}
return changedBits;
var _Object$keys = Object.keys(hashMap),
length = _Object$keys.length; // 2, 4, 8, 16...
hashMap[key] = 1 << length % (30 - skipLength) + skipLength;
return hashMap[key];
};
}
function createContext$1(initialState, calculateChangedBits) {
if (calculateChangedBits === void 0) {
calculateChangedBits = defaultCalculateChangedBits;
}
function createCalculateChangedBits(hash) {
return function calculateChangedBits(prev, next) {
var changedBits = 1;
var Context = createContext([initialState, function () {}], calculateChangedBits ? function (_ref, _ref2) {
var prev = _ref[0];
var next = _ref2[0];
return calculateChangedBits(prev, next);
} : undefined);
if (typeof next !== "object" || Array.isArray(next) || next === null) {
return changedBits;
}
var Provider = function Provider(_ref3) {
var children = _ref3.children,
devtools = _ref3.devtools;
for (var contextKey in next) {
if (prev[contextKey] !== next[contextKey]) {
changedBits |= hash(contextKey);
}
}
return changedBits;
};
}
function createProvider(Context, initialState) {
return function Provider(_ref) {
var children = _ref.children,
devtools = _ref.devtools;
var state = useState(initialState);

@@ -206,8 +198,17 @@ var value = useMemo(function () {

};
}
function createContext$1(initialState, calculateChangedBits) {
var hash = createHash(1);
var finalCalculateChangedBits = calculateChangedBits === undefined ? createCalculateChangedBits(hash) : calculateChangedBits;
var Context = createContext([initialState, function () {}], finalCalculateChangedBits ? function (_ref2, _ref3) {
var prev = _ref2[0];
var next = _ref3[0];
return finalCalculateChangedBits(prev, next);
} : undefined);
return {
Context: Context,
Provider: Provider,
useContextState: createUseContextState(Context),
useContextReducer: createUseContextReducer(Context),
Provider: createProvider(Context, initialState),
useContextState: createUseContextState(Context, hash),
useContextReducer: createUseContextReducer(Context, hash),
unstable_useContextEffect: createUseContextEffect(),

@@ -214,0 +215,0 @@ unstable_useContextLayoutEffect: createUseContextEffect("useLayoutEffect"),

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t(e.constate={},e.React)}(this,function(e,E){"use strict";var n={};function l(e){if(void 0!==n[e])return n[e];var t=Object.keys(n).length;return n[e]=1<<t,n[e]}function t(o){void 0===o&&(o="useEffect");var c={};return function(e,t,n){var u=e,r=E.useRef(E.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner.current);null==c[u]&&(c[u]=r.current),E.useMutationEffect(function(){if(u)return function(){c[u]=null}},[u]),E[o](function(){if(!u||c[u]===r.current)return t()},n?[u].concat(n):void 0)}}var d=E.createContext([]);function u(a){return function(u,r,e,t){var n=E.useContext(u?a:d,u?l(u):void 0),o=n[0],c=n[1],f=E.useReducer(r,e,t),i=f[0],s=f[1];return u&&(null!=o[u]&&(i=o[u]),s=function(n){return c(function(e){var t;return Object.assign({},e,((t={})[u]=r(e[u],n),t))})}),E.useMutationEffect(function(){u&&null==o[u]&&null!=i&&c(function(e){return null!=e[u]?e:Object.assign({},e,((t={})[u]=i,t));var t})},[u]),[i,s]}}function o(e,t){return u=e,"function"==typeof(n=t)?n(u):n;var n,u}var x="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__;function c(e,t){var n=0;for(var u in t)e[u]!==t[u]&&(n|=l(u));return n}function r(v,r){void 0===r&&(r=c);var e,n,_=E.createContext([v,function(){}],r?function(e,t){var n=e[0],u=t[0];return r(n,u)}:void 0);return{Context:_,Provider:function(e){var n,t,u,r,o,c,f,i,s=e.children,a=e.devtools,l=E.useState(v),d=E.useMemo(function(){return l},[l[0]]);return n=l[0],t=l[1],r=(void 0===(u={enabled:a})?{}:u).enabled,o=void 0===r||r,c=E.useRef(null),f=E.useRef(null),i=E.useRef(null),E.useEffect(function(){if(o&&x)return c.current=x.connect(),c.current.init(n),c.current.subscribe(function(e){"DISPATCH"===e.type&&e.state&&(i.current=JSON.parse(e.state),t(i.current))}),function(){c.current&&(c.current.unsubscribe(),x.disconnect())}},[o]),E.useEffect(function(){if(o&&x){var e;if(i.current!==n){for(var t in n)f.current&&n[t]!==f.current[t]&&(e=t);e&&c.current&&c.current.send(e,n)}f.current=n}},[n,o]),E.createElement(_.Provider,{value:d},s)},useContextState:(e=_,n=u(e),function(e,t){return n(e,o,t)}),useContextReducer:u(_),unstable_useContextEffect:t(),unstable_useContextLayoutEffect:t("useLayoutEffect"),unstable_useContextMutationEffect:t("useMutationEffect")}}var f=r({}),i=f.Context,s=f.Provider,a=f.useContextReducer,v=f.useContextState,_=f.unstable_useContextEffect,C=f.unstable_useContextLayoutEffect,b=f.unstable_useContextMutationEffect;e.createContext=r,e.Context=i,e.Provider=s,e.useContextReducer=a,e.useContextState=v,e.unstable_useContextEffect=_,e.unstable_useContextLayoutEffect=C,e.unstable_useContextMutationEffect=b,Object.defineProperty(e,"__esModule",{value:!0})});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t(e.constate={},e.React)}(this,function(e,E){"use strict";function l(o){void 0===o&&(o="useEffect");var c={};return function(e,t,n){var u=e,r=E.useRef(E.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner.current);null==c[u]&&(c[u]=r.current),E.useMutationEffect(function(){if(u)return function(){c[u]=null}},[u]),E[o](function(){if(!u||c[u]===r.current)return t()},n?[u].concat(n):void 0)}}var d=E.createContext([]);function v(a,l){return function(u,r,e,t){var n=E.useContext(u?a:d,u?l(u):void 0),o=n[0],c=n[1],f=E.useReducer(r,e,t),i=f[0],s=f[1];return u&&(null!=o[u]&&(i=o[u]),s=function(n){return c(function(e){var t;return Object.assign({},e,((t={})[u]=r(e[u],n),t))})}),E.useMutationEffect(function(){u&&null==o[u]&&null!=i&&c(function(e){return null!=e[u]?e:Object.assign({},e,((t={})[u]=i,t));var t})},[u]),[i,s]}}function _(e,t){return"function"==typeof t?t(e):t}var x="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__;function C(v,_){return function(e){var n,t,u,r,o,c,f,i,s=e.children,a=e.devtools,l=E.useState(_),d=E.useMemo(function(){return l},[l[0]]);return n=l[0],t=l[1],r=(void 0===(u={enabled:a})?{}:u).enabled,o=void 0===r||r,c=E.useRef(null),f=E.useRef(null),i=E.useRef(null),E.useEffect(function(){if(o&&x)return c.current=x.connect(),c.current.init(n),c.current.subscribe(function(e){"DISPATCH"===e.type&&e.state&&(i.current=JSON.parse(e.state),t(i.current))}),function(){c.current&&(c.current.unsubscribe(),x.disconnect())}},[o]),E.useEffect(function(){if(o&&x){if(i.current!==n){var e;for(var t in n)f.current&&n[t]!==f.current[t]&&(e=t);e&&c.current&&c.current.send(e,n)}f.current=n}},[n,o]),E.createElement(v.Provider,{value:d},s)}}function t(e,t){var n,u,r,o,c,f,i=(n=1,u={},function(e){if(void 0!==u[e])return u[e];var t=Object.keys(u).length;return u[e]=1<<t%(30-n)+n,u[e]}),s=void 0===t?(r=i,function(e,t){var n=1;if("object"!=typeof t||Array.isArray(t)||null===t)return n;for(var u in t)e[u]!==t[u]&&(n|=r(u));return n}):t,a=E.createContext([e,function(){}],s?function(e,t){var n=e[0],u=t[0];return s(n,u)}:void 0);return{Context:a,Provider:C(a,e),useContextState:(o=a,c=i,f=v(o,c),function(e,t){return f(e,_,t)}),useContextReducer:v(a,i),unstable_useContextEffect:l(),unstable_useContextLayoutEffect:l("useLayoutEffect"),unstable_useContextMutationEffect:l("useMutationEffect")}}var n=t({}),u=n.Context,r=n.Provider,o=n.useContextReducer,c=n.useContextState,f=n.unstable_useContextEffect,i=n.unstable_useContextLayoutEffect,s=n.unstable_useContextMutationEffect;e.createContext=t,e.Context=u,e.Provider=r,e.useContextReducer=o,e.useContextState=c,e.unstable_useContextEffect=f,e.unstable_useContextLayoutEffect=i,e.unstable_useContextMutationEffect=s,Object.defineProperty(e,"__esModule",{value:!0})});

@@ -6,3 +6,3 @@ import * as React from "react";

};
declare function createContext<State>(initialState: State, calculateChangedBits?: (prev: State, next: State) => number): {
declare function createContext<State>(initialState: State, calculateChangedBits?: ((prev: State, next: State) => number) | null): {
Context: React.Context<[State, import("./types").SetState<State>]>;

@@ -9,0 +9,0 @@ Provider: ({ children, devtools }: ProviderProps) => JSX.Element;

@@ -7,3 +7,3 @@ import * as React from "react";

}
declare function createUseContextReducer<State>(context: React.Context<ContextState<State>>): UseContextReducer<State>;
declare function createUseContextReducer<State>(context: React.Context<ContextState<State>>, hash: (key: string) => number): UseContextReducer<State>;
export default createUseContextReducer;

@@ -7,3 +7,3 @@ import * as React from "react";

}
declare function createUseContextState<State>(context: React.Context<ContextState<State>>): UseContextState<State>;
declare function createUseContextState<State>(context: React.Context<ContextState<State>>, hash: (key: string) => number): UseContextState<State>;
export default createUseContextState;
{
"name": "constate",
"version": "1.0.0-alpha.2",
"version": "1.0.0-alpha.3",
"description": "Yet another React state management library that lets you work with local state and scale up to global state with ease",

@@ -61,3 +61,3 @@ "license": "MIT",

"@types/prop-types": "^15.5.6",
"@types/react": "^16.7.6",
"@types/react": "^16.7.7",
"babel-core": "^7.0.0-0",

@@ -74,5 +74,5 @@ "babel-eslint": "^10.0.1",

"eslint-plugin-typescript": "^0.13.0",
"husky": "^1.1.4",
"husky": "^1.2.0",
"jest-cli": "^23.6.0",
"lint-staged": "^8.0.5",
"lint-staged": "^8.1.0",
"opn-cli": "^4.0.0",

@@ -84,3 +84,3 @@ "prettier": "^1.15.2",

"react-test-renderer": "^16.6.3",
"react-testing-library": "^5.2.3",
"react-testing-library": "^5.3.0",
"rimraf": "^2.6.2",

@@ -87,0 +87,0 @@ "rollup": "^0.67.1",

@@ -26,3 +26,3 @@ <p align="center">

<br>
<a href="https://codesandbox.io/s/github/diegohaz/constate/tree/next/examples/counter">Counter</a>
<a href="https://codesandbox.io/s/github/diegohaz/constate/tree/master/examples/counter">Counter</a>
</p>

@@ -29,0 +29,0 @@

@@ -14,7 +14,7 @@ import * as React from "react";

return (
return function useContextEffect(
contextKey: keyof State | undefined | null,
create: () => void | (() => void),
inputs?: ReadonlyArray<any>
) => {
) {
const key = contextKey as string;

@@ -21,0 +21,0 @@ const consumer = React.useRef(getCurrentOwner());

import * as React from "react";
import { hash } from "./utils";
import { ContextReducer, ContextState, Reducer } from "./types";

@@ -24,5 +23,6 @@

function createUseContextReducer<State>(
context: React.Context<ContextState<State>>
context: React.Context<ContextState<State>>,
hash: (key: string) => number
) {
return ((
return function useContextReducer(
contextKey: keyof State | undefined | null,

@@ -32,3 +32,3 @@ reducer: Reducer<State[keyof State], any>,

initialAction?: any
) => {
) {
// @ts-ignore

@@ -76,5 +76,5 @@ const [contextState, setContextState] = React.useContext(

return [state, dispatch];
}) as UseContextReducer<State>;
} as UseContextReducer<State>;
}
export default createUseContextReducer;
import * as React from "react";
import { ContextState } from "./types";
import createUseContextReducer from "./createUseContextReducer";
import { parseUpdater } from "./utils";

@@ -16,18 +15,15 @@ export interface UseContextState<State> {

function basicStateReducer(state: any, action: any) {
return parseUpdater(action, state);
return typeof action === "function" ? action(state) : action;
}
function createUseContextState<State>(
context: React.Context<ContextState<State>>
context: React.Context<ContextState<State>>,
hash: (key: string) => number
) {
const useContextReducer = createUseContextReducer(context);
return ((contextKey?: any, initialState?: any) =>
useContextReducer(
contextKey,
basicStateReducer,
initialState
)) as UseContextState<State>;
const useContextReducer = createUseContextReducer(context, hash);
return function useContextState(contextKey?: any, initialState?: any) {
return useContextReducer(contextKey, basicStateReducer, initialState);
} as UseContextState<State>;
}
export default createUseContextState;

@@ -44,4 +44,4 @@ import * as React from "react";

if (enabled && devtoolsExtension) {
let changedKey;
if (lastStateSentFromDevtools.current !== state) {
let changedKey;
for (const key in state) {

@@ -48,0 +48,0 @@ if (prevState.current && state[key] !== prevState.current[key]) {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc