Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ssl configuration to addListener #2243

Merged
merged 8 commits into from Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/sync.js
Expand Up @@ -87,6 +87,15 @@
* certificate has the highest depth. The certificate of highest depth will be presented first.
*/

/**
* This describes the different options used when adding a Global Notifier listener.
* @typedef {Object} Realm.Sync~RealmListenerConfiguration
* @property {string} serverUrl - The sync server to listen to.
* @property {SyncUser} adminUser - an admin user obtained by calling {@linkcode Realm.Sync.User.login|User.login} with admin credentials.
* @property {string} filterRegex - A regular expression used to determine which changed Realms should trigger events. Use `.*` to match all Realms.
* @property {Realm.Sync.SSLConfiguration} sslConfiguration - SSL configuration used by the Realms being observed.
*/

/**
* When opening a Realm created with Realm Mobile Platform v1.x, it is automatically
* migrated to the v2.x format. In case this migration
Expand Down Expand Up @@ -125,9 +134,38 @@ class Sync {
* passed as an argument.
*
* Only available in the Enterprise Edition.
* @deprecated Use `addListener(config, eventName, changeCallback)` instead`.
*/
static addListener(serverUrl, adminUser, filterRegex, name, changeCallback) {}

/**
* Add a sync listener to listen to changes across multiple Realms.
*
* @param {Realm.Sync.RealmListenerConfiguration} config - The configuration object for Realms being observed.
* @param {string} eventName - The name of the event to observe.
* @param {function(changeEvent)} changeCallback - The callback to invoke with the events.
*
* Registers the `changeCallback` to be called each time the given event occurs on the specified server.
* Only events on Realms with a _virtual path_ that matches the filter regex are emitted.
*
* Currently supported events:
*
* * `'available'`: Emitted whenever there is a new Realm which has a virtual
* path matching the filter regex, either due to the Realm being newly created
* or the listener being added. The virtual path (i.e. the portion of the
* URL after the protocol and hostname) is passed as an argument.
* * `'change'`: Emitted whenever the data within a Realm matching the filter
* regex has changed. A [ChangeEvent]{@link Realm.Sync.ChangeEvent} argument
* is passed containing information about which Realm changed and what
* objects within the Realm changed.
* * `'delete'`: Emitted whenever a Realm matching the filter regex has been
* deleted from the server. The virtual path of the Realm being deleted is
* passed as an argument.
*
* Only available in the Enterprise Edition.
*/
static addListener(config, eventName, changeCallback) {}

/**
* Add a sync listener to listen to changes across multiple Realms.
*
Expand Down
21 changes: 19 additions & 2 deletions lib/index.d.ts
Expand Up @@ -543,8 +543,25 @@ declare namespace Realm.Sync {
readonly realm: Realm;
}

function addListener(serverURL: string, adminUser: Realm.Sync.User, regex: string, name: string, changeCallback: (changeEvent: ChangeEvent) => void): void;
function addListener(serverURL: string, adminUser: Realm.Sync.User, regex: string, name: string, changeCallback: (changeEvent: ChangeEvent) => Promise<void>): void;
type RealmListenerEventName = 'available' | 'change' | 'delete';

interface RealmListenerConfiguration {
serverUrl: string;
adminUser: User;
filterRegex: string;
sslConfiguration?: SSLConfiguration;
}

/**
* @deprecated, to be removed in future versions
*/
function addListener(serverURL: string, adminUser: Realm.Sync.User, regex: string, name: RealmListenerEventName, changeCallback: (changeEvent: ChangeEvent) => void): void;
/**
* @deprecated, to be removed in future versions
*/
function addListener(serverURL: string, adminUser: Realm.Sync.User, regex: string, name: RealmListenerEventName, changeCallback: (changeEvent: ChangeEvent) => Promise<void>): void;
function addListener(config: RealmListenerConfiguration, eventName: RealmListenerEventName, changeCallback: (changeEvent: ChangeEvent) => void): void;
function addListener(config: RealmListenerConfiguration, eventName: RealmListenerEventName, changeCallback: (changeEvent: ChangeEvent) => Promise<void>): void;
function removeAllListeners(): Promise<void>;
function removeListener(regex: string, name: string, changeCallback: (changeEvent: ChangeEvent) => void): Promise<void>;
function setLogLevel(logLevel: 'all' | 'trace' | 'debug' | 'detail' | 'info' | 'warn' | 'error' | 'fatal' | 'off'): void;
Expand Down
27 changes: 22 additions & 5 deletions lib/notifier.js
Expand Up @@ -143,8 +143,8 @@ class OutOfProcListener {
}

class Listener {
constructor(Sync, server, user) {
this.notifier = Sync._createNotifier(server, user, (event, a1, a2) => this[event](a1, a2));
constructor(Sync, config) {
this.notifier = Sync._createNotifier(config.serverUrl, config.adminUser, (event, a1, a2) => this[event](a1, a2), config.sslConfiguration);
this.initPromises = [];
this.callbacks = [];
}
Expand Down Expand Up @@ -256,10 +256,27 @@ class Listener {

let listener;
function addListener(server, user, regex, event, callback) {
// Assume the new API is used, but we cannot change the method signature without it being a breaking change.
let config = server;
let _event = user;
let _callback = regex;

// If the old API is used, convert all arguments to the new API
if (arguments.length === 5) {
config = {
serverUrl: server,
adminUser: user,
filterRegex: regex
// SSL Configuration not supported in the old API
};
_event = event;
_callback = callback;
}

if (!listener) {
listener = new Listener(this, server, user);
listener = new Listener(this, config);
}
return listener.add(regex, event, callback);
return listener.add(config.filterRegex, _event, _callback);
}

function removeListener(regex, event, callback) {
Expand Down Expand Up @@ -298,4 +315,4 @@ module.exports = function(Realm) {
Realm.Sync.addListener = addListener.bind(Realm.Sync);
Realm.Sync.removeListener = removeListener;
Realm.Sync.removeAllListeners = removeAllListeners;
}
};