ezh 0.1.6 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -517,7 +517,7 @@ export declare const bindData: <T extends State<T>, K extends keyof T>(data: T,
517
517
  K,
518
518
  T[K]
519
519
  ];
520
- export declare const useState: <T extends State<T>>(initial: T | ((current?: T) => T), resetOnUnmount?: boolean) => T;
520
+ export declare const useState: <T extends State<T>>(initial: T, hook?: (ver: number, state: T) => void) => T;
521
521
  export declare const navigate: (to: string, replace?: true) => void;
522
522
  export declare const route: <ParamT extends object>(com: Com<ParamT>, path: string, needAuth?: boolean) => IRoute<ParamT>;
523
523
  export declare const Router: Com<RouterProp>;
package/lib/router.js CHANGED
@@ -2,10 +2,10 @@ import { Path } from 'path-parser';
2
2
  import { $ezh } from './elements';
3
3
  import { useState } from './state';
4
4
  export { navigate, route, Router, Link, RouteLink, };
5
- let d = 0;
6
- const e = useState({ c: 0 });
5
+ let c = 0;
6
+ const d = useState({ ver: 0 });
7
7
  window.addEventListener('popstate', () => {
8
- e.c = ++d;
8
+ d.ver = ++c;
9
9
  });
10
10
  const navigate = (to, replace) => {
11
11
  const { pathname, search, hash } = window.location;
@@ -18,15 +18,15 @@ const navigate = (to, replace) => {
18
18
  window.history.replaceState(undefined, '', to);
19
19
  }
20
20
  queueMicrotask(() => {
21
- e.c = ++d;
21
+ d.ver = ++c;
22
22
  });
23
23
  }
24
24
  };
