Javascript Watch.js是否仅在某些情况下不抽丝?

Javascript Watch.js是否仅在某些情况下不抽丝?,javascript,node.js,Javascript,Node.js,我正在Node.js中开发一个基于快速ws的应用程序,并且在app.ws路由中使用Watch.js watcher。打开WebSocket时,使用app.ws语句中定义的回调函数对全局对象执行watch。然后在ws.on('close',…函数中,我使用相同的对象和回调函数调用unwatch。(请参阅代码段A) 这适用于带有简单布尔项的简单JS对象(请参见代码段a中的stat\u a)。但是,我很快就会有多个对象具有不同的状态,我想将它们全部放在一个新的JS对象中。因此,这是一个包含一组对象的对

我正在Node.js中开发一个基于快速ws的应用程序,并且在
app.ws
路由中使用Watch.js watcher。打开WebSocket时,使用
app.ws
语句中定义的回调函数对全局对象执行
watch
。然后在
ws.on('close',…
函数中,我使用相同的对象和回调函数调用
unwatch
。(请参阅代码段A)

这适用于带有简单布尔项的简单JS对象(请参见代码段a中的
stat\u a
)。但是,我很快就会有多个对象具有不同的状态,我想将它们全部放在一个新的JS对象中。因此,这是一个包含一组对象的对象

代码段B是一个应该与代码段A等效的示例。但是,似乎在关闭WebSocket后,
unwatch
不知何故无法运行,并且当更新
stat['A']
时,程序试图通过关闭的WebSocket发送它,并崩溃

我被难住了。是什么原因导致
取消挂接
失败

编辑:我通过将
watch(stat,'A',send_stat)
更改为
watch(stat['A',send_stat)
,并以相同的方式更改
unwatch
,使代码段B工作。请参见代码段C。现在,我无法取消对主
stat
列表的连接,请参见代码段D

代码片段A(stat\u A在别处更新):

const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat_A = {'1': true, '2': false, '3' : false};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat_A));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat_A, send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat_A, send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat['A']));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat, 'A', send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat, 'A', send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat['A']));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat['A'], send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat['A'], send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat, send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat, send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
代码片段B(stat['A']在别处更新):

const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat_A = {'1': true, '2': false, '3' : false};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat_A));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat_A, send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat_A, send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat['A']));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat, 'A', send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat, 'A', send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat['A']));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat['A'], send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat['A'], send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat, send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat, send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
代码段C(B已修复):

const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat_A = {'1': true, '2': false, '3' : false};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat_A));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat_A, send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat_A, send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat['A']));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat, 'A', send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat, 'A', send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat['A']));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat['A'], send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat['A'], send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat, send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat, send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
代码片段D(与B相同,C中的修复不适用):

const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat_A = {'1': true, '2': false, '3' : false};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat_A));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat_A, send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat_A, send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat['A']));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat, 'A', send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat, 'A', send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat_A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat['A']));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat['A'], send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat['A'], send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat));
        }

        send_stat();
        // Setup watchers
        watchjs.watch(stat, send_stat);

        ws.on(
            'close',
            () => {
                // Stop watchers
                watchjs.unwatch(stat, send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);

最后的答案如下

const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat));
        }

        send_stat();
        // Setup watchers
        Object.keys(stat).forEach((key) => {
            watchjs.watch(stat[key], send_stat);
        })

        ws.on(
            'close',
            () => {
                // Stop watchers
                Object.keys(stat).forEach((key) => {
                    watchjs.unwatch(stat[key], send_stat);
                })
            }
        );
    }
);

app.ws(
    '/stat/A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat['A']));
        }

        send_stat();
        // Setup watcher
        watchjs.watch(stat['A'], send_stat);

        ws.on(
            'close',
            () => {
                // Stop watcher
                watchjs.unwatch(stat['A'], send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);

最后的答案如下

const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const watchjs = require('watchjs');

const port = 1338;

var stat = {
    'A': {'1': true, '2': false, '3' : false}
};

app.ws(
    '/stat',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat));
        }

        send_stat();
        // Setup watchers
        Object.keys(stat).forEach((key) => {
            watchjs.watch(stat[key], send_stat);
        })

        ws.on(
            'close',
            () => {
                // Stop watchers
                Object.keys(stat).forEach((key) => {
                    watchjs.unwatch(stat[key], send_stat);
                })
            }
        );
    }
);

app.ws(
    '/stat/A',
    (ws, req) => {
        function send_stat() {
            ws.send(JSON.stringify(stat['A']));
        }

        send_stat();
        // Setup watcher
        watchjs.watch(stat['A'], send_stat);

        ws.on(
            'close',
            () => {
                // Stop watcher
                watchjs.unwatch(stat['A'], send_stat);
            }
        );
    }
);

app.listen(
    port,
    () => {
        console.log(`Listening (HTTP) on port ${port}!`);
    }
);