【问题标题】:Return a value from inside a Fiber从 Fiber 内部返回一个值
【发布时间】:2021-03-07 19:50:50
【问题描述】:

我正在尝试提供一个集合,该集合将其方法包装在纤维中以进行同步调用。

const Fiber = require('fibers');
const wrapInFiber = require('./wrapInFiber');

let db;
Fiber(function () {
  db = wrapInFiber({
    url: global.__MONGO_URI__,
    config: { useNewUrlParser: true, useUnifiedTopology: true },
    databaseName: global.__MONGO_DB_NAME__,
    collections: ['users'],
  });
  console.log('DB', db.users);
}).run();

module.exports.Users = db.users; <-- undefined

控制台日志输出值,但传递给module.exportsdb.users 未定义,因为它要么在光纤完成之前运行,要么在光纤之外运行,我猜?那么,如何正确地将值传递给module.exports

唯一远程接近的答案是this one,我试图模仿但失败了,可能是因为他的包装函数不像我的那样涉及与纤维相关的代码。

WrapInFiber 以防万一。改编自library


const Fiber = require('fibers');
const { MongoClient } = require('mongodb');

module.exports = function ({ url, config, databaseName, collections }) {
  const Collection = function (db, name) {
    this.collection = db.collection(name);
  };

  Collection.prototype.count = function (query, options) {
    query = query || {};
    options = options || {};
    const fiber = Fiber.current;
    this.collection.count(query, options, function (err, records) {
      fiber.run(records);
    });
    return Fiber.yield();
  };

  Collection.prototype.find = function (query, options) {
    query = query || {};
    options = options || {};
    const fiber = Fiber.current;
    this.collection.find(query, options).toArray(function (err, records) {
      fiber.run(records);
    });
    return Fiber.yield();
  };

  Collection.prototype.findOne = function (query, options) {
    return this.find(query, options)[0];
  };

  Collection.prototype.update = function (query, options) {
    const fiber = Fiber.current;
    this.collection.update(query, options, function (err, records) {
      fiber.run(records);
    });
    return Fiber.yield();
  };

  Collection.prototype.insert = function (document) {
    const fiber = Fiber.current;
    this.collection.insert(document, function (err, records) {
      fiber.run(records);
    });
    return Fiber.yield();
  };

  Collection.prototype.remove = function (document) {
    const fiber = Fiber.current;
    this.collection.remove(document, function (err, records) {
      fiber.run(records);
    });
    return Fiber.yield();
  };

  const fiber = Fiber.current;

  MongoClient.connect(url, config, function (err, connection) {
    const obj = {
      close() {
        connection.close();
      },
    };
    const db = connection.db(databaseName);
    collections.forEach(function (collectionName) {
      obj[collectionName] = new Collection(db, collectionName);
    });
    fiber.run(obj);
  });

  return Fiber.yield();
};

【问题讨论】:

  • 哇,这看起来真的很复杂。你为什么需要这个?流星中的数据库收集方法已经在光纤中运行并且是同步的。那么为什么不能直接使用Meteor.users.find() 呢?为什么需要您自己版本的 Fiber-wrapped DB 查询?
  • 基本上我正在尝试为某些 Meteor 包编写测试,但测试正在运行而不调用包而不是导入文件,因为我们正在尝试迁移出 Meteor 而我不想要添加任何 Meteor 相关代码,所以我需要存根集合以便能够调用它的方法,如查找、更新等。对于上下文:github.com/unchainedshop/unchained/pull/253/…
  • 我需要存根用户集合才能测试 Roles.getUserRoles,例如 github.com/unchainedshop/unchained/pull/253/…
  • @HarryAdel 是有原因的,为什么你不使用Future 类作为proposed in the fibers documentation
  • 你建议我如何使用它?我试过这样的东西,但它不工作。 ``` const Fiber = require('fibers'); const Future = require('fibers/future'); const wrapInFiber = require('./wrapInFiber'); const fn = Future.wrap(wrapInFiber);常量未来 = 新的未来(); Fiber(function () { const db = fn({ url: global.__MONGO_URI__, config: { useNewUrlParser: true, useUnifiedTopology: true }, databaseName: global.__MONGO_DB_NAME__, collections: ['users'], }).wait() ; return future.return(db.users); }).run(); module.exports.Users = 未来; ```

标签: javascript node.js meteor fibers node-fibers


【解决方案1】:

虽然我没有解决光纤的问题,但我设法解决了导致这条 Meteor 集合的存根路径的最初问题。只需要一个简单的Sinon stub

const sinon = require('sinon');
require('sinon-mongo');

const Users = sinon.mongo.collection({
  findOne: sinon
    .stub()
    .withArgs({ _id: 'user' }, { fields: { roles: 1 } })
    .returns({ _id: 'user', roles: [] }),
});

module.exports.Users = Users;

【讨论】:

    猜你喜欢
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 2023-03-10
    • 2018-08-09
    • 1970-01-01
    • 2018-09-18
    相关资源
    最近更新 更多