@thepassle/app-tools 0.9.2 → 0.9.4

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.
@@ -0,0 +1,139 @@
1
+ # Utils
2
+
3
+ ## Install
4
+
5
+ ```
6
+ npm i -S @thepassle/app-tools
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ### `log`
12
+
13
+ You can enable logs via the `setDebug` function, or by setting a `?app-tools-debug=true` query param in the browser url.
14
+
15
+ ```js
16
+ import { log, setDebug, getDebug } from '@thepassle/app-tools/utils/log.js';
17
+
18
+ // Enable logs to show up in the console
19
+ setDebug(true);
20
+
21
+ getDebug(); // true
22
+
23
+ log('foo', { foo: 'bar'});
24
+ ```
25
+
26
+ ### `when`
27
+
28
+ ```js
29
+ import { when } from '@thepassle/app-tools/utils.js';
30
+
31
+ when(true, () => html`only truthy result`);
32
+ when(true,
33
+ () => html`truthy result`,
34
+ () => html`falsy result`
35
+ );
36
+ ```
37
+
38
+ ### `waitUntil`
39
+
40
+ ```js
41
+ import { waitUntil } from '@thepassle/app-tools/utils/async.js';
42
+
43
+ await waitUntil(() => true, {
44
+ timeout: 1000,
45
+ message: 'waitUntil timed out',
46
+ interval: 50
47
+ });
48
+ ```
49
+
50
+ ### `setAbortableTimeout`
51
+
52
+ ```js
53
+ import { setAbortableTimeout } from '@thepassle/app-tools/utils/async.js';
54
+
55
+ const controller = new AbortController();
56
+ const { signal } = controller;
57
+
58
+ setAbortableTimeout(
59
+ () => { console.log(1); },
60
+ 2000,
61
+ { signal }
62
+ );
63
+
64
+ controller.abort();
65
+ ```
66
+
67
+ ### `debounce`
68
+
69
+ ```js
70
+ import { debounce } from '@thepassle/app-tools/utils/async.js';
71
+
72
+ function foo() {
73
+ console.log(1);
74
+ }
75
+
76
+ const debouncedFoo = debounce(foo);
77
+
78
+ debouncedFoo();
79
+ debouncedFoo();
80
+ debouncedFoo();
81
+
82
+ // console.log only called once
83
+ ```
84
+
85
+ ### `media`
86
+
87
+ ```js
88
+ import { media } from '@thepassle/app-tools/utils/media.js'
89
+
90
+ media.DARK_MODE(); // true
91
+
92
+ // Or provide a callback that fires whenever the mediaquery changes
93
+ // If you provide a callback, the function returns a cleanup function
94
+ const cleanup = media.DARK_MODE((matches) => {
95
+ console.log(matches); // true
96
+ });
97
+ // Removes the listener
98
+ cleanup();
99
+
100
+ // If you don't provide a callback, the function just returns the `matches` boolean
101
+ media.MIN.XL();
102
+ media.MAX.LG();
103
+ media.MIN.MD();
104
+ media.MAX.SM();
105
+ media.MIN.XS(); // etc
106
+
107
+ media.STANDALONE();
108
+ media.REDUCED_MOTION();
109
+ media.LIGHT_MODE();
110
+ ```
111
+
112
+ ### `Service`
113
+
114
+ ```js
115
+ import { createService } from '@thepassle/app-tools/utils/Service.js';
116
+ import { html, nothing } from 'lit';
117
+
118
+ const Service = createService({
119
+ initialized: () => nothing,
120
+ pending: () => html`<app-spinner></app-spinner>`,
121
+ success: () => nothing,
122
+ error: (e) => html`<app-error>${e}</app-error>`,
123
+ });
124
+
125
+ class MyApp extends LitElement {
126
+ foo = new Service(this, () => api.get('/foo'));
127
+
128
+ connectedCallback() {
129
+ super.connectedCallback();
130
+ this.foo.request();
131
+ }
132
+
133
+ render() {
134
+ return this.foo.render({
135
+ success: (data) => html`<h1>${data.name}</h2>`
136
+ });
137
+ }
138
+ }
139
+ ```
package/utils/log.js CHANGED
@@ -2,7 +2,7 @@ const KEY = Symbol.for('app-tools::log::1.x');
2
2
 
3
3
  globalThis[KEY] = {
4
4
  setDebug,
5
- debug: new URL(window.location.href).searchParams.has('app-tools-debug')
5
+ debug: 'window' in globalThis ? new URL(window.location.href).searchParams.has('app-tools-debug') : false,
6
6
  };
7
7
 
8
8
  /**