Skip to content

Commit

Permalink
update frame tree on cri client reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding committed Apr 22, 2022
1 parent 793cac1 commit 02f7fd8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
11 changes: 10 additions & 1 deletion packages/server/lib/browsers/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,16 @@ const _connectToChromeRemoteInterface = function (port, onError, browserDisplayN
.then((wsUrl) => {
debug('received wsUrl %s for port %d', wsUrl, port)

return CriClient.create(wsUrl, onError)
return CriClient.create({
target: wsUrl,
onError,
onReconnect (client) {
// if the client disconnects (e.g. due to a computer sleeping), update
// the frame tree on reconnect in cases there were changes while
// the client was disconnected
_updateFrameTree(client)()
},
})
})
}

Expand Down
18 changes: 13 additions & 5 deletions packages/server/lib/browsers/cri-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ export { chromeRemoteInterface }

type DeferredPromise = { resolve: Function, reject: Function }

export const create = async (target: websocketUrl, onAsynchronousError: Function): Promise<CRIWrapper> => {
interface CriClientOptions {
target: websocketUrl
onError: Function
onReconnect?: (client: CRIWrapper) => void
}

export const create = async (options: CriClientOptions): Promise<CRIWrapper> => {
const { target, onError, onReconnect } = options
const subscriptions: {eventName: CRI.EventName, cb: Function}[] = []
const enableCommands: CRI.Command[] = []
let enqueuedCommands: {command: CRI.Command, params: any, p: DeferredPromise }[] = []
Expand Down Expand Up @@ -180,8 +187,12 @@ export const create = async (target: websocketUrl, onAsynchronousError: Function
})

enqueuedCommands = []

if (onReconnect) {
onReconnect(client)
}
} catch (err) {
onAsynchronousError(errors.get('CDP_COULD_NOT_RECONNECT', err))
onError(errors.get('CDP_COULD_NOT_RECONNECT', err))
}
}

Expand Down Expand Up @@ -285,9 +296,6 @@ export const create = async (target: websocketUrl, onAsynchronousError: Function

return cri.close()
},

// @ts-ignore
reconnect,
}

return client
Expand Down
5 changes: 4 additions & 1 deletion packages/server/lib/browsers/firefox-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ const attachToTabMemory = Bluebird.method((tab) => {

async function setupRemote (remotePort, automation, options) {
const wsUrl = await protocol.getWsTargetFor(remotePort, 'Firefox')
const criClient = await CriClient.create(wsUrl, options.onError)
const criClient = await CriClient.create({
target: wsUrl,
onError: options.onError,
})
const cdpAutomation = new CdpAutomation({
sendDebuggerCommandFn: criClient.send,
onFn: criClient.on,
Expand Down
7 changes: 6 additions & 1 deletion packages/server/test/unit/browsers/cri-client_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ describe('lib/browsers/cri-client', function () {
'chrome-remote-interface': criImport,
})

getClient = () => criClient.create(DEBUGGER_URL, onError)
getClient = () => {
return criClient.create({
target: DEBUGGER_URL,
onError,
})
}
})

context('.create', function () {
Expand Down

0 comments on commit 02f7fd8

Please sign in to comment.