@__path/watcher 中文文档教程

发布于 5年前 浏览 24 项目主页 更新于 3年前

PathWatcher

Path Watcher 是一个用于与 Path 的 Live Controller 交互的 javascript 库,Path Watcher 基于 WebSocket 和 Server-sent-event(SSE)。

Installation

使用 npm

$ npm i --save @__path/watcher

使用 Yarn(推荐)

$ yarn add @__path/watcher

Usage

./path/project.pconf.json 中配置 Watcher,将方法设置为您希望 Path watcher 工作的方式(WS 或 SSE)

  "WATCHER": {
    "method": "WS",//change this to SSE/WS
    "WEBSOCKET": {
      "host": "yourLocalHost.test",
      "port": 443,
      "scheme": "SSL"
    }
  }

导入

import Watcher from "@__path/watcher";

实例化,设置 Live Controller名称并继续收听更改

let watcher =  new Watcher('TestChanges')
//here we are referencing the class we created earlier
                   .watch('prop')//we told path-watcher which of the properties to watch, this case we are watching $prop property
                   .setParams({
                       key1:'a value',
                       anotherKey:'another value'
                   })//we are adding additional infos that can be retrieved from the server side with `getParam()` method of WatcherInterface instance.
watcher.onReady(watcher => {
    // here, we can assign functions to be executed when a particular property changes
    watcher.listenTo("prop", response => {
// we assign an anonymous function to execute when $prop changes on server side
//the `response` parameter will be an object where key "data" will be our property's value

        let is_logged_in = response.data;
// extract the data
        if(is_logged_in !== 'yes'){
            location.href = '/logout'
        }
    })
})
watcher.start();//tell path to begin watching

PathWatcher

Path Watcher is a javascript library for interacting with Path's Live Controller, Path Watcher is based on WebSocket and Server-sent-event(SSE).

Installation

Using npm

$ npm i --save @__path/watcher

Using Yarn(recommended)

$ yarn add @__path/watcher

Usage

Configure Watcher in ./path/project.pconf.json, Set method to how you want Path watcher to work (WS or SSE)

  "WATCHER": {
    "method": "WS",//change this to SSE/WS
    "WEBSOCKET": {
      "host": "yourLocalHost.test",
      "port": 443,
      "scheme": "SSL"
    }
  }

Import for Usage

import Watcher from "@__path/watcher";

Instantiate, Set Live Controller name and proceed to listening to changes

let watcher =  new Watcher('TestChanges')
//here we are referencing the class we created earlier
                   .watch('prop')//we told path-watcher which of the properties to watch, this case we are watching $prop property
                   .setParams({
                       key1:'a value',
                       anotherKey:'another value'
                   })//we are adding additional infos that can be retrieved from the server side with `getParam()` method of WatcherInterface instance.
watcher.onReady(watcher => {
    // here, we can assign functions to be executed when a particular property changes
    watcher.listenTo("prop", response => {
// we assign an anonymous function to execute when $prop changes on server side
//the `response` parameter will be an object where key "data" will be our property's value

        let is_logged_in = response.data;
// extract the data
        if(is_logged_in !== 'yes'){
            location.href = '/logout'
        }
    })
})
watcher.start();//tell path to begin watching
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文