@e280/stz 0.2.0-1 → 0.2.0-2
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 +95 -66
- package/package.json +1 -1
- package/s/pubsub.ts +3 -2
- package/x/pubsub.js +4 -2
- package/x/pubsub.js.map +1 -1
package/README.md
CHANGED
|
@@ -9,40 +9,77 @@ zero dependencies.
|
|
|
9
9
|
|
|
10
10
|
<br/>
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## 🥨 stz primitives
|
|
13
13
|
|
|
14
|
-
### 🍏
|
|
15
|
-
>
|
|
14
|
+
### 🍏 `pub` and `sub`
|
|
15
|
+
> ergonomic event emitters
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
const map = new MapG<number, string>([
|
|
21
|
-
[1, "hello"],
|
|
22
|
-
[2, "world"],
|
|
23
|
-
])
|
|
18
|
+
import {pub, sub} from "@e280/stz"
|
|
24
19
|
```
|
|
25
20
|
|
|
26
|
-
|
|
21
|
+
#### `pub`
|
|
22
|
+
- make a publisher fn
|
|
27
23
|
```ts
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
// create a pub fn
|
|
25
|
+
const sendMessage = pub<[string]>()
|
|
26
|
+
|
|
27
|
+
// subscribe to it
|
|
28
|
+
sendMessage.subscribe(m => console.log(m))
|
|
29
|
+
|
|
30
|
+
// publish to it
|
|
31
|
+
sendMessage("hello")
|
|
30
32
|
```
|
|
31
|
-
|
|
33
|
+
|
|
34
|
+
#### `sub`
|
|
35
|
+
- make a subscriber fn — *it's just like pub, except it's flipsy-reversey!*
|
|
32
36
|
```ts
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
```
|
|
37
|
+
// create a sub fn
|
|
38
|
+
const onMessage = sub<[string]>()
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
// subscribe to it
|
|
41
|
+
onMessage(m => console.log(m))
|
|
39
42
|
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
// publish to it
|
|
44
|
+
onMessage.publish("hello")
|
|
45
|
+
```
|
|
42
46
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
#### pub vs sub
|
|
48
|
+
- pub and sub both have the same facilities
|
|
49
|
+
- `.publish`
|
|
50
|
+
- `.subscribe`
|
|
51
|
+
- `.on`
|
|
52
|
+
- `.next`
|
|
53
|
+
- `.clear`
|
|
54
|
+
- i seem to use `sub` more often
|
|
55
|
+
|
|
56
|
+
#### the more you know, about pubsub
|
|
57
|
+
- publish actually returns a promise, to wait for all async subscribers
|
|
58
|
+
```ts
|
|
59
|
+
await onMessage.publish("hello")
|
|
60
|
+
```
|
|
61
|
+
- subscribe returns a fn to unsubscribe
|
|
62
|
+
```ts
|
|
63
|
+
const unsubscribe = onMessage(() => {})
|
|
64
|
+
unsubscribe()
|
|
65
|
+
```
|
|
66
|
+
- `.clear()` to wipe all subscribed listeners
|
|
67
|
+
```ts
|
|
68
|
+
onMessage.clear()
|
|
69
|
+
```
|
|
70
|
+
- `.next(fn?)` is a better way to do .once..
|
|
71
|
+
- you can use it like a .once:
|
|
72
|
+
```ts
|
|
73
|
+
onMessage.next(message => {})
|
|
74
|
+
```
|
|
75
|
+
- but it also gives you a promise like this:
|
|
76
|
+
```ts
|
|
77
|
+
const [message] = await onMessage.next()
|
|
78
|
+
```
|
|
79
|
+
- of course the promise can be used like this:
|
|
80
|
+
```ts
|
|
81
|
+
onMessage.next().then(([message]) => {})
|
|
82
|
+
```
|
|
46
83
|
|
|
47
84
|
### 🍏 defer
|
|
48
85
|
> defer the resolve/reject of a promise to the outside
|
|
@@ -66,62 +103,54 @@ const deferred = defer()
|
|
|
66
103
|
await deferred.promise
|
|
67
104
|
```
|
|
68
105
|
|
|
69
|
-
### 🍏
|
|
70
|
-
>
|
|
106
|
+
### 🍏 nap
|
|
107
|
+
> sleep for some milliseconds
|
|
71
108
|
|
|
72
109
|
```ts
|
|
73
|
-
import {
|
|
110
|
+
import {nap} from "@e280/stz"
|
|
111
|
+
|
|
112
|
+
await nap(900)
|
|
113
|
+
// wait for 900 milliseconds
|
|
74
114
|
```
|
|
75
115
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
// create a pub fn
|
|
79
|
-
const sendMessage = pub<[string]>()
|
|
116
|
+
### 🍏 MapG
|
|
117
|
+
> extended js Map
|
|
80
118
|
|
|
81
|
-
|
|
82
|
-
|
|
119
|
+
- many say it's *"The Deluxe Mapping Experience"*
|
|
120
|
+
```ts
|
|
121
|
+
import {MapG} from "@e280/stz"
|
|
83
122
|
|
|
84
|
-
|
|
85
|
-
|
|
123
|
+
const map = new MapG<number, string>([
|
|
124
|
+
[1, "hello"],
|
|
125
|
+
[2, "world"],
|
|
126
|
+
])
|
|
86
127
|
```
|
|
87
|
-
-
|
|
128
|
+
- `map.require(key)` — returns the value for key.. if missing, throw an error
|
|
88
129
|
```ts
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
// subscribe to it
|
|
93
|
-
onMessage(m => console.log(m))
|
|
94
|
-
|
|
95
|
-
// publish to it
|
|
96
|
-
onMessage.pub("hello")
|
|
130
|
+
const value = map.require(1)
|
|
131
|
+
// "hello"
|
|
97
132
|
```
|
|
98
|
-
- the
|
|
99
|
-
- i seem to use `sub` more often
|
|
100
|
-
- both have some extra functionality
|
|
133
|
+
- `map.guarantee(key, makeFn)` — returns the value for `key`.. if missing, run `makeFn` to set and return the value
|
|
101
134
|
```ts
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
// sub fns return an unsub fn
|
|
107
|
-
const unsub1 = onMessage(m => console.log(m))
|
|
108
|
-
unsub1() // unsubscribe that listener
|
|
109
|
-
|
|
110
|
-
const unsub2 = sendMessage.sub(m => console.log(m))
|
|
111
|
-
unsub2() // unsubscribe that listener
|
|
135
|
+
const value = map.guarantee(3, () => "rofl")
|
|
136
|
+
// "rofl"
|
|
137
|
+
```
|
|
112
138
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
139
|
+
### 🍏 SetG
|
|
140
|
+
> extended js Set
|
|
141
|
+
- `new SetG<T>()`
|
|
142
|
+
- `set.adds(item1, item2, item3)` — add multiple items without a for-loop
|
|
143
|
+
- `set.deletes(item1, item2, item3)` — add multiple items without a for-loop
|
|
116
144
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
145
|
+
### 🍏 WeakMapG
|
|
146
|
+
> extended js WeakMap
|
|
147
|
+
- `new WeakMapG<K, V>()`
|
|
148
|
+
- `weakMap.require(key)` — returns value for key.. if missing, throw an error
|
|
149
|
+
- `weakMap.guarantee(key, makeFn)` — returns the value for key.. if missing, run `makeFn` to set and return the value
|
|
121
150
|
|
|
122
151
|
<br/>
|
|
123
152
|
|
|
124
|
-
##
|
|
153
|
+
## 🥨 stz fn tools
|
|
125
154
|
|
|
126
155
|
### 🍏 `queue(fn)`
|
|
127
156
|
> execute calls in sequence (not concurrent)
|
|
@@ -211,7 +240,7 @@ stop()
|
|
|
211
240
|
|
|
212
241
|
<br/>
|
|
213
242
|
|
|
214
|
-
##
|
|
243
|
+
## 🥨 stz data utilities
|
|
215
244
|
|
|
216
245
|
### 🍏 Hex
|
|
217
246
|
> convert to/from hexadecimal string format
|
|
@@ -330,7 +359,7 @@ import {Bytename} from "@e280/stz"
|
|
|
330
359
|
|
|
331
360
|
<br/>
|
|
332
361
|
|
|
333
|
-
## 💖 stz is
|
|
362
|
+
## 💖 stz is by e280
|
|
334
363
|
reward us with github stars
|
|
335
364
|
build with us at https://e280.org/ but only if you're cool
|
|
336
365
|
|
package/package.json
CHANGED
package/s/pubsub.ts
CHANGED
|
@@ -61,9 +61,10 @@ export function xub<A extends any[] = []>() {
|
|
|
61
61
|
return subscribe(fn)
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
async function next() {
|
|
64
|
+
async function next(fn?: Listener<A>) {
|
|
65
65
|
const {promise, resolve} = defer<A>()
|
|
66
|
-
const unsubscribe = sub((...a) => {
|
|
66
|
+
const unsubscribe = sub(async(...a) => {
|
|
67
|
+
if (fn) await fn(...a)
|
|
67
68
|
resolve(a)
|
|
68
69
|
unsubscribe()
|
|
69
70
|
})
|
package/x/pubsub.js
CHANGED
|
@@ -15,9 +15,11 @@ export function xub() {
|
|
|
15
15
|
function sub(fn) {
|
|
16
16
|
return subscribe(fn);
|
|
17
17
|
}
|
|
18
|
-
async function next() {
|
|
18
|
+
async function next(fn) {
|
|
19
19
|
const { promise, resolve } = defer();
|
|
20
|
-
const unsubscribe = sub((...a) => {
|
|
20
|
+
const unsubscribe = sub(async (...a) => {
|
|
21
|
+
if (fn)
|
|
22
|
+
await fn(...a);
|
|
21
23
|
resolve(a);
|
|
22
24
|
unsubscribe();
|
|
23
25
|
});
|
package/x/pubsub.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pubsub.js","sourceRoot":"","sources":["../s/pubsub.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAA;AAyChC,6BAA6B;AAC7B,MAAM,UAAU,GAAG;IAClB,MAAM,GAAG,GAAG,IAAI,GAAG,EAAe,CAAA;IAElC,KAAK,UAAU,OAAO,CAAC,GAAG,CAAI;QAC7B,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAChD,CAAC;IAED,SAAS,SAAS,CAAC,EAAe;QACjC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;QACX,OAAO,GAAG,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA,CAAC,CAAC,CAAA;IAChC,CAAC;IAED,KAAK,UAAU,GAAG,CAAC,GAAG,CAAI;QACzB,OAAO,OAAO,CAAC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