native-document 1.0.183 → 1.0.185

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "native-document",
3
- "version": "1.0.183",
3
+ "version": "1.0.185",
4
4
  "description": "A reactive JavaScript framework that preserves native DOM simplicity without sacrificing modern features",
5
5
  "author": "AfroCodeur <https://github.com/afrocodeur>",
6
6
  "license": "MIT",
@@ -19,6 +19,7 @@ export default function NodeCloner($element) {
19
19
  this.$attrs = null;
20
20
  this.$ndMethods = null;
21
21
  this.$content = null;
22
+ this.$uniqueCallbacks= new Map();
22
23
  }
23
24
 
24
25
  NodeCloner.prototype.shouldBeHydrate = function() {
@@ -57,10 +58,28 @@ const buildProperties = (cache, properties, data) => {
57
58
  };
58
59
 
59
60
  const getPropertyValue = (callbackOrProperty, data) => {
61
+ if(callbackOrProperty?.__$Observable) {
62
+ return callbackOrProperty;
63
+ }
60
64
  const value = (typeof callbackOrProperty ==='string') ? data[0][callbackOrProperty] : callbackOrProperty;
61
65
  return (typeof value === 'function') ? value.apply(this, data) : value;
62
66
  };
63
67
 
68
+
69
+ const $nodeClonedCallbackData = new WeakMap();
70
+
71
+ const $nodeClonerCallbackCaller = (callback, event) => {
72
+ const data = $nodeClonedCallbackData.get(event.target);
73
+ callback.apply(event.target, [...data, event]);
74
+ };
75
+
76
+ NodeCloner.prototype.resolveMethods = function() {
77
+ for(const methodName in this.$ndMethods) {
78
+ const callback = this.$ndMethods[methodName];
79
+ const uniqueCallback = $nodeClonerCallbackCaller.bind(null, callback);
80
+ this.$uniqueCallbacks.set(methodName, uniqueCallback);
81
+ }
82
+ };
64
83
  /**
65
84
  * Pre-compiles all registered bindings into a sequence of optimised steps.
66
85
  * Called once before the first clone operation. Subsequent calls are no-ops.
@@ -71,21 +90,24 @@ NodeCloner.prototype.resolve = function() {
71
90
  if(this.$content) {
72
91
  return;
73
92
  }
93
+ this.resolveMethods();
74
94
  const steps = [];
75
95
  if(this.$ndMethods) {
76
96
  const methods = Object.keys(this.$ndMethods);
77
97
  if(methods.length === 1) {
78
98
  const methodName = methods[0];
79
- const callback = this.$ndMethods[methodName];
80
- steps.push((clonedNode, data) => {
81
- clonedNode.nd[methodName](callback.length === 0 ? callback : callback.bind(clonedNode, ...data));
99
+ steps.push((clonedNode) => {
100
+ const uniqueCallback = this.$uniqueCallbacks.get(methodName);
101
+ clonedNode.nd[methodName](uniqueCallback);
82
102
  });
83
103
  } else {
84
- steps.push((clonedNode, data) => {
104
+ steps.push((clonedNode) => {
85
105
  const nd = clonedNode.nd;
86
106
  for(const methodName in this.$ndMethods) {
87
- const callback = this.$ndMethods[methodName];
88
- nd[methodName](callback.length === 0 ? callback : this.$ndMethods[methodName].bind(clonedNode, ...data));
107
+ const uniqueCallback = this.$uniqueCallbacks.get(methodName);
108
+
109
+ // callback.length === 0 ? callback : this.$ndMethods[methodName].bind(clonedNode, ...data)
110
+ nd[methodName](uniqueCallback);
89
111
  }
90
112
  });
91
113
  }
@@ -147,6 +169,7 @@ NodeCloner.prototype.resolve = function() {
147
169
 
148
170
  this.cloneNode = (data) => {
149
171
  const clonedNode = $element.cloneNode(false);
172
+ $nodeClonedCallbackData.set(clonedNode, data);
150
173
  for(let i = 0; i < stepsCount; i++) {
151
174
  steps[i](clonedNode, data);
152
175
  }
@@ -188,11 +211,7 @@ NodeCloner.prototype.attach = function(methodName, callback) {
188
211
  */
189
212
  NodeCloner.prototype.text = function(valueOrProperty) {
190
213
  this.$content = valueOrProperty;
191
- if(typeof valueOrProperty === 'function') {
192
- this.cloneNode = (data) => createTextNode(valueOrProperty.apply(null, data));
193
- return this;
194
- }
195
- this.cloneNode = (data) => createTextNode(data[0][valueOrProperty]);
214
+ this.cloneNode = (data) => createTextNode(getPropertyValue(valueOrProperty, data));
196
215
  return this;
197
216
  };
198
217
 
@@ -96,6 +96,7 @@ export function TemplateCloner($fn) {
96
96
  $node.nodeCloner = new NodeCloner($node);
97
97
  }
98
98
  assignClonerToNode($node);
99
+
99
100
  if(this.$scopeDataBuilder) {
100
101
  this.clone = (data) => {
101
102
  const scopeData = this.scopeData();