You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I learn capacitor source code recently. I meet some questions. Hoping getting help here.
Above swift code inject plugin method code to javascript. But in the @capacitor/core package, runtime.ts files defaultRegisterPlugin` method do the same work with native code. Does it the native code is no more needed.
// runtime.tsconstdefaultRegisterPlugin=(pluginName: string,jsImplementations: PluginImplementations={},): any=>{constregisteredPlugin=registeredPlugins.get(pluginName);if(registeredPlugin){console.warn(`Capacitor plugin "${pluginName}" already registered. Cannot register plugins twice.`,);returnregisteredPlugin.proxy;}constplatform=getPlatform();constpluginHeader=getPluginHeader(pluginName);
let jsImplementation: any;constloadPluginImplementation=async(): Promise<any>=>{if(!jsImplementation&&platforminjsImplementations){jsImplementation=typeofjsImplementations[platform]==='function'
? (jsImplementation=awaitjsImplementations[platform]())
: (jsImplementation=jsImplementations[platform]);}elseif(capCustomPlatform!==null&&!jsImplementation&&'web'injsImplementations){jsImplementation=typeofjsImplementations['web']==='function'
? (jsImplementation=awaitjsImplementations['web']())
: (jsImplementation=jsImplementations['web']);}returnjsImplementation;};constcreatePluginMethod=(impl: any,prop: PropertyKey,): ((...args: any[])=>any)=>{if(pluginHeader){constmethodHeader=pluginHeader?.methods.find(m=>prop===m.name);if(methodHeader){if(methodHeader.rtype==='promise'){return(options: any)=>cap.nativePromise(pluginName,prop.toString(),options);}else{return(options: any,callback: any)=>cap.nativeCallback(pluginName,prop.toString(),options,callback,);}}elseif(impl){returnimpl[prop]?.bind(impl);}}elseif(impl){returnimpl[prop]?.bind(impl);}else{thrownewCapacitorException(`"${pluginName}" plugin is not implemented on ${platform}`,ExceptionCode.Unimplemented,);}};constcreatePluginMethodWrapper=(prop: PropertyKey)=>{
let remove: (()=>void)|undefined;constwrapper=(...args: any[])=>{constp=loadPluginImplementation().then(impl=>{constfn=createPluginMethod(impl,prop);if(fn){constp=fn(...args);remove=p?.remove;returnp;}else{thrownewCapacitorException(`"${pluginName}.${propasany}()" is not implemented on ${platform}`,ExceptionCode.Unimplemented,);}});if(prop==='addListener'){(pasany).remove=async()=>remove();}returnp;};// Some flair ✨wrapper.toString=()=>`${prop.toString()}() { [capacitor code] }`;Object.defineProperty(wrapper,'name',{value: prop,writable: false,configurable: false,});returnwrapper;};constaddListener=createPluginMethodWrapper('addListener');constremoveListener=createPluginMethodWrapper('removeListener');constaddListenerNative=(eventName: string,callback: any)=>{constcall=addListener({ eventName },callback);constremove=async()=>{constcallbackId=awaitcall;removeListener({
eventName,
callbackId,},callback,);};constp=newPromise(resolve=>call.then(()=>resolve({ remove })));(pasany).remove=async()=>{console.warn(`Using addListener() without 'await' is deprecated.`);awaitremove();};returnp;};constproxy=newProxy({},{get(_,prop){switch(prop){// https://github.com/facebook/react/issues/20030case'$$typeof':
returnundefined;case'toJSON':
return()=>({});case'addListener':
returnpluginHeader ? addListenerNative : addListener;case'removeListener':
returnremoveListener;default:
returncreatePluginMethodWrapper(prop);}},},);Plugins[pluginName]=proxy;registeredPlugins.set(pluginName,{name: pluginName,
proxy,platforms: newSet([
...Object.keys(jsImplementations),
...(pluginHeader ? [platform] : []),]),});returnproxy;};constregisterPlugin=capPlatforms?.currentPlatform?.registerPlugin||defaultRegisterPlugin;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I learn capacitor source code recently. I meet some questions. Hoping getting help here.
Above swift code inject plugin method code to javascript. But in the
@capacitor/core
package,runtime.ts
files
defaultRegisterPlugin` method do the same work with native code. Does it the native code is no more needed.Beta Was this translation helpful? Give feedback.
All reactions