@e280/stz 0.0.0-30 → 0.0.0-31
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/README.md +140 -25
- package/package.json +1 -1
- package/s/deadline.ts +4 -6
- package/x/deadline.d.ts +2 -2
- package/x/deadline.js +4 -4
- package/x/deadline.js.map +1 -1
package/README.md
CHANGED
|
@@ -1,38 +1,81 @@
|
|
|
1
1
|
|
|
2
2
|
# `@e280/stz`
|
|
3
|
-
|
|
4
|
-
-
|
|
3
|
+
|
|
4
|
+
**stz** is e280's standard library of environment-agnostic typescript tools.
|
|
5
|
+
|
|
6
|
+
it's our javascript toolkit.
|
|
7
|
+
|
|
8
|
+
zero dependencies.
|
|
5
9
|
|
|
6
10
|
<br/>
|
|
7
11
|
|
|
8
|
-
##
|
|
9
|
-
|
|
10
|
-
- `map.require`
|
|
11
|
-
```ts
|
|
12
|
-
import {MapG} from "@e280/stz"
|
|
12
|
+
## 🧰 STZ PRIMITIVES
|
|
13
|
+
> cool concepts we use all over the place
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
### 🍏 MapG
|
|
16
|
+
> extended js map
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import {MapG} from "@e280/stz"
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
const map = new MapG<number, string>([
|
|
22
|
+
[1, "hello"],
|
|
23
|
+
[2, "world"],
|
|
24
|
+
])
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- `map.require(key)` — throws error if the value is undefined
|
|
28
|
+
```ts
|
|
20
29
|
const value = map.require(1)
|
|
30
|
+
// "hello"
|
|
21
31
|
```
|
|
22
|
-
- `map.guarantee`
|
|
32
|
+
- `map.guarantee(key, make)` — returns the value for `key`, but if undefined, run `make` to set the value
|
|
23
33
|
```ts
|
|
24
|
-
// if the value is undefined, the new value "rofl" is set and returned
|
|
25
34
|
const value = map.guarantee(3, () => "rofl")
|
|
35
|
+
// "rofl"
|
|
26
36
|
```
|
|
27
37
|
|
|
28
|
-
|
|
38
|
+
### nap
|
|
39
|
+
> sleep for some milliseconds
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import {nap} from "@e280/stz"
|
|
43
|
+
|
|
44
|
+
await nap(900)
|
|
45
|
+
// wait for 900 milliseconds
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### defer
|
|
49
|
+
> defer the resolve/reject of a promise to the outside
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import {defer} from "@e280/stz"
|
|
53
|
+
|
|
54
|
+
const deferred = defer()
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- resolve the deferred promise
|
|
58
|
+
```ts
|
|
59
|
+
deferred.resolve()
|
|
60
|
+
```
|
|
61
|
+
- reject the deferred promise
|
|
62
|
+
```ts
|
|
63
|
+
deferred.reject(new Error("fail"))
|
|
64
|
+
```
|
|
65
|
+
- await the promise
|
|
66
|
+
```ts
|
|
67
|
+
await deferred.promise
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 🍏 `pub` and `sub`
|
|
71
|
+
> ergonomic event emitters
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import {pub, sub} from "@e280/stz"
|
|
75
|
+
```
|
|
29
76
|
|
|
30
|
-
## pub and sub
|
|
31
|
-
### ergonomic event emitters
|
|
32
77
|
- make a publisher fn
|
|
33
78
|
```ts
|
|
34
|
-
import {pub} from "@e280/stz"
|
|
35
|
-
|
|
36
79
|
// create a pub fn
|
|
37
80
|
const sendMessage = pub<[string]>()
|
|
38
81
|
|
|
@@ -44,8 +87,6 @@ standard library of environment-agnostic typescript functions we use basically e
|
|
|
44
87
|
```
|
|
45
88
|
- make a subscriber fn *(see how it's just the reverse of pub?)*
|
|
46
89
|
```ts
|
|
47
|
-
import {sub} from "@e280/stz"
|
|
48
|
-
|
|
49
90
|
// create a sub fn
|
|
50
91
|
const onMessage = sub<[string]>()
|
|
51
92
|
|
|
@@ -81,10 +122,82 @@ standard library of environment-agnostic typescript functions we use basically e
|
|
|
81
122
|
|
|
82
123
|
<br/>
|
|
83
124
|
|
|
84
|
-
##
|
|
85
|
-
>
|
|
125
|
+
## 🧰 FN TOOLS
|
|
126
|
+
> function-oriented tools
|
|
127
|
+
|
|
128
|
+
### 🍏 `queue(fn)`
|
|
129
|
+
> execute calls in sequence (not concurrent)
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import {queue, nap} from "@e280/stz"
|
|
133
|
+
|
|
134
|
+
const fn = queue(async() => nap(100))
|
|
135
|
+
|
|
136
|
+
fn()
|
|
137
|
+
fn()
|
|
138
|
+
await fn() // waits for the previous calls (sequentially)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 🍏 `once(fn)`
|
|
142
|
+
> ensure a fn is only executed one time
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import {once} from "@e280/stz"
|
|
146
|
+
|
|
147
|
+
let count = 0
|
|
148
|
+
const fn = once(() => count++)
|
|
149
|
+
console.log(count) // 0
|
|
150
|
+
|
|
151
|
+
fn()
|
|
152
|
+
console.log(count) // 1
|
|
153
|
+
|
|
154
|
+
fn()
|
|
155
|
+
console.log(count) // 1
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### 🍏 `deadline(100, message, fn)`
|
|
159
|
+
> throws an error if the async function takes too long
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
import {deadline} from "@e280/stz"
|
|
163
|
+
|
|
164
|
+
const fn = deadline(100, "deadline exceeded", async() => {
|
|
165
|
+
|
|
166
|
+
// example deliberately takes too long
|
|
167
|
+
await nap(200)
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
await fn()
|
|
171
|
+
// DeadlineError: deadline exceeded, timed out in 0.1 seconds
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### 🍏 `repeat(fn)`
|
|
175
|
+
> execute a function over and over again, back to back
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
import {repeat} from "@e280/stz"
|
|
179
|
+
|
|
180
|
+
let ticks = 0
|
|
181
|
+
|
|
182
|
+
const stop = repeat(async() => {
|
|
183
|
+
|
|
184
|
+
// use a nap to add a delay between each execution
|
|
185
|
+
await nap(200)
|
|
186
|
+
|
|
187
|
+
ticks++
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
// stop repeating whenever you want
|
|
191
|
+
stop()
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
<br/>
|
|
195
|
+
|
|
196
|
+
## 🧰 DATA UTILITIES
|
|
197
|
+
> transforming and representing binary data
|
|
86
198
|
|
|
87
|
-
### BaseX
|
|
199
|
+
### 🍏 BaseX
|
|
200
|
+
> represent data in arbitrary encodings
|
|
88
201
|
- make a BaseX instance
|
|
89
202
|
```ts
|
|
90
203
|
import {BaseX} from "@e280/stz"
|
|
@@ -130,7 +243,7 @@ standard library of environment-agnostic typescript functions we use basically e
|
|
|
130
243
|
|
|
131
244
|
<br/>
|
|
132
245
|
|
|
133
|
-
|
|
246
|
+
### 🍏 Bytename
|
|
134
247
|
> friendly string encoding for binary data
|
|
135
248
|
|
|
136
249
|
a bytename looks like `"midsen.picmyn.widrep.baclut dotreg.filtyp.nosnus.siptev"`. that's 16 bytes. each byte maps to a three-letter triplet
|
|
@@ -164,6 +277,8 @@ import {Bytename} from "@e280/stz"
|
|
|
164
277
|
|
|
165
278
|
<br/>
|
|
166
279
|
|
|
280
|
+
<br/>
|
|
281
|
+
|
|
167
282
|
## 💖 stz is made with open source love
|
|
168
283
|
reward us with github stars
|
|
169
284
|
build with us at https://e280.org/ but only if you're cool
|
package/package.json
CHANGED
package/s/deadline.ts
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
|
|
2
2
|
export class DeadlineError extends Error {
|
|
3
3
|
name = this.constructor.name
|
|
4
|
-
constructor(public milliseconds: number
|
|
5
|
-
super(
|
|
4
|
+
constructor(public milliseconds: number) {
|
|
5
|
+
super(`deadline exceeded (${(milliseconds / 1000).toFixed(1)} seconds)`)
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
/** set a deadline for a fn to do something, will reject with a `DeadlineError` if it takes too long */
|
|
10
|
-
export function deadline<R>(milliseconds: number,
|
|
10
|
+
export function deadline<R>(milliseconds: number, fn: () => Promise<R>) {
|
|
11
11
|
if (milliseconds <= 0 || milliseconds === Infinity)
|
|
12
12
|
return fn()
|
|
13
13
|
|
|
14
14
|
return new Promise<R>((resolve, reject) => {
|
|
15
|
-
|
|
16
15
|
const id = setTimeout(
|
|
17
|
-
() => reject(new DeadlineError(milliseconds
|
|
16
|
+
() => reject(new DeadlineError(milliseconds)),
|
|
18
17
|
milliseconds,
|
|
19
18
|
)
|
|
20
|
-
|
|
21
19
|
fn()
|
|
22
20
|
.then(resolve)
|
|
23
21
|
.catch(reject)
|
package/x/deadline.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export declare class DeadlineError extends Error {
|
|
2
2
|
milliseconds: number;
|
|
3
3
|
name: string;
|
|
4
|
-
constructor(milliseconds: number
|
|
4
|
+
constructor(milliseconds: number);
|
|
5
5
|
}
|
|
6
6
|
/** set a deadline for a fn to do something, will reject with a `DeadlineError` if it takes too long */
|
|
7
|
-
export declare function deadline<R>(milliseconds: number,
|
|
7
|
+
export declare function deadline<R>(milliseconds: number, fn: () => Promise<R>): Promise<R>;
|
package/x/deadline.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
export class DeadlineError extends Error {
|
|
2
2
|
milliseconds;
|
|
3
3
|
name = this.constructor.name;
|
|
4
|
-
constructor(milliseconds
|
|
5
|
-
super(
|
|
4
|
+
constructor(milliseconds) {
|
|
5
|
+
super(`deadline exceeded (${(milliseconds / 1000).toFixed(1)} seconds)`);
|
|
6
6
|
this.milliseconds = milliseconds;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
/** set a deadline for a fn to do something, will reject with a `DeadlineError` if it takes too long */
|
|
10
|
-
export function deadline(milliseconds,
|
|
10
|
+
export function deadline(milliseconds, fn) {
|
|
11
11
|
if (milliseconds <= 0 || milliseconds === Infinity)
|
|
12
12
|
return fn();
|
|
13
13
|
return new Promise((resolve, reject) => {
|
|
14
|
-
const id = setTimeout(() => reject(new DeadlineError(milliseconds
|
|
14
|
+
const id = setTimeout(() => reject(new DeadlineError(milliseconds)), milliseconds);
|
|
15
15
|
fn()
|
|
16
16
|
.then(resolve)
|
|
17
17
|
.catch(reject)
|
package/x/deadline.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deadline.js","sourceRoot":"","sources":["../s/deadline.ts"],"names":[],"mappings":"AACA,MAAM,OAAO,aAAc,SAAQ,KAAK;IAEpB;IADnB,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA;IAC5B,YAAmB,YAAoB
|
|
1
|
+
{"version":3,"file":"deadline.js","sourceRoot":"","sources":["../s/deadline.ts"],"names":[],"mappings":"AACA,MAAM,OAAO,aAAc,SAAQ,KAAK;IAEpB;IADnB,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA;IAC5B,YAAmB,YAAoB;QACtC,KAAK,CAAC,sBAAsB,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAA;QADtD,iBAAY,GAAZ,YAAY,CAAQ;IAEvC,CAAC;CACD;AAED,uGAAuG;AACvG,MAAM,UAAU,QAAQ,CAAI,YAAoB,EAAE,EAAoB;IACrE,IAAI,YAAY,IAAI,CAAC,IAAI,YAAY,KAAK,QAAQ;QACjD,OAAO,EAAE,EAAE,CAAA;IAEZ,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACzC,MAAM,EAAE,GAAG,UAAU,CACpB,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,aAAa,CAAC,YAAY,CAAC,CAAC,EAC7C,YAAY,CACZ,CAAA;QACD,EAAE,EAAE;aACF,IAAI,CAAC,OAAO,CAAC;aACb,KAAK,CAAC,MAAM,CAAC;aACb,OAAO,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,CAAA;IAClC,CAAC,CAAC,CAAA;AACH,CAAC"}
|