Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/7/core-classes/kuzzle/properties/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ order: 10
| `connected` | <pre>boolean</pre> | Returns `true` if the SDK is currently connected to a Kuzzle server. |
| `offlineQueue` | <pre>object[]</pre> | Contains the queued requests during offline mode |
| `protocol` | <pre>Protocol</pre> | Protocol used by the SDK |
| `events` | <pre>string[]</pre> | List of every events emitted by the SDK |


### connected

Expand Down Expand Up @@ -42,7 +44,7 @@ See the associated documentation:

The `authenticator` property can be set to a function returning a promise.

This function will be called after a successful reconnection if the current authentication token is not valid anymore.
This function will be called after a successful reconnection if the current authentication token is not valid anymore.

This function has to authenticate the SDK. It can be a call to [auth.login](/sdk/js/7/controllers/auth/login) for example.

Expand Down
11 changes: 11 additions & 0 deletions doc/7/essentials/events/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ An event system allows to be notified when the SDK status changes. These events

The API for interacting with events is described by our [KuzzleEventEmitter](/sdk/js/7/core-classes/kuzzle-event-emitter) class documentation.

::: info
You can listen to every events on the SDK by using the events property:

```js
for (const event of kuzzle.events) {
kuzzle.on(event, (...args) => console.log(event, ...args));
}
```

:::

**Note:** listeners are called in the order of their insertion.

# Emitted Events
Expand Down
112 changes: 66 additions & 46 deletions src/Kuzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,69 +22,79 @@ import { RequestTimeoutError } from './RequestTimeoutError';
// Defined by webpack plugin
declare const SDKVERSION: any;

const events = [
'connected',
'discarded',
'disconnected',
'loginAttempt',
'logoutAttempt',
'networkError',
'offlineQueuePush',
'offlineQueuePop',
'queryError',
'reAuthenticated',
'reconnected',
'reconnectionError',
'tokenExpired'
];

export class Kuzzle extends KuzzleEventEmitter {
// We need to define any string key because users can register new controllers
[key: string]: any;

/**
* Protocol used by the SDK to communicate with Kuzzle.
*/
protocol: any;
public protocol: any;

/**
* If true, automatically renews all subscriptions on a reconnected event.
*/
autoResubscribe: boolean;
public autoResubscribe: boolean;

/**
* Timeout before sending again a similar event.
*/
eventTimeout: number;
public eventTimeout: number;

/**
* SDK version.
*/
sdkVersion: string;
public sdkVersion: string;

/**
* SDK name (e.g: `js@7.4.2`).
*/
sdkName: string;
public sdkName: string;

/**
* Common volatile data that will be sent to all future requests.
*/
volatile: JSONObject;
public volatile: JSONObject;

/**
* Handle deprecation warning in development mode (hidden in production)
*/
deprecationHandler: Deprecation;
public deprecationHandler: Deprecation;

/**
* Authenticator function called after a reconnection if the SDK is no longer
* authenticated.
*/
authenticator: () => Promise<void> = null;

auth: AuthController;
bulk: any;
collection: CollectionController;
document: DocumentController;
index: IndexController;
ms: any;
realtime: RealtimeController;
security: SecurityController;
server: any;
public authenticator: () => Promise<void> = null;

/**
* List of every events emitted by the SDK.
*/
public events = [
'connected',
'discarded',
'disconnected',
'loginAttempt',
'logoutAttempt',
'networkError',
'offlineQueuePush',
'offlineQueuePop',
'queryError',
'reAuthenticated',
'reconnected',
'reconnectionError',
'tokenExpired',
];

public auth: AuthController;
public bulk: any;
public collection: CollectionController;
public document: DocumentController;
public index: IndexController;
public ms: any;
public realtime: RealtimeController;
public security: SecurityController;
public server: any;

private _protectedEvents: any;
private _offlineQueue: any;
Expand Down Expand Up @@ -336,7 +346,7 @@ export class Kuzzle extends KuzzleEventEmitter {
this._loggedIn = true;
return;
}

/**
* In case of login failure we need to be sure that the stored token is still valid
*/
Expand Down Expand Up @@ -376,8 +386,11 @@ export class Kuzzle extends KuzzleEventEmitter {
}) as Kuzzle;
}

/**
* Returns `true` if the SDK holds a valid token
*/
get authenticated () {
return this.auth.authenticationToken && !this.auth.authenticationToken.expired;
return Boolean(this.auth.authenticationToken && ! this.auth.authenticationToken.expired);
}

get autoQueue () {
Expand Down Expand Up @@ -407,10 +420,17 @@ export class Kuzzle extends KuzzleEventEmitter {
this._autoReplay = value;
}

/**
* Returns `true` if the SDK is using the cookie authentication mode.
* (Web only)
*/
get cookieAuthentication () {
return this._cookieAuthentication;
}

/**
* Returns `true` if the SDK is currently connected to a Kuzzle server.
*/
get connected () {
return this.protocol.connected;
}
Expand Down Expand Up @@ -517,7 +537,7 @@ export class Kuzzle extends KuzzleEventEmitter {
* Emit an event to all registered listeners
* An event cannot be emitted multiple times before a timeout has been reached.
*/
emit (eventName, ...payload) {
emit (eventName: string, ...payload) {
const
now = Date.now(),
protectedEvent = this._protectedEvents[eventName];
Expand Down Expand Up @@ -593,26 +613,26 @@ export class Kuzzle extends KuzzleEventEmitter {
if (this._reconnectInProgress) {
return;
}

if (this.autoQueue) {
this.stopQueuing();
}

// If an authenticator was set, check if a user was logged in and if the token is still valid and try
// to re-authenticate if needed. Otherwise the SDK is in disconnected state.
if ( this._loggedIn
&& ! await this.tryReAuthenticate()
) {
this._loggedIn = false;
this.disconnect();

return;
}

if (this.autoReplay) {
this.playQueue();
}

this.emit('reconnected');
}

Expand Down Expand Up @@ -690,8 +710,8 @@ export class Kuzzle extends KuzzleEventEmitter {
* @param {function} listener - callback to invoke each time an event is fired
*/
addListener (event, listener) {
if (events.indexOf(event) === -1) {
throw new Error(`[${event}] is not a known event. Known events: ${events.toString()}`);
if (this.events.indexOf(event) === -1) {
throw new Error(`[${event}] is not a known event. Known events: ${this.events.join(', ')}`);
}

return this._superAddListener(event, listener);
Expand Down Expand Up @@ -751,7 +771,7 @@ export class Kuzzle extends KuzzleEventEmitter {
if (options && options.queuable === false) {
queuable = false;
}

if (this.queueFilter) {
queuable = queuable && this.queueFilter(request);
}
Expand All @@ -767,7 +787,7 @@ export class Kuzzle extends KuzzleEventEmitter {
}
request[key] = value;
}

if (request.refresh === undefined && options.refresh !== undefined) {
request.refresh = options.refresh;
}
Expand Down
Loading