| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
// -------------------------------------------------------------------------- |
| 24 |
// --- States |
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
|
| 27 |
/** |
| 28 |
Local & Global States |
| 29 |
@packageDocumentation |
| 30 |
@module dome/data/states |
| 31 |
*/ |
| 32 |
|
| 33 |
import React from 'react'; |
| 34 |
import Emitter from 'events'; |
| 35 |
import isEqual from 'react-fast-compare'; |
| 36 |
import { Debug } from 'dome'; |
| 37 |
|
| 38 |
const D = new Debug('State'); |
| 39 |
|
| 40 |
// -------------------------------------------------------------------------- |
| 41 |
// --- State utilities |
| 42 |
// -------------------------------------------------------------------------- |
| 43 |
|
| 44 |
/** Alias to `[state,setState]` returned values */ |
| 45 |
export type State<A> = [A, (newValue: A) => void]; |
| 46 |
|
| 47 |
/** State field of an object state. */ |
| 48 |
export function keyOf<A, K extends keyof A>( |
| 49 |
state: State<A>, |
| 50 |
key: K, |
| 51 |
): State<A[K]> { |
| 52 |
const [props, setProps] = state; |
| 53 |
return [props[key], (value: A[K]) => { |
| 54 |
const newProps = { ...props }; |
| 55 |
newProps[key] = value; |
| 56 |
setProps(newProps); |
| 57 |
}]; |
| 58 |
} |
| 59 |
|
| 60 |
/** State index of an array state. */ |
| 61 |
export function index<A>( |
| 62 |
state: State<A[]>, |
| 63 |
idx: number, |
| 64 |
): State<A> { |
| 65 |
const [array, setArray] = state; |
| 66 |
return [array[idx], (value: A) => { |
| 67 |
const newArray = array.slice(); |
| 68 |
newArray[idx] = value; |
| 69 |
setArray(newArray); |
| 70 |
}]; |
| 71 |
} |
| 72 |
|
| 73 |
/** Log state updates in the console. */ |
| 74 |
export function debug<A>(msg: string, st: State<A>): State<A> { |
| 75 |
const [value, setValue] = st; |
| 76 |
return [value, (v) => { |
| 77 |
setValue(v); |
| 78 |
D.log(msg, v); |
| 79 |
}]; |
| 80 |
} |
| 81 |
|
| 82 |
/** Purely local value. No hook, no events, just a ref. */ |
| 83 |
export function local<A>(init: A): State<A> { |
| 84 |
const ref = { current: init }; |
| 85 |
return [ref.current, (v) => { ref.current = v; }]; |
| 86 |
} |
| 87 |
|
| 88 |
// -------------------------------------------------------------------------- |
| 89 |
// --- Global States |
| 90 |
// -------------------------------------------------------------------------- |
| 91 |
|
| 92 |
const UPDATE = 'dome.states.update'; |
| 93 |
|
| 94 |
/** Cross-component State. */ |
| 95 |
export class GlobalState<A> { |
| 96 |
|
| 97 |
private value: A; |
| 98 |
private emitter: Emitter; |
| 99 |
|
| 100 |
constructor(initValue: A) { |
| 101 |
this.value = initValue; |
| 102 |
this.emitter = new Emitter(); |
| 103 |
this.getValue = this.getValue.bind(this); |
| 104 |
this.setValue = this.setValue.bind(this); |
| 105 |
} |
| 106 |
|
| 107 |
/** Current state value. */ |
| 108 |
getValue(): A { return this.value; } |
| 109 |
|
| 110 |
/** Notify callbacks on change, using _deep_ structural comparison. */ |
| 111 |
setValue(value: A): void { |
| 112 |
if (!isEqual(value, this.value)) { |
| 113 |
this.value = value; |
| 114 |
this.emitter.emit(UPDATE, value); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** Callback Emitter. */ |
| 119 |
on(callback: (value: A) => void): void { |
| 120 |
this.emitter.on(UPDATE, callback); |
| 121 |
} |
| 122 |
|
| 123 |
/** Callback Emitter. */ |
| 124 |
off(callback: (value: A) => void): void { |
| 125 |
this.emitter.off(UPDATE, callback); |
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
/** React Hook, similar to `React.useState()`. |
| 131 |
Assignments to the global state also update _all_ |
| 132 |
its associated hooks and listeners. */ |
| 133 |
export function useGlobalState<A>(s: GlobalState<A>): State<A> { |
| 134 |
const [current, setCurrent] = React.useState<A>(s.getValue); |
| 135 |
React.useEffect(() => { |
| 136 |
s.on(setCurrent); |
| 137 |
return () => s.off(setCurrent); |
| 138 |
}, [s]); |
| 139 |
return [current, s.setValue]; |
| 140 |
} |
| 141 |
|
| 142 |
// -------------------------------------------------------------------------- |