@transitive-sdk/utils-web 0.7.5 → 0.7.6

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 ADDED
@@ -0,0 +1,11 @@
1
+ <p align="center">
2
+ <a href="https://transitiverobotics.com">
3
+ <img src="https://transitiverobotics.com/img/logo.svg" style="height: 64px">
4
+ </a>
5
+ </p>
6
+
7
+ ## Transitive Web Utils
8
+
9
+ Utilities used by the web components of [Transitive Robotics](https://transitiverobotics.com) capabilities.
10
+
11
+ [API Documentation](./docs/index.md)
package/client.js CHANGED
@@ -18,7 +18,7 @@ export const parseCookie = str =>
18
18
  return acc;
19
19
  }, {});
20
20
 
21
- /* get or post (if body given) json */
21
+ /** get or post (if body given) json */
22
22
  export const fetchJson = (url, callback, options = {}) => {
23
23
  fetch(url, {
24
24
  method: options.method || (options.body ? 'post' : 'get'),
package/docs/index.md ADDED
@@ -0,0 +1,708 @@
1
+ <!-- Generated by documentation.js. Update this documentation by updating the source code. -->
2
+
3
+ ## DataCache
4
+
5
+ A class implementing a local data cache, used as a local data store with
6
+ deduplication detection and update events.
7
+
8
+ #### Parameters
9
+
10
+ * `data` (optional, default `{}`)
11
+
12
+ ### filter
13
+
14
+ filter the object using path with wildcards
15
+
16
+ ##### Parameters
17
+
18
+ * `path` &#x20;
19
+
20
+ ### filterByTopic
21
+
22
+ filter the object using topic with wildcards
23
+
24
+ ##### Parameters
25
+
26
+ * `topic` &#x20;
27
+
28
+ ### forMatch
29
+
30
+ for each topic match, invoke the callback with the value, topic, and match
31
+ just like subscribePath
32
+
33
+ ##### Parameters
34
+
35
+ * `topic` &#x20;
36
+ * `callback` &#x20;
37
+
38
+ ### forPathMatch
39
+
40
+ for each path match, invoke the callback with the value, topic, and match
41
+ just like subscribePath
42
+
43
+ ##### Parameters
44
+
45
+ * `path` &#x20;
46
+ * `callback` &#x20;
47
+
48
+ ### get
49
+
50
+ get sub-value at path, or entire object if none given
51
+
52
+ ##### Parameters
53
+
54
+ * `path` (optional, default `[]`)
55
+
56
+ ### subscribe
57
+
58
+ add a callback for change events
59
+
60
+ ##### Parameters
61
+
62
+ * `callback` &#x20;
63
+
64
+ ### subscribePath
65
+
66
+ Subscribe to a specific topic only. Unlike in `subscribe`, here callback
67
+ only receives the value.
68
+
69
+ ##### Parameters
70
+
71
+ * `topic` &#x20;
72
+ * `callback` &#x20;
73
+
74
+ ### subscribePathFlat
75
+
76
+ Same as subscribePath but always get all changes in flat form
77
+
78
+ ##### Parameters
79
+
80
+ * `topic` &#x20;
81
+ * `callback` &#x20;
82
+
83
+ ### unsubscribe
84
+
85
+ remove a callback
86
+
87
+ ##### Parameters
88
+
89
+ * `callback` &#x20;
90
+
91
+ ### update
92
+
93
+ update the value at the given path (array or dot separated string)
94
+
95
+ ##### Parameters
96
+
97
+ * `path` &#x20;
98
+ * `value` &#x20;
99
+ * `tags` &#x20;
100
+
101
+ ### updateFromArray
102
+
103
+ update the object with the given value at the given path, remove empty;
104
+ return the flat changes (see toFlatObject). Add `tags` to updates to mark
105
+ them somehow based on the context, e.g., so that some subscriptions can choose
106
+ to ignore updates with a certain tag.
107
+
108
+ ##### Parameters
109
+
110
+ * `path` &#x20;
111
+ * `value` &#x20;
112
+ * `tags` (optional, default `{}`)
113
+
114
+ ### updateFromModifier
115
+
116
+ update data from a modifier object where keys are topic names to be
117
+ interpreted as paths, and values are the values to set
118
+
119
+ ##### Parameters
120
+
121
+ * `modifier` &#x20;
122
+ * `tags` &#x20;
123
+
124
+ ### updateFromTopic
125
+
126
+ set value from the given topic (with or without leading or trailing slash)
127
+
128
+ ##### Parameters
129
+
130
+ * `topic` &#x20;
131
+ * `value` &#x20;
132
+ * `tags` &#x20;
133
+
134
+ ## ErrorBoundary
135
+
136
+ **Extends React.Component**
137
+
138
+ A simple error boundary. Usage: <ErrorBoundary message="Something went wrong"> <SomeFlakyComponent /> </ErrorBoundary>
139
+
140
+ #### Parameters
141
+
142
+ * `props` &#x20;
143
+
144
+ ## MqttSync
145
+
146
+ A class that combines DataCache and MQTT to implement a data synchronization
147
+ feature over the latter. Relies on retained messages in mqtt for persistence.
148
+
149
+ #### Parameters
150
+
151
+ * `$0` **[Object][1]**&#x20;
152
+
153
+ * `$0.mqttClient` &#x20;
154
+ * `$0.onChange` &#x20;
155
+ * `$0.ignoreRetain` &#x20;
156
+ * `$0.migrate` &#x20;
157
+ * `$0.onReady` &#x20;
158
+ * `$0.sliceTopic` &#x20;
159
+
160
+ ### beforeDisconnect
161
+
162
+ Run all registered hooks before disconnecting
163
+
164
+ ### clear
165
+
166
+ Delete all retained messages in a certain topic prefix, waiting for
167
+ a mqtt broker heartbeat to collect existing retained. Use with care, never
168
+ delete topics not owned by us. Harmless within capabilities, which are
169
+ namespaced already.
170
+
171
+ `options.filter(topic)`: a function that can be provided to further,
172
+ programmatically filter the set of topics to clear, e.g., to onlt clear
173
+ topics of old versions.
174
+
175
+ Note: This may not yet work in robot-capabilities, since the subscription
176
+ prefix and received topic prefix don't match (the device prefix is added to
177
+ subscription by localMQTT.
178
+
179
+ ##### Parameters
180
+
181
+ * `prefixes` &#x20;
182
+ * `callback` (optional, default `undefined`)
183
+ * `options` (optional, default `{}`)
184
+
185
+ ### clearThrottle
186
+
187
+ clear the set throttling delay
188
+
189
+ ### isPublished
190
+
191
+ Check whether we are publishing the given topic in a non-atomic way.
192
+ This is used to determine whether to store the published value or not.
193
+
194
+ ##### Parameters
195
+
196
+ * `topic` &#x20;
197
+
198
+ ### isSubscribed
199
+
200
+ check whether we are subscribed to the given topic
201
+
202
+ ##### Parameters
203
+
204
+ * `topic` &#x20;
205
+
206
+ ### migrate
207
+
208
+ Migrate a list of {topic, newVersion, transform}. The version number in
209
+ topic will be ignored, and all versions' values will be merged, applied in
210
+ order, such that the latest version is applied last.
211
+
212
+ ##### Parameters
213
+
214
+ * `list` &#x20;
215
+ * `onReady` (optional, default `undefined`)
216
+
217
+ ### onBeforeDisconnect
218
+
219
+ register a new hook to be called before disconnecting
220
+
221
+ ##### Parameters
222
+
223
+ * `fn` &#x20;
224
+
225
+ ### publish
226
+
227
+ Register a listener for path in data. Make sure to populate the data
228
+ before calling this or set the data all at once afterwards.
229
+
230
+ With option "atomic" this will always send the whole sub-document,
231
+ not flat changes. Useful, e.g., for desiredPackages, see
232
+ [https://github.com/chfritz/transitive/issues/85][2].
233
+
234
+ ##### Parameters
235
+
236
+ * `topic` &#x20;
237
+ * `options` (optional, default `{atomic:false}`)
238
+
239
+ Returns **any** true if publication added (false, e.g., when already present)
240
+
241
+ ### publishAtLevel
242
+
243
+ Publish all values at the given level of the given object under the given
244
+ topic (plus sub-key, of course).
245
+ TODO: Is this OK, or do we need to go through this.publish?
246
+
247
+ ##### Parameters
248
+
249
+ * `topic` &#x20;
250
+ * `value` &#x20;
251
+ * `level` &#x20;
252
+
253
+ ### setThrottle
254
+
255
+ set min delay between processing of queue
256
+
257
+ ##### Parameters
258
+
259
+ * `delay` &#x20;
260
+
261
+ ### subscribe
262
+
263
+ Subscribe to the given topic (and all sub-topics). The callback will
264
+ indicate success/failure, *not* a message on the topic.
265
+
266
+ ##### Parameters
267
+
268
+ * `topic` &#x20;
269
+ * `callback` (optional, default `noop`)
270
+
271
+ ### waitForHeartbeatOnce
272
+
273
+ register a callback for the next heartbeat from the broker
274
+
275
+ ##### Parameters
276
+
277
+ * `callback` &#x20;
278
+
279
+ ### heartbeatWaitersOnce
280
+
281
+ List of callbacks waiting for next heartbeat, gets purged with each
282
+ heartbeat
283
+
284
+ ### publishedMessages
285
+
286
+ Store messages retained on mqtt so we can publish what is necessary to
287
+ achieve the "should-be" state. Note that we cannot use a structured document
288
+ for storing these publishedMessages since we need to be able to store separate
289
+ values at non-leaf nodes in the object (just like mqtt, where you can have
290
+ /a/b = 1 and /a/b/c = 1 at the same time). Note: not used in atomic mode.
291
+
292
+ ### publishQueue
293
+
294
+ The order in which we send retained messages matters, which is why we use
295
+ a queue for sending things. Note that we here use the property of Map that it
296
+ remembers insertion order of keys.
297
+
298
+ ### subscribedPaths
299
+
300
+ Directory of paths we've subscribed to in this class; this matters
301
+ because the same mqtt client may have subscriptions to paths that we don't
302
+ care to store (sync).
303
+
304
+ ## clone
305
+
306
+ clone a mqtt payload, if necessary
307
+
308
+ #### Parameters
309
+
310
+ * `payload` &#x20;
311
+
312
+ ## clone
313
+
314
+ Deep-clone the given object. All functionality is lost, just data is kept.
315
+
316
+ #### Parameters
317
+
318
+ * `obj` &#x20;
319
+
320
+ ## Code
321
+
322
+ reusable component for showing code
323
+
324
+ #### Parameters
325
+
326
+ * `$0` **[Object][1]**&#x20;
327
+
328
+ * `$0.children` &#x20;
329
+
330
+ ## componentPermitsRefs
331
+
332
+ whether or not the given react component allows refs, i.e., is either
333
+ a functional component wrapped with forwardRef or a class component
334
+
335
+ #### Parameters
336
+
337
+ * `Component` &#x20;
338
+
339
+ ## createWebComponent
340
+
341
+ Create a WebComponent from the given react component and name that is
342
+ reactive to the given attributes (if any).
343
+
344
+ #### Parameters
345
+
346
+ * `Component` &#x20;
347
+ * `name` &#x20;
348
+ * `reactiveAttributes` (optional, default `[]`)
349
+ * `version` (optional, default `'0.0.0'`)
350
+ * `options` (optional, default `{}`)
351
+
352
+ ## dropWildcardIds
353
+
354
+ reduce wildcards with Ids, such as `+sessionId`, to just +
355
+
356
+ #### Parameters
357
+
358
+ * `x` &#x20;
359
+
360
+ ## ensureHashSuffix
361
+
362
+ return new string that ends in /# for sure
363
+
364
+ #### Parameters
365
+
366
+ * `topic` &#x20;
367
+
368
+ ## fetchJson
369
+
370
+ get or post (if body given) json
371
+
372
+ #### Parameters
373
+
374
+ * `url` &#x20;
375
+ * `callback` &#x20;
376
+ * `options` (optional, default `{}`)
377
+
378
+ ## forMatchIterator
379
+
380
+ Iterate through the object and invoke callback for each match of path (with
381
+ named wildcards)
382
+
383
+ #### Parameters
384
+
385
+ * `obj` &#x20;
386
+ * `path` &#x20;
387
+ * `callback` &#x20;
388
+ * `pathSoFar` (optional, default `[]`)
389
+ * `matchSoFar` (optional, default `{}`)
390
+
391
+ ## getLogger
392
+
393
+ get a new logger; call with a name, e.g., `module.id`
394
+
395
+ ## isPrefixOf
396
+
397
+ prefixArray is a prefix of the array
398
+
399
+ #### Parameters
400
+
401
+ * `prefixArray` &#x20;
402
+ * `array` &#x20;
403
+
404
+ ## isSubTopicOf
405
+
406
+ sub is a strict sub-topic of parent, and in particular not equal
407
+
408
+ #### Parameters
409
+
410
+ * `sub` &#x20;
411
+ * `parent` &#x20;
412
+
413
+ ## LevelBadge
414
+
415
+ The right badge for the level
416
+
417
+ #### Parameters
418
+
419
+ * `$0` **[Object][1]**&#x20;
420
+
421
+ * `$0.level` &#x20;
422
+
423
+ ## mergeVersions
424
+
425
+ given an object where the keys are versions, merge this into one object
426
+ where the latest version of each subfield overwrites any previous
427
+
428
+ #### Parameters
429
+
430
+ * `versionsObject` &#x20;
431
+ * `subTopic` (optional, default `undefined`)
432
+ * `options` (optional, default `{}`)
433
+
434
+ ## mqttClearRetained
435
+
436
+ delete all retained messages in a certain topic prefix, waiting for
437
+ a given delay to collect existing retained. Use with care, never delete topics
438
+ not owned by us. Harmless within capabilities, which are namespaced already.
439
+
440
+ #### Parameters
441
+
442
+ * `mqttClient` &#x20;
443
+ * `prefixes` &#x20;
444
+ * `callback` &#x20;
445
+ * `delay` (optional, default `1000`)
446
+
447
+ ## oneDown
448
+
449
+ called each time one item is done
450
+
451
+ ## parseCookie
452
+
453
+ parse document cookies
454
+
455
+ #### Parameters
456
+
457
+ * `str` &#x20;
458
+
459
+ ## parseMQTTTopic
460
+
461
+ parse an MQTT topic according to our topic schema
462
+
463
+ #### Parameters
464
+
465
+ * `topic` &#x20;
466
+
467
+ ## parseMQTTUsername
468
+
469
+ parse usernames used in MQTT
470
+
471
+ #### Parameters
472
+
473
+ * `username` &#x20;
474
+
475
+ ## pathToTopic
476
+
477
+ convert a path array to mqtt topic; reduces +id identifiers to +
478
+
479
+ #### Parameters
480
+
481
+ * `pathArray` &#x20;
482
+
483
+ ## resolveDoubleSlashes
484
+
485
+ given a path, replace any double slashes, '//', with single ones
486
+
487
+ #### Parameters
488
+
489
+ * `path` &#x20;
490
+
491
+ ## selectFromObject
492
+
493
+ given an object and a path with wildcards (\* and +), *modify* the object
494
+ to only contain elements matched by the path, e.g.,
495
+ {a: {b: 1, c: 2}, d: 2} and \['a','+'] would give {a: {b: 1, c: 2}}
496
+
497
+ #### Parameters
498
+
499
+ * `obj` **[object][1]** The object to select from
500
+ * `path` **[array][3]** An array specifying the path to select, potentially
501
+ containing mqtt wildcards ('+').
502
+
503
+ ## setFromPath
504
+
505
+ Like \_.set but without arrays. This allows using numbers as keys.
506
+
507
+ #### Parameters
508
+
509
+ * `obj` &#x20;
510
+ * `path` &#x20;
511
+ * `value` &#x20;
512
+
513
+ ## toFlatObject
514
+
515
+ given an object, return a new object where all sub-objects are
516
+ replaced by topic-values, e.g.:
517
+ {a: {b: 1, c: 2}, d: 3} -> {'/a/b': 1, '/a/c': 2, d: 3}
518
+ Note: not idempotent!
519
+ {'/a/b': 1, '/a/c': 2, d: 3} -> {'%2Fa%2Fb': 1, '%2Fa%2Fc': 2, d: 3}
520
+
521
+ #### Parameters
522
+
523
+ * `obj` &#x20;
524
+ * `prefix` (optional, default `[]`)
525
+ * `rtv` (optional, default `{}`)
526
+
527
+ ## topicMatch
528
+
529
+ match a slash-separated topic with a selector using +XYZ for (named)
530
+ wildcards. Return the matching result.
531
+
532
+ #### Parameters
533
+
534
+ * `selector` &#x20;
535
+ * `topic` &#x20;
536
+
537
+ ## topicToPath
538
+
539
+ convert topic to path array
540
+
541
+ #### Parameters
542
+
543
+ * `topic` &#x20;
544
+
545
+ ## tryJSONParse
546
+
547
+ try parsing JSON, return null if unsuccessful
548
+
549
+ #### Parameters
550
+
551
+ * `string` &#x20;
552
+
553
+ ## unset
554
+
555
+ Unset the topic in that obj, and clean up parent if empty, recursively.
556
+ Return the path to the removed node.
557
+
558
+ #### Parameters
559
+
560
+ * `obj` &#x20;
561
+ * `path` &#x20;
562
+
563
+ ## updateObject
564
+
565
+ given a modifier {"a/b/c": "xyz"} update the object `obj` such that
566
+ obj.a.b.c = "xyz"
567
+
568
+ #### Parameters
569
+
570
+ * `obj` &#x20;
571
+ * `modifier` &#x20;
572
+
573
+ ## useMqttSync
574
+
575
+ hook for using MqttSync in React
576
+
577
+ #### Parameters
578
+
579
+ * `$0` **[Object][1]**&#x20;
580
+
581
+ * `$0.jwt` &#x20;
582
+ * `$0.id` &#x20;
583
+ * `$0.mqttUrl` &#x20;
584
+
585
+ ## useTransitive
586
+
587
+ Hook for using Transitive in React. Connects to MQTT, establishes sync, and
588
+ exposes reactive `data` state variable.
589
+
590
+ #### Parameters
591
+
592
+ * `$0` **[Object][1]**&#x20;
593
+
594
+ * `$0.jwt` &#x20;
595
+ * `$0.id` &#x20;
596
+ * `$0.host` &#x20;
597
+ * `$0.ssl` &#x20;
598
+ * `$0.capability` &#x20;
599
+ * `$0.versionNS` &#x20;
600
+
601
+ ## versionCompare
602
+
603
+ Compare to version strings. Return -1 if a is lower than b,
604
+ 0 if they are equal, and 1 otherwise. If either is not a complete version,
605
+ e.g., 2.0, interpret it as a range and use its minimum version for the
606
+ comparison. Hence, 2.0 < 2.0.1.
607
+
608
+ #### Parameters
609
+
610
+ * `a` &#x20;
611
+ * `b` &#x20;
612
+
613
+ ## visit
614
+
615
+ reusable visitor pattern: iteratively visits all nodes in the tree
616
+ described by `object`, where `childField` indicates the child-of predicate.
617
+
618
+ #### Parameters
619
+
620
+ * `object` &#x20;
621
+ * `childField` &#x20;
622
+ * `visitor` &#x20;
623
+
624
+ ## wait
625
+
626
+ wait for delay ms, usable in async functions
627
+
628
+ #### Parameters
629
+
630
+ * `delay` &#x20;
631
+
632
+ ## create
633
+
634
+ #### Parameters
635
+
636
+ * `wrapper` **JSX.Element** : the wrapper component class to be instantiated and wrapped
637
+ * `tagName` **[string][4]** The name of the web component. Has to be minus "-" delimited.
638
+ * `useShadowDom` **[boolean][5]** If the value is set to "true" the web component will use the `shadowDom`. The default value is true. (optional, default `true`)
639
+ * `observedAttributes` **[Array][3]<[string][4]>** The observed attributes of the web component (optional, default `[]`)
640
+ * `compRef` (optional, default `undefined`)
641
+
642
+ ## extractAttributes
643
+
644
+ Takes in a node attributes map and returns an object with camelCased
645
+ properties and values
646
+
647
+ #### Parameters
648
+
649
+ * `nodeMap` &#x20;
650
+
651
+ Returns **{}**&#x20;
652
+
653
+ ## getStyleElementsFromReactWebComponentStyleLoader
654
+
655
+ An optional library which is conditionally added
656
+
657
+ Returns **\[]**&#x20;
658
+
659
+ ## setAll
660
+
661
+ convenience function to set all loggers to the given level
662
+
663
+ #### Parameters
664
+
665
+ * `level` &#x20;
666
+
667
+ ## setConfig
668
+
669
+ method exposed to the wrapped component via prop that allows setting
670
+ the "config" state variable inside the wrapper (not the component
671
+ itself). This config is retrieved by the portal for inclusion in the
672
+ embedding instructions.
673
+
674
+ #### Parameters
675
+
676
+ * `config` &#x20;
677
+
678
+ ## setOnDisconnect
679
+
680
+ function used by `Component` to register a onDisconnect handler
681
+
682
+ #### Parameters
683
+
684
+ * `fn` &#x20;
685
+
686
+ ## webComponentAttributeChanged
687
+
688
+ Note this relies on the changes made in
689
+ github:amay0048/react-web-component#780950800e2962f45f0f029be618bb8b84610c89
690
+ that we used in our copy.
691
+ TODO: move this into our copy, i.e., do it internally to react-web-component
692
+ and update props.
693
+
694
+ #### Parameters
695
+
696
+ * `name` &#x20;
697
+ * `oldValue` &#x20;
698
+ * `newValue` &#x20;
699
+
700
+ [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
701
+
702
+ [2]: https://github.com/chfritz/transitive/issues/85
703
+
704
+ [3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
705
+
706
+ [4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
707
+
708
+ [5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transitive-sdk/utils-web",
3
- "version": "0.7.5",
3
+ "version": "0.7.6",
4
4
  "description": "Web utils for the Transitive framework",
5
5
  "homepage": "https://transitiverobotics.com",
6
6
  "author": {
@@ -18,7 +18,8 @@
18
18
  "scripts": {
19
19
  "test": "webpack serve -c webpack-test.config.js",
20
20
  "build-test": "webpack -c webpack-test.config.js",
21
- "prepare": "webpack --no-watch --mode=production"
21
+ "prepare": "webpack --no-watch --mode=production",
22
+ "prepack": "cat ../docs.js | node --input-type=module"
22
23
  },
23
24
  "dependencies": {
24
25
  "@babel/runtime": "^7.16.7",