Skip to content
Snippets Groups Projects
Commit e1553a45 authored by Loïc Correnson's avatar Loïc Correnson
Browse files

[dome] fixed synchronized array API

parent bfa4eb3d
No related branches found
No related tags found
No related merge requests found
...@@ -235,15 +235,21 @@ export abstract class Model<Key, Row> { ...@@ -235,15 +235,21 @@ export abstract class Model<Key, Row> {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
For a component to re-render on any updates and reloads. Make the component to synchronize with the model and re-render on
The returned number can be used to memoise effects and callbacks. every updates.
@param sync - whether to synchronize on model updates or not, `true`
by default.
@return a number that can be used to memoize other effects
*/ */
export function useModel(model: Model<any, any>): number { export function useModel(model: Model<any, any>, sync = true): number {
const [age, setAge] = React.useState(0); const [age, setAge] = React.useState(0);
React.useEffect(() => { React.useEffect(() => {
const w = model.link(() => setImmediate(() => setAge(age + 1))); if (sync) {
return w.unlink; const w = model.link(() => setImmediate(() => setAge(age + 1)));
return w.unlink;
}
return undefined;
}); });
return age; return age;
} }
......
...@@ -422,37 +422,23 @@ export function reloadArray<K, A>(arr: Array<K, A>) { ...@@ -422,37 +422,23 @@ export function reloadArray<K, A>(arr: Array<K, A>) {
/** /**
Use Synchronized Array (Custom React Hook). Use Synchronized Array (Custom React Hook).
This React Hook is _not_ responsive to model updates, it only
returns the array model.
To listen to array updates, use `Models.useModel(model)` or `useSyncArray()`.
Array views automatically listen to model updates.
*/
export function useSyncModel<K, A>(
arr: Array<K, A>,
): CompactModel<K, A> {
Dome.useUpdate(PROJECT);
const st = getSyncArray(arr);
React.useEffect(st.update);
Server.useSignal(arr.signal, st.fetch);
return st.model;
}
/** Unless specified, the hook makes the component re-rendering on every
Use Synchronized Array (Custom React Hook). updates. Disabling this automatic re-renderer can be an option when
This React Hook is _not_ responsive to model updates, it only you only use the model to make a table view,
returns the array model. which automatically synchronizes on model updates.
To listen to array updates, use `Models.useModel(model)` or `useSyncArray()`. @param sync - whether the component re-render on updates (default is `true`)
Array views automatically listen to model updates.
*/ */
export function useSyncArray<K, A>( export function useSyncArray<K, A>(
arr: Array<K, A>, arr: Array<K, A>,
): A[] { sync = true,
): CompactModel<K, A> {
Dome.useUpdate(PROJECT); Dome.useUpdate(PROJECT);
const st = getSyncArray(arr); const st = getSyncArray(arr);
React.useEffect(st.update); React.useEffect(st.update);
Server.useSignal(arr.signal, st.fetch); Server.useSignal(arr.signal, st.fetch);
useModel(st.model); useModel(st.model, sync);
return st.model.getArray(); return st.model;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
......
...@@ -92,7 +92,7 @@ const ASTview = () => { ...@@ -92,7 +92,7 @@ const ASTview = () => {
const [theme, setTheme] = Dome.useGlobalSetting('ASTview.theme', 'default'); const [theme, setTheme] = Dome.useGlobalSetting('ASTview.theme', 'default');
const [fontSize, setFontSize] = Dome.useGlobalSetting('ASTview.fontSize', 12); const [fontSize, setFontSize] = Dome.useGlobalSetting('ASTview.fontSize', 12);
const [wrapText, setWrapText] = Dome.useSwitch('ASTview.wrapText', false); const [wrapText, setWrapText] = Dome.useSwitch('ASTview.wrapText', false);
const markersInfo = States.useSyncArray(markerInfo); const markersInfo = States.useSyncArray(markerInfo).getArray();
const theFunction = selection?.current?.function; const theFunction = selection?.current?.function;
const theMarker = selection?.current?.marker; const theMarker = selection?.current?.marker;
......
...@@ -16,7 +16,7 @@ export default () => { ...@@ -16,7 +16,7 @@ export default () => {
// Hooks // Hooks
const [selection, updateSelection] = States.useSelection(); const [selection, updateSelection] = States.useSelection();
const fcts = States.useSyncArray(functions).sort( const fcts = States.useSyncArray(functions).getArray().sort(
(f, g) => alpha(f.name, g.name), (f, g) => alpha(f.name, g.name),
); );
......
...@@ -431,7 +431,7 @@ function FilterRatio({ model }: { model: PropertyModel }) { ...@@ -431,7 +431,7 @@ function FilterRatio({ model }: { model: PropertyModel }) {
const RenderTable = () => { const RenderTable = () => {
// Hooks // Hooks
const model = React.useMemo(() => new PropertyModel(), []); const model = React.useMemo(() => new PropertyModel(), []);
const data = States.useSyncArray(Properties.status); const data = States.useSyncArray(Properties.status).getArray();
useEffect(() => { useEffect(() => {
model.removeAllData(); model.removeAllData();
model.updateData(data); model.updateData(data);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment