这对我来说是一个令人费解的问题。
此node.js服务器订阅并获取消息。
这是代码
1, count: 1, key: key1
2, count: 1, key: key1
3, count: 1, key: key1
expiring, count: 1
1, count: 2, key: key2
2, count: 2, key: key2
3, count: 2, key: key2
expiring, count: 2
我希望看到以下情况,事实上我大多数时间都会看到它们。
1, count: 1, key: key1
2, count: 1, key: key1
1, count: 2, key: key2
2, count: 2, key: key2
3, count: 2, key: key2
expiring, count: 2
expiring, count: 2 (notice count 2 is shared, I guess my-script is modified in memory and shared, and wonder how to prevent it)
我有时会看到以下序列,但我不明白它是如何发生的。
/ my-generator.js
define([
'module',
'text!my-script.js',
'script-fn',
], function(module, myScript, scriptFn) {
var MyGenerator = function* (count) {
var scriptString = myScript; // + some other script
var context = scriptFn.context;
var fn = scriptFn.fn;
fn(scriptString);
var main = context.main;
main.count = count;
yield _.bind(main.wait, main);
var result; //something that I get by running fn
return result;
}
});
// script-fn.js
define([
'vm'
], function(vm) {
var sandbox = {};
var context = vm.createContext(sandbox);
var script = vm.createScript('(function(scriptString) { eval(scriptString) })');
var fn = script.runInContext(context);
return {
context: context,
fn: fn
};
});
// my-script.js
main = {
count: 0
};
var _ = requirejs('underscore');
var Backbone = requirejs('backbone');
main.messenger = new Backbone.Model();
_.extend(main, {
wait: function (callback) {
var timerId = _.delay(
main.expire,
3000
);
return main.messenger.once('done', callback);
}
});
以下是其他难题......
<span[^>]*class=\"value\"[^>]*>([^<]*)<\/span>