Categories
discuss

Questions about the order of execution of this code snippet

So I been reading a tutorial about Javascript promises these days.

here is an example from it used to explain the macrotask queue(i.e. the event loop) and the microtask queue.

let promise = Promise.reject(new Error("Promise Failed!"));
promise.catch(err => alert('caught'));

// no error, all quiet
window.addEventListener('unhandledrejection', event => alert(event.reason));

It says that because promise.catch catches the error so the last line, the event handler never gets to run. I can understand this. But then he tweaked this example a little bit.

let promise = Promise.reject(new Error("Promise Failed!"));
setTimeout(() => promise.catch(err => alert('caught')));

// Error: Promise Failed!
window.addEventListener('unhandledrejection', event => alert(event.reason));

This time he says the event handler is gonna run first and catch the error and after this the promise.catch catches the error eventually.

What I do not understand about the second example is, why did the event handler run before the promise.catch?

My understanding is,

  1. Line one, we first encounter a promise, and we put it on the microtask queue.
  2. Line two, we have a setTimeout, we put it on the macrotask queue,
  3. Line three, we have an event handler, and we put the handler on the macrotask queue waiting to be fired

Then, because microtask has higher priority than macrotask. We run the promise first. After it, we dequeue the first task on macrotask queue, which is the setTimeout. So from my understanding, the error should be caught by the function inside setTimeout.

Please correct me.

Answer

You are wrong about step 3). The handler will be added synchronously. Then the microtask queue gets run, and the promise rejects. As no .catch handler was added yet, an unhandled rejection gets thrown.

And I think you are mixing between when a callback gets added and when a callback gets executed. Consider this case:

  (new Promise).then(function callback() { });

The callback will be added synchronously, but it will never be called as the promise never resolves. In this case:

  Promise.resolve().then(function callback() { });

the callback again gets added synchronously, but the promise resolution happens in a microtask, so the callback will be executed a tick later.

Categories
discuss

Set background color for Alexa APL smart display devices

How to set a background color for an Amazon Alexa APL skill? This is how my .json file looks like: https://raw.githubusercontent.com/alexa-labs/skill-sample-nodejs-sauce-boss/master/lambda/custom/documents/recipeIntent.json

Answer

I can see you have some background images that used best-fill and also a defined size 100vwx100vh so I was wondering where would you like to add a background color.

The solution I found for a similar case was adding a Frame like this:

{
  "type": "Frame",
  "width": "100vw",
  "height": "100vh",  
  "backgroundColor": "blue"
}

I hope this helps somehow.

Categories
discuss

Continuous text in text-area cuts the PDF overflowing the text when using Itext 7.1.7

I see there are lot many questions related to the same scenario this one is little different couldn’t figure out the solution. I have a in a tabl in a cell. When I give the continous text like “cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc” Its overflowing the text when I download the print file and its normal when I give normal text with breaks.Here is my code

.generaltable {
	background-color : #5C7FBF;
	border:thin;
	width : 100%;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	}
  
  
	.column {
	background-color : #DEEDFF;
	font-weight : bold;
	padding-bottom : 1px;
	padding-left : 1px;
	padding-right : 1px;
	padding-top : 1px;
	text-align : center;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	border: none;
	}
  
 .edit {
	background-color : #DEEDFF;
	border-width: 1px;
	border-style:solid;
	border-color:#DEEDFF;
	border: 1px solid #DEEDFF;
	color: black;
	text-align : left;
	font-weight : bold;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	word-break: break-all;
	}
  
  .iedit2 {
	background-color : white;
	text-align: left;
	color: black;
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
	border-top: 1px solid #999999;
	border-right: 1px solid #333333;
	border-bottom: 1px solid #333333;
	border-left: 1px solid #999999;
	word-break: break-all;
	}
  
  
  
  
  
<table border="1" width="100%" align="center" cellpadding="2" cellspacing="1"  class="generaltable">  <tbody><tr id="Row35494#0">
    <th id="04" class="column" width="39%"><a href="javascript:alert('Self Explanatory')">Brief                 description of the issue *</a></th>
        <td id="1 04" width="39%" class="edit">
            <textarea id="269494_0" class="iedit2" cols="35" rows="5" wrap="virtual" maxlength="4000"                   name="fiy">ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
             </textarea>
        </td>
      </tr>
  </tbody>
 </table>

please find the pictures it for clear overview This is the entry

I have a click like Go once I do it it downloads the entire document here using JAVA code and the code part is working Good but the issue is with itext 7.1.7. this is happening and I have changed it to flying saucer its working good but causing other issues. I want to stay with Itext 7.1.7 and solve this continuous fix.

This is the pdf

Answer

pdfHTML allows you to either convert the form-related elements (inputs, text areas) directly into the plain PDF content, or create a PDF with AcroForm (so that those elements are editable, as they are supposed to be in HTML).

To enable that behavior, you should use setCreateAcroForm(true) in ConverterProperties that you pass to HtmlConverter.

If you don’t want to have those fields editable, you can flatten those fields as a second step after you have converted the HTML into PDF.

Having that said, the behavior you describe looks like a bug in iText. But the mode of creating the AcroForm and flattening is implemented in a slightly different way and it looks like the textarea would be converted as expected in your case. You haven’t attached the whole example so it’s hard to verify for sure, but for the small snippet you attached everything is fine. Here is the code you can use:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
HtmlConverter.convertToPdf(new FileInputStream("C:\file.html"), baos,
        new ConverterProperties().setCreateAcroForm(true));

PdfDocument document = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())), 
        new PdfWriter(new File("C:\out.pdf")));
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(document, false);
acroForm.flattenFields();
document.close();
Categories
discuss

