Skip to content

Commit

Permalink
Attempt to add channels. However it's broken now.
Browse files Browse the repository at this point in the history
It shows "WebContents #1 called ipcRenderer.sendSync() with 'promised/call/experiment' channel without listeners.".

I tried to use some timeouts to see if it's related to that but it didn't get around this problem.
  • Loading branch information
mlewand committed Jul 5, 2023
1 parent 9a87d5c commit 7995a48
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
8 changes: 5 additions & 3 deletions app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@ document.addEventListener('DOMContentLoaded', async () => {

electronBridge.receive( 'experiment-then', ( res: any ) => console.log( 'independent listener: experiment-then', res ) );

electronBridge.experiment( 'experiment' )
.then( res => console.log('DOMContentLoaded: RESOLVED', res ) )
.catch( err => console.log('DOMContentLoaded: REJECTED', err ) );
setTimeout(() => {
electronBridge.experiment( 'experiment' )
.then( res => console.log('DOMContentLoaded: RESOLVED', res ) )
.catch( err => console.log('DOMContentLoaded: REJECTED', err ) );
}, 300);
} );

function addListeners() {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
"dependencies": {
"electron": "^25.2.0",
"electron-builder": "^24.4.0",
"open": "8.1.0"
"open": "8.1.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/node": "^20.3.1",
"@types/uuid": "^9.0.2",
"ts-node": "^10.9.1",
"typescript": "^5.1.3"
}
Expand Down
30 changes: 27 additions & 3 deletions src/NoteQuickAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SetWorkspaceCommand from './Command/SetWorkspace';
import CommandSet from './Command/CommandSet';
import Config from './Config';
import type { WorkspaceInfo } from './Config';
import { v4 as uuid4 } from 'uuid';

export type SetActiveWorkspaceParameter = number | 'next' | 'previous';

Expand Down Expand Up @@ -69,6 +70,10 @@ export default class NoteQuickAdd {
this.config = results[ 0 ];
this.electronApp = results[ 1 ];

console.log('waiting');
await wait( 1000 );
console.log('the wait is done!');

this.mainWindow = await this._createMainWindow();

this._initCommands();
Expand Down Expand Up @@ -117,22 +122,33 @@ export default class NoteQuickAdd {
}

public addPromisedIpcListener( channel : string, listener : any ) {
ipcMain.on( `promised/call/${ channel }`, ( event: any, ...args ) => {
const promiseChannel = `promised/call/${ channel }`;
console.log('adding a listener for ' + promiseChannel);

ipcMain.handle( promiseChannel, ( event: any, ...args ) => {
console.log( `MAIN: got promise request (${channel} channel)` );

const ret = listener( event, ...args );
const uniqueId = uuid4();

if ( !ret.then ) {
throw new Error( 'Promised IPC listener must return a promise' );
}

ret
.then( ( result : any ) => {
this.send( `promised/then/${ channel }`, result );

setTimeout(() => {
console.log('sending to ', `promised/then/${ channel }/${uniqueId}`);

this.send( `promised/then/${ channel }/${uniqueId}`, result );
}, 2000);
} )
.catch( ( error : any ) => {
this.send( `promised/catch/${ channel }`, error );
this.send( `promised/catch/${ channel }/${uniqueId}`, error );
} );

return uniqueId;
} );
}

Expand Down Expand Up @@ -247,4 +263,12 @@ export default class NoteQuickAdd {
console.log( 'Global shortcut registration failed' );
}
}
}

function wait( howLong = 1000 ) {
return new Promise<void>( ( resolve, reject ) => {
setTimeout( () => {
resolve();
}, howLong );
} )
}
21 changes: 14 additions & 7 deletions src/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ contextBridge.exposeInMainWorld(
if ( !channel ) {
throw new Error( 'Missing channel name' );
}
const PROMISE_TIMEOUT_TIME = 30000;
const PROMISE_TIMEOUT_TIME = 5000;
// const PROMISE_TIMEOUT_TIME = 30000;

return new Promise( ( resolve, reject ) => {
console.log("frontend: sending a promise");
console.log("frontend: sending a promise", `promised/call/${ channel }`);

// const uniqueId = await ipcRenderer.invoke( `promised/call/${ channel }`, ...args );
const uniqueId = ipcRenderer.sendSync( `promised/call/${ channel }`, ...args );

// @todo add a timeout to reject the promise if it takes too long.
const cleanup = () => {
ipcRenderer.removeListener( `promised/then/${ channel }`, thenCallback );
ipcRenderer.removeListener( `promised/catch/${ channel }`, catchCallback );
ipcRenderer.removeListener( `promised/then/${ channel }/${ uniqueId }`, thenCallback );
ipcRenderer.removeListener( `promised/catch/${ channel }/${ uniqueId }`, catchCallback );
clearTimeout( timeoutHandler );
}

Expand All @@ -57,10 +62,12 @@ contextBridge.exposeInMainWorld(
reject( ...args );
};

ipcRenderer.once( `promised/then/${ channel }`, thenCallback );
ipcRenderer.once( `promised/catch/${ channel }`, catchCallback );
console.log( 'frontend, received id: ', uniqueId );

console.log('listening to ', `promised/then/${ channel }/${ uniqueId }`);

ipcRenderer.send( `promised/call/${ channel }`, ...args );
ipcRenderer.once( `promised/then/${ channel }/${ uniqueId }`, thenCallback );
ipcRenderer.once( `promised/catch/${ channel }/${ uniqueId }`, catchCallback );
} );
}
}
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@
dependencies:
"@types/node" "*"

"@types/uuid@^9.0.2":
version "9.0.2"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.2.tgz#ede1d1b1e451548d44919dc226253e32a6952c4b"
integrity sha512-kNnC1GFBLuhImSnV7w4njQkUiJi0ZXUycu1rUaouPqiKlXkh77JKgdRnTAp1x5eBwcIwbtI+3otwzuIDEuDoxQ==

"@types/verror@^1.10.3":
version "1.10.6"
resolved "https://registry.yarnpkg.com/@types/verror/-/verror-1.10.6.tgz#3e600c62d210c5826460858f84bcbb65805460bb"
Expand Down Expand Up @@ -2189,6 +2194,11 @@ util-deprecate@^1.0.1:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==

uuid@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5"
integrity sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==

v8-compile-cache-lib@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
Expand Down

0 comments on commit 7995a48

Please sign in to comment.