Comparing version 0.0.17 to 0.0.18
@@ -23,3 +23,4 @@ import { A as AnyArray, a as AnyObject, b as AnyPrimitive, P as ProxyLens, U as Update } from './proxy-lens-d323fa3f'; | ||
declare function useLens<A>(proxy: ProxyLens<A>, shouldUpdate?: ShouldUpdate<A>): [ProxyValue<A>, Update<A>]; | ||
declare function useCreateLens<S>(initialState: S): ProxyLens<S>; | ||
export { Lens, useLens }; | ||
export { Lens, useCreateLens, useLens }; |
@@ -5,4 +5,18 @@ var __create = Object.create; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop2 in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop2)) | ||
__defNormalProp(a, prop2, b[prop2]); | ||
if (__getOwnPropSymbols) | ||
for (var prop2 of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop2)) | ||
__defNormalProp(a, prop2, b[prop2]); | ||
} | ||
return a; | ||
}; | ||
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); | ||
@@ -25,5 +39,5 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); | ||
}; | ||
var __toCommonJS = /* @__PURE__ */ ((cache) => { | ||
var __toCommonJS = /* @__PURE__ */ ((cache2) => { | ||
return (module2, temp) => { | ||
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp); | ||
return cache2 && cache2.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache2 && cache2.set(module2, temp), temp); | ||
}; | ||
@@ -35,2 +49,3 @@ })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0); | ||
__export(react_exports, { | ||
useCreateLens: () => useCreateLens, | ||
useLens: () => useLens | ||
@@ -43,2 +58,279 @@ }); | ||
// src/shallow-copy.ts | ||
var shallowCopy = /* @__PURE__ */ __name((obj) => { | ||
if (isObject(obj)) { | ||
return __spreadValues({}, obj); | ||
} else if (Array.isArray(obj)) { | ||
return [...obj]; | ||
} else { | ||
throw new Error("shallowCopy expected a plain object or array"); | ||
} | ||
}, "shallowCopy"); | ||
// src/basic-lens.ts | ||
var identity = Object.freeze({ | ||
get(s) { | ||
return s; | ||
}, | ||
set(s, a) { | ||
return a; | ||
} | ||
}); | ||
var basicLens = /* @__PURE__ */ __name(() => identity, "basicLens"); | ||
var refine = /* @__PURE__ */ __name((lens, get, set) => { | ||
return { | ||
get(s) { | ||
const a = lens.get(s); | ||
return get(a); | ||
}, | ||
set(s, b) { | ||
const a = lens.get(s); | ||
return lens.set(s, set(a, b)); | ||
} | ||
}; | ||
}, "refine"); | ||
var prop = /* @__PURE__ */ __name((lens, key) => { | ||
return refine(lens, (s) => s[key], (a, ak) => { | ||
const copy = shallowCopy(a); | ||
copy[key] = ak; | ||
return copy; | ||
}); | ||
}, "prop"); | ||
// src/key-path-to-string.ts | ||
var cache = /* @__PURE__ */ new WeakMap(); | ||
var IS_NUMBER_STRING = /^\d+$/; | ||
var keyPathToString = /* @__PURE__ */ __name((keyPath) => { | ||
let cached = cache.get(keyPath); | ||
if (!cached) { | ||
cached = "root"; | ||
for (let key of keyPath) { | ||
if (typeof key === "symbol" || typeof key === "number" || key.match(IS_NUMBER_STRING)) { | ||
cached += `[${String(key)}]`; | ||
} else { | ||
cached += `.${key}`; | ||
} | ||
} | ||
cache.set(keyPath, cached); | ||
} | ||
return cached; | ||
}, "keyPathToString"); | ||
// src/react-devtools.ts | ||
var ReactDevtools = { | ||
isCalledInsideReactDevtools: () => { | ||
var _a; | ||
const err = new Error(); | ||
return (_a = err.stack) == null ? void 0 : _a.includes("react_devtools_backend"); | ||
} | ||
}; | ||
// src/proxy-lens.ts | ||
var THROW_ON_COPY = Symbol(); | ||
var focusProp = /* @__PURE__ */ __name((focus, key) => { | ||
return { | ||
keyPath: [...focus.keyPath, key], | ||
lens: prop(focus.lens, key) | ||
}; | ||
}, "focusProp"); | ||
var proxyLens = /* @__PURE__ */ __name((storeFactory, focus = { lens: basicLens(), keyPath: [] }) => { | ||
const keyCache = {}; | ||
let $key; | ||
let getStore; | ||
const proxy = new Proxy({}, { | ||
get(_target, key) { | ||
if (key === "$$typeof") { | ||
return void 0; | ||
} | ||
if (key === "$key") { | ||
$key != null ? $key : $key = keyPathToString(focus.keyPath); | ||
return $key; | ||
} | ||
if (key === "getStore") { | ||
getStore != null ? getStore : getStore = () => storeFactory(focus); | ||
return getStore; | ||
} | ||
if (keyCache[key] === void 0) { | ||
const nextFocus = focusProp(focus, key); | ||
const nextProxy = proxyLens(storeFactory, nextFocus); | ||
keyCache[key] = nextProxy; | ||
} | ||
return keyCache[key]; | ||
}, | ||
ownKeys(_target) { | ||
return ["$key", "use", "getStore", THROW_ON_COPY]; | ||
}, | ||
getOwnPropertyDescriptor(_target, key) { | ||
if (key === "$key" || key === "use" || key === "getStore") { | ||
return { | ||
configurable: true, | ||
enumerable: true, | ||
writable: false, | ||
value: proxy[key] | ||
}; | ||
} | ||
if (ReactDevtools.isCalledInsideReactDevtools()) { | ||
return { | ||
configurable: true, | ||
enumerable: false, | ||
value: void 0 | ||
}; | ||
} | ||
throw new Error("ProxyLens threw because you tried to access all property descriptors\u2014probably through `{ ...lens }` or `Object.assign({}, lens)`. Doing this will break the type safety offered by this library so it is forbidden. Sorry, buddy pal."); | ||
}, | ||
getPrototypeOf() { | ||
return null; | ||
}, | ||
preventExtensions() { | ||
return true; | ||
}, | ||
isExtensible() { | ||
return false; | ||
}, | ||
set() { | ||
throw new Error("Cannot set property on ProxyLens"); | ||
}, | ||
deleteProperty() { | ||
throw new Error("Cannot delete property on ProxyLens"); | ||
} | ||
}); | ||
return proxy; | ||
}, "proxyLens"); | ||
// src/subscription-graph.ts | ||
var id = keyPathToString; | ||
var SubscriptionNode = class { | ||
constructor(keyPath) { | ||
this.keyPath = keyPath; | ||
this.id = id(keyPath); | ||
if (keyPath.length === 0) { | ||
this.ancestor = { none: true }; | ||
} else { | ||
this.ancestor = { none: false, keyPath: this.keyPath.slice(0, -1) }; | ||
} | ||
} | ||
listeners = /* @__PURE__ */ new Set(); | ||
id; | ||
ancestor; | ||
subscribe(listener) { | ||
this.listeners.add(listener); | ||
return () => this.listeners.delete(listener); | ||
} | ||
notify() { | ||
this.listeners.forEach((fn) => fn()); | ||
} | ||
get size() { | ||
return this.listeners.size; | ||
} | ||
}; | ||
__name(SubscriptionNode, "SubscriptionNode"); | ||
var SubscriptionGraph = class { | ||
nodes = /* @__PURE__ */ new Map(); | ||
parents = /* @__PURE__ */ new Map(); | ||
children = /* @__PURE__ */ new Map(); | ||
notify(keyPath) { | ||
const nodeId = id(keyPath); | ||
const node = this.nodes.get(nodeId); | ||
if (node) { | ||
this.notifyAncestors(node); | ||
this.notifySelfAndChildren(node); | ||
} | ||
} | ||
subscribe(keyPath, listener) { | ||
const node = this.addNode(keyPath); | ||
const unsubscribe = node.subscribe(listener); | ||
return () => { | ||
unsubscribe(); | ||
this.clean(node); | ||
}; | ||
} | ||
get size() { | ||
return this.nodes.size; | ||
} | ||
addNode(keyPath) { | ||
const nodeId = id(keyPath); | ||
let node = this.nodes.get(nodeId); | ||
if (node) { | ||
return node; | ||
} | ||
node = new SubscriptionNode(keyPath); | ||
this.nodes.set(node.id, node); | ||
this.children.set(node.id, /* @__PURE__ */ new Set()); | ||
if (node.ancestor.none) { | ||
return node; | ||
} | ||
const parent = this.addNode(node.ancestor.keyPath); | ||
this.parents.set(node.id, parent); | ||
const siblings = this.children.get(parent.id); | ||
if (siblings === void 0) { | ||
throw new Error("Unexpected Error"); | ||
} | ||
siblings.add(node); | ||
return node; | ||
} | ||
notifyAncestors(node) { | ||
const ancestor = this.parents.get(node.id); | ||
if (ancestor) { | ||
this.notifyAncestors(ancestor); | ||
ancestor.notify(); | ||
} | ||
} | ||
notifySelfAndChildren(node) { | ||
var _a; | ||
node.notify(); | ||
const children = (_a = this.children.get(node.id)) != null ? _a : /* @__PURE__ */ new Set(); | ||
for (const child of children) { | ||
this.notifySelfAndChildren(child); | ||
} | ||
} | ||
clean(node) { | ||
var _a, _b; | ||
const children = (_a = this.children.get(node.id)) != null ? _a : /* @__PURE__ */ new Set(); | ||
if (children.size > 0 || node.size > 0) { | ||
return; | ||
} | ||
this.nodes.delete(node.id); | ||
this.children.delete(node.id); | ||
const parent = this.parents.get(node.id); | ||
if (parent) { | ||
this.parents.delete(node.id); | ||
const siblings = (_b = this.children.get(parent.id)) != null ? _b : /* @__PURE__ */ new Set(); | ||
siblings.delete(node); | ||
this.clean(parent); | ||
} | ||
} | ||
}; | ||
__name(SubscriptionGraph, "SubscriptionGraph"); | ||
// src/store.ts | ||
var createStoreFactory = /* @__PURE__ */ __name((initialState) => { | ||
const graph = new SubscriptionGraph(); | ||
let snapshot = initialState; | ||
return ({ keyPath, lens }) => { | ||
return { | ||
getSnapshot() { | ||
return lens.get(snapshot); | ||
}, | ||
subscribe(listener) { | ||
return graph.subscribe(keyPath, listener); | ||
}, | ||
update(updater) { | ||
const prev = lens.get(snapshot); | ||
const next = updater(prev); | ||
if (Object.is(next, prev)) { | ||
return; | ||
} | ||
snapshot = lens.set(snapshot, next); | ||
graph.notify(keyPath); | ||
} | ||
}; | ||
}; | ||
}, "createStoreFactory"); | ||
// src/create-lens.ts | ||
var createLens = /* @__PURE__ */ __name((initialState) => { | ||
const factory = createStoreFactory(initialState); | ||
return proxyLens(factory); | ||
}, "createLens"); | ||
// src/proxy-value.ts | ||
@@ -195,7 +487,12 @@ var isProxyable = /* @__PURE__ */ __name((obj) => Array.isArray(obj) || isObject(obj), "isProxyable"); | ||
__name(useLens, "useLens"); | ||
function useCreateLens(initialState) { | ||
return import_react.default.useMemo(() => createLens(initialState), []); | ||
} | ||
__name(useCreateLens, "useCreateLens"); | ||
module.exports = __toCommonJS(react_exports); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
useCreateLens, | ||
useLens | ||
}); | ||
//# sourceMappingURL=react.js.map |
{ | ||
"name": "concave", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"description": "A Lens-like interface for state management in React", | ||
@@ -5,0 +5,0 @@ "main": "dist/create-lens.js", |
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
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
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
128822
1328
0