Skip to content
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

Add release method #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ This is used to receive and respond to RPC actions received over IPC.
@param object lib - The function library used with rpc
@param string scope - This thread's scope (electron, main-renderer etc)
```

`RpcIpcManger#release`

Use this method to stop handling RPC calls.
20 changes: 14 additions & 6 deletions src/RpcIpcManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class RpcIpcManager {
return get(lib, functionName);
}

ipcReceive('RPC', (payload) => {
this.unsubscribeFunctions = [];

const unsubscribeFromRPC = ipcReceive('RPC', (payload) => {
/****************************************************************
RPC Receive.

Expand Down Expand Up @@ -48,19 +50,19 @@ class RpcIpcManager {

const functionFromAlias = getFunction(functionToRun);
// If we have a function, run it.
if (functionFromAlias){
if (functionFromAlias) {
// Run the function and get the result
const result = functionFromAlias.apply(null, functionInputs);
// We wrap the result in Promise.resolve so we can treat
// it like a promise (even if is not a promise);
Promise.resolve(result).then(resolve).catch(reject);
}
else {
reject({error: 'Function not found.'})
reject({ error: 'Function not found.' })
}
}
});

this.unsubscribeFunctions.push(unsubscribeFromRPC);


/****************************************************************
Expand All @@ -70,18 +72,24 @@ class RpcIpcManager {
to see if there is a corresponding RPC promise in the promise
cache. If we find one, we resolve the promise.
****************************************************************/
ipcReceive('RPC_RESOLVED', (payload) => {
const unsubscribeFromRPCResolved = ipcReceive('RPC_RESOLVED', (payload) => {
// Check the promise cache
const promise = promises[payload.promiseId];
if (promise) promise.resolve(payload.result);
})
this.unsubscribeFunctions.push(unsubscribeFromRPCResolved)

ipcReceive('RPC_REJECTED', (payload) => {
const unsubscribeFromRPCRejected = ipcReceive('RPC_REJECTED', (payload) => {
// Check the promise cache
const promise = promises[payload.promiseId];
if (promise) promise.reject(payload.result);
})
this.unsubscribeFunctions.push(unsubscribeFromRPCRejected)

}

release() {
this.unsubscribeFunctions.forEach(unsubscribe => unsubscribe());
}
};

Expand Down