From 921b0dc08eeaa2c673f8a362e003878e9530f140 Mon Sep 17 00:00:00 2001 From: Vladimir Y Date: Mon, 25 Jun 2018 00:38:08 +0300 Subject: [PATCH] enable argument-less api methods --- .gitignore | 1 - README.md | 4 ++-- example/electron-app/package.json | 2 +- example/electron-app/src/main/tray.ts | 2 +- .../src/renderer/browser-window/index.scss | 8 -------- .../src/shared/ipc-main-api-definition.ts | 4 ++-- example/electron-app/yarn.lock | 14 +++++++------- package.json | 6 +++--- src/lib/index.ts | 7 ++++++- src/lib/ipc-main-api-service.ts | 8 +++----- src/lib/model.ts | 4 +++- src/lib/webview-api-service.ts | 7 +++---- yarn.lock | 16 ++++++++-------- 13 files changed, 39 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index b44678f..bbfb6f5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,3 @@ yarn-error.log /dist/ /output/ -/yarn-error.log diff --git a/README.md b/README.md index a1c412a..842c102 100644 --- a/README.md +++ b/README.md @@ -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; + quitApp: ApiMethodNoArgument; } export const IPC_MAIN_API_SERVICE = new IpcMainApiService({channel: "some-ipcMain-channel"}); diff --git a/example/electron-app/package.json b/example/electron-app/package.json index 85bbd9f..ce74021 100644 --- a/example/electron-app/package.json +++ b/example/electron-app/package.json @@ -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": { diff --git a/example/electron-app/src/main/tray.ts b/example/electron-app/src/main/tray.ts index c3562a6..e5967a2 100644 --- a/example/electron-app/src/main/tray.ts +++ b/example/electron-app/src/main/tray.ts @@ -18,7 +18,7 @@ export async function init(ctx: AppContext, api: IpcMainApi): Promise { { label: "Quit", async click() { - await api.quitApp(undefined).toPromise(); + await api.quitApp().toPromise(); }, }, ]); diff --git a/example/electron-app/src/renderer/browser-window/index.scss b/example/electron-app/src/renderer/browser-window/index.scss index 0f37260..bbef173 100644 --- a/example/electron-app/src/renderer/browser-window/index.scss +++ b/example/electron-app/src/renderer/browser-window/index.scss @@ -9,11 +9,3 @@ body { padding-left: 20px; padding-right: 20px; } - -.loading { - display: none; -} - -fieldset[disabled] .loading { - display: inline-block; -} diff --git a/example/electron-app/src/shared/ipc-main-api-definition.ts b/example/electron-app/src/shared/ipc-main-api-definition.ts index d790599..5d04d9a 100644 --- a/example/electron-app/src/shared/ipc-main-api-definition.ts +++ b/example/electron-app/src/shared/ipc-main-api-definition.ts @@ -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; + quitApp: ApiMethodNoArgument; } export const IPC_MAIN_API_SERVICE = new IpcMainApiService({channel: "some-ipcMain-channel"}); diff --git a/example/electron-app/yarn.lock b/example/electron-app/yarn.lock index cf07165..7d5460c 100644 --- a/example/electron-app/yarn.lock +++ b/example/electron-app/yarn.lock @@ -1798,11 +1798,11 @@ electron-publish@20.15.0: 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: @@ -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" diff --git a/package.json b/package.json index d6eaf35..533dd2a 100644 --- a/package.json +++ b/package.json @@ -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 (https://github.com/vladimiry)", "license": "MIT", @@ -33,7 +33,7 @@ ] }, "dependencies": { - "pubsub-to-stream-api": "^0.1.0", + "pubsub-to-stream-api": "^0.2.0", "tslib": "^1.9.2" }, "devDependencies": { @@ -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", diff --git a/src/lib/index.ts b/src/lib/index.ts index 19fe676..efaa4fd 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -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 = Model.Action; +export { + ApiMethod, + ApiMethodNoArgument, +}; diff --git a/src/lib/ipc-main-api-service.ts b/src/lib/ipc-main-api-service.ts index 3b2b632..3f9015a 100644 --- a/src/lib/ipc-main-api-service.ts +++ b/src/lib/ipc-main-api-service.ts @@ -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>> extends Service { - 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), @@ -19,7 +18,7 @@ export class IpcMainApiService { ipcRenderer.on(event, (...args: AnyType[]) => listener(args[1])); return em; @@ -30,5 +29,4 @@ export class IpcMainApiService = Model.Action; + +export type ApiMethodNoArgument = Model.ActionWithoutInput; diff --git a/src/lib/webview-api-service.ts b/src/lib/webview-api-service.ts index f58dfc3..db37616 100644 --- a/src/lib/webview-api-service.ts +++ b/src/lib/webview-api-service.ts @@ -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>> extends Service { - 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; @@ -24,7 +23,7 @@ export class WebViewApiService { webView.addEventListener(listenEvent, ({channel, args}) => { if (channel !== event) { diff --git a/yarn.lock b/yarn.lock index 9a99b0c..5e1d1a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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"