id-kit / src / ardegazu / id / bridge_client.cljs
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
;; ported-from: src/bridge-client.ts @ v1.1.0
;;
;; The bridge CLIENT — what every suite app calls at boot.
;;
;; Contract: the app's own storage (`<ns>:id`, `<ns>:name`, `<ns>:profile`) is
;; a MIRROR of the suite record and always wins for booting — the iframe is
;; raced against a short timeout and its absence (offline PWA, private mode,
;; apex outage, dev without the hub) leaves behavior exactly as it was before
;; the bridge existed. When the bridge IS reachable, boot() reconciles:
;;
;;   bridge has a record, mirror empty  -> adopt (identity "just appears")
;;   mirror has a seed, bridge empty    -> publish (first app claims the suite)
;;   same seed both sides               -> merge profile by freshest timestamp
;;   different seeds                    -> NEVER silently switch: keep the local
;;     seed for this session and surface `conflict` so the app's identity UI
;;     can offer an explicit choice (adoptBridgeSeed / keepLocalSeed).
;;
;; Class-API note: instance state lives in string-keyed "_" slots and the
;; public methods are attached to the prototype with string keys, so
;; :advanced never renames the surface TS consumers use.
(ns ardegazu.id.bridge-client
  (:require [ardegazu.id.identity :as identity]
            [ardegazu.id.profile :as profile]
            [ardegazu.id.protocol :as protocol]
            [shadow.cljs.modern :refer (defclass js-await)]))

(def ^:private default-timeout-ms 2500)
;; A first visit has no mirror to fall back on — the bridge answer DECIDES what
;; the app shows (gate vs known user), so its iframe race gets a longer leash.
;; With a mirror the answer only refines what already booted; the short race
;; keeps flaky-network boots snappy.
(def ^:private first-visit-timeout-ms 8000)

(declare mirror-seed*)

(defclass IdBridge
  (constructor [this opts]
    (unchecked-set this "_ns" (unchecked-get opts "ns"))
    (unchecked-set this "_url" (let [u (unchecked-get opts "bridgeUrl")]
                                 (if (some? u) u protocol/DEFAULT-BRIDGE-URL)))
    (unchecked-set this "_origin" (.-origin (js/URL. (unchecked-get this "_url"))))
    (unchecked-set this "_timeoutMs" (let [t (unchecked-get opts "timeoutMs")]
                                       (if (some? t) t default-timeout-ms)))
    (unchecked-set this "_frameTimeoutMs"
                   (let [t (unchecked-get opts "timeoutMs")]
                     (cond
                       (some? t) t
                       (nil? (mirror-seed* this)) first-visit-timeout-ms
                       :else default-timeout-ms)))
    (unchecked-set this "_frame" nil)
    (unchecked-set this "_ready" nil)
    (unchecked-set this "_reqId" 0)
    (unchecked-set this "_pending" (js/Map.))
    (unchecked-set this "_onChange" (js/Set.))
    (unchecked-set this "_lastRec" nil)
    (unchecked-set this "_listener" nil)))

;; ---- mirror (per-app storage; every access is try/catch) -------------------

(defn- mirror-key [bridge k]
  (str (unchecked-get bridge "_ns") ":" k))

(defn- ls-get [bridge k]
  (try
    (.getItem js/localStorage (mirror-key bridge k))
    (catch :default _ nil)))

(defn- ls-set [bridge k v]
  (try
    (if (nil? v)
      (.removeItem js/localStorage (mirror-key bridge k))
      (.setItem js/localStorage (mirror-key bridge k) v))
    (catch :default _
      ;; private mode: session-only, same as before the bridge
      nil))
  js/undefined)

(defn- mirror-seed* [bridge]
  (let [s (ls-get bridge "id")]
    (if (and s (.test protocol/SEED-RE s)) s nil)))

