native-document 1.0.28 → 1.0.29

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.
@@ -141,6 +141,76 @@ var NativeDocument = (function (exports) {
141
141
  return this.observable.cleanup();
142
142
  };
143
143
 
144
+ const PluginsManager = (function() {
145
+
146
+ const $plugins = new Map();
147
+ const $pluginByEvents = new Map();
148
+
149
+ return {
150
+ list() {
151
+ return $pluginByEvents;
152
+ },
153
+ add(name, plugin){
154
+ if (!plugin || typeof plugin !== 'object') {
155
+ throw new Error(`Plugin ${name} must be an object`);
156
+ }
157
+ if($plugins.has(name)) {
158
+ return;
159
+ }
160
+
161
+ plugin.$name = name;
162
+ $plugins.set(name ,plugin);
163
+ if(typeof plugin?.init === 'function') {
164
+ plugin.init();
165
+ }
166
+ for(const methodName in plugin) {
167
+ if(/^on[A-Z]/.test(methodName)) {
168
+ if(!$pluginByEvents.has(methodName)) {
169
+ $pluginByEvents.set(methodName, new Set());
170
+ }
171
+ $pluginByEvents.get(methodName).add(plugin);
172
+ }
173
+ }
174
+ },
175
+ remove(pluginName){
176
+ if(!$plugins.has(pluginName)) {
177
+ return;
178
+ }
179
+ const plugin = $plugins.get(pluginName);
180
+ if(typeof plugin.cleanup === 'function') {
181
+ plugin.cleanup();
182
+ }
183
+ for(const [name, sets] of $pluginByEvents.entries() ) {
184
+ if(sets.has(plugin)) {
185
+ sets.delete(plugin);
186
+ }
187
+ if(sets.size === 0) {
188
+ $pluginByEvents.delete(name);
189
+ }
190
+ }
191
+ $plugins.delete(pluginName);
192
+ },
193
+ emit(event, ...data) {
194
+ const eventMethodName = 'on'+event;
195
+ if(!$pluginByEvents.has(eventMethodName)) {
196
+ return;
197
+ }
198
+ const plugins = $pluginByEvents.get(eventMethodName);
199
+
200
+ for(const plugin of plugins) {
201
+ const callback = plugin[eventMethodName];
202
+ if(typeof callback === 'function') {
203
+ try{
204
+ callback.call(plugin, ...data);
205
+ } catch (error) {
206
+ DebugManager$1.error('Plugin Manager', `Error in plugin ${plugin.$name} for event ${eventMethodName}`, error);
207
+ }
208
+ }
209
+ }
210
+ }
211
+ };
212
+ }());
213
+
144
214
  /**
145
215
  *
146
216
  * @param {*} value
@@ -155,6 +225,7 @@ var NativeDocument = (function (exports) {
155
225
  this.$watchers = null;
156
226
 
157
227
  this.$memoryId = null;
228
+ PluginsManager.emit('CreateObservable', this);
158
229
  }
159
230
 
160
231
  Object.defineProperty(ObservableItem.prototype, '$value', {
@@ -236,7 +307,9 @@ var NativeDocument = (function (exports) {
236
307
  }
237
308
  this.$previousValue = this.$currentValue;
238
309
  this.$currentValue = newValue;
310
+ PluginsManager.emit('ObservableBeforeChange', this);
239
311
  this.trigger();
312
+ PluginsManager.emit('ObservableAfterChange', this);
240
313
  };
241
314
 
242
315
  ObservableItem.prototype.val = function() {
@@ -257,6 +330,7 @@ var NativeDocument = (function (exports) {
257
330
  this.$watchers = null;
258
331
  this.trigger = noneTrigger;
259
332
  };
333
+
260
334
  ObservableItem.prototype.cleanup = function() {
261
335
  MemoryManager.unregister(this.$memoryId);
262
336
  this.disconnectAll();
@@ -267,9 +341,10 @@ var NativeDocument = (function (exports) {
267
341
  /**
268
342
  *
269
343
  * @param {Function} callback
344
+ * @param {any} target
270
345
  * @returns {(function(): void)}
271
346
  */
272
- ObservableItem.prototype.subscribe = function(callback) {
347
+ ObservableItem.prototype.subscribe = function(callback, target = null) {
273
348
  this.$listeners = this.$listeners ?? [];
274
349
  if (this.$isCleanedUp) {
275
350
  DebugManager$1.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
@@ -281,9 +356,11 @@ var NativeDocument = (function (exports) {
281
356
 
282
357
  this.$listeners.push(callback);
283
358
  this.assocTrigger();
359
+ PluginsManager.emit('ObservableSubscribe', this, target);
284
360
  return () => {
285
361
  this.unsubscribe(callback);
286
362
  this.assocTrigger();
363
+ PluginsManager.emit('ObservableUnsubscribe', this);
287
364
  };
288
365
  };
289
366
 
@@ -526,6 +603,7 @@ var NativeDocument = (function (exports) {
526
603
  function NDElement(element) {
527
604
  this.$element = element;
528
605
  this.$observer = null;
606
+ PluginsManager.emit('NDElementCreated', element, this);
529
607
  }
530
608
  NDElement.prototype.__$isNDElement = true;
531
609
 
@@ -907,7 +985,7 @@ var NativeDocument = (function (exports) {
907
985
  continue;
908
986
  }
909
987
  if(value.$observer) {
910
- element.classList.toggle(className, value.$observer.val());
988
+ element.classList.toggle(className, value.$observer.val() === value.$target);
911
989
  value.$observer.on(value.$target, function(isTargetValue) {
912
990
  element.classList.toggle(className, isTargetValue);
913
991
  });
@@ -1103,11 +1181,15 @@ var NativeDocument = (function (exports) {
1103
1181
  if(children === null) return;
1104
1182
  const childrenArray = Array.isArray(children) ? children : [children];
1105
1183
 
1184
+ PluginsManager.emit('BeforeProcessChildren', parent);
1185
+
1106
1186
  for(let i = 0, length = childrenArray.length; i < length; i++) {
1107
1187
  let child = this.getChild(childrenArray[i]);
1108
1188
  if (child === null) continue;
1109
1189
  parent.appendChild(child);
1110
1190
  }
1191
+
1192
+ PluginsManager.emit('AfterProcessChildren', parent);
1111
1193
  },
1112
1194
  getChild(child) {
1113
1195
  if(child === null) {
@@ -1136,6 +1218,7 @@ var NativeDocument = (function (exports) {
1136
1218
  return fragment;
1137
1219
  }
1138
1220
  if(Validator.isFunction(child)) {
1221
+ PluginsManager.emit('BeforeProcessComponent', child);
1139
1222
  return this.getChild(child());
1140
1223
  }
1141
1224
  return ElementCreator.createStaticTextNode(null, child);
@@ -1159,6 +1242,7 @@ var NativeDocument = (function (exports) {
1159
1242
  * @returns {HTMLElement|DocumentFragment}
1160
1243
  */
1161
1244
  setup(element, attributes, customWrapper) {
1245
+ PluginsManager.emit('Setup', element, attributes, customWrapper);
1162
1246
  return element;
1163
1247
  }
1164
1248
  };
@@ -1362,6 +1446,8 @@ var NativeDocument = (function (exports) {
1362
1446
  }
1363
1447
  const observer = Observable(target);
1364
1448
 
1449
+ PluginsManager.emit('CreateObservableArray', observer);
1450
+
1365
1451
  methods.forEach((method) => {
1366
1452
  observer[method] = function(...values) {
1367
1453
  const result = observer.val()[method](...values);
@@ -1567,6 +1653,8 @@ var NativeDocument = (function (exports) {
1567
1653
  const observable = new ObservableItem(initialValue);
1568
1654
  const updatedValue = () => observable.set(callback());
1569
1655
 
1656
+ PluginsManager.emit('CreateObservableComputed', observable, dependencies);
1657
+
1570
1658
  if(Validator.isFunction(dependencies)) {
1571
1659
  if(!Validator.isObservable(dependencies.$observer)) {
1572
1660
  throw new NativeDocumentError('Observable.computed : dependencies must be valid batch function');