Why rxjs epic not worked? - redux

tell me please what is the problem, operator zip follows conditions, by lose race, why?
export default (action$, state$) =>
race(
zip(
action$.pipe(ofType(stateImmediate)),
action$.pipe(ofType(afterChainHook))
).pipe(
ignoreElements()
)
,
action$.pipe(
ofType(afterChainHook),
pluck("payload"),
withApplications(state$),
map(mergeApps),
map(updateApp)
)
);

Related

how do i stop the pre-condition from failing in the below example in ADA Spark

For a project I am currently trying to write a mini pilot assistance system for an imaginary aircraft. The task is to learn Ada Spark, not avionics. I have modelled the plane components I wish to use, done some tests in the main file to check the components work as expected, and all is fine, and now I am to add pre and post conditions to functions to make sure my plane is super safe. One such safety measure is to make sure the engine cannot be switched on whilst the plane is in tow, or vice versa, switch to tow whilst the engine is on.
I have modelled an engine as a highly complex record, with one attribute, type OnOff, which takes one of the values On, or Off. Note I plan on expanding upon the attributes, so it isn't going to remain a one attribute record.
Here is the engines specification file
package engines with SPARK_Mode
is
type OnOff is (On, Off);
type Engine is record
isOn: OnOff;
end record;
procedure switchOn (x : in out Engine);
procedure switchOff (x : in out Engine);
end engines;
My plane is put together like so:
type Plane is record
engine1: Engine;
engine2: Engine;
gearOfLanding: LandingGear;
doorPax1, doorPax2, doorServ1, doorServ2,
doorCockpit: Door;
panelOfReadings: ReadingsPanel;
panelOfAlerts: AlertsPanel;
planOfFlight: FlightPlan;
speedLimits: SpeedLimit;
altitudeLimits: AltitudeLimit;
attitudeLimits: AttitudeLimit;
litresPerMile: Integer;
fuelTank1: FuelTank;
end record;
The procedure switchOnEngine within the planes file takes an engine as an input and calls switchOn from the engines file. Here is the specification and below, the body:
procedure switchOnEngine (x : in out Engine; y : in Plane) with
Pre => y.panelOfReadings.mode /= Tow,
Post => x = (isOn => On) and y.panelOfReadings.mode /= Tow;
procedure switchOnEngine (x : in out Engine; y : in Plane)
is
begin
switchOn(x);
end switchOnEngine;
The plane is passed in as a variable so I can check various attributes for my pre and post conditions, but I am getting warning messages I am unsure how to resolve.
precondition might fail
cannot prove y.panelOfReadings.mode /= Tow e.g when .......mode =>Tow
The following line is also giving an error from the main file where I control my plane
switchOnEngine(AirForceOne.engine1, AirForceOne);
formal parameters x and y are aliased, and this is being marked as a 'high' priority warning.
here is the initialisation of the plane in the main file
AirForceOne : Plane := (
engine1 => (isOn => Off),
engine2 => (isOn => Off),
litresPerMile => 5,
gearOfLanding => (isExtended => Extended),
doorPax1 => (isClosed => Closed, isLocked => Unlocked),
doorPax2 => (isClosed => Closed, isLocked => Unlocked),
doorServ1 => (isClosed => Closed, isLocked => Unlocked),
doorServ2 => (isClosed => Closed, isLocked => Unlocked),
doorCockpit => (isClosed => Closed, isLocked => Unlocked),
fuelTank1 => (capacity=>26000, currentFuel=>26000),
planOfFlight => (distFromDest => 1500),
panelOfReadings =>
(mode => Tow,
currentSpeed => 0,
altitud => 0,
attitud =>
(currentPitch=>0,
currentRoll =>0)
),
panelOfAlerts =>
(approachingStallSpeed => Off,
unRestrictedSpeed => Off,
withinLandingSpdRange => Off,
withinOptCruiseAlt => Off,
withinOptCruiseSpeed => Off,
takeoffSpeedReached => Off,
fuelStatus => Off,
maxPitchAngleExceeded => Off,
maxRollAngleExceeded => Off),
speedLimits =>
(minLanding => 180,
maxLanding => 200,
minStall => 110,
minTakeoff => 130,
maxRestricted => 300,
maxGroundMode => 10),
altitudeLimits =>
(minFlight => 500,
maxFlight => 41000,
optCruiseAlt => 36000,
maxRestrictedSpeed => 10000,
maxInitiateFlareMode => 100),
attitudeLimits =>
(maxRoll => 30,
maxPitch => 30,
minRoll => -30,
minPitch => -30)
);
Any help would be great. I thought it would be enough to suggest in the pre condition that the plane cannot be in tow, but it seems to not be enough.
The purpose of Switchonengine is to change the state of the plane. Writing it to change the state of an engine is going to complicate things.
Max_Engines : constant := 100; -- The Lillium jet has 36, so I hope this is enough
type Engine_Num is range 1 .. Max_Engines;
type Engine_Info is ...
type Engine_Map is array (Engine_Num range <>) of Engine_Info with
Dynamic_Predicate => Engine_Map'First = 1;
type Plane_Info (Num_Engines : Engine_Num) is record
Engine : Engine_Map (1 .. Num_Engines);
...
procedure Turn_On (Engine : in Engine_Num; Plane : in out Plane_Info) with
Pre => Engine in 1 .. Plane.Num_Engines and then
(not Running (Plane.Engine (Engine) ) and not Under_Tow (Plane),
Post => Running (Plane.Engine (Engine) );
Air_Force_One : Plane_Info (Num_Engines => 4);

Whats the correct key/value Syntax to convert this Array with Symfony/XmlEncoder?

im building my request data as an array structure and want to use the symfony XmlEncoder to encode my Array to XML.
so i guess i got the fundamental part right, it looks like that for example:
$request_object = [
"acc-id" => $all_credentials,
"req-id" => $request_guid,
"tran-type" => "spec-url"
];
the syntax im looking for encodes in the following format, with attribute and value:
<amount currency="EUR">1.99</amount>
i have the possiblity to use the # sign on an array key, but how to also fit in the value?
$request_object = [
"acc-id" => $all_credentials,
"req-id" => $request_guid,
"tran-type" => "spec-url"
"am" => ["#attr"=>"attrval"]
];
this should be
<am attr="attrval"/>
but how to write it that i can also set the value? like:
<am attr="attrval">VALUE</am>
help is much appreciated
Use '#' as the index for the scalar value.
I found it by looking through the tests for the encoder.
#src:https://github.com/symfony/serializer/blob/master/Tests/Encoder/XmlEncoderTest.php
#line: 196
public function testEncodeScalarRootAttributes()
{
$array = [
'#' => 'Paul',
'#eye-color' => 'brown',
];
$expected = '<?xml version="1.0"?>'."\n".
'<response eye-color="brown">Paul</response>'."\n";
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
}
...
#line: 234
public function testEncodeScalarWithAttribute()
{
$array = [
'person' => ['#eye-color' => 'brown', '#' => 'Peter'],
];
$expected = '<?xml version="1.0"?>'."\n".
'<response><person eye-color="brown">Peter</person></response>'."\n";
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
}

How can I join two Firestore queries using rxfire and rxjs (OR query)

The goal is simple: to join two firestore queries utilizing rxjs, rxfire, and the rnfirebase react native library.
I've read multiple tutorials 1, 2 on joining queries, but they all fail with different errors.
//Simple test for collectionData
import { collectionData } from 'rxfire/firestore';
this.myQuery = this.props.docRef.collection(`messages`).where('read', 'array-contains', this.props.me.uid)
collectionData(this.myQuery, 'id').subscribe(docs => console.log(docs))
//Fails with error: this._next is not a function.
Alternatively,
this.publicQuery = this.props.docRef.collection('messages').where('public', '==', true)
this.myQuery = this.props.docRef.collection(`messages`).where('read', 'array-contains', this.props.me.uid)
const myQuery$ = new Rx.Subject();
const publicQuery$ = new Rx.Subject();
this.myQuery.onSnapshot((querySnapshot) => {
myQuery$.next(querySnapshot.docs.map(d => d.data() ));
});
this.publicQuery.onSnapshot((querySnapshot) => {
publicQuery$.next(querySnapshot.docs.map(d => d.data() ));
});
const orQuery$ = combineLatest(this.myQuery, this.publicQuery).switchMap((docs) => {
var [one, two] = docs;
var combined = one.concat(two);
return Rx.Observable.of(combined);
})
orQuery$.subscribe((result) => {
console.log('>>>> ', result)
})
//TypeError: undefined is not a function (near ...switchMap)
How can I successfully join two firestore queries (OR)?
You're already very close to the solution. Let's go through the issues step-by-step.
First, it's not necessary to create a Subject just to transform your result from onSnapshot. Instead of this:
this.myQuery.onSnapshot((querySnapshot) => {
myQuery$.next(querySnapshot.docs.map(d => d.data()))
});
We can achieve the same using 'pipeable transformation operators':
const myQuery$ = this.myQuery.onSnapshot.pipe(
map(querySnapshot => querySnapshot.docs.map(d => d.data()))
);
The same goes for the other query:
const publicQuery$ = this.publicQuery.onSnapshot.pipe(
map(querySnapshot => querySnapshot.docs.map(d => d.data())
);
Second, to join those two queries, combineLatest is indeed the correct creation function.
However, your error might result from you using a newer RxJS version, that doesn't support 'fluent' operators (officially called "patch operators") anymore. They have been replaced by 'pipeable operators' from RxJS 6 onwards. As an example, myObs$.map(...) has become myObs$.pipe(map(...)). The tutorials probably use an older version of RxJS where the first is still possible.
Also, it shouldn't be necessary to use switchMap if the inner Observable is just an of operator. It is sufficient in this case to use the map operator, which will behave equally.
Using the new RxJS 6+ syntax together with map, the combination will look like this:
const orQuery$ = combineLatest(myQuery$, publicQuery$).pipe(
map(([one, two]) => one.concat(two))
)
The rest of your code should be correct.
Side Note: Keep in mind that the equivalent of your code in SQL is UNION (not JOIN). In order to JOIN programatically, you'd need to combine each object of result set A with each object of result set B and create a joined object for each pair. Such a function for a keyless OUTER JOIN would look like this (placed in your map pipe):
one.map(a =>
two.map(b => Object.assign({}, a, b)))
.reduce((p, c) => p.concat(c), [])
If you want to have a UNION with no duplicate objects, concat only those items from two that have no matching primary key in list one. This would be your mapping function:
one.concat(two.filter(twoItem => !one.some(oneItem => oneItem.id == twoItem.id)))
DEMO: A complete, working demo with the above code and a simulated FireStore can be found here:
https://stackblitz.com/edit/rxjs-mefynu?devtoolsheight=60

Immutable remove item from array from all elements in Map | Redux state

I am facing some problems removing an items from an array from every element within a map in my Redux state.
Using the following structure
entities: {
brands: (Map) {
first: {
...props,
strategists: [
'1', '2'
]
},
second: {
...props,
strategists: [
'1', '2'
]
}, ..
}
}
This is the best I could do:
const newBrands = state
.get('brands')
.map(brand => brand.updateIn(['strategists'], strategists => strategists.filter(s => s !== id)))
return state.set(['brands'], newBrands)
I doesn't add up tho and I coudn't really find it online either.
Thanks
I had not used Immutable.js before, but I played around with your code example and everything seems to work as expected up until the last line, where you're setting state using state.set(['brands'], newBrands) instead of state.set('brands', newBrands); Could that have been the issue? Otherwise the only change is that I assumed that the first and second should also be Maps, so if you got an error before then maybe that was also part of the problem.
I included the code just in case:
const state = Immutable.Map({});
const original = Immutable.Map({
first: Immutable.Map({
someProp: 'someValue1',
strategists: [
'1', '2', '3'
]
}),
second: Immutable.Map({
someProp: 'someValue2',
strategists: [
'1', '2', '3'
]
})
});
const id ='2';
const brands = original
.map(brand =>
brand.updateIn(['strategists'], strategists =>
strategists.filter(s => s !== id)
)
);
const newState = state.set('brands', brands);
console.log(
newState.get('brands')
.toString()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>

Meteor/ng2: Where to process a variable that requires two subscription to be resolved

Assume I have
this.subscribe('a', () => {this.allA = A.find();});
this.subscribe('b', () => {this.allB= B.find();});
And a variable that is something like
let x = *take the first A, do some calculation to get the B linked, return B*
Where such logic be put to be sure this is only processed when subscriptions 'a' and 'b' are resolved ?
Might be using zones but I am not 100% what could be the best way to do it in a #Component.
PS: avoid do-a-serverside-method :D
Regards
Instead of using callbacks in your subscriptions, return subscription handles and then check their ready() state:
const subA = this.subscribe('a');
const subB = this.subscribe('b');
const that = this;
Tracker.autorun(()=>{
if ( subA.ready() ) that.allA = A.find();
if ( subB.ready() ) that.allB = B.find();
if ( subA.ready() && subB.ready() ) {
let x = ... //compute x
}
});
The solution seems for now to be to wrap it in a autorun (MeteorComponent for instance)
this.autorun(()=>{
let s1 = this.subscribe('a', () => {this.allA = A.find();});
let s2 = this.subscribe('b', () => {this.allB= B.find();});
if(s1.ready() && s2.ready())
...

Resources