(defn- mirror-profile* [bridge]
  (let [mp (try
             (js/JSON.parse (let [v (ls-get bridge "profile")]
                              (if (some? v) v "{}")))
             (catch :default _ (js-obj)))
        clamped (profile/clamp-profile
                 (js-obj "name" (let [v (ls-get bridge "name")]
                                  (if (some? v) v ""))
                         "hue" (unchecked-get mp "hue")
                         "glyph" (unchecked-get mp "glyph")
                         "lang" (unchecked-get mp "lang")))
        ts (let [t (unchecked-get mp "ts")]
             (if (and (number? t) (js/Number.isFinite t)) t 0))
        out (js/Object.assign (js-obj) clamped)]
    (unchecked-set out "ts" ts)
    out))

(defn- write-mirror [bridge seed prof ts]
  (when (some? seed)
    (ls-set bridge "id" seed))
  (ls-set bridge "name" (let [n (unchecked-get prof "name")]
                          (if (and (some? n) (not (identical? n ""))) n nil)))
  (ls-set bridge "profile"
          (js/JSON.stringify (js-obj "hue" (unchecked-get prof "hue")
                                     "glyph" (unchecked-get prof "glyph")
                                     "lang" (unchecked-get prof "lang")
                                     "ts" ts)))
  js/undefined)

;; ---- iframe plumbing -------------------------------------------------------

(declare absorb)

(defn- ensure-frame [bridge]
  (or (unchecked-get bridge "_ready")
      (let [p (js/Promise.
               (fn [resolve reject]
                 (let [settled (volatile! false)
                       timer (js/setTimeout
                              (fn []
                                (when-not @settled
                                  (vreset! settled true)
                                  (reject (js/Error. "id bridge timeout"))))
                              (unchecked-get bridge "_frameTimeoutMs"))
                       listener
                       (fn [^js ev]
                         (let [frame (unchecked-get bridge "_frame")]
                           (when (and (identical? (.-origin ev) (unchecked-get bridge "_origin"))
                                      (some? frame)
                                      (identical? (.-source ev) (.-contentWindow ^js frame))
                                      (protocol/is-bridge-reply (.-data ev)))
                             (let [msg (.-data ev)
                                   t (unchecked-get msg "t")
                                   req-id (unchecked-get msg "reqId")]
                               (cond
                                 (identical? t "id-ready")
                                 (when-not @settled
                                   (vreset! settled true)
                                   (js/clearTimeout timer)
                                   (resolve (.-contentWindow ^js frame)))

                                 ;; unsolicited push: another app wrote the suite record
                                 (and (identical? t "state") (undefined? req-id))
                                 (let [rec (unchecked-get msg "rec")]
                                   (unchecked-set bridge "_lastRec" rec)
                                   (absorb bridge rec)
                                   (.forEach ^js (unchecked-get bridge "_onChange")
                                             (fn [cb _ _] (cb rec))))

                                 (not (undefined? req-id))
                                 (let [pending ^js (unchecked-get bridge "_pending")
                                       done (.get pending req-id)]
                                   (when done
                                     (.delete pending req-id)
                                     (done msg))))))))]
                   (unchecked-set bridge "_listener" listener)
                   (.addEventListener js/window "message" listener)
                   (let [f (.createElement js/document "iframe")]
                     (set! (.-hidden f) true)
                     (.setAttribute f "aria-hidden" "true")
                     (set! (.-src f) (unchecked-get bridge "_url"))
                     (.addEventListener f "error"
                                        (fn []
                                          (when-not @settled
                                            (vreset! settled true)
                                            (js/clearTimeout timer)
                                            (reject (js/Error. "id bridge failed to load")))))
                     (unchecked-set bridge "_frame" f)
                     (.appendChild (let [b (.-body js/document)]
                                     (if (some? b) b (.-documentElement js/document)))
                                   f)))))]
        (unchecked-set bridge "_ready" p)
        p)))

