Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meteor.bindEnvironment does not work outside of a Fiber #465

Closed
speigg opened this issue Nov 7, 2012 · 9 comments
Closed

Meteor.bindEnvironment does not work outside of a Fiber #465

speigg opened this issue Nov 7, 2012 · 9 comments

Comments

@speigg
Copy link

speigg commented Nov 7, 2012

Meteor.bindEnvironment fails to wrap code in a Fiber when there is no current Fiber.

This is the error:

app/packages/meteor/dynamics_nodejs.js:54
    var boundValues = _.clone(Fiber.current._meteor_dynamics || []);
                                           ^
TypeError: Cannot read property '_meteor_dynamics' of undefined
    at Object.Meteor.bindEnvironment (app/packages/meteor/dynamics_nodejs.js:54:44)

Seems like boundValues should also be set to an empty array if Fiber.current is null.

@glasser
Copy link
Contributor

glasser commented Nov 7, 2012

In what context are you trying to call bindEnvironment outside of a fiber?

The idea of bindEnvironment is so that, when passing callbacks to non-Meteor code, you can keep them running in the current context. On the server that includes the current fiber. So if you're finding yourself outside of a Fiber on the server, you're probably not calling bindEnvironment enough!

@speigg
Copy link
Author

speigg commented Nov 8, 2012

Here is the context:

    Meteor.default_server.stream_server.register(function(socket) {
        var intervalID = setInterval(function() {
            if (socket.meteor_session) {

                var connection = {
                    connectionID: socket.meteor_session.id,
                    ...
                };

                socket.id = socket.meteor_session.id;

                var insertConnection = Meteor.bindEnvironment(function() { 
                    Ethereal.connections.insert(connection); 
                }, function (e) {
                    Meteor._debug("Exception from connection register callback:", e);
                }); insertConnection();

                clearInterval(intervalID);
            }
        }, 1000);

        socket.on('close', function () {

            var removeConnection = Meteor.bindEnvironment(function() { 
                Ethereal.connections.remove({connectionID: socket.id});
            }, function (e) {
                Meteor._debug("Exception from connection close callback:", e);
            }); removeConnection()

        });
    });

So what you are saying then, is that I need to wrap the function that I am passing to Meteor.default_server.stream_server.register with Meteor.bindEnvironment as well?

@heavyk
Copy link

heavyk commented Nov 8, 2012

try using Meteor.setInterval ?

@glasser
Copy link
Contributor

glasser commented Nov 9, 2012

Right, Meteor.setInterval takes care of wrapping its callback in a fiber for you.

(BTW, stream_server is an undocumented internal detail of Meteor.default_server, and is likely to be renamed to something more like _streamServer in a future release. We definitely need to provide some sort of disconnect hook sometime soon...)

@glasser glasser closed this as completed Nov 9, 2012
@speigg
Copy link
Author

speigg commented Nov 9, 2012

Thanks, it seems like I had to wrap every layer of callbacks with Meteor.bindEnvironment, as well as using Meteor.setInterval. Unfortunately I ran into another problem (Issue #471), so I'm still not 100% sure if it is working correctly.

@speigg
Copy link
Author

speigg commented Nov 9, 2012

BTW, this is what I ended up doing.

Ethereal.connections = new Meteor.Collection("connections");

if (Meteor.isServer) {

    Ethereal.connections.remove({});    

    Meteor.default_server.stream_server.register( Meteor.bindEnvironment( function(socket) {
        var intervalID = Meteor.setInterval(function() {
            if (socket.meteor_session) {

                var connection = {
                    connectionID: socket.meteor_session.id,
                    connectionAddress: socket.address,
                    userID: socket.meteor_session.userId
                };

                socket.id = socket.meteor_session.id;

                Ethereal.connections.insert(connection); 

                Meteor.clearInterval(intervalID);
            }
        }, 1000);

        socket.on('close', Meteor.bindEnvironment(function () {
            Ethereal.connections.remove({connectionID: socket.id});
        }, function(e) {
            Meteor._debug("Exception from connection close callback:", e);
        }));


    }, function(e) {
        Meteor._debug("Exception from connection registration callback:", e);
    }));

}

@glasser
Copy link
Contributor

glasser commented Nov 9, 2012

Yes, that looks right to me (or as right as you can get before we provide a supported way of hooking into session/sockets like that).

@speigg
Copy link
Author

speigg commented Dec 11, 2012

Hah, glad to know this was useful for somebody :)

https://github.com/murilopolese/howmanypeoplearelooking/blob/master/server/main.js

@mizzao
Copy link
Contributor

mizzao commented Jun 23, 2013

Additionally, Meteor.bindEnvironment fails without warning if the code is already in a Fiber. Just thought it would be useful to throw an error message of some sort :)

StorytellerCZ pushed a commit that referenced this issue Sep 18, 2021
Update babel-register to version 6.24.0 🚀
StorytellerCZ pushed a commit that referenced this issue Oct 1, 2021
* Fixing `export const function` syntax error

`export const function` results in a syntax error. Fixing one example by just dropping it and changing to a default export for the template test helper.

* Code review comment-related updates

* last change from code review comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants