|
1 | 1 | <div align="center"> |
2 | | - <h1>💎 Unyielding</h1> |
3 | | - <a href="https://bundlephobia.com/result?p=unyielding"> |
4 | | - <img src="https://badgen.net/bundlephobia/minzip/unyielding@0.1.1" alt="minified and gzipped size"> |
5 | | - <img src="https://badgen.net/bundlephobia/min/unyielding@0.1.1" alt="minified size"> |
6 | | - <img src="https://badgen.net/bundlephobia/dependency-count/unyielding@0.1.1" alt="zero dependencies"> |
| 2 | + <h1>👑 ⚙️ Yield Machine</h1> |
| 3 | + <a href="https://bundlephobia.com/result?p=yieldmachine"> |
| 4 | + <img src="https://badgen.net/bundlephobia/minzip/yieldmachine@0.1.0" alt="minified and gzipped size"> |
| 5 | + <img src="https://badgen.net/bundlephobia/min/yieldmachine@0.1.0" alt="minified size"> |
| 6 | + <img src="https://badgen.net/bundlephobia/dependency-count/yieldmachine@0.1.0" alt="zero dependencies"> |
7 | 7 | </a> |
8 | 8 | </div> |
9 | 9 |
|
10 | | -Lightweight Components using Generator Functions |
| 10 | +State Machines using Generator Functions |
11 | 11 |
|
12 | 12 | ## Install |
13 | 13 |
|
14 | 14 | ```console |
15 | | -npm add unyielding |
| 15 | +npm add yieldmachine |
16 | 16 | ``` |
17 | 17 |
|
18 | 18 | ## Examples |
19 | 19 |
|
20 | | -### Components |
21 | | - |
22 | 20 | ```javascript |
23 | | -import { html, renderToString } from "unyielding"; |
24 | | - |
25 | | -function* NavLink(link) { |
26 | | - yield html`<li>`; |
27 | | - yield html`<a href="${link.url}">`; |
28 | | - yield link.title; |
29 | | - yield html`</a>`; |
30 | | - yield html`<li>`; |
31 | | -} |
| 21 | +import { call, on, start } from "yieldmachine"; |
32 | 22 |
|
33 | | -function* Nav(links) { |
34 | | - yield html`<nav aria-label="Primary">`; |
35 | | - yield html`<ul>`; |
36 | | - |
37 | | - for (const link of links) { |
38 | | - yield NavLink(link); |
| 23 | +function Loader({ url }: { url: URL }) { |
| 24 | + function* idle() { |
| 25 | + yield on("FETCH", loading); |
39 | 26 | } |
40 | | - |
41 | | - yield html`</ul>`; |
42 | | - yield html`</nav>`; |
43 | | -} |
44 | | - |
45 | | -function* PrimaryNav() { |
46 | | - yield Nav([ |
47 | | - { url: '/', title: 'Home' }, |
48 | | - { url: '/pricing', title: 'Pricing' }, |
49 | | - { url: '/features', title: 'Features' }, |
50 | | - { url: '/terms', title: 'Terms & Conditions' }, |
51 | | - ]); |
52 | | -} |
53 | | - |
54 | | -const html = await renderToString([PrimaryNav()]); |
55 | | -``` |
56 | | - |
57 | | -### Attributes |
58 | | - |
59 | | -```javascript |
60 | | -import { attributes, html } from "unyielding"; |
61 | | - |
62 | | -function CreatePhotoForm() { |
63 | | - yield html`<form ${attributes({ method: 'post', action: '/photo' })}>`; |
64 | | - // … |
65 | | - yield html`</form>`; |
66 | | -} |
67 | | -``` |
68 | | - |
69 | | -### Data attributes |
70 | | - |
71 | | -```javascript |
72 | | -import { dataset, html } from "unyielding"; |
73 | | - |
74 | | -function Item({ uuid, title }) { |
75 | | - yield html`<article ${dataset({ uuid })}>`; |
76 | | - yield html`<h2>`; |
77 | | - yield title; |
78 | | - yield html`</h2>`; |
79 | | - yield html`</article>`; |
80 | | -} |
81 | | -``` |
82 | | - |
83 | | -## TODO / Ideas |
84 | | - |
85 | | -```javascript |
86 | | -// Yield function with name of HTML tag |
87 | | -function Nav(links) { |
88 | | - yield html`<nav aria-label="Primary">`; |
89 | | - |
90 | | - yield function* ul() { |
91 | | - for (const link of links) { |
92 | | - yield NavLink(link); |
93 | | - } |
94 | | - }; |
95 | | - |
96 | | - yield html`</nav>`; |
97 | | -} |
98 | | - |
99 | | -// Yield function with name of landmark |
100 | | -function Page() { |
101 | | - yield PrimaryNav(); |
102 | | - |
103 | | - yield function* main() { // <main> |
104 | | - yield Heading("Welcome!"); |
| 27 | + function* loading() { |
| 28 | + yield call(fetch, [url.toString()]); |
| 29 | + yield on("SUCCESS", success); |
| 30 | + yield on("FAILURE", failure); |
105 | 31 | } |
106 | | - |
107 | | - yield function* contentinfo() { // <footer role=contentinfo> |
108 | | - yield FooterLinks(); |
| 32 | + function* success() {} |
| 33 | + function* failure() { |
| 34 | + yield on("RETRY", loading); |
109 | 35 | } |
| 36 | + |
| 37 | + return idle; |
110 | 38 | } |
111 | 39 |
|
112 | | -// Perhaps allow attributes to be set |
113 | | -function Nav2(links) { |
114 | | - yield function* nav() { |
115 | | - yield attributes({ "aria-label": "Primary" }); |
| 40 | +const loader = start(Loader, { url: new URL("https://example.org/") }); |
116 | 41 |
|
117 | | - yield function* ul() { |
118 | | - for (const link of links) { |
119 | | - yield NavLink(link); |
120 | | - } |
121 | | - }; |
122 | | - } |
123 | | -} |
| 42 | +loader.next("FETCH"); |
| 43 | +loader.value; // "loading" |
| 44 | + |
| 45 | +loader.promisedValue.then(response => { |
| 46 | + // Use response of fetch() |
| 47 | +}); |
124 | 48 | ``` |
0 commit comments