Skip to content

Commit 65671ab

Browse files
committed
draft
1 parent 0873d43 commit 65671ab

28 files changed

+447
-9
lines changed
Loading
Loading

7-network/07-xmlhttprequest/article.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ They exist for historical reasons, to get either a string or XML document. Nowad
181181
All states, as in [the specification](http://www.w3.org/TR/XMLHttpRequest/#states):
182182

183183
```js
184-
const unsigned short UNSENT = 0; // initial state
185-
const unsigned short OPENED = 1; // open called
186-
const unsigned short HEADERS_RECEIVED = 2; // response headers received
187-
const unsigned short LOADING = 3; // response is loading (a data packed is received)
188-
const unsigned short DONE = 4; // request complete
184+
UNSENT = 0; // initial state
185+
OPENED = 1; // open called
186+
HEADERS_RECEIVED = 2; // response headers received
187+
LOADING = 3; // response is loading (a data packed is received)
188+
DONE = 4; // request complete
189189
```
190190

191191
An `XMLHttpRequest` object travels them in the order `0` -> `1` -> `2` -> `3` -> ... -> `3` -> `4`. State `3` repeats every time a data packet is received over the network.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# From the orbital height
2+
3+
This section describes a set of modern standards for "web components".
4+
5+
As of now, these standards are under development. Some features are well-supported and integrated into the modern HTML/DOM standard, while others are yet in draft stage. You can try examples in any browser, Google Chrome is probably the most up to date with these features. Guess, that's because Google fellows are behind many of the related specifications.
6+
7+
The whole component idea is nothing new. It's used in many frameworks and elsewhere.
8+
9+
## What's common between...
10+
11+
Before we move to implementation details, take a look at this great achievement of humanity:
12+
13+
![](satellite.jpg)
14+
15+
That's the International Space Station (ISS).
16+
17+
And this is how it's made inside (approximately):
18+
19+
![](satellite-expanded.jpg)
20+
21+
The International Space Station:
22+
- Consists of many components.
23+
- Each component, in its turn, has many smaller details inside.
24+
- The components are very complex, much more complicated than most websites.
25+
- Components are developed internationally, by teams from different countries, speaking different languages.
26+
27+
...And this thing is flying, keeping humans alive in space!
28+
29+
How such complex devices are created?
30+
31+
Which principles we could borrow, to make our development same-level reliable and scalable? Or, at least, close to it.
32+
33+
## Component architecture
34+
35+
The well known rule for developing complex software is: don't make complex software.
36+
37+
If something becomes complex -- split it into simpler parts and connect in the most obvious way.
38+
39+
**A good architect is the one who can make the complex simple.**
40+
41+
We can split a user interface into components -- visual entities, each of them has own place on the page, can "do" a well-described task, and is separate from the others.
42+
43+
Let's take a look at a website, for example Twitter.
44+
45+
It naturally splits into components:
46+
47+
![](web-components-twitter.png)
48+
49+
1. Top navigation.
50+
2. User info.
51+
3. Follow suggestions.
52+
4. Submit form.
53+
5. And also 6, 7 - messages.
54+
55+
Components may have subcomponents, e.g. messages may be parts of a higher-level "message list" component. A clickable user picture itself may be a component, and so on.
56+
57+
How do we decide, what is a component? That comes from intuition, experience and common sense. In the case above, the page has blocks, each of them plays its own role.
58+
59+
So, what comprises a component?
60+
61+
- A component has its own JavaScript class.
62+
- DOM structure, managed solely by its class, outside code doesn't access it ("encapsulation" principle).
63+
- CSS styles, applied to the component.
64+
- API: events, class methods etc, to interact with other components.
65+
66+
"Web components" provide built-in browser capabilities for components:
67+
68+
- [Custom elements](https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements) -- to define custom HTML elements.
69+
- [Shadow DOM](https://dom.spec.whatwg.org/#shadow-trees) -- to create an internal DOM for the component, hidden from the others.
70+
- [CSS Scoping](https://drafts.csswg.org/css-scoping/) -- to declare styles that only apply inside the component.
71+
72+
There exist many frameworks and development methodologies that aim to do the similar thing, each one with its own bells and whistles. Usually, special CSS classes and conventions are used to provide "component feel" -- CSS scoping and DOM encapsulation.
73+
74+
Web components provide built-in browser capabilities for that, so we don't have to emulate them any more.
75+
76+
In the next chapter we'll go into details of "Custom Elements" -- the fundamental and well-supported feature of web components, good on its own.
Loading
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class LiveTimer extends HTMLElement {
2+
3+
render() {
4+
this.innerHTML = `
5+
<time-formatted hour="numeric" minute="numeric" second="numeric">
6+
</time-formatted>
7+
`;
8+
9+
this.timerElem = this.firstElementChild;
10+
}
11+
12+
connectedCallback() { // (2)
13+
if (!this.rendered) {
14+
this.render();
15+
this.rendered = true;
16+
}
17+
this.timer = setInterval(() => this.update(), 1000);
18+
}
19+
20+
update() {
21+
this.date = new Date();
22+
this.timerElem.setAttribute('datetime', this.date);
23+
this.dispatchEvent(new CustomEvent('tick', { detail: this.date }));
24+
}
25+
26+
disconnectedCallback() {
27+
clearInterval(this.timer); // important to let the element be garbage-collected
28+
}
29+
30+
}
31+
32+
customElements.define("live-timer", LiveTimer);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class TimeFormatted extends HTMLElement {
2+
3+
render() {
4+
let date = new Date(this.getAttribute('datetime') || Date.now());
5+
6+
this.innerHTML = new Intl.DateTimeFormat("default", {
7+
year: this.getAttribute('year') || undefined,
8+
month: this.getAttribute('month') || undefined,
9+
day: this.getAttribute('day') || undefined,
10+
hour: this.getAttribute('hour') || undefined,
11+
minute: this.getAttribute('minute') || undefined,
12+
second: this.getAttribute('second') || undefined,
13+
timeZoneName: this.getAttribute('time-zone-name') || undefined,
14+
}).format(date);
15+
}
16+
17+
connectedCallback() { // (2)
18+
if (!this.rendered) {
19+
this.render();
20+
this.rendered = true;
21+
}
22+
}
23+
24+
static get observedAttributes() { // (3)
25+
return ['datetime', 'year', 'month', 'day', 'hour', 'minute', 'second', 'time-zone-name'];
26+
}
27+
28+
attributeChangedCallback(name, oldValue, newValue) { // (4)
29+
this.render();
30+
}
31+
32+
}
33+
34+
customElements.define("time-formatted", TimeFormatted);

0 commit comments

Comments
 (0)