TomFrost / Jexl

Javascript Expression Language: Powerful context-based expression parser and evaluator
MIT License
561 stars 92 forks source link

Synchronous Jexl #40

Closed kyyash closed 5 years ago

kyyash commented 6 years ago

Is there anyway I can use Jexl synchronously ?

TomFrost commented 6 years ago

At this point, no. In order for Jexl to support asynchronous operations at all, the entire library must be asynchronous.

I'm considering creating a synchronous mode in v2.0 as this is a frequent request. If that's enabled, Jexl will inject its own dummy Promise interface that runs synchronously, but you the user would have to give up any support for async operations in your expressions. Would you be interested in that?

danrot commented 5 years ago

I would be definitely interested in that! My use case is quite similar to the ones in your README (simply checking some values of a predefined object), and having async operations makes that unnecessary complicated (for this specific use case).

GaborTorma commented 5 years ago

Now 2.0 supported synchronous mode? How can I use it? Thanx!

Charuru commented 5 years ago

Yes please sync mode!

Charuru commented 5 years ago

This exists: https://www.npmjs.com/package/jexl-sync but I have no idea how to use it as var jexl = require('Jexl'); no longer works.

GaborTorma commented 5 years ago

This exists: https://www.npmjs.com/package/jexl-sync but I have no idea how to use it as var jexl = require('Jexl'); no longer works.

You can use: var jexl = require(4); You need the modify the js file in two place:

  1. remove the ) sing from first line 408. char
  2. replace the first ( sing from the first line to require=

your first line in jexl-sync-master\dist\jexl-sync.min.js the following: require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){

TomFrost commented 5 years ago

Jexl v2 has been released without support for a synchronous mode (yet). I'm still hesitant to create such a parallel code path when it's easier than ever to run asynchronous code, but if anyone has a strong use case for this, I'd really love to hear more about it. In what cases do you need Jexl's functionality that prevent you from using an async API?

danrot commented 5 years ago

@TomFrost It's not really that it prevents me from using it, but it makes stuff a lot more complex. I am using it already, but I could get rid of quite some code if a synchronous mode would be available.

Actually the code I am talking about is open sourced, so I can paste some examples. I've added a comment in this file. I could probably get rid of this entire function if jexl would have a synchronous mode, since I don't have to store the results of the expressions in a separate variable, but could just put the function call in the first place.

thettler commented 5 years ago

Hey @TomFrost, I am currently working on a project for Vue.js, which should also be open sourced soon. In this project we rely heavily on “Render-Functions” which have to be synchronous in Vue. So we can not use asynchronous functions in the “Render-Functions” and thus not Jexl in its current form. Because Jexl would fit perfectly for our needs, I created a PR which extends Jexl with a way to use it synchronously. It would help us a lot if this PR could be merged after you checked it and submitted your suggestions for improvement / modification :leichtes_lächeln: Exact explanations are in the PR.

TomFrost commented 5 years ago

After much deliberation on implementation (See #46 for most recent discussion, #48 for final solution), synchronous support for Jexl has been released! 🎉

After upgrading to version 2.1.0, you can call jexl.evalSync with the same arguments you'd normally pass to eval, but get a synchronous response. Enjoy, and please let me know if you run into any issues that the tests didn't catch.