GithubHelp home page GithubHelp logo

node-zephyros-callback's Introduction

Node Zephyros Callback

Like node-zephyros, but simplified, and prefer callbacks then promise.

With Zephros, you are able to get window_ids, screen_ids, app_ids and talk ot listen to them. Mainly it is used to add key bindings for window resizing and moving.

Thie module is a thin wrapper on Zephros's protocol. It has language bindings in several languages, even one called [node-zephyros] wrapped with promise. You may find [more about the bindings in the repo][bindngs].

Usage

We got lots of callbacks:

npm install --save node-zephyros-callback
z = require './bind.coffee'
{task} = require 'proto-task-wait-done'

z.connect port: 1235, ->

  z.all_windows (windows) ->
    # return
    windows.map (window_id) ->
      z.send window_id, 'title', (title) ->
        z.send window_id, 'app', (app_id) ->
          z.send app_id, 'title', (app_title) ->
            console.log title, app_title

  z.send null, 'bind', 'e', ['cmd', 'alt'], ->
    console.log 'cmd alt e'

  z.bind 'l', ['cmd', 'alt'], ->
    z.focused_window (window_id) ->
      z.main_screen (screen_id) ->
        z.send screen_id, 'frame_including_dock_and_menu', (frame) ->
          half_w = Math.round (frame.w / 2)
          half_h = Math.round (frame.h / 2)
          z.send window_id, 'set_frame',
            x: 0
            y: 0
            w: half_w
            h: frame.h
            ->

  z.bind 'r', ['cmd', 'alt'], ->
    z.focused_window (window_id) ->
      # console.log 'window_id', window_id
      z.main_screen (screen_id) ->
        z.send screen_id, 'frame_including_dock_and_menu', (frame) ->
          half_w = Math.round (frame.w / 2)
          half_h = Math.round (frame.h / 2)
          z.send window_id, 'set_frame',
            x: half_w
            y: 0
            w: half_w
            h: frame.h
            ->

Guide

Before using this, you should read its protocol and have some basic ideas about using that.

node-zephyros-callback connects to Zephros with a TCP connection, which means you need to set Zephros to listen to a TCP port in its preference. The default port is 1235, code it like this:

z = require 'node-zephyros-callback'
z.connect port: 1235, ->
  # `z` is the entry
  # do stuffs after connection is established

Every message was consists of 4 parts like: [msg_id, receiver_id, method, *args], node-zephyros-callback will take case of msg_id and make it into a callback, so you don't have to write msg_id, but receiver_ids are required at times.

Basicly, z.send is for sending messages and attaching each with a callback, followed with a window_id -- the receiver_id, a 'title' -- the method, some or zero *args, and an optional callback, like this demo:

z.all_windows (windows) ->
  # return
  windows.map (window_id) ->
    z.send window_id, 'title', (title) ->
      z.send window_id, 'app', (app_id) ->
        z.send app_id, 'title', (app_title) ->
          console.log title, app_title

Top level APIs use null as receiver_id, so they are attached to z directly. That why there's code like this:

z.all_windows (windows) ->
  # `windows` is an array of `id`s, or exactly an array of numbers by now

Events are attached to z too, you may write like this:

z.window_moved (window_id) ->
  console.log window_id

More

Please read test.coffee and my.coffee for what I have now. This module is not well tested, you may still find some problems. If you find bugs, please report at issue or folk it, the code is short.

License

BSD

node-zephyros-callback's People

Contributors

tiye avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.