2021-06-18 13:05:06 +02:00
|
|
|
import type {IdentityDerivationProvider} from './Atom'
|
2021-08-07 23:24:37 +02:00
|
|
|
import type {Pointer} from './pointer'
|
2021-06-18 13:05:06 +02:00
|
|
|
import pointer from './pointer'
|
2021-08-07 23:24:37 +02:00
|
|
|
import type {IBox} from './Box'
|
2021-06-18 13:05:06 +02:00
|
|
|
import Box from './Box'
|
|
|
|
import type {$FixMe, $IntentionalAny} from './types'
|
2021-08-07 23:24:37 +02:00
|
|
|
import {valueDerivation} from './Atom'
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
export default class PointerProxy<O extends {}>
|
|
|
|
implements IdentityDerivationProvider
|
|
|
|
{
|
|
|
|
readonly $$isIdentityDerivationProvider = true
|
|
|
|
private readonly _currentPointerBox: IBox<Pointer<O>>
|
|
|
|
readonly pointer: Pointer<O>
|
|
|
|
|
|
|
|
constructor(currentPointer: Pointer<O>) {
|
|
|
|
this._currentPointerBox = new Box(currentPointer)
|
|
|
|
this.pointer = pointer({root: this as $FixMe, path: []})
|
|
|
|
}
|
|
|
|
|
|
|
|
setPointer(p: Pointer<O>) {
|
|
|
|
this._currentPointerBox.set(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
getIdentityDerivation(path: Array<string | number>) {
|
|
|
|
return this._currentPointerBox.derivation.flatMap((p) => {
|
|
|
|
const subPointer = path.reduce(
|
|
|
|
(pointerSoFar, pathItem) => (pointerSoFar as $IntentionalAny)[pathItem],
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
return valueDerivation(subPointer)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|