Meteor: Replace deanius:promise with okgrow:promise - meteor

my app was using deaunius:promise package for promises, now it is deprecated, and I have to translate all my promises to the syntax of okfrow:promise package, I was trying to understand how to create meteor Promises with that package and how to translate my current Promises to the new package but Im not sure how to do it in the proper way, it is quite different for me...
This is one example of a promise I used to write with deanius:promise
Meteor.promise('sendSubmission', null, submission)
.then( (result) ->
FlashMessages.sendSuccess "Successfully Finished the Test"
Router.go 'submissionView', _id: result.submissionId
).catch (error) ->
FlashMessages.sendError error.reason
Router.go 'takeTest', slug: currentTest.slug
How to write the above promise using okgrow:promise package? the examples they provide are not helpful at all for me. Thanks for your help

my app was using deaunius:promise package for promises, now it is deprecated
That wording seems to be unfortunate. Instead of being "deprecated", I'd have said "moved" - nothing really changed but the repository; it's still maintained by the same contributor. The code is just a fork, much of it is probably still the same.
I have to translate all my promises to the syntax of okfrow:promise package
No. The API has not changed a bit. All you would need to do is update the name of your dependency.

Related

Where did LoaderService go?

Upgrading AngleSharp from 0.9.6 to 0.9.9 I have this line of code no longer compiling:
return configuration.With(LoaderService(new[] { requester }));
It complains that LoaderService does not exist in the current context. So what happened to LoaderService? Is there a replacement? Does it still exist but just somewhere else?
Good question. Sorry for being late to the party, but even though you may have solved your problem someone else is having a hard time figuring it out.
LoaderService was essentially just a helper to create a loader. But having a service for anything creating a little thing would be overkill and not scale much. Also AngleSharp.Core would need to define all these. So, instead a generic mechanism was introduced, which allows registering such "creator services" via Func<IBrowsingContext, TService>.
However, to solve your piece of code I guess the following line would do the trick:
return configuration.WithDefaultLoader(requesters: requester);
This registers the default loader creator services (one for documents, one for resources inside documents) with the default options (options involve some middleware etc.).
Under the hood (besides some other things) the following is happening:
// just one example, config.Filter is created based on the passed in options
return configuration.With<IDocumentLoader>(ctx => new DocumentLoader(ctx, config.Filter));

Meteor.autorun vs Tracker.autorun?

What is the difference between Meteor.autorun and Tracker.autorun?
are they just aliases?
is one deprecated?
is there any instance where one is preferable to the other?
I'm well aware of the difference in using this.autorun in template lifecycle callbacks, but have seen these two used interchangeably and just want to be sure I haven't missed a trick.
Well, it can easily be found out with the identity operator.
This will be false because it is not the same function:
(function() {} === function() {})
Let's try with the two autorun :
(Meteor.autorun === Tracker.autorun)
This returns true. So yes it's only a pure alias.
However, only Tracker.autorun is documented. I suspect some kind of old API left for compatibility...
Let's check some Meteor code on GitHub!
File : deprecated.js
Meteor.autorun = Tracker.autorun;
This is in deprecated.js, it says some things about //Deprecated functions and some backward compatibility with Meteor 0.5.4. It seems pretty clear which one you should use.
You can find some other old timers in there, such as Deps...
Try to run Meteor.autorun(); in the console, it throws the following error Uncaught Error: Tracker.autorun requires a function argument like you were trying to run Tracker.autorun();

Uncaught TypeError: Blaze.Var is not a function

This one is driving me a little crazy.
I am playing with blaze and trying to create simple increment decrement with it.
here is my working code in codepen
http://codepen.io/distalx/pen/rVbQmm?editors=100
But when i am implementing it with meteor it gives me this error at following line
var counter = new Blaze.Var(0);
'Uncaught TypeError: Blaze.Var is not a function.'
here is meteorpad of it
http://meteorpad.com/pad/nhgMaMKNMQEainhcY/increment%20decrement
what am i doing wrong here.!!?
The Codepen you are referring to uses an old version of Blaze (as a standalone library) which seems to not be maintained or updated anymore (see discussions in the Issues list).
The version of Blaze used by Meteor is a package available here and it seems to differ a lot.
In fact, this version of Blaze does not have any .Var() method.
But you can have the same functionalities by using a ReactiveVar with the reactive-var package.

Meteor.userId vs Meteor.userId()

I have a short piece of code, like so, to update the name in my user's profile:
Meteor.users.update({_id: Meteor.userId()}, {$set:{"profile.name": name}});
When I'm working locally, I can use Meteor.userId or Meteor.userId() without issue. However, when I deploy to Modulus, I run into issues. If I don't have the operator on it, it will do the initial $set, but no more. If I user the operators, it behaves as I would expect.
Why is this? I assume that I shouldn't have been using this without the operator to begin with, but is there a reason why it worked at all?
Have a look in the documentation
The function Meteor.userId() is available "Anywhere but publish functions"
The variable this.userId is available "Anywhere" (which explicitly is also called out for the Server side Publish function).
I have had the same issue with Meteor.userId() when trying to get a unit testing with mocha to work.
A simple fix is to go to tasks.js and replace Meteor.userId() with this.userId which
is to use the this context of the function.

