Migrate old externs 3/x

This commit is contained in:
2026-06-14 07:28:00 +08:00
parent 77a1e5c22e
commit 7cdcd3b681
23 changed files with 251 additions and 187 deletions
+144 -88
View File
@@ -26,9 +26,11 @@ var __const = __static_method;
/* End Shorthand Functions }}}*/
/*{{{ BotanEvent & EventDispatcher */
/** @constructor */
/** @this {BotanEvent} */
var BotanEvent = function( name, data )
/** @type {typeof BotanEvent} */
var BotanEvent;
BotanEvent = function( name, data )
{
var __propagating = false;
var __propagated = false;
@@ -66,69 +68,155 @@ var BotanEvent = function( name, data )
}.bind( this );
};
/** type {typeof EventDispatcher} */
var EventDispatcher;
EventDispatcher = function() {
/**
* @constructor
* @implements {EventTarget}
*/
var EventDispatcher = function() {
var events = {};
var _self = this;
var getStack = function( name )
var getStack = function(name)
{
if( !events[ name ] ) events[ name ] = [];
return events[ name ];
if (!events[name]) events[name] = [];
return events[name];
};
var _dispatch = function()
{
this.evt.propagate();
for( var i in this.stack )
for (var i in this.stack)
{
if( this.evt.propagating )
if (this.evt.propagating)
{
this.stack[ i ]( this.evt );
this.stack[i](this.evt);
}
}
this.evt.stopPropagating();
};
this.addEventListener = function( type, handler )
{
var stack = getStack( type );
stack[ stack.length ] = handler;
};
this.removeEventListener = function( type, handler )
{
var stack = getStack( type );
var i = stack.indexOf( handler );
if( i < 0 ) return;
delete stack[ i ];
};
/** @type {Function}
* @param {BotanEvent} evt
/**
* @param {string} type
* @param {function(!BotanEvent): void} handler
* @return {void}
*/
this.dispatchEvent = function( _evt )
this.addEventListener = function(type, handler)
{
var _stack = getStack( _evt.type );
if( _evt.setTarget ) _evt.setTarget( _self );
// Dispatch the event asynchronously
setTimeout( _dispatch.bind({ evt: _evt, stack: _stack }), 0 );
var stack = getStack(type);
stack[stack.length] = handler;
};
/**
* @param {string} type
* @param {function(!BotanEvent): void} handler
* @return {void}
*/
this.removeEventListener = function(type, handler)
{
var stack = getStack(type);
var i = stack.indexOf(handler);
if (i < 0) return;
delete stack[i];
};
/**
* @param {!BotanEvent} _evt
* @return {boolean}
*/
this.dispatchEvent = function(_evt)
{
var _stack = getStack(_evt.type);
if (_evt.setTarget) _evt.setTarget(_self);
setTimeout(_dispatch.bind({ evt: _evt, stack: _stack }), 0);
return true;
};
};
/* End BotanEvent & EventDispatcher }}}*/
/** @type {Array}
* @extends {EventDispatcher}
/**
* @constructor
* @extends {EventDispatcher}
* @param {!Object<string, *>} target
* @param {string} name
*/
var NamespaceObj = function() {
EventDispatcher.call( this );
var NamespaceObj = function(target, name)
{
EventDispatcher.call(this);
/** @private {!Object<string, *>} */
this.t_ = target;
/** @private {string} */
this.n_ = name;
};
NamespaceObj.prototype = new Array();
NamespaceObj.prototype = Object.create(EventDispatcher.prototype);
NamespaceObj.prototype.constructor = NamespaceObj;
/**
* @param {string} type
* @param {string} name
* @param {*} obj
* @return {void}
*/
NamespaceObj.prototype.exportSymbol = function(type, name, obj)
{
if (this.t_[name]) return;
this.t_[name] = [type, obj];
var evt = new BotanEvent("NS_EXPORT", {
"name": this.n_ + "." + name,
"type": type
});
BotanJS.dispatchEvent(evt);
};
/**
* @param {string} target
* @return {*}
*/
NamespaceObj.prototype.invoke = function(target)
{
if (!this.t_[target])
{
throw new Error(
"[" + this.n_ + "] "
+ "Invoke failed: " + target + " does not exists"
);
}
return this.t_[target][1];
};
/**
* @param {string} code
* @param {function(...?): *} func
* @return {void}
*/
NamespaceObj.prototype.trigger = function(code, func)
{
this.t_.__TRIGGERS[code] = func;
};
/**
* @param {string} message
* @param {string=} subclass
* @return {void}
*/
NamespaceObj.prototype.throwError = function(message, subclass)
{
subclass = subclass ? ("." + subclass) : "";
throw new Error(
"[" + this.n_ + subclass + "] " + message
);
};
var packages = {};
var _global = {};
@@ -193,65 +281,33 @@ BotanJS = BotanJS || (function()
/* End Root level bug-free handlers }}}*/
/*{{{ Namespace declarator */
__namespace = __namespace || function( ns )
__namespace = __namespace || function(ns)
{
if( _NSs[ ns ] ) return _NSs[ ns ];
if (_NSs[ns]) return _NSs[ns];
var p = ns.split(".");
var l = p.length;
var target = packages;
for( var i = 0; i < l; i ++ ) {
target[ p[i] ] = target[ p[i] ] || {};
target = target[ p[i] ];
for (var i = 0; i < l; i++)
{
target[p[i]] = target[p[i]] || {};
target = target[p[i]];
}
target.__TRIGGERS = [];
/** @type {!Object<string, function(...?): *>} */
target.__TRIGGERS = {};
var nsObj = new NamespaceObj;
nsObj[ NS_EXPORT ] = function( type, name, obj )
{
if( this.t[ name ] ) return;
this.t[ name ] = [ type, obj ];
var nsObj = new NamespaceObj(target, ns);
/** @type {BotanEvent} */
var evt = new BotanEvent( "NS_EXPORT", {
"name": this.n + "." + name
, "type": type
});
BotanJS.dispatchEvent(new BotanEvent("NS_INIT", ns));
BotanJS.dispatchEvent( evt );
nsObj[NS_EXPORT] = nsObj.exportSymbol.bind(nsObj);
nsObj[NS_INVOKE] = nsObj.invoke.bind(nsObj);
nsObj[NS_TRIGGER] = nsObj.trigger.bind(nsObj);
nsObj[NS_THROW] = nsObj.throwError.bind(nsObj);
}.bind({ t: target, n: ns });
nsObj[ NS_INVOKE ] = function( target )
{
if( !this.t[ target ] )
{
throw new Error(
"[" + this.n + "] "
+ "Invoke failed: " + target + " does not exists"
);
}
return this.t[ target ][1];
}.bind({ t: target, n: ns });
nsObj[ NS_TRIGGER ] = function( code, func )
{
this.__TRIGGERS[ code ] = func;
}.bind( target );
nsObj[ NS_THROW ] = function( message, subclass )
{
subclass = subclass ? ( "." + subclass ) : "";
throw new Error(
"[" + this.n + subclass + "] " + message
);
}.bind({ n: ns });
BotanJS.dispatchEvent( new BotanEvent( "NS_INIT", ns ) );
return ( _NSs[ ns ] = nsObj );
return (_NSs[ns] = nsObj);
};
/* End Namespace declarator }}}*/
@@ -261,7 +317,7 @@ __import = __import || function( ns, noCache )
var nss = ns.replace( ".*", "" );
if( _NSs[ nss ] )
{
_NSs[ nss ].dispatchEvent( new BotanEvent( "NS_IMPORT", target ) );
_NSs[ nss ].dispatchEvent( new BotanEvent( "NS_IMPORT", nss ) );
}
// Read The Cache First