CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ionic-native--core

TypeScript foundation for Ionic Native plugin wrappers providing decorators, base classes, and utilities for Cordova/Capacitor integration

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

promise-observable.mddocs/

Promise and Observable Utilities

Ionic Native Core provides comprehensive utilities for creating promises and wrapping plugin methods with Promise/Observable patterns, including special handling for Angular 1 integration and various callback styles.

Capabilities

Promise Creation

/**
 * Creates a promise with automatic Angular 1 integration
 * Falls back to native Promise if Angular 1 $q service is not available
 * @param callback - Function that receives resolve and reject callbacks
 * @returns Promise that integrates with Angular 1 digest cycle
 */
function getPromise<T>(
  callback: (resolve: Function, reject?: Function) => any
): Promise<T>;

Usage Example:

import { getPromise } from "@ionic-native/core";

const myPromise = getPromise<string>((resolve, reject) => {
  setTimeout(() => {
    resolve("Hello World");
  }, 1000);
});

myPromise.then(result => {
  console.log(result); // "Hello World"
});

Method Wrapping

/**
 * Wraps plugin methods with Promise/Observable support and error handling
 * Core utility used by the @Cordova decorator
 * @param pluginObj - Plugin instance or class
 * @param methodName - Name of the method to wrap
 * @param opts - Configuration options for wrapping behavior
 * @returns Wrapped function that returns Promise or Observable
 */
function wrap(
  pluginObj: any,
  methodName: string,
  opts?: CordovaOptions
): WrapFn;

type WrapFn = (...args: any[]) => any;

/**
 * Wraps plugin instance methods with Promise/Observable support
 * Used for methods that operate on _objectInstance
 * @param pluginObj - Plugin object with _objectInstance property
 * @param methodName - Name of the method to wrap
 * @param opts - Configuration options for wrapping behavior
 * @returns Wrapped function that returns Promise or Observable
 */
function wrapInstance(
  pluginObj: any,
  methodName: string,
  opts?: any
): Function;

Usage Examples:

import { wrap, wrapInstance } from "@ionic-native/core";

// Wrap a standard plugin method
const wrappedMethod = wrap(pluginObj, "getData", { 
  successIndex: 0, 
  errorIndex: 1 
});

// Usage returns a Promise
wrappedMethod(param1, param2).then(result => {
  console.log("Success:", result);
}).catch(error => {
  console.log("Error:", error);
});

// Wrap an instance method
const wrappedInstanceMethod = wrapInstance(pluginObj, "readFile", {
  observable: true
});

// Usage returns an Observable
wrappedInstanceMethod("path/to/file").subscribe(data => {
  console.log("File data:", data);
});

Low-level Plugin Execution

/**
 * Calls a Cordova plugin method with proper error handling and callback setup
 * Handles availability checking and argument positioning
 * @param pluginObj - Plugin instance or class
 * @param methodName - Method name to call
 * @param args - Arguments to pass to the method
 * @param opts - Configuration options
 * @param resolve - Success callback function
 * @param reject - Error callback function
 * @returns Result from plugin method or error object
 */
function callCordovaPlugin(
  pluginObj: any,
  methodName: string,
  args: any[],
  opts?: any,
  resolve?: Function,
  reject?: Function
): any;

/**
 * Calls a plugin instance method with proper error handling
 * Similar to callCordovaPlugin but for _objectInstance methods
 * @param pluginObj - Plugin object with _objectInstance
 * @param methodName - Method name to call on the instance
 * @param args - Arguments to pass to the method
 * @param opts - Configuration options
 * @param resolve - Success callback function
 * @param reject - Error callback function
 * @returns Result from instance method
 */
function callInstance(
  pluginObj: any,
  methodName: string,
  args: any[],
  opts?: any,
  resolve?: Function,
  reject?: Function
): any;

Advanced Configuration Options

Callback Style Handling

The wrapping utilities support various callback patterns:

