@__path/watcher 中文文档教程
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