nucleus-core-ts 0.9.848 → 0.9.849

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.
@@ -7,6 +7,7 @@ import { integrationsPageTheme } from '../theme';
7
7
  import { Badge, CodeBlock, Field, Notice, Stat } from './Primitives';
8
8
  import { pathParamNames } from './pathParams';
9
9
  import { CardSkeleton, StatGridSkeleton } from './Skeletons';
10
+ import { settlementOf } from './settleOwnership';
10
11
  /**
11
12
  * One real request, shown in full.
12
13
  *
@@ -21,6 +22,7 @@ export function EndpointSample({ endpoint, actions }) {
21
22
  const [values, setValues] = useState({});
22
23
  // Kept in step with the render so an in-flight reply can ask what is selected
23
24
  // NOW, not what was selected when it was sent.
25
+ const generationRef = useRef(0);
24
26
  const selectedRef = useRef(store.selectedEndpointId);
25
27
  selectedRef.current = store.selectedEndpointId;
26
28
  const params = pathParamNames(endpoint.path);
@@ -29,20 +31,39 @@ export function EndpointSample({ endpoint, actions }) {
29
31
  for (const name of params){
30
32
  if (!(values[name] ?? '').trim()) unfilled += 1;
31
33
  }
34
+ /**
35
+ * Discard a late answer, but hand the busy flag back on the way out.
36
+ *
37
+ * The discard itself is right — the operator has moved on and the field list
38
+ * must describe the endpoint they are looking at. It used to be a bare
39
+ * `return`, which skipped the clearing too: `sampling` stayed set with nobody
40
+ * left to unset it, and the sample card showed a skeleton until the page was
41
+ * reloaded. See settleOwnership.ts for who owns the flag.
42
+ */ const settleElsewhere = (mine, requestedFor)=>{
43
+ const { discard, releaseFlag } = settlementOf({
44
+ mine,
45
+ latest: generationRef.current,
46
+ requestedFor,
47
+ selectedNow: selectedRef.current
48
+ });
49
+ if (releaseFlag) store.setSampling(false);
50
+ return discard;
51
+ };
32
52
  const run = ()=>{
33
53
  // The endpoint this request is FOR. A sample can take seconds against a slow
34
54
  // source, and an operator who clicks another endpoint meanwhile would
35
55
  // otherwise get the first one's answer written in as the second one's field
36
56
  // list — and then map against fields the endpoint does not have.
37
57
  const requestedFor = endpoint.id;
58
+ const mine = ++generationRef.current;
38
59
  store.setSampling(true);
39
60
  store.setError(null);
40
61
  actions.sampleEndpoint(requestedFor, params.length > 0 ? values : undefined).then((result)=>{
41
- if (selectedRef.current !== requestedFor) return;
62
+ if (settleElsewhere(mine, requestedFor)) return;
42
63
  store.setSample(result);
43
64
  if (!result.ok && result.error) store.setError(result.error);
44
65
  }).catch((error)=>{
45
- if (selectedRef.current !== requestedFor) return;
66
+ if (settleElsewhere(mine, requestedFor)) return;
46
67
  store.setSample(null);
47
68
  store.setError(error instanceof Error ? error.message : 'Sampling the endpoint failed.');
48
69
  });
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Who owns a busy flag when a late answer arrives.
3
+ *
4
+ * Every request in this panel sets a shared "in progress" flag on the way in and
5
+ * clears it on the way out. That breaks the moment an answer is DISCARDED —
6
+ * because the operator moved on, or because a newer request replaced this one —
7
+ * since a bare `return` skips the clearing too and the flag stays set with
8
+ * nobody left to unset it. The panel then shows a skeleton for a request that
9
+ * has already finished and will never be written.
10
+ *
11
+ * Two questions, and they are not the same question:
12
+ *
13
+ * superseded — a NEWER request of the same kind has started. It set the flag
14
+ * after this one did and will clear it when it settles, so this
15
+ * answer must leave the flag strictly alone.
16
+ * movedOn — the operator is looking at something else. Nobody else is
17
+ * coming: this request is the last one out and must clear the
18
+ * flag before dropping its answer.
19
+ */
20
+ export type Settlement = {
21
+ /** True when this answer must not be written. */
22
+ discard: boolean;
23
+ /** True when this request is responsible for clearing the busy flag. */
24
+ releaseFlag: boolean;
25
+ };
26
+ export declare function settlementOf(args: {
27
+ /** The generation this request was given when it started. */
28
+ mine: number;
29
+ /** The latest generation handed out. */
30
+ latest: number;
31
+ /** What this request was for. */
32
+ requestedFor: string;
33
+ /** What is selected right now. */
34
+ selectedNow: string | null;
35
+ }): Settlement;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Who owns a busy flag when a late answer arrives.
3
+ *
4
+ * Every request in this panel sets a shared "in progress" flag on the way in and
5
+ * clears it on the way out. That breaks the moment an answer is DISCARDED —
6
+ * because the operator moved on, or because a newer request replaced this one —
7
+ * since a bare `return` skips the clearing too and the flag stays set with
8
+ * nobody left to unset it. The panel then shows a skeleton for a request that
9
+ * has already finished and will never be written.
10
+ *
11
+ * Two questions, and they are not the same question:
12
+ *
13
+ * superseded — a NEWER request of the same kind has started. It set the flag
14
+ * after this one did and will clear it when it settles, so this
15
+ * answer must leave the flag strictly alone.
16
+ * movedOn — the operator is looking at something else. Nobody else is
17
+ * coming: this request is the last one out and must clear the
18
+ * flag before dropping its answer.
19
+ */ export function settlementOf(args) {
20
+ const superseded = args.latest !== args.mine;
21
+ const movedOn = args.selectedNow !== args.requestedFor;
22
+ return {
23
+ discard: superseded || movedOn,
24
+ // Only the request that still holds the flag may clear it — otherwise a
25
+ // slow first answer would switch off the light on a second request that is
26
+ // still running, and the operator would watch a finished-looking panel fill
27
+ // in by itself a moment later.
28
+ releaseFlag: !superseded && movedOn
29
+ };
30
+ }
@@ -0,0 +1,56 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import { settlementOf } from './settleOwnership';
3
+ const forEndpoint = (over = {})=>settlementOf({
4
+ mine: 1,
5
+ latest: 1,
6
+ requestedFor: 'a',
7
+ selectedNow: 'a',
8
+ ...over
9
+ });
10
+ describe('settlementOf', ()=>{
11
+ it('writes the answer when nothing has changed', ()=>{
12
+ expect(forEndpoint()).toEqual({
13
+ discard: false,
14
+ releaseFlag: false
15
+ });
16
+ });
17
+ /**
18
+ * The bug this exists for: the operator clicked another endpoint while a
19
+ * sample was in flight, the answer was dropped, and the busy flag was left on
20
+ * with nobody coming to clear it.
21
+ */ it('drops the answer AND clears the flag when the operator moved on', ()=>{
22
+ expect(forEndpoint({
23
+ selectedNow: 'b'
24
+ })).toEqual({
25
+ discard: true,
26
+ releaseFlag: true
27
+ });
28
+ });
29
+ it('leaves the flag alone when a newer request has taken over', ()=>{
30
+ expect(forEndpoint({
31
+ latest: 2
32
+ })).toEqual({
33
+ discard: true,
34
+ releaseFlag: false
35
+ });
36
+ });
37
+ it('never clears the flag for a superseded request, even one that also moved on', ()=>{
38
+ // Both conditions at once: the newer request owns the flag, so this one must
39
+ // not switch it off while that one is still running.
40
+ expect(forEndpoint({
41
+ latest: 2,
42
+ selectedNow: 'b'
43
+ })).toEqual({
44
+ discard: true,
45
+ releaseFlag: false
46
+ });
47
+ });
48
+ it('treats nothing-selected as having moved on', ()=>{
49
+ expect(forEndpoint({
50
+ selectedNow: null
51
+ })).toEqual({
52
+ discard: true,
53
+ releaseFlag: true
54
+ });
55
+ });
56
+ });
@@ -89,6 +89,12 @@ export const { useStore: useIntegrationsStore } = createStore(initial, {
89
89
  // The sample describes ONE endpoint; carrying it over would offer the
90
90
  // wrong field list to the next mapping.
91
91
  store.sample = null;
92
+ // And the WAIT for a sample belongs to the endpoint that was left, not to
93
+ // the one arrived at. Clearing the record without clearing the flag left
94
+ // the next endpoint showing a skeleton for a request that was never going
95
+ // to be written — permanently, because the answer, when it came, saw the
96
+ // selection had moved and returned without touching anything.
97
+ store.sampling = false;
92
98
  }),
93
99
  setMappings: (store)=>(items, total)=>batch(()=>{
94
100
  store.mappings = items;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nucleus-core-ts",
3
- "version": "0.9.848",
3
+ "version": "0.9.849",
4
4
  "description": "Production-ready, enterprise-grade TypeScript framework for building multi-tenant APIs",
5
5
  "author": "Hidayet Can Özcan <hidayetcan@gmail.com>",
6
6
  "license": "SEE LICENSE IN LICENSE",