(defn- request [bridge build]
  (js-await [win (ensure-frame bridge)]
    (let [req-id (inc (unchecked-get bridge "_reqId"))
          _ (unchecked-set bridge "_reqId" req-id)
          msg (build req-id)]
      (js/Promise.
       (fn [resolve reject]
         (let [timer (js/setTimeout
                      (fn []
                        (.delete ^js (unchecked-get bridge "_pending") req-id)
                        (reject (js/Error. "id bridge timeout")))
                      (unchecked-get bridge "_timeoutMs"))]
           (.set ^js (unchecked-get bridge "_pending") req-id
                 (fn [r]
                   (js/clearTimeout timer)
                   (resolve r)))
           (.postMessage ^js win msg (unchecked-get bridge "_origin"))))))))

(defn- absorb
  "A mirror-equal seed absorbs fresher profile fields from a pushed record."
  [bridge rec]
  (when (and (some? rec) (identical? (unchecked-get rec "seed") (mirror-seed* bridge)))
    (when (> (unchecked-get rec "updatedAt")
             (unchecked-get (mirror-profile* bridge) "ts"))
      (write-mirror bridge (unchecked-get rec "seed") (profile/clamp-profile rec)
                    (unchecked-get rec "updatedAt"))))
  js/undefined)

(defn- put* [bridge seed prof ts expect]
  (request bridge
           (fn [req-id]
             ;; sequential assignment: cljs's js-obj scrambles literals past
             ;; 8 keys, and the record shape (TEN fields) should match the
             ;; stored wire shape exactly
             (let [rec (js-obj)]
               (unchecked-set rec "v" 1)
               (unchecked-set rec "seed" seed)
               (unchecked-set rec "name" (unchecked-get prof "name"))
               (unchecked-set rec "hue" (unchecked-get prof "hue"))
               (unchecked-set rec "glyph" (unchecked-get prof "glyph"))
               (unchecked-set rec "lang" (unchecked-get prof "lang"))
               (unchecked-set rec "soc" nil)
               ;; placeholder only: the host carries the live `apps` map over
               ;; a same-seed put, so a profile write never wipes a section
               (unchecked-set rec "apps" nil)
               (unchecked-set rec "createdAt" ts)
               (unchecked-set rec "updatedAt" ts)
               (js-obj "t" "put" "v" 1 "reqId" req-id "rec" rec "expect" expect)))))

(defn- publish-later [bridge seed prof ts]
  ;; apex still unreachable; the next boot retries
  (.catch (put* bridge seed prof ts nil) (fn [_] js/undefined)))

;; ---- app-facing operations -------------------------------------------------

(defn- put-profile* [bridge patch]
  (let [seed (mirror-seed* bridge)
        cur (mirror-profile* bridge)
        merged (profile/clamp-profile
                (js-obj "name" (if (js-in "name" patch)
                                 (unchecked-get patch "name")
                                 (unchecked-get cur "name"))
                        "hue" (if (js-in "hue" patch)
                                (unchecked-get patch "hue")
                                (unchecked-get cur "hue"))
                        "glyph" (if (js-in "glyph" patch)
                                  (unchecked-get patch "glyph")
                                  (unchecked-get cur "glyph"))
                        "lang" (if (js-in "lang" patch)
                                 (unchecked-get patch "lang")
                                 (unchecked-get cur "lang"))))]
    (write-mirror bridge seed merged (js/Date.now))
    (if (nil? seed)
      (js/Promise.resolve js/undefined)
      (-> (request bridge
                   (fn [req-id]
                     (js-obj "t" "profile" "v" 1 "reqId" req-id "expect" seed "patch" patch)))
          (.then (fn [r]
                   (when (identical? (unchecked-get r "t") "state")
                     (unchecked-set bridge "_lastRec" (unchecked-get r "rec")))
                   js/undefined))
          ;; offline: mirror ts is newer, next boot pushes it
          (.catch (fn [_] js/undefined))))))

(defn- put-soc* [bridge soc]
  (let [seed (mirror-seed* bridge)]
    (if (nil? seed)
      (js/Promise.resolve false)
      (-> (request bridge
                   (fn [req-id]
                     (js-obj "t" "soc" "v" 1 "reqId" req-id "expect" seed "soc" soc)))
          (.then (fn [r]
                   (if (identical? (unchecked-get r "t") "state")
                     (do (unchecked-set bridge "_lastRec" (unchecked-get r "rec"))
                         true)
                     false)))
          (.catch (fn [_] false))))))