Confused about Meteor: how to send data to all clients without writing to the database?

I've actually been toying with Meteor for a little bit now, but I realized that I still lack some (or a lot!) comprehension on the topic.
For example, here is a tutorial that uses node.js/express/socket.io to make a simple real-time chat: http://net.tutsplus.com/tutorials/javascript-ajax/real-time-chat-with-nodejs-socket-io-and-expressjs/
In that above example, through socket.io, the webserver receives some data and passes it onto all of the connected clients -- all without any database accesses.
With Meteor, in all the examples that I've seen, clients are updated by writing to the mongodb, which then updates all the clients. But what if I don't need to write data to the database? It seems like an expensive step to pass data to all clients.
I am sure I am missing something here. What would be the Meteor way of updating all the clients (say, like with a simple chat app), but without needing the expense of writing to a database first?
Thank you!
At the moment there isn't an official way to send data to clients without writing it to a collection. Its a little tricker in meteor because the step to send data to multiple clients when there isn't a place to write to comes from when multiple meteor's are used together. I.e items sent from one meteor won't come to clients subscribed on the other.
There is a temporary solution using Meteor Streams (http://meteorhacks.com/introducing-meteor-streams.html) that can let you do what you want without writing to the database in the meanwhile.
There is also a pretty extensive discussion about this on meteor-talk (https://groups.google.com/forum/#!topic/meteor-talk/Ze9U9lEozzE) if you want to understand some of the technical details. This will actually become possible when the linker branch is merged into master, for a single server
Here's a bit of way to have a 'virtual collection, its not perfect but it can do until Meteor has a more polished way of having it done.
Meteor.publish("virtual_collection", function() {
this.added("virtual_coll", "some_id_of_doc", {key: "value"});
//When done
this.ready()
});
Then subscribe to this on the client:
var Virt_Collection = new Meteor.Collection("virtual_coll");
Meteor.subscribe("virtual_collection");
Then you could run this when the subscription is complete:
Virt_Collection.findOne();
=> { _id: "some_id_of_doc", key: "value"}
This is a bit messy but you could also hook into it to update or remove collections. At least this way though you won't be using any plugins or packages.
See : https://www.eventedmind.com/posts/meteor-how-to-publish-to-a-client-only-collection for more details and a video example.
The publish function on the server sends data to clients. It has some convenient shortcuts to publish query results from the database but you do not have to use these. The publish function has this.added(), this.removed(), and this.changed() that allow you to publish anything you choose. The client then subscribes and receives the published data.
For example:
if ( Meteor.isClient ){
var someMessages = new Meteor.Collection( "messages" ); //messages is name of collection on client side
Meteor.subscribe ( "messagesSub" ); //messagesSub tells the server which publish function to request data from
Deps.autorun( function(){
var message = someMessages.findOne({});
if ( message ) console.log( message.m ); // prints This is not from a database
});
}
if (Meteor.isServer ) {
Meteor.publish( "messagesSub", function(){
var self = this;
self.added ( "messages", "madeUpId1", { m: "This is not from a database"} ); //messages is the collection that will be published to
self.ready();
});
}
There is an example in meteor docs explained here and another example here. I also have an example that shares data between clients without ever using a database just to teach myself how the publish and subscribe works. Nothing used but basic meteor.
It's possible to use Meteor's livedata package (their DDP implementation) without the need of a database on the server. This was demoed by Avital Oliver and below I'll point out the pertinent part.
The magic happens here:
if (Meteor.isServer) {
TransientNotes = new Meteor.Collection("transientNotes", {connection: null});
Meteor.publish("transientNotes", function () {
return TransientNotes.find();
});
}
if (Meteor.isClient) {
TransientNotes = new Meteor.Collection("transientNotes");
Meteor.subscribe("transientNotes");
}
Setting connection: null specifies no connection (see Meteor docs).
Akshat suggested using streams. I'm unable to reply to his comment due to lack of reputation, so I will put this here. The package he links to is no longer actively maintained (see author's tweet). I recommend using the yuukan:streamy (look it up on Atmosphere) package instead or use the underlying SockJS lib used in Meteor itself—you can learn how to do this by going through the Meteor code and look how Meteor.server.Stream_server, and Meteor.connection._stream are used, which is what the Streamy package does.
I tested an implementation of the Streamy chat example and found performance to be negligibly different but this was only on rudimentary tests. Using the first approach you get the benefits of the minimongo implementation (e.g. finding) and Meteor reactivity. Reactivity is possible with Streamy, though is does through things like using ReactiveVar's.
There is a way! At least theoretically The protocol used by Meteor to sync between client and server is called DDP. The spec is here
And although there some examples here and here of people implementing their own DDP clients, I'm afraid haven't seen examples of implementations of DDP servers. But I think the protocol is straightforward and would guess it wouldn't be so hard to implement.

Resources