Comparing version 0.0.1 to 0.0.2
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.update = exports.prop = exports.refine = exports.basicLens = void 0; | ||
var shallow_copy_1 = require("./shallow-copy"); | ||
var identity = Object.freeze({ | ||
get: function (s) { | ||
const shallow_copy_1 = require("./shallow-copy"); | ||
const identity = Object.freeze({ | ||
get(s) { | ||
return s; | ||
}, | ||
set: function (s, a) { | ||
set(s, a) { | ||
return a; | ||
}, | ||
}); | ||
var basicLens = function () { return identity; }; | ||
const basicLens = () => identity; | ||
exports.basicLens = basicLens; | ||
var refine = function (lens, get, set) { | ||
const refine = (lens, get, set) => { | ||
return { | ||
get: function (s) { | ||
var a = lens.get(s); | ||
get(s) { | ||
const a = lens.get(s); | ||
return get(a); | ||
}, | ||
set: function (s, b) { | ||
var a = lens.get(s); | ||
set(s, b) { | ||
const a = lens.get(s); | ||
return lens.set(s, set(a, b)); | ||
@@ -28,5 +28,5 @@ }, | ||
exports.refine = refine; | ||
var prop = function (lens, key) { | ||
return (0, exports.refine)(lens, function (s) { return s[key]; }, function (a, ak) { | ||
var copy = (0, shallow_copy_1.shallowCopy)(a); | ||
const prop = (lens, key) => { | ||
return (0, exports.refine)(lens, (s) => s[key], (a, ak) => { | ||
const copy = (0, shallow_copy_1.shallowCopy)(a); | ||
copy[key] = ak; | ||
@@ -37,8 +37,7 @@ return copy; | ||
exports.prop = prop; | ||
var update = function (lens, updater) { | ||
return function (s) { | ||
var a = lens.get(s); | ||
return lens.set(s, updater(a)); | ||
}; | ||
const update = (lens, updater) => (s) => { | ||
const a = lens.get(s); | ||
return lens.set(s, updater(a)); | ||
}; | ||
exports.update = update; | ||
//# sourceMappingURL=basic-lens.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.externalStore = void 0; | ||
var externalStore = function (initialState) { | ||
var listeners = new Set(); | ||
var state = initialState; | ||
const externalStore = (initialState) => { | ||
const listeners = new Set(); | ||
let state = initialState; | ||
return { | ||
subscribe: function (sub) { | ||
subscribe(sub) { | ||
listeners.add(sub); | ||
return function () { return listeners.delete(sub); }; | ||
return () => listeners.delete(sub); | ||
}, | ||
getSnapshot: function () { | ||
getSnapshot() { | ||
return state; | ||
}, | ||
update: function (updater) { | ||
update(updater) { | ||
state = updater(state); | ||
listeners.forEach(function (fn) { return fn(); }); | ||
listeners.forEach((fn) => fn()); | ||
}, | ||
@@ -22,1 +22,2 @@ }; | ||
exports.externalStore = externalStore; | ||
//# sourceMappingURL=external-store.js.map |
@@ -6,1 +6,2 @@ "use strict"; | ||
Object.defineProperty(exports, "react", { enumerable: true, get: function () { return react_1.react; } }); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isObject = void 0; | ||
var isObject = function (obj) { return Object.prototype.toString.call(obj) === "[object Object]"; }; | ||
const isObject = (obj) => Object.prototype.toString.call(obj) === "[object Object]"; | ||
exports.isObject = isObject; | ||
//# sourceMappingURL=is-object.js.map |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.proxyLens = void 0; | ||
var basic_lens_1 = require("./basic-lens"); | ||
var is_object_1 = require("./is-object"); | ||
var PROXY_VALUE = Symbol(); | ||
var TO_LENS = Symbol(); | ||
var THROW_ON_COPY = Symbol(); | ||
var keyCounter = 0; | ||
var proxyLensKey = function () { return "$$ProxyLens(".concat(keyCounter++, ")"); }; | ||
var isProxyable = function (obj) { return Array.isArray(obj) || (0, is_object_1.isObject)(obj); }; | ||
var createUse = function (fixtures, lens) { | ||
var use = fixtures.createUse(fixtures.lens); | ||
return function (shouldUpdate) { | ||
var _a = use(shouldUpdate), state = _a[0], setState = _a[1]; | ||
var next = proxyValue(state, lens); | ||
const basic_lens_1 = require("./basic-lens"); | ||
const is_object_1 = require("./is-object"); | ||
const PROXY_VALUE = Symbol(); | ||
const TO_LENS = Symbol(); | ||
const THROW_ON_COPY = Symbol(); | ||
let keyCounter = 0; | ||
const proxyLensKey = () => `$$ProxyLens(${keyCounter++})`; | ||
const isProxyable = (obj) => Array.isArray(obj) || (0, is_object_1.isObject)(obj); | ||
const createUse = (fixtures, lens) => { | ||
const use = fixtures.createUse(fixtures.lens); | ||
return (shouldUpdate) => { | ||
const [state, setState] = use(shouldUpdate); | ||
const next = proxyValue(state, lens); | ||
return [next, setState]; | ||
}; | ||
}; | ||
var proxyValue = function (obj, lens) { | ||
const proxyValue = (obj, lens) => { | ||
if (!isProxyable(obj)) { | ||
@@ -47,5 +27,5 @@ return obj; | ||
} | ||
var toJSON; | ||
var proxy = new Proxy(obj, { | ||
get: function (target, key) { | ||
let toJSON; | ||
const proxy = new Proxy(obj, { | ||
get(target, key) { | ||
if (key === PROXY_VALUE) { | ||
@@ -55,3 +35,3 @@ return proxy; | ||
if (key === "toJSON") { | ||
toJSON !== null && toJSON !== void 0 ? toJSON : (toJSON = function () { return target; }); | ||
toJSON !== null && toJSON !== void 0 ? toJSON : (toJSON = () => target); | ||
return toJSON; | ||
@@ -62,10 +42,10 @@ } | ||
} | ||
var nextValue = target[key]; | ||
var nextLens = lens[key]; | ||
const nextValue = target[key]; | ||
const nextLens = lens[key]; | ||
return proxyValue(nextValue, nextLens); | ||
}, | ||
ownKeys: function (target) { | ||
return __spreadArray(__spreadArray([], Reflect.ownKeys(target), true), [THROW_ON_COPY], false); | ||
ownKeys(target) { | ||
return [...Reflect.ownKeys(target), THROW_ON_COPY]; | ||
}, | ||
getOwnPropertyDescriptor: function (target, key) { | ||
getOwnPropertyDescriptor(target, key) { | ||
if (key === THROW_ON_COPY) { | ||
@@ -76,6 +56,6 @@ throw new Error(""); | ||
}, | ||
set: function () { | ||
set() { | ||
throw new Error("Cannot set property on ProxyValue"); | ||
}, | ||
deleteProperty: function () { | ||
deleteProperty() { | ||
throw new Error("Cannot delete property on ProxyValue"); | ||
@@ -97,9 +77,9 @@ }, | ||
}; | ||
var proxyLens = function (fixtures) { | ||
var cache = {}; | ||
var use; | ||
var toLens; | ||
var $key; | ||
var proxy = new Proxy({}, { | ||
get: function (_target, key) { | ||
const proxyLens = (fixtures) => { | ||
const cache = {}; | ||
let use; | ||
let toLens; | ||
let $key; | ||
const proxy = new Proxy({}, { | ||
get(_target, key) { | ||
if (key === "$key") { | ||
@@ -115,3 +95,3 @@ $key !== null && $key !== void 0 ? $key : ($key = proxyLensKey()); | ||
if (key === TO_LENS) { | ||
toLens !== null && toLens !== void 0 ? toLens : (toLens = function () { return proxy; }); | ||
toLens !== null && toLens !== void 0 ? toLens : (toLens = () => proxy); | ||
return toLens; | ||
@@ -124,4 +104,4 @@ } | ||
if (cache[key] === undefined) { | ||
var nextFixtures = __assign(__assign({}, fixtures), { lens: (0, basic_lens_1.prop)(fixtures.lens, key) }); | ||
var nextProxy = (0, exports.proxyLens)(nextFixtures); | ||
const nextFixtures = Object.assign(Object.assign({}, fixtures), { lens: (0, basic_lens_1.prop)(fixtures.lens, key) }); | ||
const nextProxy = (0, exports.proxyLens)(nextFixtures); | ||
cache[key] = nextProxy; | ||
@@ -131,6 +111,6 @@ } | ||
}, | ||
set: function () { | ||
set() { | ||
throw new Error("Cannot set property on ProxyLens"); | ||
}, | ||
deleteProperty: function () { | ||
deleteProperty() { | ||
throw new Error("Cannot delete property on ProxyLens"); | ||
@@ -142,1 +122,2 @@ }, | ||
exports.proxyLens = proxyLens; | ||
//# sourceMappingURL=proxy-lens.js.map |
@@ -8,37 +8,35 @@ "use strict"; | ||
exports.react = void 0; | ||
var react_1 = __importDefault(require("react")); | ||
var basic_lens_1 = require("./basic-lens"); | ||
var external_store_1 = require("./external-store"); | ||
var proxy_lens_1 = require("./proxy-lens"); | ||
var use_sync_external_store_with_lens_1 = require("./use-sync-external-store-with-lens"); | ||
var nothing = Symbol(); | ||
var react = function () { | ||
var ExternalStoreContext = react_1.default.createContext(nothing); | ||
const react_1 = __importDefault(require("react")); | ||
const basic_lens_1 = require("./basic-lens"); | ||
const external_store_1 = require("./external-store"); | ||
const proxy_lens_1 = require("./proxy-lens"); | ||
const use_sync_external_store_with_lens_1 = require("./use-sync-external-store-with-lens"); | ||
const nothing = Symbol(); | ||
const react = () => { | ||
const ExternalStoreContext = react_1.default.createContext(nothing); | ||
ExternalStoreContext.displayName = "Lens(ExternalStoreContext)"; | ||
var createUse = function (lens) { | ||
return function (shouldUpdate) { | ||
var store = react_1.default.useContext(ExternalStoreContext); | ||
if (store === nothing) { | ||
throw new Error("Cannot call `lens.use()` in a component outside of <LensProvider />"); | ||
} | ||
return (0, use_sync_external_store_with_lens_1.useSyncExternalStoreWithLens)(store, lens, shouldUpdate); | ||
}; | ||
const createUse = (lens) => (shouldUpdate) => { | ||
const store = react_1.default.useContext(ExternalStoreContext); | ||
if (store === nothing) { | ||
throw new Error("Cannot call `lens.use()` in a component outside of <LensProvider />"); | ||
} | ||
return (0, use_sync_external_store_with_lens_1.useSyncExternalStoreWithLens)(store, lens, shouldUpdate); | ||
}; | ||
var lens = (0, proxy_lens_1.proxyLens)({ | ||
const lens = (0, proxy_lens_1.proxyLens)({ | ||
lens: (0, basic_lens_1.basicLens)(), | ||
createUse: createUse, | ||
createUse, | ||
}); | ||
var LensProvider = function (props) { | ||
var storeRef = react_1.default.useRef(); | ||
const LensProvider = (props) => { | ||
const storeRef = react_1.default.useRef(); | ||
if (!storeRef.current) { | ||
storeRef.current = (0, external_store_1.externalStore)(props.value); | ||
} | ||
var store = storeRef.current; | ||
react_1.default.useEffect(function () { | ||
let store = storeRef.current; | ||
react_1.default.useEffect(() => { | ||
if (store.getSnapshot() !== props.value) { | ||
store.update(function () { return props.value; }); | ||
store.update(() => props.value); | ||
} | ||
}, [props.value]); | ||
react_1.default.useEffect(function () { | ||
return store.subscribe(function () { return props.onChange(store.getSnapshot()); }); | ||
react_1.default.useEffect(() => { | ||
return store.subscribe(() => props.onChange(store.getSnapshot())); | ||
}, [props.onChange]); | ||
@@ -48,5 +46,5 @@ return react_1.default.createElement(ExternalStoreContext.Provider, { value: store }, props.children); | ||
LensProvider.displayName = "Lens(Provider)"; | ||
var StatefulLensProvider = react_1.default.forwardRef(function (props, ref) { | ||
var _a = react_1.default.useState(props.initialValue), state = _a[0], setState = _a[1]; | ||
react_1.default.useImperativeHandle(ref, function () { return ({ state: state }); }, [state]); | ||
const StatefulLensProvider = react_1.default.forwardRef((props, ref) => { | ||
const [state, setState] = react_1.default.useState(props.initialValue); | ||
react_1.default.useImperativeHandle(ref, () => ({ state }), [state]); | ||
return (react_1.default.createElement(LensProvider, { value: state, onChange: setState }, props.children)); | ||
@@ -57,6 +55,7 @@ }); | ||
return { | ||
lens: lens, | ||
LensProvider: LensProvider, | ||
lens, | ||
LensProvider, | ||
}; | ||
}; | ||
exports.react = react; | ||
//# sourceMappingURL=react.js.map |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.shallowCopy = void 0; | ||
var is_object_1 = require("./is-object"); | ||
var deepCopy = function (obj) { return JSON.parse(JSON.stringify(obj)); }; | ||
var shallowCopy = function (obj) { | ||
const is_object_1 = require("./is-object"); | ||
const deepCopy = (obj) => JSON.parse(JSON.stringify(obj)); | ||
const shallowCopy = (obj) => { | ||
/** | ||
@@ -32,6 +12,6 @@ * Need to do this check to ensure that referential | ||
if ((0, is_object_1.isObject)(obj)) { | ||
return __assign({}, obj); | ||
return Object.assign({}, obj); | ||
} | ||
else if (Array.isArray(obj)) { | ||
return __spreadArray([], obj, true); | ||
return [...obj]; | ||
} | ||
@@ -46,1 +26,2 @@ else { | ||
exports.shallowCopy = shallowCopy; | ||
//# sourceMappingURL=shallow-copy.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.normalizeShouldUpdate = void 0; | ||
var normalizeShouldUpdate = function (shouldUpdate) { | ||
const normalizeShouldUpdate = (shouldUpdate) => { | ||
if (typeof shouldUpdate === "function") { | ||
@@ -9,5 +9,4 @@ return shouldUpdate; | ||
if (Array.isArray(shouldUpdate)) { | ||
return function (prev, next) { | ||
for (var _i = 0, shouldUpdate_1 = shouldUpdate; _i < shouldUpdate_1.length; _i++) { | ||
var key = shouldUpdate_1[_i]; | ||
return (prev, next) => { | ||
for (const key of shouldUpdate) { | ||
if (prev[key] !== next[key]) { | ||
@@ -21,4 +20,4 @@ return true; | ||
else { | ||
return function (prev, next) { | ||
for (var key in shouldUpdate) { | ||
return (prev, next) => { | ||
for (const key in shouldUpdate) { | ||
if (shouldUpdate[key] === true && prev[key] !== next[key]) { | ||
@@ -33,1 +32,2 @@ return true; | ||
exports.normalizeShouldUpdate = normalizeShouldUpdate; | ||
//# sourceMappingURL=should-update.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.testLens = void 0; | ||
var basic_lens_1 = require("./basic-lens"); | ||
var external_store_1 = require("./external-store"); | ||
var proxy_lens_1 = require("./proxy-lens"); | ||
var use_sync_external_store_with_lens_1 = require("./use-sync-external-store-with-lens"); | ||
var testLens = function (initialState) { | ||
var store = (0, external_store_1.externalStore)(initialState); | ||
var lens = (0, proxy_lens_1.proxyLens)({ | ||
const basic_lens_1 = require("./basic-lens"); | ||
const external_store_1 = require("./external-store"); | ||
const proxy_lens_1 = require("./proxy-lens"); | ||
const use_sync_external_store_with_lens_1 = require("./use-sync-external-store-with-lens"); | ||
const testLens = (initialState) => { | ||
const store = (0, external_store_1.externalStore)(initialState); | ||
const lens = (0, proxy_lens_1.proxyLens)({ | ||
lens: (0, basic_lens_1.basicLens)(), | ||
createUse: function (lens) { return function (shouldUpdate) { return (0, use_sync_external_store_with_lens_1.useSyncExternalStoreWithLens)(store, lens, shouldUpdate); }; }, | ||
createUse: (lens) => (shouldUpdate) => (0, use_sync_external_store_with_lens_1.useSyncExternalStoreWithLens)(store, lens, shouldUpdate), | ||
}); | ||
var ref = { | ||
const ref = { | ||
get current() { | ||
@@ -19,3 +19,3 @@ return store.getSnapshot(); | ||
set current(next) { | ||
store.update(function () { return next; }); | ||
store.update(() => next); | ||
}, | ||
@@ -26,1 +26,2 @@ }; | ||
exports.testLens = testLens; | ||
//# sourceMappingURL=test-lens.js.map |
@@ -7,15 +7,14 @@ "use strict"; | ||
exports.useSyncExternalStoreWithLens = void 0; | ||
var react_1 = __importDefault(require("react")); | ||
var basic_lens_1 = require("./basic-lens"); | ||
var should_update_1 = require("./should-update"); | ||
var nothing = Symbol(); | ||
var useSyncExternalStoreWithLens = function (store, lens, shouldUpdate) { | ||
if (shouldUpdate === void 0) { shouldUpdate = function () { return true; }; } | ||
const react_1 = __importDefault(require("react")); | ||
const basic_lens_1 = require("./basic-lens"); | ||
const should_update_1 = require("./should-update"); | ||
const nothing = Symbol(); | ||
const useSyncExternalStoreWithLens = (store, lens, shouldUpdate = () => true) => { | ||
/** | ||
* Track the previously resolved state, starting with `Nothing`. | ||
*/ | ||
var prevRef = react_1.default.useRef(nothing); | ||
var getSnapshot = function () { | ||
var prev = prevRef.current; | ||
var next = lens.get(store.getSnapshot()); | ||
const prevRef = react_1.default.useRef(nothing); | ||
const getSnapshot = () => { | ||
const prev = prevRef.current; | ||
const next = lens.get(store.getSnapshot()); | ||
/** | ||
@@ -28,3 +27,3 @@ * If the `prev` is `Nothing` then this is the first render, | ||
} | ||
var normalShouldUpdate = (0, should_update_1.normalizeShouldUpdate)(shouldUpdate); | ||
const normalShouldUpdate = (0, should_update_1.normalizeShouldUpdate)(shouldUpdate); | ||
/** | ||
@@ -42,4 +41,4 @@ * If we should update then return the `next`. | ||
}; | ||
var state = react_1.default.useSyncExternalStore(store.subscribe, getSnapshot); | ||
var setState = react_1.default.useCallback(function (updater) { return store.update((0, basic_lens_1.update)(lens, updater)); }, [store]); | ||
const state = react_1.default.useSyncExternalStore(store.subscribe, getSnapshot); | ||
const setState = react_1.default.useCallback((updater) => store.update((0, basic_lens_1.update)(lens, updater)), [store]); | ||
/** | ||
@@ -53,1 +52,2 @@ * Assign the current state to the previous state so that when `getSnapshot` | ||
exports.useSyncExternalStoreWithLens = useSyncExternalStoreWithLens; | ||
//# sourceMappingURL=use-sync-external-store-with-lens.js.map |
{ | ||
"name": "concave", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Basic lensing for React", | ||
@@ -9,3 +9,8 @@ "main": "build/index.js", | ||
"license": "MIT", | ||
"keywords": ["react", "lens", "optics", "state"], | ||
"keywords": [ | ||
"react", | ||
"lens", | ||
"optics", | ||
"state" | ||
], | ||
"repository": { | ||
@@ -12,0 +17,0 @@ "type": "git", |
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
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
32
31776
487