@lumjs/core 1.38.2 → 1.38.3

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.
@@ -1,139 +0,0 @@
1
- "use strict";
2
-
3
- const {F,isObj} = require('../types');
4
- const Event = require('./event');
5
-
6
- const REMOVE_OPTS = ['listener','handler','eventNames','eventTypes'];
7
-
8
- function makeOpts(spec)
9
- {
10
- const opts = Object.assign({}, spec, spec.options);
11
- for (const rm of REMOVE_OPTS)
12
- {
13
- delete opts[rm];
14
- }
15
- return opts;
16
- }
17
-
18
- /**
19
- * Is something a valid value for an event listener?
20
- *
21
- * Valid listener values are inspired by the `DOM.EventTarget` interface:
22
- *
23
- * - A `function`
24
- * - An `object` with a `handleEvent()` method
25
- *
26
- * @param {*} v - Value we are testing
27
- * @returns {boolean}
28
- * @alias module:@lumjs/core/events.Listener.isListener
29
- */
30
- function isListener(v)
31
- {
32
- return (typeof v === F || (isObj(v) && typeof v.handleEvent === F));
33
- }
34
-
35
- /**
36
- * An Event Listener instance used by a Registry
37
- *
38
- * Used internally by the Registry class, there's likely very few
39
- * reasons you'd want to call any methods on this manually.
40
- *
41
- * @prop {module:@lumjs/core/events.Registry} registry
42
- * The Registry instance this Listener belongs to.
43
- * @prop {(function|object)} handler - Event handler callback
44
- * @prop {Set} eventTypes - A set of all event types handled by this
45
- * @prop {Set} eventNames - Alias to `eventTypes`
46
- * @prop {object} options - Options specific to this listener.
47
- *
48
- * See {@link module:@lumjs/core/events.Registry#makeListener makeListener()}
49
- * for details on what this may contain and how it is populated.
50
- *
51
- * @alias module:@lumjs/core/events.Listener
52
- */
53
- class LumEventListener
54
- {
55
- /**
56
- * Build a listener; called by Registry instance
57
- * @private
58
- * @param {module:@lumjs/core/events.Registry} registry
59
- * @param {object} spec
60
- */
61
- constructor(registry, spec)
62
- {
63
- if (isListener(spec.listener))
64
- {
65
- this.handler = spec.listener;
66
- }
67
- else if (isListener(spec.handler))
68
- {
69
- this.handler = spec.handler;
70
- }
71
- else
72
- {
73
- console.error({spec,registry});
74
- throw new TypeError("Invalid listener/handler in spec");
75
- }
76
-
77
- // Assign the rest here.
78
- this.registry = registry;
79
- this.options = makeOpts(spec);
80
- const events = spec.eventTypes ?? spec.eventNames;
81
- this.eventTypes = this.eventNames = registry.getEventNames(events);
82
-
83
- const setup = this.options.setupListener ?? registry.options.setupListener;
84
- if (typeof setup === F)
85
- {
86
- setup.call(registry, this);
87
- }
88
- }
89
-
90
- /**
91
- * See if there is at least one item in `this.eventTypes`
92
- * @type {boolean}
93
- */
94
- get hasEvents()
95
- {
96
- return this.eventTypes.size > 0;
97
- }
98
-
99
- /**
100
- * Used by {@link module:@lumjs/core/events.Registry#emit emit()} to create
101
- * and emit a new Event instance for a specified event name and target.
102
- *
103
- * This is a *protected method* and should not be called directly.
104
- * @protected
105
- * @param {string} type - A single event type/name that was triggered
106
- * @param {object} target - A single target object
107
- * @param {Array} args - Arguments passed to `emit()`
108
- * @param {module:@lumjs/core/events~Status} status - Emit status info
109
- * @returns {module:@lumjs/core/events.Event} The new Event that was emitted
110
- */
111
- emitEvent(type, target, args, status)
112
- {
113
- const event = new Event(this, target, type, args, status);
114
-
115
- if (typeof this.handler === F)
116
- { // The simplest is the good old function
117
- this.handler.call(event.target, event);
118
- }
119
- else
120
- { // An object with a `handleEvent()` method
121
- this.handler.handleEvent(event);
122
- }
123
-
124
- if (event.options.once)
125
- { // This listener is to be removed
126
- status.onceRemoved.add(this);
127
- }
128
-
129
- return event;
130
- }
131
-
132
- static get classProps()
133
- {
134
- return Object.getOwnPropertyNames(this.prototype);
135
- }
136
- }
137
-
138
- LumEventListener.isListener = isListener;
139
- module.exports = LumEventListener;