MCPcopy
hub / github.com/ionic-team/capacitor / createPluginMethodWrapper

Function createPluginMethodWrapper

core/src/runtime.ts:100–134  ·  view source on GitHub ↗
(prop: PropertyKey)

Source from the content-addressed store, hash-verified

98 };
99
100 const createPluginMethodWrapper = (prop: PropertyKey) => {
101 let remove: (() => void) | undefined;
102 const wrapper = (...args: any[]) => {
103 const p = loadPluginImplementation().then((impl) => {
104 const fn = createPluginMethod(impl, prop);
105
106 if (fn) {
107 const p = fn(...args);
108 remove = p?.remove;
109 return p;
110 } else {
111 throw new CapacitorException(
112 `"${pluginName}.${prop as any}()" is not implemented on ${platform}`,
113 ExceptionCode.Unimplemented,
114 );
115 }
116 });
117
118 if (prop === 'addListener') {
119 (p as any).remove = async () => remove();
120 }
121
122 return p;
123 };
124
125 // Some flair ✨
126 wrapper.toString = () => `${prop.toString()}() { [capacitor code] }`;
127 Object.defineProperty(wrapper, 'name', {
128 value: prop,
129 writable: false,
130 configurable: false,
131 });
132
133 return wrapper;
134 };
135
136 const addListener = createPluginMethodWrapper('addListener');
137 const removeListener = createPluginMethodWrapper('removeListener');

Callers 2

registerPluginFunction · 0.85
getFunction · 0.85

Calls 1

toStringMethod · 0.45

Tested by

no test coverage detected