-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] GraphQL Electron Tinder

, 22 2017 . 15:38 +



, . 2016 . - Tinder. , - . :



- . - , - , . , , . Tinder++ Tinder . , .




, Tinder API, 2014 MITM. .


, , , Electron.


React, react + redux + redux-saga + immutable. , . , , redux : , shouldComponentUpdate, .


,

, , store Immutable.Map.mergeDeep


, redux redux-saga . , , .


Redux

, redux . . , . , .


, redux , react. .




, . , , , , . , .


1


Inferno MobX. ( , ). , MobX , .


?



localStorage. localForage. JSON.stringify JSON.parse ( ) . , , .


   IndexedDB  WebWorker


IndexedDB, Dexie.js. , , . IndexedDB WebWorker .


?


    IndexedDB  WebWorker + MobX


API Tinder Android-. JS , main Electron.


, :


  1. main WebWorker
  2. , IndexedDB, renderer
  3. MobX,

, stores IndexedDB, MobX . , .


Inferno



Inferno , . inferno-compat, React- . material-ui, react-vistualized.



, . - React- . , . Inferno, . . - .


2


   GraphQL


. React React, . , , main . MobX , MobX , - .


, - . , GraphQL. Apollo. , , :


  • , IPC,
  • Apollo redux
  • optimistic updates

, Apollo IPC, . :


import { ipcRenderer } from 'electron'
import { GRAPHQL } from 'shared/constants'
import uuid from 'uuid'
import { print } from 'graphql/language/printer'

export class ElectronInterface {
    ipc
    listeners = new Map()

    constructor(ipc = ipcRenderer) {
        this.ipc = ipc
        this.ipc.on(GRAPHQL, this.listener)
    }

    listener = (event, args) => {
        const { id, payload } = args
        if (!id) {
            throw new Error('Listener ID is not present!')
        }
        const resolve = this.listeners.get(id)
        if (!resolve) {
            throw new Error(`Listener with id ${id} does not exist!`)
        }
        resolve(payload)
        this.listeners.delete(id)
    }

    printRequest(request) {
        return {
            ...request,
            query: print(request.query)
        }
    }

    generateMessage(id, request) {
        return {
            id,
            payload: this.printRequest(request)
        }
    }

    setListener(request, resolve) {
        const id = uuid.v1()
        this.listeners.set(id, resolve)
        const message = this.generateMessage(id, request)
        this.ipc.send(GRAPHQL, message)
    }

    query = request => {
        return new Promise(this.setListener.bind(this, request))
    }
}

main . ServerAPI.


GraphQL :


// @flow
import { ServerAPI } from './ServerAPI'
import { graphql } from 'graphql'

export default function callGraphQLFactory(instance: ServerAPI) {
    return function callGraphQL(payload: any) {
        const { query, variables, operationName } = payload
        return graphql(
            instance.schema,
            query,
            null,
            instance,
            variables,
            operationName
        )
    }
}

:


// @flow
export default function generateMessage(id: string, res: any) {
    return {
        id,
        payload: res
    }
}

, :


// @flow
import { ServerAPI } from './ServerAPI'
import { GRAPHQL } from 'shared/constants'

type RequestArguments = {
    id: string,
    payload: any
}

export default function processRequestFactory(instance: ServerAPI) {
    return async function processRequest(event: Event, args: RequestArguments) {
        const { id, payload } = args
        const res = await instance.callGraphQL(payload)
        const message = instance.generateMessage(id, res)

        if (instance.app.window !== null) {
            instance.app.window.webContents.send(GRAPHQL, message)
        }
    }
}

, , :


import { ipcMain } from 'electron'

ipcMain.on(GRAPHQL, instance.processRequest)

NeDB, main IPC renderer .




react-router. , API . router5 + middleware, MobX. Electron - URL , . , , .


react-router@v4 Flexbox CSS Grid. . , react-router !



webpack electron-packager, electron-forge. , Electron. electron-packager electron-compile, JS/TS, (Less, Stylus, SCSS), .



  GraphQL


GraphQL ( ). . .


, - Electron. GraphQL-over-IPC npm , .



2.0


  • TypeScript main


-> GitHub
->



  • , X, :
    • ?
    • X ?

!

Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/331446/

:  

: [1] []
 

:
: 

: ( )

:

  URL