MCPcopy
hub / github.com/hotwired/stimulus / StringMapObserver

Class StringMapObserver

src/mutation-observers/string_map_observer.ts:8–117  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6}
7
8export class StringMapObserver {
9 readonly element: Element
10 readonly delegate: StringMapObserverDelegate
11 private started: boolean
12 private stringMap: Map<string, string>
13 private mutationObserver: MutationObserver
14
15 constructor(element: Element, delegate: StringMapObserverDelegate) {
16 this.element = element
17 this.delegate = delegate
18 this.started = false
19 this.stringMap = new Map()
20 this.mutationObserver = new MutationObserver((mutations) => this.processMutations(mutations))
21 }
22
23 start() {
24 if (!this.started) {
25 this.started = true
26 this.mutationObserver.observe(this.element, { attributes: true, attributeOldValue: true })
27 this.refresh()
28 }
29 }
30
31 stop() {
32 if (this.started) {
33 this.mutationObserver.takeRecords()
34 this.mutationObserver.disconnect()
35 this.started = false
36 }
37 }
38
39 refresh() {
40 if (this.started) {
41 for (const attributeName of this.knownAttributeNames) {
42 this.refreshAttribute(attributeName, null)
43 }
44 }
45 }
46
47 // Mutation record processing
48
49 private processMutations(mutations: MutationRecord[]) {
50 if (this.started) {
51 for (const mutation of mutations) {
52 this.processMutation(mutation)
53 }
54 }
55 }
56
57 private processMutation(mutation: MutationRecord) {
58 const attributeName = mutation.attributeName
59 if (attributeName) {
60 this.refreshAttribute(attributeName, mutation.oldValue)
61 }
62 }
63
64 // State tracking
65

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…