Skip to content

Commit 8ebffcb

Browse files
committed
Improve extend handling
1 parent 4577598 commit 8ebffcb

1 file changed

Lines changed: 67 additions & 59 deletions

File tree

test-app/app/src/main/assets/internal/ts_helpers.js

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(function() {
2-
var __extends_ts = function (d, b) {
2+
var __extends_ns = function (d, b) {
33
if (!b.extend) {
44
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
55
}
@@ -9,6 +9,17 @@
99
d.prototype = new __();
1010
};
1111

12+
var extendStatics = Object.setPrototypeOf ||
13+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
14+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
15+
16+
var __extends_ts = function (d, b) {
17+
extendStatics(d, b);
18+
function __() { this.constructor = d; }
19+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20+
};
21+
22+
1223
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
1324
var c = arguments.length;
1425
var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -59,68 +70,65 @@
5970
};
6071

6172
var __extends = function(Child, Parent) {
73+
var extendNativeClass = !!Parent.extend && /native/.test(Parent.extend.toString());
74+
if (!extendNativeClass) {
75+
__extends_ts(Child, Parent);
76+
return;
77+
}
78+
if (Parent.__isPrototypeImplementationObject) {
79+
throw new Error("Can not extend an already extended native object.");
80+
}
6281

63-
if (Parent.extend) {
64-
if (Parent.__isPrototypeImplementationObject) {
65-
throw new Error("Can not extend an already extended native object.");
66-
}
67-
68-
function extend(thiz) {
69-
var child = thiz.__proto__.__child;
70-
if (!child.__extended) {
71-
var parent = thiz.__proto__.__parent;
72-
child.__extended = parent.extend(child.name, child.prototype, true);
73-
// This will deal with "i instanceof child"
74-
child[Symbol.hasInstance] = function(instance) {
75-
return instance instanceof this.__extended;
76-
}
77-
}
78-
return child.__extended;
79-
};
80-
81-
Parent.__activityExtend = function(parent, name, implementationObject) {
82-
__log("__activityExtend called");
83-
return parent.extend(name, implementationObject);
84-
};
85-
86-
Parent.call = function(thiz) {
87-
var Extended = extend(thiz);
88-
thiz.__container__ = true;
89-
if (arguments.length > 1)
90-
{
91-
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1))));
92-
}
93-
else
94-
{
95-
thiz.__proto__ = new Extended()
96-
}
97-
return thiz.__proto__;
98-
};
99-
100-
Parent.apply = function(thiz, args) {
101-
var Extended = extend(thiz);
102-
thiz.__container__ = true;
103-
if (args && args.length > 0)
104-
{
105-
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args)));
82+
function extend(thiz) {
83+
var child = thiz.__proto__.__child;
84+
if (!child.__extended) {
85+
var parent = thiz.__proto__.__parent;
86+
child.__extended = parent.extend(child.name, child.prototype, true);
87+
// This will deal with "i instanceof child"
88+
child[Symbol.hasInstance] = function(instance) {
89+
return instance instanceof this.__extended;
10690
}
107-
else
108-
{
109-
thiz.__proto__ = new Extended();
110-
}
111-
return thiz.__proto__;
112-
};
113-
}
91+
}
92+
return child.__extended;
93+
};
11494

115-
__extends_ts(Child, Parent);
95+
Parent.__activityExtend = function(parent, name, implementationObject) {
96+
__log("__activityExtend called");
97+
return parent.extend(name, implementationObject);
98+
};
11699

100+
Parent.call = function(thiz) {
101+
var Extended = extend(thiz);
102+
thiz.__container__ = true;
103+
if (arguments.length > 1)
104+
{
105+
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1))));
106+
}
107+
else
108+
{
109+
thiz.__proto__ = new Extended()
110+
}
111+
return thiz.__proto__;
112+
};
117113

118-
if (Parent.extend) {
119-
Child.__isPrototypeImplementationObject = true;
120-
Child.__proto__ = Parent;
121-
Child.prototype.__parent = Parent;
122-
Child.prototype.__child = Child;
123-
}
114+
Parent.apply = function(thiz, args) {
115+
var Extended = extend(thiz);
116+
thiz.__container__ = true;
117+
if (args && args.length > 0)
118+
{
119+
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args)));
120+
}
121+
else
122+
{
123+
thiz.__proto__ = new Extended();
124+
}
125+
return thiz.__proto__;
126+
};
127+
__extends_ns(Child, Parent);
128+
Child.__isPrototypeImplementationObject = true;
129+
Child.__proto__ = Parent;
130+
Child.prototype.__parent = Parent;
131+
Child.prototype.__child = Child;
124132
}
125133

126134
function JavaProxy(className) {
@@ -146,4 +154,4 @@
146154

147155
global.JavaProxy = JavaProxy;
148156
global.Interfaces = Interfaces;
149-
})()
157+
})()

0 commit comments

Comments
 (0)