2017-12-04 12:07:41 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Dynamic Links representation wrapper
|
|
|
|
*/
|
2018-03-30 08:25:41 +00:00
|
|
|
import { Platform } from 'react-native';
|
2018-03-23 13:24:31 +00:00
|
|
|
import DynamicLink from './DynamicLink';
|
2017-12-22 15:24:31 +00:00
|
|
|
import { SharedEventEmitter } from '../../utils/events';
|
2018-03-22 12:46:37 +00:00
|
|
|
import { getLogger } from '../../utils/log';
|
2017-12-22 15:24:31 +00:00
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
2018-01-05 17:20:02 +00:00
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-07-19 11:32:17 +00:00
|
|
|
|
2018-02-14 13:00:19 +00:00
|
|
|
import type App from '../core/app';
|
2017-12-04 12:07:41 +00:00
|
|
|
|
2018-03-23 10:33:17 +00:00
|
|
|
const NATIVE_EVENTS = ['links_link_received'];
|
2017-12-22 15:57:33 +00:00
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export const MODULE_NAME = 'RNFirebaseLinks';
|
|
|
|
export const NAMESPACE = 'links';
|
|
|
|
|
2017-07-19 11:32:17 +00:00
|
|
|
/**
|
|
|
|
* @class Links
|
|
|
|
*/
|
2017-09-30 23:48:50 +00:00
|
|
|
export default class Links extends ModuleBase {
|
2018-01-05 17:20:02 +00:00
|
|
|
constructor(app: App) {
|
|
|
|
super(app, {
|
2018-01-03 20:00:38 +00:00
|
|
|
events: NATIVE_EVENTS,
|
|
|
|
moduleName: MODULE_NAME,
|
2018-01-09 17:31:00 +00:00
|
|
|
multiApp: false,
|
2017-11-21 23:37:05 +00:00
|
|
|
hasShards: false,
|
2018-01-03 20:00:38 +00:00
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
2018-03-22 12:46:37 +00:00
|
|
|
|
|
|
|
SharedEventEmitter.addListener(
|
|
|
|
// sub to internal native event - this fans out to
|
|
|
|
// public event name: onMessage
|
|
|
|
'links_link_received',
|
|
|
|
(link: string) => {
|
|
|
|
SharedEventEmitter.emit('onLink', link);
|
|
|
|
}
|
|
|
|
);
|
2018-03-30 08:25:41 +00:00
|
|
|
|
|
|
|
// Tell the native module that we're ready to receive events
|
2018-03-30 08:52:37 +00:00
|
|
|
if (Platform.OS === 'ios') {
|
2018-03-30 08:25:41 +00:00
|
|
|
getNativeModule(this).jsInitialised();
|
|
|
|
}
|
2017-07-19 11:32:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-08 15:43:59 +00:00
|
|
|
/**
|
|
|
|
* Create long Dynamic Link from parameters
|
|
|
|
* @param parameters
|
|
|
|
* @returns {Promise.<String>}
|
|
|
|
*/
|
2018-03-23 13:24:31 +00:00
|
|
|
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
|
|
|
);
|
2017-10-09 00:42:57 +00:00
|
|
|
}
|
2018-03-23 17:03:53 +00:00
|
|
|
try {
|
|
|
|
return getNativeModule(this).createDynamicLink(link.build());
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
2017-07-19 11:32:17 +00:00
|
|
|
}
|
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>}
|
|
|
|
*/
|
2018-03-23 13:24:31 +00:00
|
|
|
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
|
|
|
);
|
2017-10-09 00:42:57 +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
|
|
|
}
|
2018-03-22 12:46:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the link that triggered application open
|
|
|
|
* @returns {Promise.<String>}
|
|
|
|
*/
|
2018-03-23 17:03:53 +00:00
|
|
|
getInitialLink(): Promise<?string> {
|
2018-03-22 12:46:37 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
2017-07-19 11:32:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 14:36:16 +00:00
|
|
|
export const statics = {
|
|
|
|
DynamicLink,
|
|
|
|
};
|