react-native-firebase/lib/modules/links/index.js

118 lines
2.9 KiB
JavaScript
Raw Normal View History

/**
* @flow
* Dynamic Links representation wrapper
*/
import { Platform } from 'react-native';
import DynamicLink from './DynamicLink';
import { SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log';
import ModuleBase from '../../utils/ModuleBase';
import { getNativeModule } from '../../utils/native';
import type App from '../core/app';
const NATIVE_EVENTS = ['links_link_received'];
export const MODULE_NAME = 'RNFirebaseLinks';
export const NAMESPACE = 'links';
/**
* @class Links
*/
2017-09-30 23:48:50 +00:00
export default class Links extends ModuleBase {
constructor(app: App) {
super(app, {
events: NATIVE_EVENTS,
moduleName: MODULE_NAME,
multiApp: false,
hasShards: false,
namespace: NAMESPACE,
});
SharedEventEmitter.addListener(
// sub to internal native event - this fans out to
// public event name: onMessage
'links_link_received',
(link: string) => {
SharedEventEmitter.emit('onLink', link);
}
);
// Tell the native module that we're ready to receive events
2018-03-30 08:52:37 +00:00
if (Platform.OS === 'ios') {
getNativeModule(this).jsInitialised();
}
}
2017-10-08 15:43:59 +00:00
/**
* Create long Dynamic Link from parameters
* @param parameters
* @returns {Promise.<String>}
*/
createDynamicLink(link: DynamicLink): Promise<string> {
2018-03-23 13:26:20 +00:00
if (!(link instanceof DynamicLink)) {
2018-03-23 17:03:53 +00:00
return Promise.reject(
new Error(
`Links:createDynamicLink expects a 'DynamicLink' but got type ${typeof link}`
)
2018-03-23 13:26:20 +00:00
);
}
2018-03-23 17:03:53 +00:00
try {
return getNativeModule(this).createDynamicLink(link.build());
} catch (error) {
return Promise.reject(error);
}
}
2017-08-20 14:57:06 +00:00
2017-10-08 15:43:59 +00:00
/**
* Create short Dynamic Link from parameters
* @param parameters
* @returns {Promise.<String>}
*/
createShortDynamicLink(
link: DynamicLink,
type?: 'SHORT' | 'UNGUESSABLE'
): Promise<String> {
2018-03-23 13:26:20 +00:00
if (!(link instanceof DynamicLink)) {
2018-03-23 17:03:53 +00:00
return Promise.reject(
new Error(
`Links:createShortDynamicLink expects a 'DynamicLink' but got type ${typeof link}`
)
2018-03-23 13:26:20 +00:00
);
}
2018-03-23 17:03:53 +00:00
try {
return getNativeModule(this).createShortDynamicLink(link.build(), type);
} catch (error) {
return Promise.reject(error);
}
2017-08-21 16:20:44 +00:00
}
/**
* Returns the link that triggered application open
* @returns {Promise.<String>}
*/
2018-03-23 17:03:53 +00:00
getInitialLink(): Promise<?string> {
return getNativeModule(this).getInitialLink();
}
/**
* Subscribe to dynamic links
* @param listener
* @returns {Function}
*/
onLink(listener: string => any): () => any {
getLogger(this).info('Creating onLink listener');
SharedEventEmitter.addListener('onLink', listener);
return () => {
getLogger(this).info('Removing onLink listener');
SharedEventEmitter.removeListener('onLink', listener);
};
}
}
2018-03-23 14:36:16 +00:00
export const statics = {
DynamicLink,
};