;; The app's own section of the suite record. ONLINE-ONLY, like putSoc: there
;; is no mirror for it (a section is read by OTHER apps, so a local-only copy
;; would be a lie), and the host — not the caller — decides the key and the ts.
;;
;; Failures are NOT all alike: "cas" is worth retrying, "size"/"key" never are,
;; and "full" needs the caller to shrink or drop its section. The host says
;; which on the conflict reply; a host too old to say gets the generic
;; "conflict". putAppState keeps resolving a plain boolean — putAppStateDetailed
;; is the same call with the reason attached.
(defn- app-result [ok reason]
  (js-obj "ok" ok "reason" reason))

(defn- put-app-state* [bridge state]
  (let [seed (mirror-seed* bridge)]
    (if (nil? seed)
      (js/Promise.resolve (app-result false "noseed"))
      (-> (request bridge
                   (fn [req-id]
                     (js-obj "t" "app" "v" 1 "reqId" req-id "expect" seed "state" state)))
          (.then (fn [r]
                   (if (identical? (unchecked-get r "t") "state")
                     (do (unchecked-set bridge "_lastRec" (unchecked-get r "rec"))
                         (app-result true nil))
                     (let [reason (unchecked-get r "reason")]
                       (app-result false (if (string? reason) reason "conflict"))))))
          (.catch (fn [_] (app-result false "offline")))))))

(defn- app-key* []
  (if (exists? js/location)
    (protocol/app-key-of-origin (.-origin js/location))
    nil))

(defn- app-state-of* [bridge k]
  (let [rec (unchecked-get bridge "_lastRec")
        apps (if (some? rec) (unchecked-get rec "apps") nil)
        e (if (some? apps) (unchecked-get apps k) nil)]
    (if (some? e)
      (let [s (unchecked-get e "state")]
        (if (undefined? s) nil s))
      nil)))

(defn- publish-seed* [bridge seed opts]
  (if-not (.test protocol/SEED-RE seed)
    (js/Promise.resolve false)
    (let [prof (mirror-profile* bridge)
          force? (and (some? opts) (unchecked-get opts "force"))
          expect (if force?
                   (let [last (unchecked-get bridge "_lastRec")]
                     (if (some? last)
                       (let [s (unchecked-get last "seed")] (if (some? s) s nil))
                       nil))
                   nil)]
      (-> (put* bridge seed prof (js/Date.now) expect)
          (.then (fn [r]
                   (if (identical? (unchecked-get r "t") "state")
                     (do (unchecked-set bridge "_lastRec" (unchecked-get r "rec"))
                         true)
                     false)))
          (.catch (fn [_] false))))))

;; ---- boot ------------------------------------------------------------------

(defn- boot-result [seed identity-obj prof source conflict bridged]
  ;; strip any internal fields (mirror profiles carry a ts)
  (js-obj "seed" seed
          "identity" identity-obj
          "profile" (js-obj "name" (unchecked-get prof "name")
                            "hue" (unchecked-get prof "hue")
                            "glyph" (unchecked-get prof "glyph")
                            "lang" (unchecked-get prof "lang"))
          "source" source
          "conflict" conflict
          "bridged" bridged))

(defn- finish [supported bridged seed prof source conflict]
  (if (and (some? seed) supported)
    (-> (identity/from-seed-impl seed)
        (.then (fn [id] (boot-result seed id prof source conflict bridged)))
        (.catch (fn [_] (boot-result seed nil prof source conflict bridged))))
    (js/Promise.resolve (boot-result seed nil prof source conflict bridged))))