25
- const f = 'ezhEZH';
26
- class i {
25
+ const e = 'ezhEZH';
26
+ class h {
27
27
  constructor(com, path, needAuth) {
28
28
  this.com = com;
29
- this.b = Path.createPath(path + f);
29
+ this.b = Path.createPath(path + e);
30
30
  this.needAuth = needAuth;
31
31
  }
32
32
  com;
@@ -34,16 +34,16 @@ class i {
34
34
  parseParam(url) {
35
35
  const param = this.b.test(url);
36
36
  if (param) {
37
- delete param[f];
37
+ delete param[e];
38
38
  return param;
39
39
  }
40
40
  }
41
- buildUrl(k) {
41
+ buildUrl(j) {
42
42
  throw new Error('WildcardRoute does not support buildUrl');
43
43
  }
44
44
  b;
45
45
  }
46
- class j {
46
+ class i {
47
47
  constructor(com, path, needAuth) {
48
48
  this.com = com;
49
49
  this.b = Path.createPath(path);
@@ -60,10 +60,10 @@ class j {
60
60
  b;
61
61
  }
62
62
  const route = (com, path, needAuth = true) => {
63
- return path.endsWith('*') ? new i(com, path, needAuth) : new j(com, path, needAuth);
63
+ return path.endsWith('*') ? new h(com, path, needAuth) : new i(com, path, needAuth);
64
64
  };
65
65
  const Router = ({ routes, notFound, checkAuth }) => {
66
- d = e.c;
66
+ c = d.ver;
67
67
  const { pathname, search, hash } = window.location;
68
68
  const a = `${pathname}${search}${hash}`;
69
69
  const { $s, $k, $c } = $ezh;
@@ -72,9 +72,9 @@ const Router = ({ routes, notFound, checkAuth }) => {
72
72
  const param = route.parseParam(a);
73
73
  if (param) {
74
74
  if (checkAuth && route.needAuth) {
75
- const g = checkAuth(a);
76
- if (g) {
77
- navigate(g);
75
+ const f = checkAuth(a);
76
+ if (f) {
77
+ navigate(f);
78
78
  return;
79
79
  }
80
80
  }
@@ -83,8 +83,8 @@ const Router = ({ routes, notFound, checkAuth }) => {
83
83
  }
84
84
  return notFound ? $s($k('404'), $c('_', notFound, {})) : undefined;
85
85
  };
86
- const h = (v, url, replace) => {
87
- v.preventDefault();
86
+ const g = (u, url, replace) => {
87
+ u.preventDefault();
88
88
  const { pathname, search, hash } = window.location;
89
89
  const a = `${pathname}${search}${hash}`;
90
90
  if (url !== a) {
@@ -94,18 +94,18 @@ const h = (v, url, replace) => {
94
94
  else {
95
95
  window.history.replaceState(undefined, '', url);
96
96
  }
97
- e.c = ++d;
97
+ d.ver = ++c;
98
98
  }
99
99
  };
100
100
  const Link = (props, children) => {
101
101
  const { href, className, style, title, replace } = props;
102
- const onclick = href ? (v) => h(v, href, replace) : undefined;
102
+ const onclick = href ? (u) => g(u, href, replace) : undefined;
103
103
  return $ezh.$('_', 'a', { className, style, title, href, onclick }, children, -1, 1);
104
104
  };
105
105
  const RouteLink = (props, children) => {
106
106
  const { route, params, className, style, title, replace } = props;
107
107
  const href = route.buildUrl(params);
108
- const onclick = (v) => h(v, href, replace);
108
+ const onclick = (u) => g(u, href, replace);
109
109
  return $ezh.$('_', 'a', { className, style, title, href, onclick }, children, -1, 1);
110
110
  };
111
111
  //# sourceMappingURL=router.js.map
package/lib/state.js CHANGED
@@ -1,86 +1,86 @@
1
- import { addComReader, p, refreshComReaders } from './elements';
2
- const f = Symbol();
3
- const j = Array.prototype;
1
+ import { addComReader, o, refreshComReaders } from './elements';
2
+ const i = Symbol();
3
+ const k = Array.prototype;
4
4
  const x = addComReader;
5
- let l = (readerRecord, key) => {
6
- l = refreshComReaders;
7
- l(readerRecord, key);
5
+ let m = (readerRecord, key) => {
6
+ m = refreshComReaders;
7
+ m(readerRecord, key);
8
8
  };
9
- export let t = false;
9
+ export let u = false;
10
10
  export const y = new WeakMap();
11
- const q = (_) => {
12
- return function (...m) {
13
- const i = this.length;
14
- let begin, c, k;
11
+ const r = (_) => {
12
+ return function (...n) {
13
+ const h = this.length;
14
+ let begin, d, l;
15
15
  switch (_) {
16
- case j.push:
17
- begin = i;
18
- c = begin + m.length;
19
- k = i;
16
+ case k.push:
17
+ begin = h;
18
+ d = begin + n.length;
19
+ l = h;
20
20
  break;
21
- case j.unshift:
21
+ case k.unshift:
22
22
  begin = 0;
23
- c = m.length;
24
- k = i;
23
+ d = n.length;
24
+ l = h;
25
25
  break;
26
- case j.fill:
27
- begin = m[1];
28
- if (begin === undefined || begin <= -i) {
26
+ case k.fill:
27
+ begin = n[1];
28
+ if (begin === undefined || begin <= -h) {
29
29
  begin = 0;
30
30
  }
31
31
  else if (begin < 0) {
32
- begin = i + begin;
32
+ begin = h + begin;
33
33
  }
34
- c = m[2];
35
- if (c === undefined || c > i) {
36
- c = i;
34
+ d = n[2];
35
+ if (d === undefined || d > h) {
36
+ d = h;
37
37
  }
38
- else if (c <= -i) {
39
- c = 0;
38
+ else if (d <= -h) {
39
+ d = 0;
40
40
  }
41
- else if (c < 0) {
42
- c = i + c;
41
+ else if (d < 0) {
42
+ d = h + d;
43
43
  }
44
- k = c;
44
+ l = d;
45
45
  break;
46
- case j.sort:
47
- case j.reverse:
48
- begin = c = 0;
49
- k = i;
46
+ case k.sort:
47
+ case k.reverse:
48
+ begin = d = 0;
49
+ l = h;
50
50
  break;
51
- case j.pop:
52
- begin = c = i - 1;
53
- k = i;
51
+ case k.pop:
52
+ begin = d = h - 1;
53
+ l = h;
54
54
  break;
55
- case j.shift:
56
- begin = c = 0;
57
- k = i;
55
+ case k.shift:
56
+ begin = d = 0;
57
+ l = h;
58
58
  break;
59
59
  default:
60
- begin = m[0];
60
+ begin = n[0];
61
61
  if (begin < 0) {
62
- begin = i + begin;
62
+ begin = h + begin;
63
63
  }
64
- c = m.length > 2 ? m.length - 2 : 0;
65
- k = m[1];
66
- if (k === undefined) {
67
- k = i - begin;
64
+ d = n.length > 2 ? n.length - 2 : 0;
65
+ l = n[1];
66
+ if (l === undefined) {
67
+ l = h - begin;
68
68
  }
69
- k = k > 0 ? (k !== c ? i : begin + c) : (c ? i : begin);
70
- c += begin;
69
+ l = l > 0 ? (l !== d ? h : begin + d) : (d ? h : begin);
70
+ d += begin;
71
71
  }
72
- const h = _.bind(this)(...m);
73
- const n = this[f];
74
- let o;
75
- for (let b = begin; b < c; b++) {
76
- o = this[b];
77
- switch (typeof o) {
72
+ const e = _.bind(this)(...n);
73
+ const p = this[i];
74
+ let q;
75
+ for (let b = begin; b < d; b++) {
76
+ q = this[b];
77
+ switch (typeof q) {
78
78
  case 'object':
79
- if (o) {
80
- while (b < c) {
81
- o = this[b];
82
- if (o && !o[f]) {
83
- this[b] = r(o);
79
+ if (q) {
80
+ while (b < d) {
81
+ q = this[b];
82
+ if (q && !q[i]) {
83
+ this[b] = s(q);
84
84
  }
85
85
  b++;
86
86
  }
@@ -91,126 +91,126 @@ const q = (_) => {
91
91
  }
92
92
  break;
93
93
  }
94
- if (i !== this.length) {
95
- l(n, 'length');
94
+ if (h !== this.length) {
95
+ m(p, 'length');
96
96
  }
97
- l(n, Symbol.iterator);
98
- return h;
97
+ m(p, Symbol.iterator);
98
+ return e;
99
99
  };
100
100
  };
101
- const D = {
102
- 'push': q(j.push),
103
- 'unshift': q(j.unshift),
104
- 'fill': q(j.fill),
105
- 'sort': q(j.sort),
106
- 'reverse': q(j.reverse),
107
- 'pop': q(j.pop),
108
- 'shift': q(j.shift),
109
- 'splice': q(j.splice),
101
+ const C = {
102
+ 'push': r(k.push),
103
+ 'unshift': r(k.unshift),
104
+ 'fill': r(k.fill),
105
+ 'sort': r(k.sort),
106
+ 'reverse': r(k.reverse),
107
+ 'pop': r(k.pop),
108
+ 'shift': r(k.shift),
109
+ 'splice': r(k.splice),
110
110
  };
111
- const E = (target, d) => {
112
- const v = d.length;
113
- const i = target.length;
111
+ const D = (target, f) => {
112
+ const w = f.length;
113
+ const h = target.length;
114
114
  let g;
115
- for (let b = 0; b < v; b++) {
116
- let a = d[b];
117
- if (a && typeof a === 'object') {
115
+ for (let b = 0; b < w; b++) {
116
+ let c = f[b];
117
+ if (c && typeof c === 'object') {
118
118
  do {
119
- a = d[b];
119
+ c = f[b];
120
120
  g = target[b];
121
- if (a) {
121
+ if (c) {
122
122
  if (g) {
123
- if (g !== a) {
124
- g[f] = a;
123
+ if (g !== c) {
124
+ g[i] = c;
125
125
  }
126
126
  }
127
127
  else {
128
- target[b] = r(a);
128
+ target[b] = s(c);
129
129
  }
130
130
  }
131
131
  else {
132
- target[b] = a;
132
+ target[b] = c;
133
133
  }
134
- } while (++b < v);
134
+ } while (++b < w);
135
135
  break;
136
136
  }
137
- else if (a !== null && a !== undefined) {
137
+ else if (c !== null && c !== undefined) {
138
138
  do {
139
- target[b] = d[b];
140
- } while (++b < v);
139
+ target[b] = f[b];
140
+ } while (++b < w);
141
141
  break;
142
142
  }
143
143
  else {
144
- target[b] = a;
144
+ target[b] = c;
145
145
  }
146
146
  }
147
- const n = target[f];
148
- if (i !== v) {
149
- if (i > v) {
150
- target.length = v;
147
+ const p = target[i];
148
+ if (h !== w) {
149
+ if (h > w) {
150
+ target.length = w;
151
151
  }
152
- l(n, 'length');
152
+ m(p, 'length');
153
153
  }
154
- l(n, Symbol.iterator);
154
+ m(p, Symbol.iterator);
155
155
  };
156
- const F = (target, z, d) => {
156
+ const E = (target, z, f) => {
157
157
  const A = { ...target };
158
158
  let g;
159
- for (const u in d) {
160
- delete A[u];
161
- const a = d[u];
162
- if (a && typeof a === 'object') {
163
- g = target[u];
159
+ for (const v in f) {
160
+ delete A[v];
161
+ const c = f[v];
162
+ if (c && typeof c === 'object') {
163
+ g = target[v];
164
164
  if (g) {
165
- g[f] = a;
165
+ g[i] = c;
166
166
  continue;
167
167
  }
168
168
  }
169
- if (target[u] !== a) {
170
- z[u] = a;
169
+ if (target[v] !== c) {
170
+ z[v] = c;
171
171
  }
172
172
  }
173
- for (const u in A) {
174
- delete z[u];
173
+ for (const v in A) {
174
+ delete z[v];
175
175
  }
176
176
  };
177
- const G = {
177
+ const F = {
178
178
  get(target, key) {
179
- if (key !== f) {
180
- let h = target[key];
181
- if (h && h === j[key]) {
182
- const C = D[key];
183
- if (C) {
184
- return C.bind(target);
179
+ if (key !== i) {
180
+ let e = target[key];
181
+ if (e && e === k[key]) {
182
+ const B = C[key];
183
+ if (B) {
184
+ return B.bind(target);
185
185
  }
186
- h = h.bind(target);
186
+ e = e.bind(target);
187
187
  }
188
- x(target[f], key !== 'length' ? Symbol.iterator : key);
189
- return h;
188
+ x(target[i], key !== 'length' ? Symbol.iterator : key);
189
+ return e;
190
190
  }
191
191
  return target;
192
192
  },
193
- set(target, key, d) {
194
- if (key !== f) {
195
- if (target[key] === d) {
193
+ set(target, key, f) {
194
+ if (key !== i) {
195
+ if (target[key] === f) {
196
196
  return true;
197
197
  }
198
- const n = target[f];
199
- if (d && typeof d === 'object') {
200
- d = r(d);
198
+ const p = target[i];
199
+ if (f && typeof f === 'object') {
200
+ f = s(f);
201
201
  }
202
- target[key] = d;
202
+ target[key] = f;
203
203
  if (key === 'length') {
204
- l(n, key);
204
+ m(p, key);
205
205
  }
206
- l(n, Symbol.iterator);
206
+ m(p, Symbol.iterator);
207
207
  return true;
208
208
  }
209
- E(target, d);
209
+ D(target, f);
210
210
  return true;
211
211
  },
212
212
  has(target, key) {
213
- x(target[f], !isNaN(parseInt(key)) ? Symbol.iterator : key);
213
+ x(target[i], !isNaN(parseInt(key)) ? Symbol.iterator : key);
214
214
  return key in target;
215
215
  },
216
216
  deleteProperty(target, key) {
@@ -218,137 +218,123 @@ const G = {
218
218
  key = Symbol.iterator;
219
219
  }
220
220
  if (target[key] !== undefined) {
221
- l(target[f], key);
221
+ m(target[i], key);
222
222
  }
223
223
  return delete target[key];
224
224
  },
225
225
  };
226
- const H = {
226
+ const G = {
227
227
  get(target, key) {
228
- if (key !== f) {
229
- const h = target[key];
228
+ if (key !== i) {
229
+ const e = target[key];
230
230
  if (key !== 'id') {
231
- x(target[f], key);
231
+ x(target[i], key);
232
232
  }
233
- return h;
233
+ return e;
234
234
  }
235
235
  return target;
236
236
  },
237
- set(target, key, d, z) {
238
- if (key !== f) {
239
- if (target[key] === d) {
237
+ set(target, key, f, z) {
238
+ if (key !== i) {
239
+ if (target[key] === f) {
240
240
  return true;
241
241
  }
242
- const n = target[f];
243
- if (d && typeof d === 'object') {
244
- d = r(d);
242
+ const p = target[i];
243
+ if (f && typeof f === 'object') {
244
+ f = s(f);
245
245
  }
246
- target[key] = d;
247
- l(n, key);
246
+ target[key] = f;
247
+ m(p, key);
248
248
  return true;
249
249
  }
250
- F(target, z, d);
250
+ E(target, z, f);
251
251
  return true;
252
252
  },
253
253
  has(target, key) {
254
- x(target[f], key);
254
+ x(target[i], key);
255
255
  return key in target;
256
256
  },
257
257
  deleteProperty(target, key) {
258
258
  if (target[key] !== undefined) {
259
- l(target[f], key);
259
+ m(target[i], key);
260
260
  }
261
261
  return delete target[key];
262
262
  },
263
263
  };
264
- const r = (e) => {
264
+ const s = (state) => {
265
265
  let target;
266
- if (!Array.isArray(e)) {
266
+ if (!Array.isArray(state)) {
267
267
  target = {};
268
- for (const key in e) {
269
- const a = e[key];
270
- target[key] = a && typeof a === 'object' ? r(a) : a;
268
+ for (const key in state) {
269
+ const c = state[key];
270
+ target[key] = c && typeof c === 'object' ? s(c) : c;
271
271
  }
272
- e = new Proxy(target, H);
272
+ state = new Proxy(target, G);
273
273
  }
274
274
  else {
275
275
  target = [];
276
- const length = e.length;
276
+ const length = state.length;
277
277
  for (let b = 0; b < length; b++) {
278
- let a = e[b];
279
- if (a && typeof a === 'object') {
278
+ let c = state[b];
279
+ if (c && typeof c === 'object') {
280
280
  do {
281
- target[b] = (a = e[b]) && r(a);
281
+ target[b] = (c = state[b]) && s(c);
282
282
  } while (++b < length);
283
283
  break;
284
284
  }
285
- else if (a !== null && a !== undefined) {
285
+ else if (c !== null && c !== undefined) {
286
286
  do {
287
- target[b] = e[b];
287
+ target[b] = state[b];
288
288
  } while (++b < length);
289
289
  break;
290
290
  }
291
- target[b] = a;
291
+ target[b] = c;
292
292
  }
293
- e = new Proxy(target, G);
293
+ state = new Proxy(target, F);
294
294
  }
295
- target[f] = {};
296
- return e;
295
+ target[i] = {};
296
+ return state;
297
297
  };
298
- const $ = (B, a2) => {
299
- if (a2) {
300
- B.w = true;
298
+ const $ = (a, a1) => {
299
+ if (a1) {
300
+ a.ver = 0;
301
+ a.hook(0, a.state);
301
302
  }
302
303
  else {
303
- B.com.watchMountChange('s', $, B);
304
+ a.com.watchMountChange('s', $, a);
304
305
  }
305
306
  };
306
307
  export const bindData = (data, key) => {
307
308
  return [data, key, data[key]];
308
309
  };
309
- export const useState = (initial, resetOnUnmount) => {
310
- const com = p;
310
+ export const useState = (initial, hook) => {
311
+ const com = o;
311
312
  if (com.id) {
312
- let s = y.get(com);
313
- if (s) {
314
- const e = s.e;
315
- if (!s.w) {
316
- if (typeof initial === 'function') {
317
- t = true;
318
- initial = initial(e);
319
- if (initial !== e) {
320
- e[f] = initial;
321
- }
322
- t = false;
313
+ let t = y.get(com);
314
+ if (t) {
315
+ const { state, hook } = t;
316
+ if (hook) {
317
+ if (!t.ver) {
318
+ com.watchMountChange('s', $, t);
323
319
  }
320
+ u = true;
321
+ hook(++t.ver, state);
322
+ u = false;
324
323
  }
325
- else {
326
- t = true;
327
- if (typeof initial === 'function') {
328
- initial = initial();
329
- }
330
- e[f] = initial;
331
- t = false;
332
- s.w = false;
333
- com.watchMountChange('s', $, s);
334
- }
335
- return e;
324
+ return state;
336
325
  }
337
- if (typeof initial === 'function') {
338
- initial = initial();
326
+ const state = s(initial);
327
+ t = { com, state, ver: 1, hook };
328
+ y.set(com, t);
329
+ if (hook) {
330
+ com.watchMountChange('s', $, t);
331
+ u = true;
332
+ hook(1, state);
333
+ u = false;
339
334
  }
340
- const e = r(initial);
341
- s = { com, e, w: false };
342
- y.set(com, s);
343
- if (resetOnUnmount) {
344
- com.watchMountChange('s', $, s);
345
- }
346
- return e;
347
- }
348
- if (typeof initial === 'function') {
349
- initial = initial();
335
+ return state;
350
336
  }
351
- const e = r(initial);
352
- return e;
337
+ const state = s(initial);
338
+ return state;
353
339
  };
354
340
  //# sourceMappingURL=state.js.map