Building GUI App with Electron and Lisp

62
How Building GUI App with Lisp The Case of Pocket Change, Inc. LISP MEETUP #50 Mar 27, 2017

Transcript of Building GUI App with Electron and Lisp

Page 1: Building GUI App with Electron and Lisp

How Building GUI App with Lisp The Case of Pocket Change, Inc.

LISP MEETUP #50 Mar 27, 2017

Page 2: Building GUI App with Electron and Lisp

I’m Eitaro Fukamachi @nitro_idiot fukamachi

Page 3: Building GUI App with Electron and Lisp

Joined Pocket Change in Feb 1

Page 4: Building GUI App with Electron and Lisp

Pocket Change

http://www.pocket-change.jp

Page 5: Building GUI App with Electron and Lisp

Available at Haneda Airport

Page 6: Building GUI App with Electron and Lisp

Today’s topic

Page 7: Building GUI App with Electron and Lisp

What I want to tell today

What I don’t tell

How we use Common Lisp in our work

How you can use Common Lisp in your work

Page 8: Building GUI App with Electron and Lisp
Page 9: Building GUI App with Electron and Lisp

Touch Screen

Coins BillsIC Card Reader

Receipt Printer

API Server

Page 10: Building GUI App with Electron and Lisp

Touch Screen

Coins BillsIC Card Reader

Receipt Printer

API Server

Page 11: Building GUI App with Electron and Lisp

Building the next version of this software is my work.

Page 12: Building GUI App with Electron and Lisp

How to build Pocket Change

Page 13: Building GUI App with Electron and Lisp

• Windows OS (Rich!)

• Fullscreen GUI application

Page 14: Building GUI App with Electron and Lisp

We decided to use Electron

https://electron.atom.io

by

Page 15: Building GUI App with Electron and Lisp

Web Server (Express)

Main Process (Node.js)

Page 16: Building GUI App with Electron and Lisp

Open

Web Server (Express)

Main Process (Node.js)

Page 17: Building GUI App with Electron and Lisp

Open

Web Server (Express)

Main Process (Node.js)

Renderer Process (Chromium)

React.js

Page 18: Building GUI App with Electron and Lisp

Open

Web Server (Express)

Main Process (Node.js)

Bidirectional Communication

Renderer Process (Chromium)

React.js

Page 19: Building GUI App with Electron and Lisp

Why Electron?

• Easy to deploy

• Can package into a single EXE installer

• Can build GUI with Web technologies

• Embedded Chromium

Page 20: Building GUI App with Electron and Lisp

However

Page 21: Building GUI App with Electron and Lisp

The language is JavaScript. 👎

Page 22: Building GUI App with Electron and Lisp

Why can’t we use Common Lisp?

Page 23: Building GUI App with Electron and Lisp

None (just a launcher)

Main Process (Node.js)

+ Common Lisp

Page 24: Building GUI App with Electron and Lisp

Open

None (just a launcher)

Main Process (Node.js)

+ Common Lisp

Page 25: Building GUI App with Electron and Lisp

Open

None (just a launcher)

Main Process (Node.js)

Renderer Process (Chromium)

React.js

+ Common Lisp

Page 26: Building GUI App with Electron and Lisp

Open

None (just a launcher)

Main Process (Node.js)

Renderer Process (Chromium)

React.js

Spawn

+ Common Lisp

Page 27: Building GUI App with Electron and Lisp

Open

None (just a launcher)

Main Process (Node.js)

Renderer Process (Chromium)

React.js

Common Lisp Process (SBCL)

Spawn

+ Common Lisp

Page 28: Building GUI App with Electron and Lisp

Open

None (just a launcher)

Main Process (Node.js)

Renderer Process (Chromium)

React.js

Common Lisp Process (SBCL)

Spawn

Bidirectional Communication

+ Common Lisp

Page 29: Building GUI App with Electron and Lisp

Open

None (just a launcher)

Main Process (Node.js)

Renderer Process (Chromium)

React.js

Common Lisp Process (SBCL)

Spawn

Bidirectional Communication

+ Common Lisp

Make an SBCL executable and bundle it

Page 30: Building GUI App with Electron and Lisp

Looks a quite easy trick, huh? :)

Page 31: Building GUI App with Electron and Lisp

How communicating between CL and Browser

Page 32: Building GUI App with Electron and Lisp

Open

None (just a launcher)

Main Process (Node.js)

Renderer Process (Chromium)

React.js

Common Lisp Process (SBCL)

Spawn

Bidirectional Communication

+ Common Lisp

Page 33: Building GUI App with Electron and Lisp

Open

None (just a launcher)

Main Process (Node.js)

Renderer Process (Chromium)

React.js

Common Lisp Process (SBCL)

Spawn

Bidirectional Communication

+ Common Lisp

Page 34: Building GUI App with Electron and Lisp

Communication between CL and Browser