(defn- boot* [bridge]
  (let [mirror-seed (mirror-seed* bridge)
        mirror-prof (mirror-profile* bridge)]
    (js-await [supported (.catch (identity/is-ed25519-supported) (fn [_] false))]
      (js-await [get-r (-> (request bridge (fn [req-id] (js-obj "t" "get" "v" 1 "reqId" req-id)))
                           ;; unreachable: mirror-only session
                           (.catch (fn [_] nil)))]
        (let [bridged (and (some? get-r) (identical? (unchecked-get get-r "t") "state"))
              bridge-rec (if bridged (unchecked-get get-r "rec") nil)]
          (when bridged
            (unchecked-set bridge "_lastRec" bridge-rec))
          (cond
            ;; Bridge unreachable → today's behavior, plus a background publish retry.
            (not bridged)
            (cond
              (some? mirror-seed) (finish supported bridged mirror-seed mirror-prof "mirror" nil)
              (not supported) (finish supported bridged nil profile/EMPTY-PROFILE "none" nil)
              :else
              (let [seed (identity/new-seed)
                    ts (js/Date.now)]
                (write-mirror bridge seed profile/EMPTY-PROFILE ts)
                (publish-later bridge seed profile/EMPTY-PROFILE ts)
                (finish supported bridged seed profile/EMPTY-PROFILE "fresh" nil)))

            ;; Bridge empty → publish the mirror (or a fresh mint).
            (nil? bridge-rec)
            (let [seed (if (some? mirror-seed)
                         mirror-seed
                         (if supported (identity/new-seed) nil))]
              (if (nil? seed)
                (finish supported bridged nil profile/EMPTY-PROFILE "none" nil)
                (let [ts (if (some? mirror-seed)
                           (let [t (unchecked-get mirror-prof "ts")]
                             (if (identical? t 0) (js/Date.now) t))
                           (js/Date.now))
                      prof (if (some? mirror-seed) mirror-prof profile/EMPTY-PROFILE)
                      source (if (some? mirror-seed) "mirror" "fresh")]
                  (when (nil? mirror-seed)
                    (write-mirror bridge seed prof ts))
                  (js-await [r (.catch (put* bridge seed prof ts nil) (fn [_] nil))]
                    (if (and (some? r)
                             (identical? (unchecked-get r "t") "conflict")
                             (some? (unchecked-get r "rec"))
                             (not (identical? (unchecked-get (unchecked-get r "rec") "seed") seed)))
                      ;; raced: someone else claimed the suite between get and put
                      (finish supported bridged seed prof source (unchecked-get r "rec"))
                      (finish supported bridged seed prof source nil))))))

            ;; Bridge has a record, mirror empty → adopt.
            (nil? mirror-seed)
            (let [prof (profile/clamp-profile bridge-rec)]
              (write-mirror bridge (unchecked-get bridge-rec "seed") prof
                            (unchecked-get bridge-rec "updatedAt"))
              (finish supported bridged (unchecked-get bridge-rec "seed") prof "bridge" nil))

            ;; Same seed → reconcile profile by freshest timestamp.
            (identical? (unchecked-get bridge-rec "seed") mirror-seed)
            (if (> (unchecked-get mirror-prof "ts") (unchecked-get bridge-rec "updatedAt"))
              (do (put-profile* bridge
                                (js-obj "name" (unchecked-get mirror-prof "name")
                                        "hue" (unchecked-get mirror-prof "hue")
                                        "glyph" (unchecked-get mirror-prof "glyph")
                                        "lang" (unchecked-get mirror-prof "lang")))
                  (finish supported bridged mirror-seed mirror-prof "mirror" nil))
              (let [prof (profile/clamp-profile bridge-rec)]
                (write-mirror bridge mirror-seed prof (unchecked-get bridge-rec "updatedAt"))
                (finish supported bridged mirror-seed prof "mirror" nil)))

            ;; Different seeds → conflict (unless this exact bridge seed was dismissed).
            :else
            (if (identical? (ls-get bridge "id-conflict-dismissed")
                            (unchecked-get bridge-rec "seed"))
              (finish supported bridged mirror-seed mirror-prof "mirror" nil)
              (finish supported bridged mirror-seed mirror-prof "mirror" bridge-rec))))))))

;; ---- the public surface ----------------------------------------------------

