2021-06-18 13:05:06 +02:00
|
|
|
import get from 'lodash-es/get'
|
|
|
|
import isPlainObject from 'lodash-es/isPlainObject'
|
|
|
|
import last from 'lodash-es/last'
|
2022-12-01 14:24:09 +01:00
|
|
|
import type {Prism} from './derivations/Interface'
|
|
|
|
import {isPrism} from './derivations/Interface'
|
2021-06-18 13:05:06 +02:00
|
|
|
import type {Pointer, PointerType} from './pointer'
|
2021-06-27 13:37:10 +02:00
|
|
|
import {isPointer} from './pointer'
|
2021-06-18 13:05:06 +02:00
|
|
|
import pointer, {getPointerMeta} from './pointer'
|
|
|
|
import type {$FixMe, $IntentionalAny} from './types'
|
|
|
|
import type {PathBasedReducer} from './utils/PathBasedReducer'
|
|
|
|
import updateDeep from './utils/updateDeep'
|
2022-11-28 14:45:39 +01:00
|
|
|
import prism from './derivations/prism/prism'
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
type Listener = (newVal: unknown) => void
|
|
|
|
|
|
|
|
enum ValueTypes {
|
|
|
|
Dict,
|
|
|
|
Array,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Interface for objects that can provide a derivation at a certain path.
|
|
|
|
*/
|
2021-06-18 13:05:06 +02:00
|
|
|
export interface IdentityDerivationProvider {
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
2022-04-29 19:00:14 +02:00
|
|
|
* Future: We could consider using a `Symbol.for("dataverse/IdentityDerivationProvider")` as a key here, similar to
|
|
|
|
* how {@link Iterable} works for `of`.
|
2022-01-19 13:06:13 +01:00
|
|
|
*/
|
2021-06-18 13:05:06 +02:00
|
|
|
readonly $$isIdentityDerivationProvider: true
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Returns a derivation of the value at the provided path.
|
|
|
|
*
|
2022-02-23 22:53:39 +01:00
|
|
|
* @param path - The path to create the derivation at.
|
2022-01-19 13:06:13 +01:00
|
|
|
*/
|
2022-12-01 14:20:50 +01:00
|
|
|
getIdentityDerivation(path: Array<string | number>): Prism<unknown>
|
2021-06-18 13:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const getTypeOfValue = (v: unknown): ValueTypes => {
|
|
|
|
if (Array.isArray(v)) return ValueTypes.Array
|
|
|
|
if (isPlainObject(v)) return ValueTypes.Dict
|
|
|
|
return ValueTypes.Other
|
|
|
|
}
|
|
|
|
|
|
|
|
const getKeyOfValue = (
|
|
|
|
v: unknown,
|
|
|
|
key: string | number,
|
|
|
|
vType: ValueTypes = getTypeOfValue(v),
|
|
|
|
): unknown => {
|
|
|
|
if (vType === ValueTypes.Dict && typeof key === 'string') {
|
|
|
|
return (v as $IntentionalAny)[key]
|
|
|
|
} else if (vType === ValueTypes.Array && isValidArrayIndex(key)) {
|
|
|
|
return (v as $IntentionalAny)[key]
|
|
|
|
} else {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const isValidArrayIndex = (key: string | number): boolean => {
|
|
|
|
const inNumber = typeof key === 'number' ? key : parseInt(key, 10)
|
|
|
|
return (
|
|
|
|
!isNaN(inNumber) &&
|
|
|
|
inNumber >= 0 &&
|
|
|
|
inNumber < Infinity &&
|
|
|
|
(inNumber | 0) === inNumber
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
class Scope {
|
|
|
|
children: Map<string | number, Scope> = new Map()
|
|
|
|
identityChangeListeners: Set<Listener> = new Set()
|
|
|
|
constructor(
|
|
|
|
readonly _parent: undefined | Scope,
|
|
|
|
readonly _path: (string | number)[],
|
|
|
|
) {}
|
|
|
|
|
|
|
|
addIdentityChangeListener(cb: Listener) {
|
|
|
|
this.identityChangeListeners.add(cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeIdentityChangeListener(cb: Listener) {
|
|
|
|
this.identityChangeListeners.delete(cb)
|
|
|
|
this._checkForGC()
|
|
|
|
}
|
|
|
|
|
|
|
|
removeChild(key: string | number) {
|
|
|
|
this.children.delete(key)
|
|
|
|
this._checkForGC()
|
|
|
|
}
|
|
|
|
|
|
|
|
getChild(key: string | number) {
|
|
|
|
return this.children.get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
getOrCreateChild(key: string | number) {
|
|
|
|
let child = this.children.get(key)
|
|
|
|
if (!child) {
|
|
|
|
child = child = new Scope(this, this._path.concat([key]))
|
|
|
|
this.children.set(key, child)
|
|
|
|
}
|
|
|
|
return child
|
|
|
|
}
|
|
|
|
|
|
|
|
_checkForGC() {
|
|
|
|
if (this.identityChangeListeners.size > 0) return
|
|
|
|
if (this.children.size > 0) return
|
|
|
|
|
|
|
|
if (this._parent) {
|
|
|
|
this._parent.removeChild(last(this._path) as string | number)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Wraps an object whose (sub)properties can be individually tracked.
|
|
|
|
*/
|
2021-06-18 13:05:06 +02:00
|
|
|
export default class Atom<State extends {}>
|
|
|
|
implements IdentityDerivationProvider
|
|
|
|
{
|
|
|
|
private _currentState: State
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2021-06-18 13:05:06 +02:00
|
|
|
readonly $$isIdentityDerivationProvider = true
|
|
|
|
private readonly _rootScope: Scope
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Convenience property that gives you a pointer to the root of the atom.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* Equivalent to `pointer({ root: thisAtom, path: [] })`.
|
|
|
|
*/
|
2021-06-18 13:05:06 +02:00
|
|
|
readonly pointer: Pointer<State>
|
|
|
|
|
|
|
|
constructor(initialState: State) {
|
|
|
|
this._currentState = initialState
|
|
|
|
this._rootScope = new Scope(undefined, [])
|
|
|
|
this.pointer = pointer({root: this as $FixMe, path: []})
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Sets the state of the atom.
|
|
|
|
*
|
2022-02-23 22:53:39 +01:00
|
|
|
* @param newState - The new state of the atom.
|
2022-01-19 13:06:13 +01:00
|
|
|
*/
|
2021-06-18 13:05:06 +02:00
|
|
|
setState(newState: State) {
|
|
|
|
const oldState = this._currentState
|
|
|
|
this._currentState = newState
|
|
|
|
|
|
|
|
this._checkUpdates(this._rootScope, oldState, newState)
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Gets the current state of the atom.
|
|
|
|
*/
|
2021-06-18 13:05:06 +02:00
|
|
|
getState() {
|
|
|
|
return this._currentState
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Gets the state of the atom at `path`.
|
|
|
|
*/
|
2021-06-18 13:05:06 +02:00
|
|
|
getIn(path: (string | number)[]): unknown {
|
|
|
|
return path.length === 0 ? this.getState() : get(this.getState(), path)
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Creates a new state object from the current one, where the value at `path`
|
|
|
|
* is replaced by the return value of `reducer`, then sets it.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* Doesn't mutate the old state, and preserves referential equality between
|
|
|
|
* values of the old state and the new state where possible.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* someAtom.getIn(['a']) // 1
|
|
|
|
* someAtom.reduceState(['a'], (state) => state + 1);
|
|
|
|
* someAtom.getIn(['a']) // 2
|
|
|
|
* ```
|
|
|
|
*
|
2022-02-23 22:53:39 +01:00
|
|
|
* @param path - The path to call the reducer at.
|
|
|
|
* @param reducer - The function to use for creating the new state.
|
2022-01-19 13:06:13 +01:00
|
|
|
*/
|
|
|
|
// TODO: Why is this a property and not a method?
|
2021-06-18 13:05:06 +02:00
|
|
|
reduceState: PathBasedReducer<State, State> = (
|
|
|
|
path: $IntentionalAny[],
|
|
|
|
reducer: $IntentionalAny,
|
|
|
|
) => {
|
|
|
|
const newState = updateDeep(this.getState(), path, reducer)
|
|
|
|
this.setState(newState)
|
|
|
|
return newState
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Sets the state of the atom at `path`.
|
|
|
|
*/
|
2021-06-18 13:05:06 +02:00
|
|
|
setIn(path: $FixMe[], val: $FixMe) {
|
|
|
|
return this.reduceState(path, () => val)
|
|
|
|
}
|
|
|
|
|
|
|
|
private _checkUpdates(scope: Scope, oldState: unknown, newState: unknown) {
|
|
|
|
if (oldState === newState) return
|
2022-06-09 19:12:40 +02:00
|
|
|
for (const cb of scope.identityChangeListeners) {
|
|
|
|
cb(newState)
|
|
|
|
}
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
if (scope.children.size === 0) return
|
2021-06-27 13:37:10 +02:00
|
|
|
|
|
|
|
// @todo we can probably skip checking value types
|
2021-06-18 13:05:06 +02:00
|
|
|
const oldValueType = getTypeOfValue(oldState)
|
|
|
|
const newValueType = getTypeOfValue(newState)
|
|
|
|
|
|
|
|
if (oldValueType === ValueTypes.Other && oldValueType === newValueType)
|
|
|
|
return
|
|
|
|
|
2022-06-09 19:12:40 +02:00
|
|
|
for (const [childKey, childScope] of scope.children) {
|
2021-06-18 13:05:06 +02:00
|
|
|
const oldChildVal = getKeyOfValue(oldState, childKey, oldValueType)
|
|
|
|
const newChildVal = getKeyOfValue(newState, childKey, newValueType)
|
|
|
|
this._checkUpdates(childScope, oldChildVal, newChildVal)
|
2022-06-09 19:12:40 +02:00
|
|
|
}
|
2021-06-18 13:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private _getOrCreateScopeForPath(path: (string | number)[]): Scope {
|
|
|
|
let curScope = this._rootScope
|
|
|
|
for (const pathEl of path) {
|
|
|
|
curScope = curScope.getOrCreateChild(pathEl)
|
|
|
|
}
|
|
|
|
return curScope
|
|
|
|
}
|
|
|
|
|
|
|
|
private _onPathValueChange = (
|
|
|
|
path: (string | number)[],
|
|
|
|
cb: (v: unknown) => void,
|
|
|
|
) => {
|
|
|
|
const scope = this._getOrCreateScopeForPath(path)
|
|
|
|
scope.identityChangeListeners.add(cb)
|
|
|
|
const untap = () => {
|
|
|
|
scope.identityChangeListeners.delete(cb)
|
|
|
|
}
|
|
|
|
return untap
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Returns a new derivation of the value at the provided path.
|
|
|
|
*
|
2022-02-23 22:53:39 +01:00
|
|
|
* @param path - The path to create the derivation at.
|
2022-01-19 13:06:13 +01:00
|
|
|
*/
|
2022-12-01 14:20:50 +01:00
|
|
|
getIdentityDerivation(path: Array<string | number>): Prism<unknown> {
|
2022-11-28 14:45:39 +01:00
|
|
|
const subscribe = (listener: (val: unknown) => void) =>
|
|
|
|
this._onPathValueChange(path, listener)
|
|
|
|
|
|
|
|
const getValue = () => this.getIn(path)
|
|
|
|
|
|
|
|
return prism(() => {
|
2022-12-01 12:58:59 +01:00
|
|
|
return prism.source(subscribe, getValue)
|
2022-11-28 14:45:39 +01:00
|
|
|
})
|
2021-06-18 13:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 14:20:50 +01:00
|
|
|
const identityDerivationWeakMap = new WeakMap<{}, Prism<unknown>>()
|
2021-06-18 13:05:06 +02:00
|
|
|
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Returns a derivation of the value at the provided pointer. Derivations are
|
|
|
|
* cached per pointer.
|
|
|
|
*
|
2022-02-23 22:53:39 +01:00
|
|
|
* @param pointer - The pointer to return the derivation at.
|
2022-01-19 13:06:13 +01:00
|
|
|
*/
|
2022-12-01 14:26:17 +01:00
|
|
|
export const pointerToPrism = <P extends PointerType<$IntentionalAny>>(
|
2021-06-18 13:05:06 +02:00
|
|
|
pointer: P,
|
2022-12-01 14:20:50 +01:00
|
|
|
): Prism<P extends PointerType<infer T> ? T : void> => {
|
2021-06-18 13:05:06 +02:00
|
|
|
const meta = getPointerMeta(pointer)
|
|
|
|
|
|
|
|
let derivation = identityDerivationWeakMap.get(meta)
|
|
|
|
if (!derivation) {
|
|
|
|
const root = meta.root
|
2022-04-23 21:32:12 +02:00
|
|
|
if (!isIdentityDerivationProvider(root)) {
|
2021-06-18 13:05:06 +02:00
|
|
|
throw new Error(
|
2022-12-01 14:26:17 +01:00
|
|
|
`Cannot run pointerToPrism() on a pointer whose root is not an IdentityChangeProvider`,
|
2021-06-18 13:05:06 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
const {path} = meta
|
|
|
|
derivation = root.getIdentityDerivation(path)
|
|
|
|
identityDerivationWeakMap.set(meta, derivation)
|
|
|
|
}
|
|
|
|
return derivation as $IntentionalAny
|
|
|
|
}
|
|
|
|
|
2022-04-23 21:32:12 +02:00
|
|
|
function isIdentityDerivationProvider(
|
2021-06-18 13:05:06 +02:00
|
|
|
val: unknown,
|
|
|
|
): val is IdentityDerivationProvider {
|
|
|
|
return (
|
|
|
|
typeof val === 'object' &&
|
|
|
|
val !== null &&
|
|
|
|
(val as $IntentionalAny)['$$isIdentityDerivationProvider'] === true
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:06:13 +01:00
|
|
|
/**
|
|
|
|
* Convenience function that returns a plain value from its argument, whether it
|
|
|
|
* is a pointer, a derivation or a plain value itself.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* For pointers, the value is returned by first creating a derivation, so it is
|
|
|
|
* reactive e.g. when used in a `prism`.
|
|
|
|
*
|
2022-04-09 15:02:39 +02:00
|
|
|
* @param input - The argument to return a value from.
|
2022-01-19 13:06:13 +01:00
|
|
|
*/
|
2022-04-09 15:02:39 +02:00
|
|
|
export const val = <
|
|
|
|
P extends
|
|
|
|
| PointerType<$IntentionalAny>
|
2022-12-01 14:20:50 +01:00
|
|
|
| Prism<$IntentionalAny>
|
2022-04-09 15:02:39 +02:00
|
|
|
| undefined
|
|
|
|
| null,
|
|
|
|
>(
|
|
|
|
input: P,
|
2021-06-18 13:05:06 +02:00
|
|
|
): P extends PointerType<infer T>
|
|
|
|
? T
|
2022-12-01 14:20:50 +01:00
|
|
|
: P extends Prism<infer T>
|
2021-06-18 13:05:06 +02:00
|
|
|
? T
|
2021-07-02 20:47:25 +02:00
|
|
|
: P extends undefined | null
|
|
|
|
? P
|
2021-06-18 13:05:06 +02:00
|
|
|
: unknown => {
|
2022-04-09 15:02:39 +02:00
|
|
|
if (isPointer(input)) {
|
2022-12-01 14:26:17 +01:00
|
|
|
return pointerToPrism(input).getValue() as $IntentionalAny
|
2022-12-01 14:22:49 +01:00
|
|
|
} else if (isPrism(input)) {
|
2022-04-09 15:02:39 +02:00
|
|
|
return input.getValue() as $IntentionalAny
|
2021-06-18 13:05:06 +02:00
|
|
|
} else {
|
2022-04-09 15:02:39 +02:00
|
|
|
return input as $IntentionalAny
|
2021-06-18 13:05:06 +02:00
|
|
|
}
|
|
|
|
}
|