简体   繁体   English

如何在Node.js中设置交叉同步? (相关通函依赖)

[英]How to set Cross Synchronous in Node.js? (related Circular Dependency)

I have 2 different js files that depend each other like this: 我有2个彼此依赖的js文件,如下所示:

// slider.js
'use strict';
import Loop from './loop.js';
export default class Slider {
    constructor(elem) {
        this.elem = elem;
        this.Loop = new Loop(this.elem);
        this.Loop.routine();
    }
    updateImage() {
        console.log(
            'Cross Synchronous Test: Updated images',
            'Called the function from loop.js'
        );
    }
}
let proSlider = new Slider('Number')

// loop.js
import Slider from './slider.js';
export default class Loop {
    constructor(elem) {
        this.elem = elem;
        this.Slider = new Slider(this.elem);
        this.Slider.updateImage();
    }
    routine() {
        console.log(
            'Cross Synchronous Test: references a group of actions',
            'Called the function from Slider.js'
        );
    }
}

My goal is to call the function updateImage() inside of loop.js and call the another function routine() inside of slider.js at the instance level, same time. 我的目标是调用函数updateImage()loop.js并调用另一个函数routine()slider.js在实例级别,同一时间。 So they can be separated as 2 different files, but still can access each other whenever I want it. 因此它们可以分为2个不同的文件,但仍可以在需要时互相访问。

The problem is this throws an error called maximum callback stack. 问题是这引发了一个错误,称为最大回调堆栈。

Uncaught RangeError: Maximum call stack size exceeded 未捕获的RangeError:超出最大调用堆栈大小

I've read some of articles about Circular Dependency #1 , #2 but the article #2 is mentioning based on typescript. 我已经阅读了一些有关循环依赖#1#2的文章,但文章#2是基于打字稿的。 I've changed the code w/o the typescript keywords and then the browser fires the same error. 我更改了代码,没有打字机关键字,然后浏览器触发了相同的错误。

// slider.js
constructor(elem) {
    this.elem = elem;
    this.getLoop();
}
getLoop() {
    return new Loop(this.elem).routine();
}

// loop.js
constructor(elem) {
    this.elem = elem;
    this.getSlider();
}
getSlider() {
    return new Slider(this.elem).updateImage();
}

Are there any ways to set cross-call functions in Node.js? 有什么方法可以在Node.js中设置交叉调用功能?

Your problem is not a circular dependency of modules but an non terminating indirect recursion. 您的问题不是模块的循环依赖关系,而是非终止的间接递归。

All unnecessary parts removed your code boils down to: 删除代码的所有不必要部分归结为:

class Slider {
    constructor() {
       this.Loop = new Loop();
    }
}

class Loop {
    constructor() {
        this.Slider = new Slider();
    }
}

As you can see, the contructors for Slider and Loop are recursively calling each other in an endless loop that never terminates and creates an unlimited number of alternating instances of Slider , Loop , Slider , Loop , Slider , Loop , ... 如您所见, SliderLoop的构造函数在一个永无休止的循环中相互递归调用,该循环永不终止,并创建无数个SliderLoopSliderLoopSliderLoop ,...的交替实例。

One resort out of this dilemma could be that the second constructed instance gets an pointer to the first one and terminate the recursion: 解决此难题的一种方法可能是,第二个构造的实例获得一个指向第一个实例的指针并终止递归:

class Slider {
    constructor(elem, loop) {
        this.elem = elem;
        this.Loop = loop || new Loop(this.elem, this);
        this.Loop.routine();
    }
    updateImage() {
        console.log(
            'Cross Synchronous Test: Updated images',
            'Called the function from loop.js'
        );
    }
}

class Loop {
    constructor(elem, slider) {
        this.elem = elem;
        this.Slider = slider || new Slider(this.elem, this);
        this.Slider.updateImage();
    }
    routine() {
        console.log(
            'Cross Synchronous Test: references a group of actions',
            'Called the function from Slider.js'
        );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM