Using Extract, Generic T extends to control designated properties of Unioned classes (typescript)

ref – https://stackoverflow.com/questions/56067863/discriminating-a-union-of-classes

We declare abstract class Fruit, which says any class that extends this MUST declare readonly property fruitType of type string.

So we create Banana, which extends Fruity. We must declare property readonly fruitType and initialize it with string banana
We also give it unique properties length and color.

We do this same for Pear and Apple, and give them their own unique characteristics. But they all must have property readonly fruitType of type string.

Then, we create a Union of class types called KnownFruits. KnownFruits is a Union of class Banana, Pear, and Apple. Basically we’re grouping them together. KnownFruits is the group fruits I want to select from later.

So we know that KnownFruits type restricts the type that it can reference into the group that consists of Banana, Pear, and Apple.

Now let’s get a union of its keys. We basically get the group of keys by giving the property name ‘fruitType’ to the group (Union) of class types like so:

Extracting the wanted class by using key string

we declare a type called FruitDiscriminator

This type consists of generic T where it extends from FruitTypes. When generic T extends from FruitTypes this means, it means
whatever object T you pass in must be ‘apple’ OR ‘banana’ OR ‘pear’.

So when we pass in this object, it must contain a property called fruitType that is of type union FruitTypes.
In other words, T extends FruitTypes means we restrict generic class to MUST BE FruitTypes, which is “banana” | “pear” | “apple”
So if we put in ‘pineapple’ for fruitType, it will get error.

Now, the functionality or class we want to use is called Extract.

We Extract the class we want from the union of our classes (KnownFruits), by using property fruitType. As mentioned above, property fruitType must be of type FruitTypes.
Extract simply takes the fruitType (say ‘apple’), and then extract the class type from union KnownFruits. In our case of ‘apple’, it will extract Apple.

Assignment example

So let’s try it out with a basic example.

Type ‘”pineapple”‘ does not satisfy the constraint ‘”banana” | “pear” | “apple”‘.(2344)

So let’s use a string that satisfies union FruitTypes.

Great it worked, but we see another error here. Type ‘{}’ is missing the following properties from type ‘Banana’: fruitType, length, color(2739)
So because ‘banana’ will return us the class type Banana, we must fill in the missing properties length and color.

So now, the error goes away. Notice also that FruitDiscriminator’s generic type T forces fruitType to have the same string of ‘banana’.

Function example

Now we create a function called createFruit. It takes in two parameters. The firstone is fruitType, which is of type generic T, that extends from FruitTypes. Ok so it is union “banana” | “pear” | “apple”.
Then we have props, which is FruitDiscriminator of the same generic T.

or using generics:

Mapped Types (typescript)

ref – https://www.typescriptlang.org/docs/handbook/2/mapped-types.html

declare a type called AllAreBooleanProperties that takes in a type object called Type.
Then for every property in object Type, declare it to be boolean.

Example usage, say we have an object type that has properties type () => void:

now we use our AllAreBooleanProperties type generator and create a type that says all properties must be boolean:

Thus creating a type that forces object type to have all boolean properties.

mqtt qos

ref-
http://www.steves-internet-guide.com/understanding-mqtt-qos-1/

Understanding MQTT QOS Levels- Part 2

The QOS levels are a way of guaranteeing message delivery and they refer to the connection between a broker and a client.
In this two part tutorial we will look in detail at the message flow when publishing using all three QOS levels.

We also look at the advantages and disadvantages of using the various levels

QOS 0 – Once

This is the fastest method and requires only 1 message. It is also the most unreliable transfer mode.
The message is not stored on the sender, and is not acknowledged.
The message will be delivered only once, or not at all.
Once the message has been sent by the client it is deleted from the outbound message queue.

Therefore with this QOS level there is no possibility of duplicate messages.

QOS 1 – At Least Once

This level guarantees that the message will be delivered at least once, but may be delivered more than once.

Publishing with QOS of 1 requires 2 messages.

The sender sends a message and waits for an acknowledgement (PUBACK).

If it receives an acknowledgement then it notifies the client app, (thus the callback) and deletes the message from the outbound queue..

If it doesn’t receive an acknowledgement it will resend the message with the DUP flag set (Duplicate Flag).

The message will continue to be resent at regular intervals, until the sender receives an acknowledgement.

If the message is being sent to the broker then the broker will forward that message to subscribers even though the duplicate flag is set.
Therefore subscribers can receive the same message multiple times.

QOS 2 – Only Once

This level guarantees that the message will be delivered only once. You keep them in line by messageId.

This is the slowest method as it requires 4 messages.

1- The sender sends a message and waits for an acknowledgement (PUBREC)

2 -The receiver sends a PUBREC message

3. If the sender doesn’t receive an acknowledgement ( PUBREC) it will resend the message with the DUP flag set.

4. When the sender receives an acknowledgement message PUBREC it then sends a message release message (PUBREL). The message can be deleted from the queue.

5. If the receiver doesn’t receive the PUBREL it will resend the PUBREC message

5. When the receiver receives the PUBREL message it can now forward the message onto any subscribers.

6. The receiver then send a publish complete (PUBCOMP) .

7. If the sender doesn’t receive the PUBCOMP message it will resend the PUBREL message.

8. When the sender receives the PUBCOMP the process is complete and it can delete the message from the outbound queue, and also the message state.

useFocusEffect vs useEffect

useFocusEffect

By defintion:

It just has an effect to run.

For useFocusEffect, when you hit the current page, it runs the effect, and caches the cleanup.
When you go to the next page, it’ll execute the next page’s effect first. And then runs the clean up of the previous page.

So in our case

when we hit Page1, execute effect Page 1 (A).
Goes to page 2, execute effect page 2 (K), clean up Page 1 (Z)
Goes to page 3, execute effect page 3 (M), clean up Page 2 (R)

From page 3 back to page 2, clean up Page 3 (N), execute effect Page 2 (K)
From page 2 back to page 1, clean up Page 2 (R), execute effect Page 1 (A)

A huge difference is that useFocusEffect does not have a dependency list, which it depends on for it to run its cleanup/effect.

useEffect

Whereas for useEffect, we also have an effect and cleanup, but they depend on the dependency list’s variables changing.
If it changes, then we’ll run the clean up, and then the effect.

If nothing changes in the variables, then we do nothing!

mqtt keep alive

KeepAlive
The keep alive is a time interval in seconds that the client specifies and communicates to the broker when the connection established. This interval defines the longest period of time that the broker and client can endure without sending a message. The client commits to sending regular PING Request messages to the broker. The broker responds with a PING response. This method allows both sides to determine if the other one is still available. See part 10 of this series for detailed information about the MQTT keep alive functionality.

Basically, that is all the information that is all you need to connect to an MQTT broker from an MQTT 3.1.1 client. Individual libraries often have additional options that you can configure. For example, the way that queued messages are stored in a specific implementation.

Combining types and test for it in typescript

Let’s combine some types and test for them:

Command PhaseScriptExecution failed with a nonzero exit code

ref – https://developer.apple.com/forums/thread/123790

I usually have this problem when a dependency is not updated.

Delete your Podfile.lock (I like to use the command ‘-rm -rf Podfile.lock’ on the terminal for this)
Delete your Pods folder (I like to use the command ‘-rm -rf Pods’ in the terminal for this)
Pod install

Clear your project in XCode > Product > Clean Build Folder