(let [proto (.-prototype IdBridge)]
  (unchecked-set proto "mirrorSeed"
                 (fn [] (this-as self (mirror-seed* self))))
  (unchecked-set proto "mirrorProfile"
                 (fn [] (this-as self (mirror-profile* self))))
  (unchecked-set proto "onChange"
                 (fn [cb]
                   (this-as self
                     (.add ^js (unchecked-get self "_onChange") cb)
                     (fn [] (.delete ^js (unchecked-get self "_onChange") cb)))))
  ;; Last record the bridge reported this session (null = none/unreached).
  (unchecked-set proto "lastRecord"
                 (fn [] (this-as self (unchecked-get self "_lastRec"))))
  (unchecked-set proto "boot"
                 (fn [] (this-as self (boot* self))))
  ;; Update profile fields: mirror immediately, bridge best-effort.
  (unchecked-set proto "putProfile"
                 (fn [patch] (this-as self (put-profile* self patch))))
  ;; Replace the suite social blob (owned by ardegazu-social-kit).
  (unchecked-set proto "putSoc"
                 (fn [soc] (this-as self (put-soc* self soc))))
  ;; Replace THIS app's section of the suite record (write-isolated by origin,
  ;; readable by every app through lastRecord/onChange). Online-only: false
  ;; when the bridge is unreachable or the CAS lost. state null deletes it.
  (unchecked-set proto "putAppState"
                 (fn [state]
                   (this-as self
                     (.then (put-app-state* self state)
                            (fn [r] (unchecked-get r "ok"))))))
  ;; Same write, with WHY it failed: {ok, reason}. reason is null on success,
  ;; else "cas" | "key" | "size" | "full" (from the host) or "noseed" |
  ;; "offline" | "conflict" (decided locally / an older host).
  (unchecked-set proto "putAppStateDetailed"
                 (fn [state] (this-as self (put-app-state* self state))))
  ;; The `apps` key this origin owns (nil off-suite).
  (unchecked-set proto "appKey"
                 (fn [] (app-key*)))
  ;; Any app's section, from the last record the bridge reported.
  (unchecked-set proto "appStateOf"
                 (fn [k] (this-as self (app-state-of* self k))))
  ;; Conflict chooser: make the SUITE identity this app's identity too.
  ;; Backs up the old seed to `<ns>:id-prev`. Caller reloads the app.
  (unchecked-set proto "adoptBridgeSeed"
                 (fn [bridge-rec]
                   (this-as self
                     (let [old (mirror-seed* self)]
                       (when (and (some? old)
                                  (not (identical? old (unchecked-get bridge-rec "seed"))))
                         (ls-set self "id-prev" old))
                       (write-mirror self (unchecked-get bridge-rec "seed")
                                     (profile/clamp-profile bridge-rec)
                                     (unchecked-get bridge-rec "updatedAt"))
                       (ls-set self "id-conflict-dismissed" nil)
                       js/undefined))))
  ;; Conflict chooser: keep the app-local identity; stop asking about this
  ;; particular suite seed (a future different suite seed asks again).
  (unchecked-set proto "keepLocalSeed"
                 (fn [bridge-rec]
                   (this-as self
                     (ls-set self "id-conflict-dismissed" (unchecked-get bridge-rec "seed"))
                     js/undefined)))
  ;; Explicitly make `seed` the suite identity (identity-sheet import flow).
  ;; force=false claims only an empty bridge; force=true overwrites.
  (unchecked-set proto "publishSeed"
                 (fn [seed opts]
                   (this-as self (publish-seed* self seed opts))))
  (unchecked-set proto "destroy"
                 (fn []
                   (this-as self
                     (when-some [listener (unchecked-get self "_listener")]
                       (.removeEventListener js/window "message" listener))
                     (when-some [frame (unchecked-get self "_frame")]
                       (.remove ^js frame))
                     (unchecked-set self "_frame" nil)
                     (unchecked-set self "_ready" nil)
                     (.clear ^js (unchecked-get self "_pending"))
                     (.clear ^js (unchecked-get self "_onChange"))
                     js/undefined))))

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/id-kit.git