braid-blob 0.0.61 → 0.0.62

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.
Files changed (2) hide show
  1. package/img-live.js +11 -46
  2. package/package.json +1 -1
package/img-live.js CHANGED
@@ -1,45 +1,21 @@
1
1
  // Braid-Blob Live Images
2
2
  // requires client.js
3
3
 
4
- var live_images = new Map() // url -> { blob, images: Set<img> }
4
+ var live_images = new Map() // img -> ac
5
5
 
6
6
  function sync(img) {
7
7
  var url = img.src
8
8
  if (!url) return
9
+ if (live_images.has(img)) return
9
10
 
10
- var entry = live_images.get(url)
11
- if (entry) {
12
- entry.images.add(img)
13
- // Apply current blob URL if we have one
14
- if (entry.objectUrl)
15
- img.src = entry.objectUrl
16
- return
17
- }
18
-
19
- // Create new subscription for this URL
20
- entry = { images: new Set([img]), objectUrl: null, blob: null }
21
- live_images.set(url, entry)
11
+ var ac = new AbortController()
12
+ live_images.set(img, ac)
22
13
 
23
- entry.blob = braid_blob_client(url, {
14
+ braid_blob_client(url, {
15
+ signal: ac.signal,
24
16
  on_update: (body, content_type) => {
25
- // Revoke old object URL if exists
26
- if (entry.objectUrl)
27
- URL.revokeObjectURL(entry.objectUrl)
28
-
29
- // Create new blob and object URL
30
17
  var blob = new Blob([body], { type: content_type || 'image/png' })
31
- entry.objectUrl = URL.createObjectURL(blob)
32
-
33
- // Update all images subscribed to this URL
34
- entry.images.forEach(img => {
35
- img.src = entry.objectUrl
36
- })
37
- },
38
- on_delete: () => {
39
- if (entry.objectUrl) {
40
- URL.revokeObjectURL(entry.objectUrl)
41
- entry.objectUrl = null
42
- }
18
+ img.src = URL.createObjectURL(blob)
43
19
  },
44
20
  on_error: (error) => {
45
21
  console.error('Live image error for', url, error)
@@ -48,21 +24,10 @@ function sync(img) {
48
24
  }
49
25
 
50
26
  function unsync(img) {
51
- // Find which entry this image belongs to
52
- for (var [url, entry] of live_images) {
53
- if (entry.images.has(img)) {
54
- entry.images.delete(img)
55
-
56
- // If no more images using this URL, clean up
57
- if (entry.images.size === 0) {
58
- if (entry.objectUrl)
59
- URL.revokeObjectURL(entry.objectUrl)
60
- // Note: braid_blob_client doesn't expose unsubscribe,
61
- // would need to pass AbortSignal in options to cancel
62
- live_images.delete(url)
63
- }
64
- break
65
- }
27
+ var ac = live_images.get(img)
28
+ if (ac) {
29
+ ac.abort()
30
+ live_images.delete(img)
66
31
  }
67
32
  }
68
33
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-blob",
3
- "version": "0.0.61",
3
+ "version": "0.0.62",
4
4
  "description": "Library for collaborative blobs over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-blob",