Stages
What is an observable and why has it been proposed as part of the Javascript ECMA-262 standard?
An observable is a constructor that takes an observer and calls that observer with a stream of event(s) when the event(s) occur.
import fs from 'fs/promises'
import { Observable } from 'rxjs'
const myFunc = async function (observer) {
const result = await fs.readFile('package.json', 'utf8')
observer.next(result)
}
const observer = x => console.log(x)
const myObs = new Observable(myFunc)
myObs.subscribe(observer)
Single Shot Event
import fs from 'fs/promises'
import { Observable } from 'rxjs'
import { map, distinctUntilChanged } from 'rxjs/operators'
const myFunc = async function (observer) {
const handler = async () => {
const result = await fs.readFile('package.json', 'utf8')
observer.next(result)
}
setInterval(handler, 1000)
}
const observer = x => {
console.log(Array(100).join('-'))
console.log(' my observer')
console.log(Array(100).join('-'))
console.log(x)
}
const myObs = new Observable(myFunc)
const myMappedObs = myObs.pipe(map(pkg => parseInt(JSON.parse(pkg).version)))
const distinctObs = myMappedObs.pipe(distinctUntilChanged())
distinctObs.subscribe(observer)
Stream of changed events
interfaceint Observable {
constructor(subscriber : SubscriberFunction);
// Subscribes to the sequence with an observer
subscribe(observer : Observer) : Subscription;
// Subscribes to the sequence with callbacks
subscribe(next : Function,
error? : Function,
complete? : Function) : Subscription;
}
interface Subscription {
// Cancels the subscription
unsubscribe() : void;
}Interfaces
The observables proposal was started in May 2015, it is complex, and uncharacteristic
of the process.
>1000 Days so far in the process
// wouldn't this be nice
const myArray = [1, 2, 3];
console.log(myArray.lastItem);
// instead of
console.log(myArray[myArray.length-1])
#include <iostream>
#include <vector>
int main ()
{
// constructors used in the same order as described above:
std::vector<int> first; // empty vector of ints
std::vector<int> second (4,100); // four ints with value 100
std::vector<int> third (second.begin(),second.end()); // iterating through second
std::vector<int> fourth (third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
std::cout << "The contents of fifth are:";
for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}C++ has libraries that fix this (STL)
some quotes from a committee member
“It takes a long time, but we want to make sure we do it right”
“A lot of the work is soliciting feedback from the community”
So if you think you would like a lastItem operation on arrays please go and star the following link https://github.com/keithamus/proposal-array-last
because it is likely that we can grow the Javascript language incrementally.
(if you are in this room you should be taking an interest )
Observables demos at https://github.com/ghinks/bootcampers-talk-august-2018.git
other TC 39 proposals demonstrated at https://github.com/ghinks/tc-39-examples.git