Skip to content

Commit

Permalink
✨ Implement reload api
Browse files Browse the repository at this point in the history
  • Loading branch information
trickypr committed Apr 13, 2024
1 parent 5a49146 commit 6e89242
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
64 changes: 42 additions & 22 deletions apps/extensions/lib/parent/ext-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,38 @@ this.tabs = class extends ExtensionAPIPersistent {
getAPI(context) {
const { extension } = context

return {
tabs: {
async get(tabId) {
const window = [
...lazy.WindowTracker.registeredWindows.values(),
].find((window) =>
window.windowTabs().some((tab) => tab.view.browserId === tabId),
)

if (!window) {
return Promise.reject({
message: `Cannot find tab matching the id ${tabId}`,
})
}
/**
* @param {number} tabId
*/
async function get(tabId) {
const window = [...lazy.WindowTracker.registeredWindows.values()].find(
(window) =>
window.windowTabs().some((tab) => tab.view.browserId === tabId),
)

const tab = window
.windowTabs()
.find((tab) => tab.view.browserId === tabId)
if (!window) {
return Promise.reject({
message: `Cannot find tab matching the id ${tabId}`,
})
}

if (!tab) {
return Promise.reject({
message: `Cannot find tab matching the id ${tabId}`,
})
}
const tab = window
.windowTabs()
.find((tab) => tab.view.browserId === tabId)

if (!tab) {
return Promise.reject({
message: `Cannot find tab matching the id ${tabId}`,
})
}

return { tab, window }
}

return {
tabs: {
async get(tabId) {
const { tab, window } = await get(tabId)
return serialize(extension)([tab, window])
},

Expand Down Expand Up @@ -149,6 +156,19 @@ this.tabs = class extends ExtensionAPIPersistent {
}
},

async reload(tabIds) {
if (typeof tabIds === 'number') {
const { tab } = await get(tabIds)
tab.view.browser.reload()
return
}

for (const id of tabIds) {
const { tab } = await get(id)
tab.view.browser.reload()
}
},

async update(tabId, updateProperties) {
const windows = lazy.WindowTracker.registeredWindows.values()
for (const window of windows) {
Expand Down
1 change: 1 addition & 0 deletions apps/extensions/lib/schemaTypes/tabs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ declare module tabs__tabs {
get: (tabId: number) => Promise<Tab>
query: (queryInfo: QueryInfo) => Promise<Tab[]>
remove: (tabIds: number | number[]) => unknown
reload: (tabIds: number | number[]) => unknown
update: (tabId: number, updateProperties: UpdateInfo) => unknown
}
}
Expand Down
14 changes: 14 additions & 0 deletions apps/extensions/lib/schemas/tabs.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@
}
]
},
{
"name": "reload",
"type": "function",
"async": true,
"parameters": [
{
"name": "tabIds",
"choices": [
{ "type": "integer" },
{ "type": "array", "items": { "type": "integer" } }
]
}
]
},
{
"name": "update",
"type": "function",
Expand Down

0 comments on commit 6e89242

Please sign in to comment.