• MUST be bidirectional

• MUST be asynchronous

• MUST be easy to handle with JavaScript

• SHOULD be real-time

Page 35: Building GUI App with Electron and Lisp

We decided to use JSON-RPC

• Small specification

• Not depends on a specific transport layer

• We use WebSocket

• http://jsonrpc.org

• https://github.com/fukamachi/jsonrpc

Page 36: Building GUI App with Electron and Lisp

JSON-RPC--> {"jsonrpc": “2.0", "method": “subtract", "params": [42, 23], "id": 1}

Request

Page 37: Building GUI App with Electron and Lisp

JSON-RPC--> {"jsonrpc": “2.0", "method": “subtract", "params": [42, 23], "id": 1}

Request

<-- {"jsonrpc": “2.0", "result": 19, "id": 1}

Response

Page 38: Building GUI App with Electron and Lisp

JSON-RPC--> {"jsonrpc": “2.0", "method": “subtract", "params": [42, 23], "id": 1}

Request

<-- {"jsonrpc": “2.0", "result": 19, "id": 1}

Response

--> {"jsonrpc": “2.0", "method": “update", "params": [1,2,3,4,5]}

Notification (Request without “id”)

Page 39: Building GUI App with Electron and Lisp

How to structure GUI Application with Electron

Page 40: Building GUI App with Electron and Lisp

No best practices (yet) to build an Electron app.

Page 41: Building GUI App with Electron and Lisp

In our case

Page 42: Building GUI App with Electron and Lisp

Renderer Process

Store

View

Action Reducer

Flux Architecture

Page 43: Building GUI App with Electron and Lisp

functionfunctionfunctionfunctionfunctionfunctionfunctionfunctionfunctionfunctionfunction

Renderer Process

Store

View

ActionReducer

Flux Architecture

Common Lisp

JSON-RPC serverfunctionfunctionfunction

functionfunctionfunction

← JSON-RPC over WebSocket →

Page 44: Building GUI App with Electron and Lisp

Not only Browser ⇔ Common Lisp

Page 45: Building GUI App with Electron and Lisp
Page 46: Building GUI App with Electron and Lisp

Coin Counter IC Card Reader PrinterAPI Server

Page 47: Building GUI App with Electron and Lisp

Benefits of Flux + JRPC Architecture

• JavaScript part is just a UI renderer

• Business logics are all in Common Lisp

• Common Lisp functions are easy to test

• Other components are also easy to test

• Because they all talk in JSON-RPC

Page 48: Building GUI App with Electron and Lisp

How to develop the app

Page 49: Building GUI App with Electron and Lisp

Development

• You can use your machine (Mac, Linux, etc)

• Virtual devices are used while development

• Easy to make a mock of components

Page 50: Building GUI App with Electron and Lisp

Development

1. Run a Electron process (npm run dev)

2. Open REPL of the Common Lisp process from Emacs with Swank

3. Enjoy

Page 51: Building GUI App with Electron and Lisp

Packaging

1. Run Windows in Virtual Environment

2. npm run package

3. Wait

Page 52: Building GUI App with Electron and Lisp

Experiments

Page 53: Building GUI App with Electron and Lisp

Coin Counter IC Card Reader PrinterAPI Server

Page 54: Building GUI App with Electron and Lisp

Coin Counter IC Card Reader PrinterAPI Server

Hard to share data.

Page 55: Building GUI App with Electron and Lisp

“brain”, the central storage

• All data(state) are in Common Lisp part

• What service the user chose?

• How much money is in the user’s IC card?

• How many coins are in?

• etc.

• It’s just a hash-table (with thread-lock).

Page 56: Building GUI App with Electron and Lisp

“Propagation Class”Metaclass for synchronize data between components

Page 57: Building GUI App with Electron and Lisp

“Propagation Class”

(defclass user () ((balance :initarg :balance :accessor user-balance) (coins :initarg :coins :accessor user-coins)) (:metaclass propagation-class))

Page 58: Building GUI App with Electron and Lisp

“Propagation Class”

(defclass user () ((balance :initarg :balance :accessor user-balance) (coins :initarg :coins :accessor user-coins)) (:metaclass propagation-class))

(defmethod on-update ((user user) slot-name) (declare (ignore slot-name)) (notify “browser” “user/update-state” user))

Page 59: Building GUI App with Electron and Lisp

“Propagation Class”

(defvar *user* (make-instance ‘user …)))

;; Notify this modification ;; to the browser implicitly. (setf (user-balance *user*) 1730)

Bless the Meta Object Protocol.

Page 60: Building GUI App with Electron and Lisp

So, Why Common Lisp?

Page 61: Building GUI App with Electron and Lisp

Why Common Lisp?

• We ♥ Common Lisp :)

• Good for writing unknown/complicated applications.

• Interactive development with REPL.

Page 62: Building GUI App with Electron and Lisp

Thanks.