// Standard callback order (success, error)
wrap(pluginObj, "method", {
  successIndex: 0,
  errorIndex: 1
});

// Reverse callback order (error, success)  
wrap(pluginObj, "method", {
  callbackOrder: "reverse"
});

// Node.js style callbacks (error, result)
wrap(pluginObj, "method", {
  callbackStyle: "node"
});

// Object-style callbacks
wrap(pluginObj, "method", {
  callbackStyle: "object",
  successName: "onSuccess",
  errorName: "onError"
});

Observable Configuration

// Basic Observable
wrap(pluginObj, "watchMethod", {
  observable: true
});

// Observable with cleanup function
wrap(pluginObj, "watchMethod", {
  observable: true,
  clearFunction: "clearWatch",
  clearWithArgs: true
});

// Event-based Observable
wrap(pluginObj, "eventMethod", {
  eventObservable: true,
  event: "deviceready",
  element: window
});

Synchronous Methods

// Synchronous method (no Promise wrapping)
wrap(pluginObj, "syncMethod", {
  sync: true
});

Internal Utilities

Argument Processing

/**
 * Processes arguments array to insert success/error callbacks at correct positions
 * @param args - Original arguments array
 * @param opts - Configuration options specifying callback positions
 * @param resolve - Success callback to insert
 * @param reject - Error callback to insert
 * @returns Modified arguments array with callbacks inserted
 */
function setIndex(
  args: any[],
  opts?: any,
  resolve?: Function,
  reject?: Function
): any;

Promise Wrapping Variants

Internal functions used by the core wrapping utilities:

/**
 * Wraps plugin method calls in promises with error handling
 * @param pluginObj - Plugin instance
 * @param methodName - Method to call
 * @param args - Method arguments
 * @param opts - Configuration options
 * @returns Promise that resolves with method result
 */
function wrapPromise(
  pluginObj: any,
  methodName: string,
  args: any[],
  opts?: CordovaOptions
): Promise<any>;

/**
 * Wraps plugin method calls in observables with cleanup support
 * @param pluginObj - Plugin instance
 * @param methodName - Method to call
 * @param args - Method arguments
 * @param opts - Configuration options
 * @returns Observable that emits method results
 */
function wrapObservable(
  pluginObj: any,
  methodName: string,
  args: any[],
  opts?: any
): Observable<any>;

/**
 * Wraps plugin methods that return promises natively
 * @param pluginObj - Plugin instance
 * @param methodName - Method to call
 * @param args - Method arguments
 * @param opts - Configuration options
 * @returns Promise that resolves with method result
 */
function wrapOtherPromise(
  pluginObj: any,
  methodName: string,
  args: any[],
  opts?: any
): Promise<any>;

/**
 * Creates observable from global event
 * @param event - Event name to listen for
 * @param element - Element to attach listener to (defaults to window)
 * @returns Observable that emits event data
 */
function wrapEventObservable(event: string, element?: any): Observable<any>;

Angular 1 Integration

The promise utilities automatically detect and integrate with Angular 1:

// Automatically uses Angular's $q service if available
const promise = getPromise((resolve, reject) => {
  // Promise will integrate with Angular's digest cycle
  resolve(data);
});

// Falls back to native Promise if Angular not available

This ensures that promise resolutions trigger Angular's change detection when used in Angular 1 applications.

Error Handling Patterns

The utilities provide consistent error handling:

// Promises automatically catch and handle plugin errors
wrap(pluginObj, "method").then(result => {
  // Success
}).catch(error => {
  if (error.error === "cordova_not_available") {
    // Handle Cordova not available
  } else if (error.error === "plugin_not_installed") {
    // Handle plugin not installed
  }
});

// Observables handle errors in the error callback
wrap(pluginObj, "method", { observable: true }).subscribe(
  result => {
    // Success
  },
  error => {
    // Handle error
  }
);

docs

availability.md

base-plugin.md

decorators.md

index.md

promise-observable.md

tile.json