Repository navigation
OpenConceptLab/ocl_online#786 | Algorithm replies apply to the current list #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ import APIService from '../../services/APIService'; | |
| import { needsSessionToken, getTokenFlag, tokenInputValue, removeSavedToken } from '../../services/tokenScope'; | ||
| import { getRequestLimits, getRequestSettings } from './requestLimits' | ||
| import { dropVersion } from '../../common/utils' | ||
| import { patchSelected, isStaleReply, customCheckReply } from './selectedAlgos' | ||
| import { | ||
| getDefaultBridgeRepoVersion, | ||
| getLatestReleasedBridgeVersion, | ||
|
|
@@ -132,6 +133,19 @@ export default function MultiAlgoSelector({ | |
| if (normalizedValue.some((v, i) => v !== value[i])) onChange(normalizedValue); | ||
| }, [normalizedValue]); | ||
|
|
||
| // The list as it is now. Replies to pending requests and effects from an | ||
| // earlier render read it, so they don't overwrite edits made since | ||
| // (ocl_online#786). Each change sets it straight away, so changes made | ||
| // before the parent re-renders build on each other. | ||
| const latestValueRef = React.useRef(normalizedValue) | ||
| React.useLayoutEffect(() => { | ||
| latestValueRef.current = normalizedValue | ||
| }, [normalizedValue]) | ||
| const commitValue = next => { | ||
| latestValueRef.current = next | ||
| onChange(next) | ||
| } | ||
|
|
||
| const algosById = useMemo(() => { | ||
| const m = new Map(); | ||
| (algos || []).forEach((a) => m.set(a.id, a)); | ||
|
|
@@ -199,10 +213,10 @@ export default function MultiAlgoSelector({ | |
| }) | ||
|
|
||
| const updateSelected = (key, patch) => { | ||
| const next = normalizedValue.map(v => (v.__key === key ? { ...v, ...patch } : v)); | ||
| onChange(next); | ||
| const current = latestValueRef.current | ||
| commitValue(patchSelected(current, key, patch)); | ||
| if((patch.id || patch.name)) { | ||
| if(filter(value, patch).length > 0) | ||
| if(filter(current, patch).length > 0) | ||
| setErrors(prev => ({...prev, [key]: {...prev[key], ...patch}})) | ||
| else if (patch.id) | ||
| setErrors(prev => ({...prev, [key]: {...omit(prev[key], 'id')}})) | ||
|
|
@@ -227,7 +241,8 @@ export default function MultiAlgoSelector({ | |
| if(syncedBridgeUrlRef.current.get(sel.__key) === url) continue | ||
| syncedBridgeUrlRef.current.set(sel.__key, url) | ||
| fetchBridgeCanonical(url).then(canonical => { | ||
| if(!canonical) return | ||
| // Removed or pointed at another repo while the fetch was pending. | ||
| if(!canonical || isStaleReply(latestValueRef.current, sel.__key, {target_repo_url: url})) return | ||
| updateSelected(sel.__key, { | ||
| bridge_repo: { canonical_url: canonical, canonical_url_source: 'repo' } | ||
| }) | ||
|
|
@@ -270,8 +285,8 @@ export default function MultiAlgoSelector({ | |
| }, [value, bridgeVersionsByUrl]) | ||
|
|
||
| const removeSelected = (key) => { | ||
| const next = (value || []).filter((v) => v.__key !== key); | ||
| onChange(next); | ||
| commitValue((latestValueRef.current || []).filter((v) => v.__key !== key)); | ||
| delete customCheckIdRef.current[key]; | ||
| setCustomAlgoMeta(prev => omit(prev, [key])); | ||
|
|
||
| setExpanded((prev) => { | ||
|
|
@@ -316,7 +331,7 @@ export default function MultiAlgoSelector({ | |
| __key: Math.random(100).toString() | ||
| }; | ||
|
|
||
| onChange([...(value || []), defaults]); | ||
| commitValue([...(latestValueRef.current || []), defaults]); | ||
| setExpanded((prev) => { | ||
| const n = new Map(prev); | ||
| n.set(defaults.__key, algo.type === 'custom' || algo.type?.includes('bridge')); | ||
|
|
@@ -344,18 +359,38 @@ export default function MultiAlgoSelector({ | |
| setCustomAlgoMeta(prev => ({...prev, [key]: {...prev[key], ...patch}})); | ||
| }; | ||
|
|
||
| // The newest check for each algorithm; replies to older checks are dropped. | ||
| const customCheckIdRef = React.useRef({}) | ||
| const verifyCustomAlgorithm = async (key, tokenOverride) => { | ||
| const selected = normalizedValue.find(v => v.__key === key); | ||
| const selected = latestValueRef.current.find(v => v.__key === key); | ||
| if(!selected?.url) | ||
| return; | ||
| const url = selected.url; | ||
| const checkId = (customCheckIdRef.current[key] || 0) + 1; | ||
| customCheckIdRef.current[key] = checkId; | ||
|
|
||
| const token = tokenOverride ?? selected?.token; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (optional): The stale-reply check only compares the URL. If the user edits the token while a check is pending, a 401 for the old token can still set
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed: the check compares the URL and token sent with the current entry; a change only stops loading. |
||
| setCustomAlgoState(key, {isLoading: true, requestError: '', requestSuccess: '', tokenRequired: false}); | ||
|
|
||
| const service = APIService.new(); | ||
| service.URL = selected.url; | ||
| service.URL = url; | ||
|
|
||
| const response = await service.get(token || false, {}, undefined, true); | ||
| const reply = customCheckReply({ | ||
| list: latestValueRef.current, | ||
| key, | ||
| sent: {url, token: selected.token}, | ||
| checkId, | ||
| latestCheckId: customCheckIdRef.current[key], | ||
| }); | ||
| if(reply === 'ignore') | ||
| return; | ||
| // The URL or token changed while the check was pending: the reply is for | ||
| // an algorithm that is no longer there. | ||
| if(reply === 'clear') { | ||
| setCustomAlgoState(key, {isLoading: false}); | ||
| return; | ||
| } | ||
| const status = response?.response?.status || response?.status; | ||
|
|
||
| if([401, 403].includes(status)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * Replies to a pending request apply to the current algorithm list | ||
| * (ocl_online#786). | ||
| * | ||
| * Run with: npm test | ||
| */ | ||
|
|
||
| import test from 'node:test' | ||
| import assert from 'node:assert/strict' | ||
|
|
||
| import { patchSelected, isStaleReply, customCheckReply } from '../selectedAlgos.js' | ||
|
|
||
| const custom = { __key: 'a', type: 'custom', url: 'https://algo.example/a', id: '', name: '' } | ||
| const search = { __key: 'b', type: 'ocl-search', id: 'ocl-search', name: 'Search' } | ||
|
|
||
| test('patchSelected: patches only the matching entry', () => { | ||
| const next = patchSelected([custom, search], 'a', { id: 'x', name: 'X' }) | ||
| assert.deepEqual(next[0], { ...custom, id: 'x', name: 'X' }) | ||
| assert.equal(next[1], search) | ||
| }) | ||
|
|
||
| test('patchSelected: an edit made while a check was pending is kept', () => { | ||
| const edited = [{ ...custom, description: 'typed while waiting' }, search] | ||
| const next = patchSelected(edited, 'a', { id: 'x', name: 'X' }) | ||
| assert.equal(next.length, 2) | ||
| assert.equal(next[0].description, 'typed while waiting') | ||
| assert.equal(next[0].id, 'x') | ||
| assert.equal(next[1], search) | ||
| }) | ||
|
|
||
| test('patchSelected: an empty or missing list stays empty', () => { | ||
| assert.deepEqual(patchSelected(undefined, 'a', { id: 'x' }), []) | ||
| assert.deepEqual(patchSelected([], 'a', { id: 'x' }), []) | ||
| }) | ||
|
|
||
| const sent = { url: custom.url, token: undefined } | ||
|
|
||
| test('isStaleReply: the entry is still there with the same sent fields', () => { | ||
| assert.equal(isStaleReply([custom, search], 'a', sent), false) | ||
| }) | ||
|
|
||
| test('isStaleReply: the entry was removed', () => { | ||
| assert.equal(isStaleReply([search], 'a', sent), true) | ||
| assert.equal(isStaleReply(undefined, 'a', sent), true) | ||
| }) | ||
|
|
||
| test('isStaleReply: the URL or token was changed', () => { | ||
| assert.equal(isStaleReply([{ ...custom, url: 'https://algo.example/b' }], 'a', sent), true) | ||
| assert.equal(isStaleReply([{ ...custom, token: 'new' }], 'a', sent), true) | ||
| }) | ||
|
|
||
| test('isStaleReply: a bridge repo URL that was changed', () => { | ||
| const bridge = { __key: 'c', type: 'ocl-bridge', target_repo_url: '/orgs/A/sources/A/' } | ||
| assert.equal(isStaleReply([bridge], 'c', { target_repo_url: '/orgs/A/sources/A/' }), false) | ||
| assert.equal(isStaleReply([{ ...bridge, target_repo_url: '/orgs/B/sources/B/' }], 'c', { target_repo_url: '/orgs/A/sources/A/' }), true) | ||
| }) | ||
|
|
||
| test('customCheckReply: applies to the algorithm as it is now', () => { | ||
| assert.equal(customCheckReply({ list: [custom], key: 'a', sent, checkId: 1, latestCheckId: 1 }), 'apply') | ||
| }) | ||
|
|
||
| test('customCheckReply: a newer check owns the state', () => { | ||
| assert.equal(customCheckReply({ list: [custom], key: 'a', sent, checkId: 1, latestCheckId: 2 }), 'ignore') | ||
| }) | ||
|
|
||
| test('customCheckReply: the algorithm was removed', () => { | ||
| assert.equal(customCheckReply({ list: [search], key: 'a', sent, checkId: 1, latestCheckId: undefined }), 'ignore') | ||
| assert.equal(customCheckReply({ list: [search], key: 'a', sent, checkId: 1, latestCheckId: 1 }), 'ignore') | ||
| }) | ||
|
|
||
| test('customCheckReply: the URL or token changed while pending only stops loading', () => { | ||
| assert.equal(customCheckReply({ list: [{ ...custom, url: 'https://algo.example/b' }], key: 'a', sent, checkId: 1, latestCheckId: 1 }), 'clear') | ||
| assert.equal(customCheckReply({ list: [{ ...custom, token: 'new' }], key: 'a', sent, checkId: 1, latestCheckId: 1 }), 'clear') | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Edits to the selected algorithm list (ocl_online#786). | ||
| // | ||
| // A reply that arrives after a request (a custom algorithm check, a bridge | ||
| // repo's canonical URL) is applied to the list as it is now, not to the list | ||
| // from the render that sent the request. | ||
|
|
||
| export const patchSelected = (list, key, patch) => | ||
| (list || []).map(v => (v.__key === key ? { ...v, ...patch } : v)) | ||
|
|
||
| // The reply was for an algorithm that has since been removed, or one whose | ||
| // sent fields (such as its URL or token) the user has since changed. | ||
| export const isStaleReply = (list, key, sent) => { | ||
| const current = (list || []).find(v => v.__key === key) | ||
| return !current || Object.keys(sent || {}).some(field => current[field] !== sent[field]) | ||
| } | ||
|
|
||
| // What to do with a custom algorithm check's reply: | ||
| // - 'ignore': a newer check owns this algorithm's state, or it was removed | ||
| // - 'clear': its URL or token changed while pending; only stop loading | ||
| // - 'apply': the reply is for the algorithm as it is now | ||
| export const customCheckReply = ({ list, key, sent, checkId, latestCheckId }) => { | ||
| if(latestCheckId !== checkId) | ||
| return 'ignore' | ||
| if(!(list || []).some(v => v.__key === key)) | ||
| return 'ignore' | ||
| return isStaleReply(list, key, sent) ? 'clear' : 'apply' | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important: A bridge algorithm can end up with the canonical URL of a repo the user has since moved away from. The
fetchBridgeCanonical(url).then(...)reply (line 241) now patches the current list through this function without checking thattarget_repo_urlis still the one fetched, so a slow reply for repo A lands after the user switched to repo B. Fix: skip the patch unless the current entry still hastarget_repo_url === url.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed: the canonical reply is skipped unless the entry still has the fetched
target_repo_url.