How can I manually document Swagger datamodel for a JAX-RS parameter?

Context

Let’s say that I have a simple data class in Java:

public class Person {
    private final String name;
    private final int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    int String getAge() {
        return age;
    }
}

Note: in practice, I use Immutables to generate this, but am showing a POJO here for the sake of simplicity.

To document the model of the GET response, even if the return type is Response, I can refer to the class in @ApiOperation:

@GET
@ApiOperation(response = Person.class)
public Response getPerson() {
    return Response.ok(new Person("Julien", 28)).build();
}

Based on that, Swagger will document this correctly:

Model:

Person {
  name (string),
  age (number)
}

Example value:

{
  "name": "string",
  "age": 0
}

To document the model of the POST body, I use the class directly in the code, Swagger finds it and documents it as desired:

@POST
@ApiOperation(response = Person.class)
public Response addPerson(Person newPerson) {
    return Response.ok(store.insert(newPerson)).build();
}

Problem

I want to support partial updates as well. I cannot use the POJO itself, because all fields are mandatory in the POJO, I rely on that to get safe checks and clear error messages when invalid JSON is sent to e.g. the POST method.

In my actual use-case, my datamodel contains maps. I want users to be able to specify a certain key in the update and set the value to null, to delete elements from an existing maps.

I chose to support PATCH requests where the body is typed as a plain JsonNode. That allows any JSON to be received by my server and I can apply the updates as I wish.

@PATCH
@Path("/{name}")
@ApiOperation(response = Person.class)
public Response updatePerson(@PathParam("name") String name, JsonNode update) {
    return Response.ok(store.update(name, update)).build();
}

I’m happy with the result, except that Swagger now documents the partial update’s model with the properties of the JsonNode Java object:

Model:

JsonNode {
  array (boolean, optional),
  null (boolean, optional),
  number (boolean, optional),
  float (boolean, optional),
  pojo (boolean, optional),
  valueNode (boolean, optional),
  containerNode (boolean, optional),
  object (boolean, optional),
  missingNode (boolean, optional),
  nodeType (string, optional) = ['ARRAY', 'BINARY', 'BOOLEAN', 'MISSING', 'NULL', 'NUMBER', 'OBJECT', 'POJO', 'STRING'],
  integralNumber (boolean, optional),
  floatingPointNumber (boolean, optional),
  short (boolean, optional),
  int (boolean, optional),
  long (boolean, optional),
  double (boolean, optional),
  bigDecimal (boolean, optional),
  bigInteger (boolean, optional),
  textual (boolean, optional),
  boolean (boolean, optional),
  binary (boolean, optional)
}

Example value:

{
  "array": true,
  "null": true,
  "number": true,
  "float": true,
  "pojo": true,
  "valueNode": true,
  "containerNode": true,
  "object": true,
  "missingNode": true,
  "nodeType": "ARRAY",
  "integralNumber": true,
  "floatingPointNumber": true,
  "short": true,
  "int": true,
  "long": true,
  "double": true,
  "bigDecimal": true,
  "bigInteger": true,
  "textual": true,
  "boolean": true,
  "binary": true
}

I would like to specify in my code that the model is like Person, so that the example given in the Swagger UI is more relevant. I did try @ApiImplicitParams:

@PATCH
@Path("/{name}")
@ApiOperation(response = Person.class)
@ApiImplicitParams({
  @ApiImplicitParam(name = "update", dataTypeClass = Person.class)
})
public Response updatePerson(@PathParam("name") String name, JsonNode update) {
    return Response.ok(store.update(name, update)).build();
}

That did not make any difference. Swagger still documents JsonNode itself. The documentation for @ApiImplicitParams mentions:

While ApiParam is bound to a JAX-RS parameter, method or field, this allows you to manually define a parameter in a fine-tuned manner. This is the only way to define parameters when using Servlets or other non-JAX-RS environments.

Since I am using JAX-RS, this might mean that I cannot use @ApiImplicitParams, but @ApiParam does not provide anything to override the class.

How can I specify manually the datamodel of a JAX-RS parameter that is detected by Swagger automatically?

Answer

Adding this answer to make it somewhat generic for better understanding of @ApiImplicitParams.

You would have to use @ApiImplicitParams for wrapping the parameters as you wish to save in the documentation. @ApiImplicitParam has many not-so-obvious benefits, like to pass in extra header param without adding it as a method param or in your case wrap the the params so as to make them some sense.

For your problem, you would have to use @ApiImplicitParam along with paramType = "body" as you want to make changes in the body, similarly paramType = "head" if you wanted changes in header.

You can also control the mandatory fields in @ApiImplicitParam with property required = true/false.

As stated earlier, you can pass a param without having it in the method param, you can control its value using the property value = "required value".

You can also control the allowable values in @ApiImplicitParam using comma seperated values. Eg.allowableValues = "no-cache, no-store".

Categories
discuss

Flutter pubspec.yaml Android versionCode

My app before using Flutter had

android:versionCode="17"
android:versionName="17"

In my Flutter application in pubspec.yaml, I have to set a version as x.y.z and I am trying set the new version to 19.0.0. But when I try to install the application on my device that has a version with android:versionCode="17" installed, I get an error message saying that the version on device is bigger than version I am trying to install.

How do I fix this?

Answer

The flutter.versionCode local property gets populated using the part after the + of the pubspec.yaml version.

This means that for Android, the pubspec.yaml version is parsed as follows:

version: {versionName}+{versionCode}

The example from your questions would consequently look like this in order to work (in pubspec.yaml:

name: ...
version: 19.0.0+19

...

As a side note, it is idiomatic for Dart to follow Semantic Versioning, although the + suffix does not work as it should when trying to build for Android with Flutter as I explained.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..