Comparing version 0.0.13 to 0.0.14
@@ -1,10 +0,1 @@ | ||
declare type Listener = () => void; | ||
declare type Unsubscribe = () => void; | ||
declare type Updater$1<A> = (a: A) => A; | ||
declare type Store<A> = { | ||
getSnapshot(): A; | ||
subscribe(onStoreChange: Listener): Unsubscribe; | ||
update(updater: Updater$1<A>): void; | ||
}; | ||
declare type ShouldUpdateBoolean = boolean; | ||
@@ -24,4 +15,4 @@ declare type ShouldUpdateArray<A> = (keyof A)[]; | ||
declare type JSON = AnyArray | AnyObject | AnyPrimitive; | ||
declare type Updater<A> = (a: A) => A; | ||
declare type Update<A> = (updater: Updater<A>) => void; | ||
declare type Updater$1<A> = (a: A) => A; | ||
declare type Update<A> = (updater: Updater$1<A>) => void; | ||
declare type UseLensProxy<A> = (shouldUpdate?: ShouldUpdate<A>) => readonly [ProxyValue<A>, Update<A>]; | ||
@@ -70,2 +61,11 @@ declare type BaseProxyValue<A> = { | ||
declare type Listener = () => void; | ||
declare type Unsubscribe = () => void; | ||
declare type Updater<A> = (a: A) => A; | ||
declare type Store<A> = { | ||
getSnapshot(): A; | ||
subscribe(onStoreChange: Listener): Unsubscribe; | ||
update(updater: Updater<A>): void; | ||
}; | ||
declare type Lens<A> = Omit<ProxyLens<A>, symbol>; | ||
@@ -75,2 +75,2 @@ declare const concave: <S>(initialState: S) => [Lens<S>, Store<S>]; | ||
export { Lens, concave, useConcave }; | ||
export { Lens, Store, concave, useConcave }; |
@@ -117,131 +117,2 @@ var __create = Object.create; | ||
// 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); | ||
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/react-devtools.ts | ||
@@ -415,2 +286,132 @@ var ReactDevtools = { | ||
// 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/use-store.ts | ||
@@ -417,0 +418,0 @@ var import_react = __toESM(require("react")); |
{ | ||
"name": "concave", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "A Lens-like interface for state management in React", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -15,12 +15,4 @@ # 🧐 Concave | ||
### `stateless<S>(): [Lens<S>, LensProvider<S>]` | ||
### `useConcave<S>(initialState: S): [Lens<S>, Store<S>]` | ||
### `useStateful<S>(initialState: S): [Lens<S>, MutableRefObject<S>]` | ||
The React equivalent of `stateful` as it is convenient to create a stateful lens as part of initializing a component. | ||
Note: There is no `useStateless` because the return value of `stateless` is static and can just be constructed outside the React life-cycle. | ||
### `LensProvider<S>` | ||
### `Lens<A>` | ||
@@ -42,5 +34,6 @@ | ||
2. If do use a shouldUpdate argument for the lens, you can either memoize it with `React.useMemo` or `React.useCallback` or store it outside | ||
of the component. | ||
2. If do use a shouldUpdate argument for the lens, you can either memoize it with `React.useMemo` or `React.useCallback` or store it outside of the component. | ||
3. Memoize every component with `React.memo` foward lenses as props rather than globals. | ||
## Example | ||
@@ -47,0 +40,0 @@ |
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
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
107984
1054
0
108