Skip to content

Commit

Permalink
add fallbacks to get windowId in panel
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasGollenstede committed Jun 8, 2021
1 parent b0643e3 commit e2dfcf5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
19 changes: 11 additions & 8 deletions src/background/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,21 @@ const queueClearCache = debounce(() => { cache = null; }, 30e3);
const States = {
// note: cleaning up the states is probably not worth it
data: /**@type{Record<number, WindowState>}*/({ __proto__: null, }),
new() {
new(/**@type{number}*/windowId) {
const state = /**@type{WindowState}*/({
tabId: -1, term: '', result: { term: '', matches: 0, cleared: true, },
tabId: -1, term: '', result: { windowId, term: '', matches: 0, cleared: true, },
}); state.fireSearched = setEvent(state, 'onSearched');
return state;
},
get(/**@type{number}*/windowId) { return this.data[windowId] || (this.data[windowId] = this.new()); },
get(/**@type{number}*/windowId) { return this.data[windowId] || (this.data[windowId] = this.new(windowId)); },
set(/**@type{number}*/windowId, /**@type{Partial<Omit<WindowState, 'onSearched'|'fireSearched'>>}*/state) {
const base = this.get(windowId); Object.assign(base, state);
state.result && base.fireSearched([ base.result, ]); return base;
},
};


/** @typedef { { term: string, matches: number, } & (
/** @typedef { { windowId: number, term: string, matches: number, } & (
* { index: number, cleared?: undefined, failed?: undefined, }
* | { index?: undefined, cleared: true, failed?: undefined, }
* | { index?: undefined, cleared?: undefined, failed: true, }
Expand Down Expand Up @@ -157,7 +157,7 @@ async function doSearch({
// clear previous search on empty term
if (!term) {
TST.methods.removeTabState({ tabs: '*', state: [ ].concat(...Object.values(classes).slice(0, -1/*searching*/)), }).catch(onTstError);
return States.set(windowId, { tabId: -1, term: '', result: { term: '', matches: 0, cleared: true, }, }).result;
return States.set(windowId, { tabId: -1, term: '', result: { windowId, term: '', matches: 0, cleared: true, }, }).result;
} term += '';

// pick tab properties to search
Expand Down Expand Up @@ -248,10 +248,10 @@ async function doSearch({
state.tabId >= 0 && TST.methods.addTabState({ tabs: [ state.tabId, ], state: classes.active, }),
]));

const finalResult = { term, matches: result.matching.size, index: matching.indexOf(tabs.byId.get(state.tabId)), };
const finalResult = { windowId, term, matches: result.matching.size, index: matching.indexOf(tabs.byId.get(state.tabId)), };
States.set(windowId, { result: finalResult, }); return finalResult;

} catch (error) { notify.error('Search failed!', error); return { term: typeof term === 'string' ? term : '', matches: 0, failed: true, }; } }
} catch (error) { notify.error('Search failed!', error); return { windowId, term: typeof term === 'string' ? term : '', matches: 0, failed: true, }; } }


async function startSearch() { options.search.children.searchByTabIds.value[1] && TST.methods.addTabState({ tabs: '*', state: classes.searching, }).catch(onTstError); }
Expand Down Expand Up @@ -304,7 +304,10 @@ const { getOptions, onOptions, } = (() => { // let panel instances know about `o
})();


const RPC = { doSearch, startSearch, stopSearch, focusActiveTab, getTerm, onSearched, getOptions, onOptions, };
async function getSingleWindowId() { const windows = (await Windows.getAll()); return windows.length === 1 ? windows[0].id : -1; }


const RPC = { doSearch, startSearch, stopSearch, focusActiveTab, getTerm, onSearched, getOptions, onOptions, getSingleWindowId, };
Runtime.onConnect.addListener(port => { new Port(port, web_ext_Port).addHandlers('', RPC); });

let lastGlobalFocus = 0;
Expand Down
10 changes: 6 additions & 4 deletions src/content/embed.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import Port, { web_ext_Port, } from 'multiport/index.esm.js';
const port = new Port(Browser.Runtime.connect(), web_ext_Port);


/**@type{Await<typeof import('../background/index.esm.js').default>['RPC']}*/ const RPC = globalThis.require?.cache['module!background/index']?.exports.RPC || Object.fromEntries([ 'doSearch', 'startSearch', 'stopSearch', 'focusActiveTab', 'getOptions', 'onOptions', 'getTerm', 'onSearched', ].map(name => [ name, (...args) => /**@type{Promise<any>}*/(port.request(null, name, ...args)), ]));
/**@type{Await<typeof import('../background/index.esm.js').default>['RPC']}*/ const RPC = globalThis.require?.cache['module!background/index']?.exports.RPC || Object.fromEntries([ 'doSearch', 'startSearch', 'stopSearch', 'focusActiveTab', 'getOptions', 'onOptions', 'getTerm', 'onSearched', 'getSingleWindowId', ].map(name => [ name, (...args) => /**@type{Promise<any>}*/(port.request(null, name, ...args)), ]));

(async () => {
const windowId = +(new globalThis.URL(globalThis.location.href).searchParams.get('windowId') ?? -1);
const windowId = +(new globalThis.URL(globalThis.location.href).searchParams.get('windowId') ?? (await RPC.getSingleWindowId()));

const initialTerm = windowId === -1 ? '' : (await RPC.onSearched({ windowId, }, result => { !inputs.term.matches(':focus') && setResult(result); })).term;
const onWindowId = async (/**@type{number}*/windowId) => (await RPC.onSearched({ windowId, }, result => { !inputs.term.matches(':focus') && setResult(result); })).term;

const { inputs, setResult, applyOptions, } = (await render(window, { RPC, destructive: true, windowId, initialTerm, }));
const initialTerm = windowId === -1 ? '' : (await onWindowId(windowId));

const { inputs, setResult, applyOptions, } = (await render(window, { RPC, destructive: true, windowId, initialTerm, onWindowId: windowId === -1 ? onWindowId : null, }));

applyOptions((await RPC.onOptions(applyOptions)));

Expand Down
7 changes: 6 additions & 1 deletion src/content/form.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ return dom; })();

/** Renders the interactive search panel into an empty `Window`. */
export default async function render(/**@type{Window}*/window, {
windowId = -1, destructive = false, initialTerm = '', RPC,
windowId = -1, destructive = false, initialTerm = '', RPC, onWindowId,
} = {
windowId: -1, destructive: !!false, initialTerm: '',
RPC: /**@type{Await<typeof import('../background/index.esm.js').default>['RPC']}*/(null),
onWindowId: /**@type{(windowId: number) => void}*/(null),
}) { const { document, } = window;

document.documentElement.classList.toggle('dark-theme', darkPref.matches);
Expand All @@ -30,6 +31,10 @@ export default async function render(/**@type{Window}*/window, {
setResult((await RPC.doSearch(form)));
}
function setResult(/**@type{import('../background/index.esm.js').SearchResult}*/result) {
if (result.windowId != null && result.windowId !== -1) {
windowId = result.windowId;
if (onWindowId) { onWindowId(result.windowId); onWindowId = null; }
}
!inputs.term.matches(':focus') && (inputs.term.value = result.term);
matchIndex.textContent = result.failed ? '??' : result.cleared ? '' : result.index >= 0 ? ((result.index + 1) +' / '+ result.matches) : (result.matches || 'none') +'';
document.documentElement.style.setProperty('--count-width', matchIndex.clientWidth +'px');
Expand Down

0 comments on commit e2dfcf5

Please sign in to comment.