本文介绍了如何处理循环依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出:
-
StringPreconditions
扩展ObjectPreconditions
-
ObjectPreconditions
取决于StringPreconditions
(其中之一其方法返回子类) -
前提条件
是ObjectPreconditions
的看门人,并且StringPreconditions
(确保它们在返回实例之前均已加载) -
User
取决于前提条件
StringPreconditions
extendsObjectPreconditions
ObjectPreconditions
depends onStringPreconditions
(one of its methods returns the subclass)Preconditions
is a gatekeeper toObjectPreconditions
andStringPreconditions
(making sure they both load before returning an instance)User
depends onPreconditions
我有此代码:
define(["ObjectPreconditions"], function(ObjectPreconditions)
{
console.log("Inside StringPreconditions");
function StringPreconditions() {}
StringPreconditions.prototype = Object.create(ObjectPreconditions.prototype);
StringPreconditions.prototype.constructor = ObjectPreconditions;
return StringPreconditions;
});
define(["require"], function(require)
{
console.log("Inside ObjectPreconditions");
// Resolve circular dependencies
var StringPreconditions;
require(["StringPreconditions"], function(theStringPreconditions)
{
StringPreconditions = theStringPreconditions;
console.log("ObjectPreconditions finished loading StringPreconditions");
});
function ObjectPreconditions() {}
ObjectPreconditions.prototype.isInstanceOf(type)
{
console.log("ObjectPreconditions.isInstanceOf() invoked");
if (type === String)
return new StringPreconditions();
}
});
define(["ObjectPreconditions", "StringPreconditions"], function(ObjectPreconditions, StringPreconditions)
{
console.log("Inside Preconditions");
var Preconditions = {};
Preconditions.requireThat(parameter) = function()
{
return new ObjectPreconditions(parameter);
};
return Preconditions;
});
define(["Preconditions"], function(Preconditions)
{
console.log("Inside User");
function User() {}
User.prototype.doSomething = function()
{
var StringPrecondition = Preconditions.requireThat("test").isInstanceOf(String);
}
});
问题在于,我有10%的时间获得此装载订单:
The problem is that 10% of the time I get this load order:
- 内部用户
- 内部先决条件
- 内部对象先决条件
- 内部StringPreconditions
- ObjectPreconditions.isInstanceOf()( CRASH ,因为未定义StringPreconditions)
- ObjectPreconditions完成加载StringPreconditions
- Inside User
- Inside Preconditions
- Inside ObjectPreconditions
- Inside StringPreconditions
- ObjectPreconditions.isInstanceOf() (CRASH because StringPreconditions is undefined)
- ObjectPreconditions finished loading StringPreconditions
我已经阅读,但我相信他们正在做与我相同的事情。
I've already read http://requirejs.org/docs/api.html#circular but I believe they are doing the same thing I am.
有什么想法吗?
推荐答案
更新:包含ES6模块的更新答案。
UPDATE: https://stackoverflow.com/a/42264822/14731 contains an updated answer for ES6 modules.
我知道了:我们需要创建一个 gatekeeper文件,该文件将定义d
I figured it out: We need to create a "gatekeeper" file that will define functions that depend on the circular dependencies.
- 将
ObjectPreconditions.js
重命名为AbstractObjectPreconditions.js
。 - 创建一个新的
ObjectPreconditions.js
文件(我们的新网闸)。 - 将所有循环依赖项从
AbstractObjectPreconditions.js
移到ObjectPreconditions.js
- 用户代码应
require(ObjectPreconditions)
。涉及循环依赖关系的代码(例如子类)应require(AbstractObjectPreconditions)
。
- Rename
ObjectPreconditions.js
toAbstractObjectPreconditions.js
. - Create a new
ObjectPreconditions.js
file (our new gatekeeper). - Move any circular dependencies out of
AbstractObjectPreconditions.js
intoObjectPreconditions.js
- User code should
require(ObjectPreconditions)
. Code involved in the circular dependency (e.g. subclasses) shouldrequire(AbstractObjectPreconditions)
.
结果代码如下:
Here is what the resulting code looks like:
define(["AbstractObjectPreconditions"], function(ObjectPreconditions)
{
console.log("Inside StringPreconditions");
function StringPreconditions() {}
StringPreconditions.prototype = Object.create(ObjectPreconditions.prototype);
StringPreconditions.prototype.constructor = ObjectPreconditions;
return StringPreconditions;
});
define(["require"], function(require)
{
console.log("Inside AbstractObjectPreconditions");
function ObjectPreconditions() {}
return ObjectPreconditions;
});
define(["AbstractObjectPreconditions"], function(ObjectPreconditions)
{
// Gatekeeper for circular dependencies
ObjectPreconditions.prototype.isInstanceOf(type)
{
console.log("ObjectPreconditions.isInstanceOf() invoked");
if (type === String)
return new StringPreconditions();
}
return ObjectPreconditions;
});
define(["ObjectPreconditions", "StringPreconditions"], function(ObjectPreconditions, StringPreconditions)
{
console.log("Inside Preconditions");
var Preconditions = {};
Preconditions.requireThat(parameter) = function()
{
return new ObjectPreconditions(parameter);
};
return Preconditions;
});
define(["Preconditions"], function(Preconditions)
{
console.log("Inside User code");
function User() {}
User.prototype.doSomething = function()
{
var StringPrecondition = Preconditions.requireThat("test").isInstanceOf(String);
}
});
这篇关于如何处理循环依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!