Skip to content

Allow object to be empty on client side #41

@bliepp

Description

@bliepp

Hey there,

I came across your project and I really love it!

I recently realized that the Velox constructor on the client side doesn't allow for the obj parameter to be empty. Your examples use global objects like

// load script /velox.js
var foo = {};
var v = velox("/sync", foo);
v.onupdate = function() {
  //foo.A === 42 and foo.B === 21
};

As it can be seen, there are no requirements to the passed object besides being of type object.

However, since the onupdate callback passes this.obj as a parameter anyway, the object containing the received data doesn't need to be global (when using SSE, at least). Right now I'm using

var v = velox("/sync", {});
v.onupdate = function(obj) {
  //obj.A === 42 and obj.B === 21
};

to avoid using global objects, but this still looks a bit ugly. It would be better if the empty object could be omitted and instead be implicitly created by the constructor like velox("/sync").

I would therefore propose a change in the constructor of the Velox class like

obj = obj || {};
if (typeof obj !== "object") {
  throw "Invalid object";
}
this.obj = obj;

to implicitly create an empty object when the parameter obj is omitted instead of throwing an error.

Kindly
bliepp

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions