代码之家  ›  专栏  ›  技术社区  ›  srgbnd

如果更新项目,则不会触发集合上的映射

gun
  •  1
  • srgbnd  · 技术社区  · 7 年前

    枪0.8.7,节点。js到节点。js,没有浏览器。

    节点已成功创建并添加到 tasks

    const Gun = require('gun');
    const _ = require('lodash');
    require( "gun/lib/path" );
    
    const gun = new Gun({peers:['http://localhost:8080/gun', 'http://localhost:8081/gun']});
    
    const watchers = [
      {
        id: '123',
        type: 'skeleton',
        stat: {
          num: 0
        }
      },
      {
        id: '456',
        type: 'snowmann',
        stat: {
          num: 0
        }
      },
      {
        id: '789',
        type: 'moose',
        stat: {
          num: 0
        },
      }
    ]; 
    
    const tasks = gun.get('tasks'); 
    
    _.forEach(watchers, function (watcher) {
      let task = gun.get(`watcher/${watcher.id}`).put(watcher);
      tasks.set(task);
    });
    

    有以下结果: .map

    a2task { _:
       { '#': 'watcher/123',
         '>': { id: 1506959623558, type: 1506959623558, stat: 1506959623558 } },
      id: '123',
      type: 'skeleton',
      stat: { '#': 'j8acunbf70NblJptwXWa' } }
    task { _:
       { '#': 'watcher/456',
         '>':
          { id: 1506959623579.002,
            type: 1506959623579.002,
            stat: 1506959623579.002 } },
      id: '456',
      type: 'snowmann',
      stat: { '#': 'j8acunbv03sor9v0NeHs7cITj' } }
    task { _:
       { '#': 'watcher/789',
         '>':
          { id: 1506959623581.002,
            type: 1506959623581.002,
            stat: 1506959623581.002 } },
      id: '789',
      type: 'moose',
      stat: { '#': 'j8acunbx03sorM0hWZdQz0IyL' } }
    

    在听者一侧

    const Gun = require('gun');
    
    const gun = new Gun({peers:['http://localhost:8080/gun']});
    const tasks = gun.get('tasks');
    tasks.map().val(function (task) {
      console.log('task', task);
    });
    

    但如果其中一个属性更新,则没有结果:

    _.forEach(watchers, function (watcher) {
      gun.get(`watcher/${watcher.id}`).put({
        stat: {
          num: 1
        }
      });
    

    下面是要使用的代码 https://github.com/sergibondarenko/shared-schedule

    1 回复  |  直到 7 年前
        1
  •  1
  •   J Decker    7 年前

    tasks.map().map().val(function (task_stat) {
      console.log('task stat', task_stat);
    });
    
    推荐文章