MCPcopy
hub / github.com/editablejs/editable / on

Method on

packages/editor/src/plugin/emitter.ts:56–92  ·  view source on GitHub ↗
(type: string | number, listener: Listener, prepend = false)

Source from the content-addressed store, hash-verified

54 }
55
56 on(type: string | number, listener: Listener, prepend = false): this {
57 let existing = this.events[type]
58 if (existing === undefined) {
59 // Optimize the case of one listener. Don't need the extra array object.
60 this.events[type] = listener
61 ++this.eventsCount
62 } else {
63 if (typeof existing === 'function') {
64 // Adding the second element, need to change to array.
65 existing = this.events[type] = prepend ? [listener, existing] : [existing, listener]
66 // If we've already got an array, just append.
67 } else if (prepend) {
68 existing.unshift(listener)
69 } else {
70 existing.push(listener)
71 }
72
73 // Check for listener leak
74 const maxCount = this.getMaxListeners()
75 if (maxCount > 0 && existing.length > maxCount) {
76 // No error code for this since it is a Warning
77 // eslint-disable-next-line no-restricted-syntax
78 const e = new Error(
79 'Possible EventEmitter memory leak detected. ' +
80 existing.length +
81 ' ' +
82 String(type) +
83 ' listeners ' +
84 'added. Use emitter.setMaxListeners() to ' +
85 'increase limit',
86 )
87 console.warn(e)
88 }
89 }
90
91 return this
92 }
93
94 off(type: string | number, listener: Listener): this {
95 const list = this.events[type]

Callers 15

onceMethod · 0.95
constructorMethod · 0.80
withEditableFunction · 0.80
event.tsFile · 0.80
EditableProviderFunction · 0.80
ContentEditableFunction · 0.80
useEventFunction · 0.80
constructorMethod · 0.80
start.tsFile · 0.80
initPersistenceFunction · 0.80
constructorMethod · 0.80
_subDocsHandlerMethod · 0.80

Calls 2

getMaxListenersMethod · 0.95
StringFunction · 0.85

Tested by

no test coverage detected