Giter Club home page Giter Club logo

Comments (5)

github-actions avatar github-actions commented on July 19, 2024

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

from glee.

KhudaDad414 avatar KhudaDad414 commented on July 19, 2024

Based on my knowledge of Node.js I don't think it is possible. Node is single-threaded and it's not like we can run the user's code in another thread and kill it whenever we want. (Maybe someone with a deep knowledge of Node.js has a way of doing it 🤔 ) besides, I don't think it is our job to prevent users from writing infinite loops. 🤷

from glee.

oviecodes avatar oviecodes commented on July 19, 2024

@Souvikns @KhudaDad414 , I'm yet to understand how Glee lifecycle works but from the code snippet above, it seems that it'll be nice to have a connection status, that could be checked in order to terminate a loop once the connection ends.

from glee.

fmvilas avatar fmvilas commented on July 19, 2024

I don't think it is our job to prevent users from writing infinite loops.

Completely agree with this. We shouldn't prevent users from creating infinite loops because they can be helpful too. For instance, reporting the CPU and RAM memory load every few seconds.

it'll be nice to have a connection status, that could be checked in order to terminate a loop once the connection ends

If the connection ends, Glee will try to connect again so I don't think we can rely on this.


That said, it's possible to stop this infinite loop. You would have to do something like this:

// File: lifecycle/onConnect.js

import dummyjson from 'dummy-json'
import { Message } from '@asyncapi/glee'

let timeout;

export default async function ({ glee, connection }) {
  (function myLoop(i) {
    timeout = setTimeout(() => {
      glee.send(new Message({
        channel: '/travel/status',
        connection,
        payload: generateResponse()
      }))
      if (--i) myLoop(i)
    }, 1000)
  }(100))

  function generateResponse() {
    const template = `{
      "destination": "{{city}}",
      "arrival": "{{int 2 6}}h",
      "distance": "{{int 18 65}}km"
    }`
    return JSON.parse(dummyjson.parse(template))
  }
}

export function stopLoop() {
  if (timeout) clearTimeout(timeout)
}

export const lifecycleEvent = 'onServerConnectionOpen'
export const channels = ['/travel/status']
// File: lifecycle/onConnectionClose.js

import { stopLoop } from './onConnect.js'

export default async function () {
  stopLoop()
}

export const lifecycleEvent = 'onServerConnectionClose'
export const channels = ['/travel/status']

By doing the above, every time the onServerConnectionClose lifecycle event happens, it will stop the loop created by the onServerConnectionOpen lifecycle event. And every time the client connects to the server again, it will start the loop one more time.

This is not any kind of magic, this is pure Node.js stuff. Node.js modules are only imported once. If you try to import it again, Node will not import it but it will give you the cached version it has. That's why you can use the timeout variable at the module level and it will always be the same reference to the same variable. Not everything has to be solved at the framework (Glee) level :)

from glee.

github-actions avatar github-actions commented on July 19, 2024

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

from glee.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.