@jsenv/navi 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +51 -0
- package/package.json +38 -0
- package/src/action_private_properties.js +11 -0
- package/src/action_proxy_test.html +353 -0
- package/src/action_run_states.js +5 -0
- package/src/actions.js +1377 -0
- package/src/browser_integration/browser_integration.js +191 -0
- package/src/browser_integration/document_back_and_forward.js +17 -0
- package/src/browser_integration/document_loading_signal.js +100 -0
- package/src/browser_integration/document_state_signal.js +9 -0
- package/src/browser_integration/document_url_signal.js +9 -0
- package/src/browser_integration/use_is_visited.js +19 -0
- package/src/browser_integration/via_history.js +199 -0
- package/src/browser_integration/via_navigation.js +168 -0
- package/src/components/action_execution/form_context.js +8 -0
- package/src/components/action_execution/render_actionable_component.jsx +27 -0
- package/src/components/action_execution/use_action.js +330 -0
- package/src/components/action_execution/use_execute_action.js +161 -0
- package/src/components/action_renderer.jsx +136 -0
- package/src/components/collect_form_element_values.js +79 -0
- package/src/components/demos/0_button_demo.html +155 -0
- package/src/components/demos/1_checkbox_demo.html +257 -0
- package/src/components/demos/2_input_textual_demo.html +354 -0
- package/src/components/demos/3_radio_demo.html +222 -0
- package/src/components/demos/4_select_demo.html +104 -0
- package/src/components/demos/5_list_scrollable_demo.html +153 -0
- package/src/components/demos/action/0_button_demo.html +204 -0
- package/src/components/demos/action/10_shortcuts_demo.html +189 -0
- package/src/components/demos/action/11_nested_shortcuts_demo.html +401 -0
- package/src/components/demos/action/1_input_text_demo.html +461 -0
- package/src/components/demos/action/2_form_multiple.html +303 -0
- package/src/components/demos/action/3_details_demo.html +172 -0
- package/src/components/demos/action/4_input_checkbox_demo.html +611 -0
- package/src/components/demos/action/6_checkbox_list_demo.html +109 -0
- package/src/components/demos/action/7_radio_list_demo.html +217 -0
- package/src/components/demos/action/8_editable_text_demo.html +442 -0
- package/src/components/demos/action/9_link_demo.html +172 -0
- package/src/components/demos/demo.md +0 -0
- package/src/components/demos/route/basic/basic.html +14 -0
- package/src/components/demos/route/basic/basic_route_demo.jsx +224 -0
- package/src/components/demos/route/multi/multi.html +14 -0
- package/src/components/demos/route/multi/multi_route_demo.jsx +277 -0
- package/src/components/details/details.jsx +248 -0
- package/src/components/details/summary_marker.jsx +141 -0
- package/src/components/editable_text/editable_text.jsx +96 -0
- package/src/components/error_boundary_context.js +9 -0
- package/src/components/form.jsx +144 -0
- package/src/components/input/button.jsx +333 -0
- package/src/components/input/checkbox_list.jsx +294 -0
- package/src/components/input/field.jsx +61 -0
- package/src/components/input/field_css.js +118 -0
- package/src/components/input/input.jsx +15 -0
- package/src/components/input/input_checkbox.jsx +370 -0
- package/src/components/input/input_radio.jsx +299 -0
- package/src/components/input/input_textual.jsx +338 -0
- package/src/components/input/radio_list.jsx +283 -0
- package/src/components/input/select.jsx +273 -0
- package/src/components/input/use_form_event.js +20 -0
- package/src/components/input/use_on_change.js +12 -0
- package/src/components/link/link.jsx +291 -0
- package/src/components/loader/loader_background.jsx +324 -0
- package/src/components/loader/loading_spinner.jsx +68 -0
- package/src/components/loader/network_speed.js +83 -0
- package/src/components/loader/rectangle_loading.jsx +225 -0
- package/src/components/route.jsx +15 -0
- package/src/components/selection/selection.js +5 -0
- package/src/components/selection/selection_context.jsx +262 -0
- package/src/components/shortcut/os.js +9 -0
- package/src/components/shortcut/shortcut_context.jsx +390 -0
- package/src/components/use_action_events.js +37 -0
- package/src/components/use_auto_focus.js +43 -0
- package/src/components/use_debounce_true.js +31 -0
- package/src/components/use_focus_group.js +19 -0
- package/src/components/use_initial_value.js +104 -0
- package/src/components/use_is_visited.js +19 -0
- package/src/components/use_ref_array.js +38 -0
- package/src/components/use_signal_sync.js +50 -0
- package/src/components/use_state_array.js +40 -0
- package/src/docs/actions.md +228 -0
- package/src/docs/demos/resource/action_status.jsx +42 -0
- package/src/docs/demos/resource/demo.md +1 -0
- package/src/docs/demos/resource/resource_demo_0.html +84 -0
- package/src/docs/demos/resource/resource_demo_10_post_gc.html +364 -0
- package/src/docs/demos/resource/resource_demo_11_describe_many.html +362 -0
- package/src/docs/demos/resource/resource_demo_2.html +173 -0
- package/src/docs/demos/resource/resource_demo_3_filtered_users.html +415 -0
- package/src/docs/demos/resource/resource_demo_4_details.html +284 -0
- package/src/docs/demos/resource/resource_demo_5_renderer_lazy.html +115 -0
- package/src/docs/demos/resource/resource_demo_6_gc.html +217 -0
- package/src/docs/demos/resource/resource_demo_7_child_gc.html +240 -0
- package/src/docs/demos/resource/resource_demo_8_proxy_gc.html +319 -0
- package/src/docs/demos/resource/resource_demo_9_describe_one.html +472 -0
- package/src/docs/demos/resource/tata.jsx +3 -0
- package/src/docs/demos/resource/toto.jsx +3 -0
- package/src/docs/demos/user_nav/user_nav.html +12 -0
- package/src/docs/demos/user_nav/user_nav.jsx +330 -0
- package/src/docs/resource_dependencies.md +103 -0
- package/src/docs/resource_with_params.md +80 -0
- package/src/notes.md +13 -0
- package/src/route/route.js +518 -0
- package/src/route/route.test.html +228 -0
- package/src/store/array_signal_store.js +537 -0
- package/src/store/local_storage_signal.js +17 -0
- package/src/store/resource_graph.js +1303 -0
- package/src/store/tests/resource_graph_autoreload_demo.html +12 -0
- package/src/store/tests/resource_graph_autoreload_demo.jsx +964 -0
- package/src/store/tests/resource_graph_dependencies.test.js +95 -0
- package/src/store/value_in_local_storage.js +187 -0
- package/src/symbol_object_signal.js +1 -0
- package/src/use_action_data.js +10 -0
- package/src/use_action_status.js +47 -0
- package/src/utils/add_many_event_listeners.js +15 -0
- package/src/utils/array_add_remove.js +61 -0
- package/src/utils/array_signal.js +15 -0
- package/src/utils/compare_two_js_values.js +172 -0
- package/src/utils/execute_with_cleanup.js +21 -0
- package/src/utils/get_caller_info.js +85 -0
- package/src/utils/iterable_weak_set.js +62 -0
- package/src/utils/js_value_weak_map.js +162 -0
- package/src/utils/js_value_weak_map_demo.html +690 -0
- package/src/utils/merge_two_js_values.js +53 -0
- package/src/utils/stringify_for_display.js +150 -0
- package/src/utils/weak_effect.js +48 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { updateDocumentUrl } from "./document_url_signal.js";
|
|
2
|
+
|
|
3
|
+
let DEBUG = false;
|
|
4
|
+
|
|
5
|
+
export const setupRoutingViaNavigation = (handler) => {
|
|
6
|
+
updateDocumentUrl(navigation.currentEntry.url);
|
|
7
|
+
navigation.addEventListener("currententrychange", () => {
|
|
8
|
+
updateDocumentUrl(navigation.currentEntry.url);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
let isReloadFromNavigationAPI = false;
|
|
12
|
+
const navigationReload = navigation.reload;
|
|
13
|
+
navigation.reload = (...args) => {
|
|
14
|
+
isReloadFromNavigationAPI = true;
|
|
15
|
+
navigationReload.call(navigation, ...args);
|
|
16
|
+
isReloadFromNavigationAPI = false;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
navigation.addEventListener("navigate", (event) => {
|
|
20
|
+
if (!event.canIntercept) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (event.hashChange || event.downloadRequest !== null) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (
|
|
27
|
+
!event.userInitiated &&
|
|
28
|
+
event.navigationType === "reload" &&
|
|
29
|
+
event.isTrusted &&
|
|
30
|
+
!isReloadFromNavigationAPI
|
|
31
|
+
) {
|
|
32
|
+
// let window.location.reload() reload the whole document
|
|
33
|
+
// (used by jsenv hot reload)
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const { signal } = event;
|
|
37
|
+
const formAction = event.info?.formAction;
|
|
38
|
+
const isReload = event.navigationType === "reload";
|
|
39
|
+
const isReplace = event.navigationType === "replace";
|
|
40
|
+
const currentUrl = navigation.currentEntry.url;
|
|
41
|
+
const destinationUrl = event.destination.url;
|
|
42
|
+
const currentState = navigation.currentEntry.getState();
|
|
43
|
+
const destinationState = event.destination.getState();
|
|
44
|
+
const formData = event.formData || event.info?.formData;
|
|
45
|
+
const formUrl = event.info?.formUrl;
|
|
46
|
+
const stopAbortController = new AbortController();
|
|
47
|
+
const stopSignal = stopAbortController.signal;
|
|
48
|
+
const removeStopButtonClickDetector = detectBrowserStopButtonClick(
|
|
49
|
+
signal,
|
|
50
|
+
() => {
|
|
51
|
+
stopAbortController.abort("stop button clicked");
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (DEBUG) {
|
|
56
|
+
console.log("receive navigate event", {
|
|
57
|
+
destinationUrl,
|
|
58
|
+
destinationState,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
event.intercept({
|
|
63
|
+
handler: async () => {
|
|
64
|
+
if (event.info?.onStart) {
|
|
65
|
+
event.info.onStart();
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
await handler(event, {
|
|
69
|
+
abortSignal: signal,
|
|
70
|
+
stopSignal,
|
|
71
|
+
formAction,
|
|
72
|
+
formData,
|
|
73
|
+
formUrl,
|
|
74
|
+
isReload,
|
|
75
|
+
isReplace,
|
|
76
|
+
currentUrl,
|
|
77
|
+
destinationUrl,
|
|
78
|
+
currentState,
|
|
79
|
+
destinationState,
|
|
80
|
+
});
|
|
81
|
+
} catch (e) {
|
|
82
|
+
console.error(e); // browser remains silent in case of error during handler so we explicitely log the error to the console
|
|
83
|
+
throw e;
|
|
84
|
+
} finally {
|
|
85
|
+
removeStopButtonClickDetector();
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
// https://github.com/WICG/navigation-api?tab=readme-ov-file#focus-management
|
|
89
|
+
// without this, after clicking <a href="...">, the focus does to document.body
|
|
90
|
+
// which is problematic for shortcuts for instance
|
|
91
|
+
focusReset: "manual",
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// setupNavigateHandler(async (event, { stopSignal, formAction, formData }) => {
|
|
97
|
+
// if (formAction) {
|
|
98
|
+
// const result = await applyAction(formAction, {
|
|
99
|
+
// signal: stopSignal,
|
|
100
|
+
// formData,
|
|
101
|
+
// });
|
|
102
|
+
// event.info.formActionCallback(result);
|
|
103
|
+
// }
|
|
104
|
+
|
|
105
|
+
// await navMethods.applyRouting({
|
|
106
|
+
// sourceUrl: currentUrl,
|
|
107
|
+
// targetUrl: formUrl || destinationUrl,
|
|
108
|
+
// sourceState: currentState,
|
|
109
|
+
// targetState: destinationState || currentState,
|
|
110
|
+
// abortSignal,
|
|
111
|
+
// stopSignal,
|
|
112
|
+
// isReload,
|
|
113
|
+
// isReplace,
|
|
114
|
+
// info: event.info,
|
|
115
|
+
// });
|
|
116
|
+
// if (formUrl) {
|
|
117
|
+
// const finishedPromise = event.target.transition.finished;
|
|
118
|
+
// (async () => {
|
|
119
|
+
// try {
|
|
120
|
+
// await finishedPromise;
|
|
121
|
+
// } finally {
|
|
122
|
+
// navigation.navigate(window.location.href, {
|
|
123
|
+
// state: navigation.currentEntry.getState(),
|
|
124
|
+
// history: "replace",
|
|
125
|
+
// });
|
|
126
|
+
// }
|
|
127
|
+
// })();
|
|
128
|
+
// }
|
|
129
|
+
// });
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* There is 2 distinct reason to abort a navigation:
|
|
133
|
+
* - the user clicked the browser stop button
|
|
134
|
+
* - the user navigate to an other page
|
|
135
|
+
*
|
|
136
|
+
* When navigating to an other page we don't want to abort anything, the routing does that
|
|
137
|
+
* When clicking the stop button we want to cancel everything
|
|
138
|
+
*
|
|
139
|
+
* To detect that when aborted, we wait a setTimeout to see if we receive a new navigation
|
|
140
|
+
* If yes it means this is an abort due to a new navigation
|
|
141
|
+
* Otherwise it's an abort due to the stop button
|
|
142
|
+
*
|
|
143
|
+
* On top of that stop button must cancel X navigation so the last navigation detecting the stop click
|
|
144
|
+
* is notifying any current navigation that stop button was clicked
|
|
145
|
+
*/
|
|
146
|
+
let callEffect = () => {};
|
|
147
|
+
const browserStopButtonClickCallbackSet = new Set();
|
|
148
|
+
const detectBrowserStopButtonClick = (navigateEventSignal, callback) => {
|
|
149
|
+
callEffect();
|
|
150
|
+
browserStopButtonClickCallbackSet.add(callback);
|
|
151
|
+
navigateEventSignal.addEventListener("abort", async () => {
|
|
152
|
+
const timeout = setTimeout(() => {
|
|
153
|
+
callEffect = () => {};
|
|
154
|
+
|
|
155
|
+
for (const browserStopButtonClickCallback of browserStopButtonClickCallbackSet) {
|
|
156
|
+
browserStopButtonClickCallback();
|
|
157
|
+
}
|
|
158
|
+
browserStopButtonClickCallbackSet.clear();
|
|
159
|
+
});
|
|
160
|
+
callEffect = () => {
|
|
161
|
+
clearTimeout(timeout);
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
return () => {
|
|
166
|
+
browserStopButtonClickCallbackSet.delete(callback);
|
|
167
|
+
};
|
|
168
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useFormContext } from "./form_context.js";
|
|
2
|
+
|
|
3
|
+
export const renderActionableComponent = (
|
|
4
|
+
props,
|
|
5
|
+
ref,
|
|
6
|
+
{ Basic, WithAction, InsideForm, WithActionInsideForm },
|
|
7
|
+
) => {
|
|
8
|
+
const { action, shortcuts, ignoreForm } = props;
|
|
9
|
+
const formContext = useFormContext();
|
|
10
|
+
const hasActionProps = Boolean(action || (shortcuts && shortcuts.length > 0));
|
|
11
|
+
const considerInsideForm = ignoreForm ? false : Boolean(formContext);
|
|
12
|
+
|
|
13
|
+
if (hasActionProps && WithAction) {
|
|
14
|
+
if (considerInsideForm && WithActionInsideForm) {
|
|
15
|
+
return (
|
|
16
|
+
<WithActionInsideForm formContext={formContext} ref={ref} {...props} />
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
return <WithAction ref={ref} {...props} />;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (considerInsideForm && InsideForm) {
|
|
23
|
+
return <InsideForm formContext={formContext} ref={ref} {...props} />;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return <Basic ref={ref} {...props} />;
|
|
27
|
+
};
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { signal } from "@preact/signals";
|
|
2
|
+
import { useCallback, useRef } from "preact/hooks";
|
|
3
|
+
import { createAction } from "../../actions.js";
|
|
4
|
+
import { addIntoArray, removeFromArray } from "../../utils/array_add_remove.js";
|
|
5
|
+
import { isSignal } from "../../utils/stringify_for_display.js";
|
|
6
|
+
import { useInitialValue } from "../use_initial_value.js";
|
|
7
|
+
import { useFormContext } from "./form_context.js";
|
|
8
|
+
|
|
9
|
+
let debug = false;
|
|
10
|
+
let componentActionIdCounter = 0;
|
|
11
|
+
const useComponentActionCacheKey = () => {
|
|
12
|
+
const componentActionCacheKeyRef = useRef(null);
|
|
13
|
+
// It's very important to use an object here as componentId and not just in integer
|
|
14
|
+
// because this key will be used as a key in a WeakMap
|
|
15
|
+
// and if we pass an integer the browser allows itself to garbage collect it
|
|
16
|
+
// but not if it's an object as the useRef above keeps referencing it
|
|
17
|
+
// this is a subtle different and might be the reason why WeakMap does not accept primitive as keys
|
|
18
|
+
if (!componentActionCacheKeyRef.current) {
|
|
19
|
+
const id = ++componentActionIdCounter;
|
|
20
|
+
componentActionCacheKeyRef.current = {
|
|
21
|
+
id,
|
|
22
|
+
toString: () => `component_action_id_${id}`,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return componentActionCacheKeyRef.current;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// used by <form> to have their own action bound to many parameters
|
|
29
|
+
// any form element within the <form> will update these params
|
|
30
|
+
// these params are also assigned just before executing the action to ensure they are in sync
|
|
31
|
+
// (could also be used by <fieldset> but I think fieldset are not going to be used this way and
|
|
32
|
+
// we will reserve this behavior to <form>)
|
|
33
|
+
export const useFormActionBoundToFormParams = (action) => {
|
|
34
|
+
const actionCacheKey = useComponentActionCacheKey();
|
|
35
|
+
const cacheKey = typeof action === "function" ? actionCacheKey : action;
|
|
36
|
+
const [formParamsSignal, updateFormParams] = useActionParamsSignal(cacheKey);
|
|
37
|
+
const formActionBoundActionToFormParams = useBoundAction(
|
|
38
|
+
action,
|
|
39
|
+
formParamsSignal,
|
|
40
|
+
);
|
|
41
|
+
return [
|
|
42
|
+
formActionBoundActionToFormParams,
|
|
43
|
+
formParamsSignal,
|
|
44
|
+
updateFormParams,
|
|
45
|
+
];
|
|
46
|
+
};
|
|
47
|
+
export const useOneFormParam = (
|
|
48
|
+
name,
|
|
49
|
+
externalValue,
|
|
50
|
+
fallbackValue,
|
|
51
|
+
defaultValue,
|
|
52
|
+
) => {
|
|
53
|
+
const { formParamsSignal } = useFormContext();
|
|
54
|
+
const previousFormParamsSignalRef = useRef(null);
|
|
55
|
+
const formActionChanged =
|
|
56
|
+
previousFormParamsSignalRef.current !== null &&
|
|
57
|
+
previousFormParamsSignalRef.current !== formParamsSignal;
|
|
58
|
+
previousFormParamsSignalRef.current = formParamsSignal;
|
|
59
|
+
|
|
60
|
+
const getValue = useCallback(
|
|
61
|
+
() => formParamsSignal.value[name],
|
|
62
|
+
[formParamsSignal],
|
|
63
|
+
);
|
|
64
|
+
const setValue = useCallback(
|
|
65
|
+
(value) => updateParamsSignal(formParamsSignal, { [name]: value }),
|
|
66
|
+
[formParamsSignal],
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const initialValue = useInitialValue(
|
|
70
|
+
name,
|
|
71
|
+
externalValue,
|
|
72
|
+
fallbackValue,
|
|
73
|
+
defaultValue,
|
|
74
|
+
setValue,
|
|
75
|
+
);
|
|
76
|
+
if (formActionChanged) {
|
|
77
|
+
if (debug) {
|
|
78
|
+
console.debug(
|
|
79
|
+
`useOneFormParam(${name}) form action changed, re-initializing with: ${initialValue}`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
setValue(initialValue);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const resetValue = useCallback(() => {
|
|
86
|
+
setValue(initialValue);
|
|
87
|
+
}, [initialValue, formParamsSignal]);
|
|
88
|
+
|
|
89
|
+
return [getValue(), setValue, resetValue];
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// used by form elements such as <input>, <select>, <textarea> to have their own action bound to a single parameter
|
|
93
|
+
// when inside a <form> the form params are updated when the form element single param is updated
|
|
94
|
+
export const useActionBoundToOneParam = (
|
|
95
|
+
action,
|
|
96
|
+
name,
|
|
97
|
+
externalValue,
|
|
98
|
+
fallbackValue,
|
|
99
|
+
defaultValue,
|
|
100
|
+
) => {
|
|
101
|
+
const externalValueIsSignal = isSignal(externalValue);
|
|
102
|
+
let externalValueSignal;
|
|
103
|
+
if (externalValueIsSignal) {
|
|
104
|
+
externalValueSignal = externalValue;
|
|
105
|
+
externalValue = externalValueSignal.peek();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (action.isProxy && !externalValueSignal) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`useActionBoundToOneParam(${name}) action is a proxy but no valueSignal provided`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const actionCacheKey = useComponentActionCacheKey();
|
|
115
|
+
const cacheKey = typeof action === "function" ? actionCacheKey : action;
|
|
116
|
+
const [paramsSignal, updateParams] = useActionParamsSignal(
|
|
117
|
+
cacheKey,
|
|
118
|
+
externalValueSignal,
|
|
119
|
+
);
|
|
120
|
+
const previousParamsSignalRef = useRef(null);
|
|
121
|
+
const actionChanged =
|
|
122
|
+
previousParamsSignalRef.current !== null &&
|
|
123
|
+
previousParamsSignalRef.current !== paramsSignal;
|
|
124
|
+
previousParamsSignalRef.current = paramsSignal;
|
|
125
|
+
|
|
126
|
+
const boundAction = useBoundAction(
|
|
127
|
+
action,
|
|
128
|
+
externalValueSignal ? null : paramsSignal,
|
|
129
|
+
);
|
|
130
|
+
const getValue = externalValueSignal
|
|
131
|
+
? useCallback(() => paramsSignal.value, [paramsSignal])
|
|
132
|
+
: useCallback(() => paramsSignal.value[name], [paramsSignal]);
|
|
133
|
+
const setValue = externalValueSignal
|
|
134
|
+
? useCallback(
|
|
135
|
+
(value) => {
|
|
136
|
+
paramsSignal.value = value;
|
|
137
|
+
},
|
|
138
|
+
[paramsSignal],
|
|
139
|
+
)
|
|
140
|
+
: useCallback(
|
|
141
|
+
(value) => {
|
|
142
|
+
if (debug) {
|
|
143
|
+
console.debug(
|
|
144
|
+
`useActionBoundToOneParam(${name}) set value to ${value} (old value is ${getValue()} )`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return updateParams({ [name]: value });
|
|
148
|
+
},
|
|
149
|
+
[updateParams],
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
const initialValue = useInitialValue(
|
|
153
|
+
name,
|
|
154
|
+
externalValue,
|
|
155
|
+
fallbackValue,
|
|
156
|
+
defaultValue,
|
|
157
|
+
setValue,
|
|
158
|
+
);
|
|
159
|
+
if (actionChanged) {
|
|
160
|
+
if (debug) {
|
|
161
|
+
console.debug(
|
|
162
|
+
`useActionBoundToOneParam(${name}) action changed, re-initializing with: ${initialValue}`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
setValue(initialValue);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const reset = useCallback(() => {
|
|
169
|
+
setValue(initialValue);
|
|
170
|
+
}, [initialValue, paramsSignal]);
|
|
171
|
+
|
|
172
|
+
return [boundAction, getValue(), setValue, reset];
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// export const useActionBoundToOneBooleanParam = (action, name, value) => {
|
|
176
|
+
// const [boundAction, getValue, setValue, resetValue] =
|
|
177
|
+
// useActionBoundToOneParam(action, name, Boolean(value));
|
|
178
|
+
|
|
179
|
+
// return [
|
|
180
|
+
// boundAction,
|
|
181
|
+
// () => Boolean(getValue()),
|
|
182
|
+
// (value) => setValue(Boolean(value)),
|
|
183
|
+
// resetValue,
|
|
184
|
+
// ];
|
|
185
|
+
// };
|
|
186
|
+
|
|
187
|
+
export const useActionBoundToOneArrayParam = (
|
|
188
|
+
action,
|
|
189
|
+
name,
|
|
190
|
+
initialValue,
|
|
191
|
+
fallbackValue,
|
|
192
|
+
defaultValue = [],
|
|
193
|
+
) => {
|
|
194
|
+
const [boundAction, value, setValue, resetValue] = useActionBoundToOneParam(
|
|
195
|
+
action,
|
|
196
|
+
name,
|
|
197
|
+
initialValue,
|
|
198
|
+
fallbackValue,
|
|
199
|
+
defaultValue,
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const add = (valueToAdd, valueArray = value) => {
|
|
203
|
+
setValue(addIntoArray(valueArray, valueToAdd));
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const remove = (valueToRemove, valueArray = value) => {
|
|
207
|
+
setValue(removeFromArray(valueArray, valueToRemove));
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
return [boundAction, value, add, remove, resetValue];
|
|
211
|
+
};
|
|
212
|
+
export const useOneFormArrayParam = (
|
|
213
|
+
name,
|
|
214
|
+
initialValue,
|
|
215
|
+
fallbackValue,
|
|
216
|
+
defaultValue = [],
|
|
217
|
+
) => {
|
|
218
|
+
const [getValue, setValue, resetValue] = useOneFormParam(
|
|
219
|
+
name,
|
|
220
|
+
initialValue,
|
|
221
|
+
fallbackValue,
|
|
222
|
+
defaultValue,
|
|
223
|
+
);
|
|
224
|
+
const add = (valueToAdd, valueArray = getValue()) => {
|
|
225
|
+
setValue(addIntoArray(valueArray, valueToAdd));
|
|
226
|
+
};
|
|
227
|
+
const remove = (valueToRemove, valueArray = getValue()) => {
|
|
228
|
+
setValue(removeFromArray(valueArray, valueToRemove));
|
|
229
|
+
};
|
|
230
|
+
return [getValue, add, remove, resetValue];
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// used by <details> to just call their action
|
|
234
|
+
export const useAction = (action, paramsSignal) => {
|
|
235
|
+
return useBoundAction(action, paramsSignal);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const sharedSignalCache = new WeakMap();
|
|
239
|
+
const useActionParamsSignal = (cacheKey, valueSignal) => {
|
|
240
|
+
if (valueSignal) {
|
|
241
|
+
return [
|
|
242
|
+
valueSignal,
|
|
243
|
+
(value) => {
|
|
244
|
+
valueSignal.value = value;
|
|
245
|
+
},
|
|
246
|
+
];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ✅ cacheKey peut être componentId (Symbol) ou action (objet)
|
|
250
|
+
const fromCache = sharedSignalCache.get(cacheKey);
|
|
251
|
+
if (fromCache) {
|
|
252
|
+
return fromCache;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const paramsSignal = signal({});
|
|
256
|
+
const result = [
|
|
257
|
+
paramsSignal,
|
|
258
|
+
(value) => updateParamsSignal(paramsSignal, value, cacheKey),
|
|
259
|
+
];
|
|
260
|
+
sharedSignalCache.set(cacheKey, result);
|
|
261
|
+
if (debug) {
|
|
262
|
+
console.debug(`Created params signal for ${cacheKey} with params:`, {});
|
|
263
|
+
}
|
|
264
|
+
return result;
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
export const updateParamsSignal = (paramsSignal, object, cacheKey) => {
|
|
268
|
+
const currentParams = paramsSignal.peek();
|
|
269
|
+
const paramsCopy = { ...currentParams };
|
|
270
|
+
let modified = false;
|
|
271
|
+
for (const key of Object.keys(object)) {
|
|
272
|
+
const value = object[key];
|
|
273
|
+
const currentValue = currentParams[key];
|
|
274
|
+
if (Object.hasOwn(currentParams, key)) {
|
|
275
|
+
if (value !== currentValue) {
|
|
276
|
+
modified = true;
|
|
277
|
+
paramsCopy[key] = value;
|
|
278
|
+
}
|
|
279
|
+
} else {
|
|
280
|
+
modified = true;
|
|
281
|
+
paramsCopy[key] = value;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (modified) {
|
|
285
|
+
if (debug) {
|
|
286
|
+
console.debug(
|
|
287
|
+
`Updating params for ${cacheKey} with new params:`,
|
|
288
|
+
object,
|
|
289
|
+
`result:`,
|
|
290
|
+
paramsCopy,
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
paramsSignal.value = paramsCopy;
|
|
294
|
+
} else if (debug) {
|
|
295
|
+
console.debug(
|
|
296
|
+
`No change in params for ${cacheKey}, not updating.`,
|
|
297
|
+
`current params:`,
|
|
298
|
+
currentParams,
|
|
299
|
+
`new params:`,
|
|
300
|
+
object,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const useBoundAction = (action, actionParamsSignal) => {
|
|
306
|
+
const actionRef = useRef();
|
|
307
|
+
const actionCallbackRef = useRef();
|
|
308
|
+
|
|
309
|
+
if (!action) {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
if (typeof action === "function" && !action.isAction) {
|
|
313
|
+
let actionInstance = actionRef.current;
|
|
314
|
+
if (!actionInstance) {
|
|
315
|
+
actionInstance = createAction((...args) => {
|
|
316
|
+
return actionCallbackRef.current(...args);
|
|
317
|
+
});
|
|
318
|
+
if (actionParamsSignal) {
|
|
319
|
+
actionInstance = actionInstance.bindParams(actionParamsSignal);
|
|
320
|
+
}
|
|
321
|
+
actionRef.current = actionInstance;
|
|
322
|
+
}
|
|
323
|
+
actionCallbackRef.current = action;
|
|
324
|
+
return actionInstance;
|
|
325
|
+
}
|
|
326
|
+
if (actionParamsSignal) {
|
|
327
|
+
return action.bindParams(actionParamsSignal);
|
|
328
|
+
}
|
|
329
|
+
return action;
|
|
330
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { addCustomMessage, removeCustomMessage } from "@jsenv/validation";
|
|
2
|
+
import { useCallback, useLayoutEffect, useRef, useState } from "preact/hooks";
|
|
3
|
+
import { useResetErrorBoundary } from "../error_boundary_context.js";
|
|
4
|
+
|
|
5
|
+
let debug = false;
|
|
6
|
+
|
|
7
|
+
export const useExecuteAction = (
|
|
8
|
+
elementRef,
|
|
9
|
+
{
|
|
10
|
+
errorEffect = "show_validation_message", // "show_validation_message" or "throw"
|
|
11
|
+
} = {},
|
|
12
|
+
) => {
|
|
13
|
+
// see https://medium.com/trabe/catching-asynchronous-errors-in-react-using-error-boundaries-5e8a5fd7b971
|
|
14
|
+
// and https://codepen.io/dmail/pen/XJJqeGp?editors=0010
|
|
15
|
+
// To change if https://github.com/preactjs/preact/issues/4754 lands
|
|
16
|
+
const [error, setError] = useState(null);
|
|
17
|
+
const resetErrorBoundary = useResetErrorBoundary();
|
|
18
|
+
useLayoutEffect(() => {
|
|
19
|
+
if (error) {
|
|
20
|
+
error.__handled__ = true; // prevent jsenv from displaying it
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
}, [error]);
|
|
24
|
+
|
|
25
|
+
const validationMessageTargetRef = useRef(null);
|
|
26
|
+
const addErrorMessage = (error) => {
|
|
27
|
+
const validationMessageTarget = validationMessageTargetRef.current;
|
|
28
|
+
addCustomMessage(validationMessageTarget, "action_error", error, {
|
|
29
|
+
// This error should not prevent <form> submission
|
|
30
|
+
// so whenever user tries to submit the form the error is cleared
|
|
31
|
+
// (Hitting enter key, clicking on submit button, etc. would allow to re-submit the form in error state)
|
|
32
|
+
removeOnRequestAction: true,
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
const removeErrorMessage = () => {
|
|
36
|
+
const validationMessageTarget = validationMessageTargetRef.current;
|
|
37
|
+
if (validationMessageTarget) {
|
|
38
|
+
removeCustomMessage(validationMessageTarget, "action_error");
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
useLayoutEffect(() => {
|
|
43
|
+
const element = elementRef.current;
|
|
44
|
+
if (!element) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
const form = element.tagName === "FORM" ? element : element.form;
|
|
48
|
+
if (!form) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
const onReset = () => {
|
|
52
|
+
removeErrorMessage();
|
|
53
|
+
};
|
|
54
|
+
form.addEventListener("reset", onReset);
|
|
55
|
+
return () => {
|
|
56
|
+
form.removeEventListener("reset", onReset);
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const executeAction = useCallback((actionEvent) => {
|
|
61
|
+
const { action, requester, event, method } = actionEvent.detail;
|
|
62
|
+
|
|
63
|
+
if (debug) {
|
|
64
|
+
console.debug(
|
|
65
|
+
"executing action, requested by",
|
|
66
|
+
requester,
|
|
67
|
+
`(event: ${event?.type})`,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const dispatchCustomEvent = (type, options) => {
|
|
72
|
+
const element = elementRef.current;
|
|
73
|
+
const customEvent = new CustomEvent(type, options);
|
|
74
|
+
element.dispatchEvent(customEvent);
|
|
75
|
+
};
|
|
76
|
+
if (resetErrorBoundary) {
|
|
77
|
+
resetErrorBoundary();
|
|
78
|
+
}
|
|
79
|
+
removeErrorMessage();
|
|
80
|
+
setError(null);
|
|
81
|
+
|
|
82
|
+
const validationMessageTarget = requester || elementRef.current;
|
|
83
|
+
validationMessageTargetRef.current = validationMessageTarget;
|
|
84
|
+
|
|
85
|
+
dispatchCustomEvent("actionstart", {
|
|
86
|
+
detail: {
|
|
87
|
+
action,
|
|
88
|
+
requester,
|
|
89
|
+
event,
|
|
90
|
+
method,
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return action[method]({
|
|
95
|
+
onAbort: (reason) => {
|
|
96
|
+
if (
|
|
97
|
+
// at this stage the action side effect might have removed the <element> from the DOM
|
|
98
|
+
// (in theory no because action side effect are batched to happen after)
|
|
99
|
+
// but other side effects might do this
|
|
100
|
+
elementRef.current
|
|
101
|
+
) {
|
|
102
|
+
dispatchCustomEvent("actionabort", {
|
|
103
|
+
detail: {
|
|
104
|
+
reason,
|
|
105
|
+
|
|
106
|
+
action,
|
|
107
|
+
requester,
|
|
108
|
+
event,
|
|
109
|
+
method,
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
onError: (error) => {
|
|
115
|
+
if (
|
|
116
|
+
// at this stage the action side effect might have removed the <element> from the DOM
|
|
117
|
+
// (in theory no because action side effect are batched to happen after)
|
|
118
|
+
// but other side effects might do this
|
|
119
|
+
elementRef.current
|
|
120
|
+
) {
|
|
121
|
+
dispatchCustomEvent("actionerror", {
|
|
122
|
+
detail: {
|
|
123
|
+
error,
|
|
124
|
+
|
|
125
|
+
action,
|
|
126
|
+
requester,
|
|
127
|
+
event,
|
|
128
|
+
method,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (errorEffect === "show_validation_message") {
|
|
133
|
+
addErrorMessage(error);
|
|
134
|
+
} else if (errorEffect === "throw") {
|
|
135
|
+
setError(error);
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
onComplete: (data) => {
|
|
139
|
+
if (
|
|
140
|
+
// at this stage the action side effect might have removed the <element> from the DOM
|
|
141
|
+
// (in theory no because action side effect are batched to happen after)
|
|
142
|
+
// but other side effects might do this
|
|
143
|
+
elementRef.current
|
|
144
|
+
) {
|
|
145
|
+
dispatchCustomEvent("actionend", {
|
|
146
|
+
detail: {
|
|
147
|
+
data,
|
|
148
|
+
|
|
149
|
+
action,
|
|
150
|
+
requester,
|
|
151
|
+
event,
|
|
152
|
+
method,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
}, []);
|
|
159
|
+
|
|
160
|
+
return executeAction;
|
|
161
|
+
};
|