Skip to content

Commit

Permalink
enable argument-less api methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimiry committed Jun 24, 2018
1 parent 21d4bd6 commit 921b0dc
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 44 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ yarn-error.log

/dist/
/output/
/yarn-error.log
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ This module provides services for interacting with `ipcMain` and `webview` provi

- First of all an API structure needs to be defined ([example/electron-app/src/shared/ipc-main-api-definition.ts](example/electron-app/src/shared/ipc-main-api-definition.ts)):
```typescript
import {ApiMethod, IpcMainApiService} from "electron-rpc-api";
import {ApiMethod, ApiMethodNoArgument, IpcMainApiService} from "electron-rpc-api";

export interface IpcMainApi {
ping: ApiMethod<{ domain: string, times: number }, { domain: string, value: number }>;
quitApp: ApiMethod<undefined, never>;
quitApp: ApiMethodNoArgument<never>;
}

export const IPC_MAIN_API_SERVICE = new IpcMainApiService<IpcMainApi>({channel: "some-ipcMain-channel"});
Expand Down
2 changes: 1 addition & 1 deletion example/electron-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"start:web": "cross-env NODE_ENV=development webpack-dev-server -c ./webpack.config.ts"
},
"dependencies": {
"electron-rpc-api": "^0.1.0",
"electron-rpc-api": "^0.2.0",
"rxjs": "^6.2.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion example/electron-app/src/main/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function init(ctx: AppContext, api: IpcMainApi): Promise<Tray> {
{
label: "Quit",
async click() {
await api.quitApp(undefined).toPromise();
await api.quitApp().toPromise();
},
},
]);
Expand Down
8 changes: 0 additions & 8 deletions example/electron-app/src/renderer/browser-window/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,3 @@ body {
padding-left: 20px;
padding-right: 20px;
}

.loading {
display: none;
}

fieldset[disabled] .loading {
display: inline-block;
}
4 changes: 2 additions & 2 deletions example/electron-app/src/shared/ipc-main-api-definition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {ApiMethod, IpcMainApiService} from "electron-rpc-api";
import {ApiMethod, ApiMethodNoArgument, IpcMainApiService} from "electron-rpc-api";

export interface IpcMainApi {
ping: ApiMethod<{ domain: string, times: number }, { domain: string, value: number }>;
quitApp: ApiMethod<undefined, never>;
quitApp: ApiMethodNoArgument<never>;
}

export const IPC_MAIN_API_SERVICE = new IpcMainApiService<IpcMainApi>({channel: "some-ipcMain-channel"});
14 changes: 7 additions & 7 deletions example/electron-app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1798,11 +1798,11 @@ [email protected]:
lazy-val "^1.0.3"
mime "^2.3.1"

electron-rpc-api@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/electron-rpc-api/-/electron-rpc-api-0.1.0.tgz#37c523e5b686ce0ad2a5fcdc62c1e47c0c2703a6"
electron-rpc-api@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/electron-rpc-api/-/electron-rpc-api-0.2.0.tgz#0e534d6bbad892d525818068bee989d24374dc14"
dependencies:
pubsub-to-stream-api "^0.1.0"
pubsub-to-stream-api "^0.2.0"
tslib "^1.9.2"

electron-to-chromium@^1.2.7:
Expand Down Expand Up @@ -4577,9 +4577,9 @@ public-encrypt@^4.0.0:
parse-asn1 "^5.0.0"
randombytes "^2.0.1"

pubsub-to-stream-api@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/pubsub-to-stream-api/-/pubsub-to-stream-api-0.1.0.tgz#84b282cf56f2b52453c590044e17d597750fdecd"
pubsub-to-stream-api@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/pubsub-to-stream-api/-/pubsub-to-stream-api-0.2.0.tgz#6950d57b4547d166b22106bc876bb3a72bd6437e"
dependencies:
deserialize-error "^0.0.3"
rxjs "^6.2.1"
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-rpc-api",
"version": "0.1.0",
"version": "0.2.0",
"description": "Wrapper around the Electron's IPC for building type-safe API based RPC-like interactions",
"author": "Vladimir Yakovlev <[email protected]> (https://github.com/vladimiry)",
"license": "MIT",
Expand Down Expand Up @@ -33,7 +33,7 @@
]
},
"dependencies": {
"pubsub-to-stream-api": "^0.1.0",
"pubsub-to-stream-api": "^0.2.0",
"tslib": "^1.9.2"
},
"devDependencies": {
Expand All @@ -45,7 +45,7 @@
"npm-run-all": "^4.1.3",
"rewiremock": "^3.7.2",
"rimraf": "^2.6.2",
"sinon": "^6.0.0",
"sinon": "^6.0.1",
"ts-node": "^7.0.0",
"tslint": "^5.10.0",
"tslint-rules-bunch": "^0.0.4",
Expand Down
7 changes: 6 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {Model} from "pubsub-to-stream-api";

import {ApiMethod, ApiMethodNoArgument} from "./model";

export * from "./ipc-main-api-service";
export * from "./webview-api-service";

export type ApiMethod<I, O> = Model.Action<I, O>;
export {
ApiMethod,
ApiMethodNoArgument,
};
8 changes: 3 additions & 5 deletions src/lib/ipc-main-api-service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {IpcMain, IpcRenderer} from "electron";
import {Model, Service} from "pubsub-to-stream-api";

import {AnyType, CombinedEventEmitter} from "./model";
import {AnyType} from "./model";

export class IpcMainApiService<Api extends Model.ActionsRecord<Extract<keyof Api, string>>> extends Service<Api> {

public registerApi(actions: Api, {ipcMain: instance}: { ipcMain?: IpcMain } = {}) {
const ipcMain = instance || require("electron").ipcMain;
const em: CombinedEventEmitter = {
const em: Model.CombinedEventEmitter = {
on: ipcMain.addListener.bind(ipcMain),
off: ipcMain.removeListener.bind(ipcMain),
emit: ipcMain.emit.bind(ipcMain),
Expand All @@ -19,7 +18,7 @@ export class IpcMainApiService<Api extends Model.ActionsRecord<Extract<keyof Api

public buildClient({ipcRenderer: instance, options}: { ipcRenderer?: IpcRenderer, options?: Model.CallOptions } = {}) {
const ipcRenderer = instance || require("electron").ipcRenderer;
const em: CombinedEventEmitter = {
const em: Model.CombinedEventEmitter = {
on: (event, listener) => {
ipcRenderer.on(event, (...args: AnyType[]) => listener(args[1]));
return em;
Expand All @@ -30,5 +29,4 @@ export class IpcMainApiService<Api extends Model.ActionsRecord<Extract<keyof Api

return this.caller({emitter: em, listener: em}, options);
}

}
4 changes: 3 additions & 1 deletion src/lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ import {Model, Service} from "pubsub-to-stream-api";
// tslint:disable-next-line:no-any
export type AnyType = any;

export type CombinedEventEmitter = Model.EventListener & Model.EventEmitter;
export type ApiMethod<I, O> = Model.Action<I, O>;

export type ApiMethodNoArgument<I> = Model.ActionWithoutInput<I>;
7 changes: 3 additions & 4 deletions src/lib/webview-api-service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {IpcRenderer} from "electron";
import {Model, Service} from "pubsub-to-stream-api";

import {AnyType, CombinedEventEmitter} from "./model";
import {AnyType} from "./model";

export class WebViewApiService<Api extends Model.ActionsRecord<Extract<keyof Api, string>>> extends Service<Api> {

public registerApi(actions: Api, {ipcRenderer: instance}: { ipcRenderer?: IpcRenderer } = {}) {
const ipcRenderer = instance || require("electron").ipcRenderer;
const em: CombinedEventEmitter = {
const em: Model.CombinedEventEmitter = {
on: (event, listener) => {
ipcRenderer.on(event, (...args: AnyType[]) => listener(args[1]));
return em;
Expand All @@ -24,7 +23,7 @@ export class WebViewApiService<Api extends Model.ActionsRecord<Extract<keyof Api

public buildClient(webView: Electron.WebviewTag, {options}: { options?: Model.CallOptions } = {}) {
const listenEvent = "ipc-message";
const eventEmitter: CombinedEventEmitter = {
const eventEmitter: Model.CombinedEventEmitter = {
on: (event, listener) => {
webView.addEventListener(listenEvent, ({channel, args}) => {
if (channel !== event) {
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1358,8 +1358,8 @@ escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.4, escape-string-regexp@^
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"

esm@^3.0.40:
version "3.0.53"
resolved "https://registry.yarnpkg.com/esm/-/esm-3.0.53.tgz#dcab296db0531cf10d611dc783ba31c73fb58b59"
version "3.0.54"
resolved "https://registry.yarnpkg.com/esm/-/esm-3.0.54.tgz#df44b0815008b204fa7105251d4fc8690816c61f"

espower-location-detector@^1.0.0:
version "1.0.0"
Expand Down Expand Up @@ -3073,9 +3073,9 @@ public-encrypt@^4.0.0:
parse-asn1 "^5.0.0"
randombytes "^2.0.1"

pubsub-to-stream-api@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/pubsub-to-stream-api/-/pubsub-to-stream-api-0.1.0.tgz#84b282cf56f2b52453c590044e17d597750fdecd"
pubsub-to-stream-api@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/pubsub-to-stream-api/-/pubsub-to-stream-api-0.2.0.tgz#6950d57b4547d166b22106bc876bb3a72bd6437e"
dependencies:
deserialize-error "^0.0.3"
rxjs "^6.2.1"
Expand Down Expand Up @@ -3477,9 +3477,9 @@ single-line-log@^1.1.2:
dependencies:
string-width "^1.0.1"

sinon@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-6.0.0.tgz#f26627e4830dc34279661474da2c9e784f166215"
sinon@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-6.0.1.tgz#4247b97dd8fecbe1e19f8b816a5e7eed3993bcfe"
dependencies:
"@sinonjs/formatio" "^2.0.0"
diff "^3.5.0"
Expand Down

0 comments on commit 921b0dc

Please sign in to comment.