@supabase/realtime-js 1.7.3 → 1.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main/RealtimeClient.d.ts +4 -4
- package/dist/main/RealtimeClient.d.ts.map +1 -1
- package/dist/main/RealtimeClient.js +1 -40
- package/dist/main/RealtimeClient.js.map +1 -1
- package/dist/main/index.d.ts +1 -3
- package/dist/main/index.d.ts.map +1 -1
- package/dist/main/index.js +1 -5
- package/dist/main/index.js.map +1 -1
- package/dist/main/lib/push.d.ts +2 -3
- package/dist/main/lib/push.d.ts.map +1 -1
- package/dist/main/lib/push.js +3 -16
- package/dist/main/lib/push.js.map +1 -1
- package/dist/main/lib/version.d.ts +1 -1
- package/dist/main/lib/version.js +1 -1
- package/dist/module/RealtimeClient.d.ts +4 -4
- package/dist/module/RealtimeClient.d.ts.map +1 -1
- package/dist/module/RealtimeClient.js +1 -40
- package/dist/module/RealtimeClient.js.map +1 -1
- package/dist/module/index.d.ts +1 -3
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +1 -3
- package/dist/module/index.js.map +1 -1
- package/dist/module/lib/push.d.ts +2 -3
- package/dist/module/lib/push.d.ts.map +1 -1
- package/dist/module/lib/push.js +3 -13
- package/dist/module/lib/push.js.map +1 -1
- package/dist/module/lib/version.d.ts +1 -1
- package/dist/module/lib/version.js +1 -1
- package/package.json +5 -3
- package/src/RealtimeClient.ts +9 -48
- package/src/index.ts +0 -4
- package/src/lib/push.ts +4 -12
- package/src/lib/version.ts +1 -1
- package/dist/main/RealtimeChannel.d.ts +0 -81
- package/dist/main/RealtimeChannel.d.ts.map +0 -1
- package/dist/main/RealtimeChannel.js +0 -242
- package/dist/main/RealtimeChannel.js.map +0 -1
- package/dist/main/RealtimePresence.d.ts +0 -97
- package/dist/main/RealtimePresence.d.ts.map +0 -1
- package/dist/main/RealtimePresence.js +0 -201
- package/dist/main/RealtimePresence.js.map +0 -1
- package/dist/module/RealtimeChannel.d.ts +0 -81
- package/dist/module/RealtimeChannel.d.ts.map +0 -1
- package/dist/module/RealtimeChannel.js +0 -236
- package/dist/module/RealtimeChannel.js.map +0 -1
- package/dist/module/RealtimePresence.d.ts +0 -97
- package/dist/module/RealtimePresence.d.ts.map +0 -1
- package/dist/module/RealtimePresence.js +0 -198
- package/dist/module/RealtimePresence.js.map +0 -1
- package/src/RealtimeChannel.ts +0 -289
- package/src/RealtimePresence.ts +0 -326
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supabase/realtime-js",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.5",
|
|
4
4
|
"description": "Listen to realtime updates to your PostgreSQL database",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"realtime",
|
|
@@ -31,8 +31,10 @@
|
|
|
31
31
|
"build:module": "tsc -p tsconfig.module.json",
|
|
32
32
|
"mocha": "node -r esm node_modules/.bin/mocha ./test/**/*.js -r jsdom-global/register",
|
|
33
33
|
"test": "run-s clean build mocha",
|
|
34
|
-
"docs": "typedoc --mode file --target ES6 --theme minimal",
|
|
35
|
-
"docs:json": "typedoc --json docs/spec.json --mode modules --includeDeclarations --excludeExternals"
|
|
34
|
+
"v1:docs": "typedoc --mode file --target ES6 --theme minimal --out docs/v1",
|
|
35
|
+
"v1:docs:json": "typedoc --json docs/v1/spec.json --mode modules --includeDeclarations --excludeExternals",
|
|
36
|
+
"v2:docs": "typedoc --mode file --target ES6 --theme minimal --out docs/v2",
|
|
37
|
+
"v2:docs:json": "typedoc --json docs/v2/spec.json --mode modules --includeDeclarations --excludeExternals"
|
|
36
38
|
},
|
|
37
39
|
"dependencies": {
|
|
38
40
|
"@types/phoenix": "^1.5.4",
|
package/src/RealtimeClient.ts
CHANGED
|
@@ -12,7 +12,6 @@ import {
|
|
|
12
12
|
import Timer from './lib/timer'
|
|
13
13
|
import Serializer from './lib/serializer'
|
|
14
14
|
import RealtimeSubscription from './RealtimeSubscription'
|
|
15
|
-
import RealtimeChannel from './RealtimeChannel'
|
|
16
15
|
|
|
17
16
|
export type Options = {
|
|
18
17
|
transport?: WebSocket
|
|
@@ -31,6 +30,7 @@ type Message = {
|
|
|
31
30
|
event: string
|
|
32
31
|
payload: any
|
|
33
32
|
ref: string
|
|
33
|
+
join_ref?: string
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
type ChannelParams = {
|
|
@@ -42,7 +42,7 @@ const noop = () => {}
|
|
|
42
42
|
|
|
43
43
|
export default class RealtimeClient {
|
|
44
44
|
accessToken: string | null = null
|
|
45
|
-
channels:
|
|
45
|
+
channels: RealtimeSubscription[] = []
|
|
46
46
|
endPoint: string = ''
|
|
47
47
|
headers?: { [key: string]: string } = DEFAULT_HEADERS
|
|
48
48
|
params?: { [key: string]: string } = {}
|
|
@@ -256,51 +256,14 @@ export default class RealtimeClient {
|
|
|
256
256
|
*
|
|
257
257
|
* @param channel An open subscription.
|
|
258
258
|
*/
|
|
259
|
-
remove(channel: RealtimeSubscription
|
|
259
|
+
remove(channel: RealtimeSubscription) {
|
|
260
260
|
this.channels = this.channels.filter(
|
|
261
|
-
(c: RealtimeSubscription
|
|
262
|
-
c.joinRef() !== channel.joinRef()
|
|
261
|
+
(c: RealtimeSubscription) => c.joinRef() !== channel.joinRef()
|
|
263
262
|
)
|
|
264
263
|
}
|
|
265
264
|
|
|
266
|
-
channel(
|
|
267
|
-
topic
|
|
268
|
-
chanParams: ChannelParams = {}
|
|
269
|
-
): RealtimeChannel | RealtimeSubscription {
|
|
270
|
-
const { selfBroadcast, ...params } = chanParams
|
|
271
|
-
|
|
272
|
-
if (selfBroadcast) {
|
|
273
|
-
params.self_broadcast = selfBroadcast
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
const chan = this.params?.vsndate
|
|
277
|
-
? new RealtimeChannel(topic, params, this)
|
|
278
|
-
: new RealtimeSubscription(topic, params, this)
|
|
279
|
-
|
|
280
|
-
if (chan instanceof RealtimeChannel) {
|
|
281
|
-
chan.presence.onJoin((key, currentPresences, newPresences) => {
|
|
282
|
-
chan.trigger('presence', {
|
|
283
|
-
event: 'JOIN',
|
|
284
|
-
key,
|
|
285
|
-
currentPresences,
|
|
286
|
-
newPresences,
|
|
287
|
-
})
|
|
288
|
-
})
|
|
289
|
-
|
|
290
|
-
chan.presence.onLeave((key, currentPresences, leftPresences) => {
|
|
291
|
-
chan.trigger('presence', {
|
|
292
|
-
event: 'LEAVE',
|
|
293
|
-
key,
|
|
294
|
-
currentPresences,
|
|
295
|
-
leftPresences,
|
|
296
|
-
})
|
|
297
|
-
})
|
|
298
|
-
|
|
299
|
-
chan.presence.onSync(() => {
|
|
300
|
-
chan.trigger('presence', { event: 'SYNC' })
|
|
301
|
-
})
|
|
302
|
-
}
|
|
303
|
-
|
|
265
|
+
channel(topic: string, chanParams: ChannelParams = {}): RealtimeSubscription {
|
|
266
|
+
const chan = new RealtimeSubscription(topic, chanParams, this)
|
|
304
267
|
this.channels.push(chan)
|
|
305
268
|
return chan
|
|
306
269
|
}
|
|
@@ -344,10 +307,8 @@ export default class RealtimeClient {
|
|
|
344
307
|
payload
|
|
345
308
|
)
|
|
346
309
|
this.channels
|
|
347
|
-
.filter((channel: RealtimeSubscription
|
|
348
|
-
|
|
349
|
-
)
|
|
350
|
-
.forEach((channel: RealtimeSubscription | RealtimeChannel) =>
|
|
310
|
+
.filter((channel: RealtimeSubscription) => channel.isMember(topic))
|
|
311
|
+
.forEach((channel: RealtimeSubscription) =>
|
|
351
312
|
channel.trigger(event, payload, ref)
|
|
352
313
|
)
|
|
353
314
|
this.stateChangeCallbacks.message.forEach((callback) => callback(msg))
|
|
@@ -435,7 +396,7 @@ export default class RealtimeClient {
|
|
|
435
396
|
}
|
|
436
397
|
|
|
437
398
|
private _triggerChanError() {
|
|
438
|
-
this.channels.forEach((channel: RealtimeSubscription
|
|
399
|
+
this.channels.forEach((channel: RealtimeSubscription) =>
|
|
439
400
|
channel.trigger(CHANNEL_EVENTS.error)
|
|
440
401
|
)
|
|
441
402
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,14 +3,10 @@ import RealtimeClient, {
|
|
|
3
3
|
Options as RealtimeClientOptions,
|
|
4
4
|
} from './RealtimeClient'
|
|
5
5
|
import RealtimeSubscription from './RealtimeSubscription'
|
|
6
|
-
import RealtimeChannel from './RealtimeChannel'
|
|
7
|
-
import RealtimePresence from './RealtimePresence'
|
|
8
6
|
|
|
9
7
|
export {
|
|
10
8
|
RealtimeClient,
|
|
11
9
|
RealtimeClientOptions,
|
|
12
10
|
RealtimeSubscription,
|
|
13
|
-
RealtimeChannel,
|
|
14
|
-
RealtimePresence,
|
|
15
11
|
Transformers,
|
|
16
12
|
}
|
package/src/lib/push.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { DEFAULT_TIMEOUT } from '../lib/constants'
|
|
2
|
-
import RealtimeChannel from '../RealtimeChannel'
|
|
3
2
|
import RealtimeSubscription from '../RealtimeSubscription'
|
|
4
3
|
|
|
5
4
|
export default class Push {
|
|
@@ -25,7 +24,7 @@ export default class Push {
|
|
|
25
24
|
* @param timeout The push timeout in milliseconds
|
|
26
25
|
*/
|
|
27
26
|
constructor(
|
|
28
|
-
public channel: RealtimeSubscription
|
|
27
|
+
public channel: RealtimeSubscription,
|
|
29
28
|
public event: string,
|
|
30
29
|
public payload: { [key: string]: unknown } = {},
|
|
31
30
|
public timeout: number = DEFAULT_TIMEOUT
|
|
@@ -52,6 +51,7 @@ export default class Push {
|
|
|
52
51
|
event: this.event,
|
|
53
52
|
payload: this.payload,
|
|
54
53
|
ref: this.ref,
|
|
54
|
+
join_ref: this.channel.joinRef(),
|
|
55
55
|
})
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -82,11 +82,7 @@ export default class Push {
|
|
|
82
82
|
this._matchReceive(payload)
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
this.channel.on(this.refEvent, callback)
|
|
87
|
-
} else {
|
|
88
|
-
this.channel.on(this.refEvent, {}, callback)
|
|
89
|
-
}
|
|
85
|
+
this.channel.on(this.refEvent, callback)
|
|
90
86
|
|
|
91
87
|
this.timeoutTimer = <any>setTimeout(() => {
|
|
92
88
|
this.trigger('timeout', {})
|
|
@@ -107,11 +103,7 @@ export default class Push {
|
|
|
107
103
|
return
|
|
108
104
|
}
|
|
109
105
|
|
|
110
|
-
|
|
111
|
-
this.channel.off(this.refEvent)
|
|
112
|
-
} else {
|
|
113
|
-
this.channel.off(this.refEvent, {})
|
|
114
|
-
}
|
|
106
|
+
this.channel.off(this.refEvent)
|
|
115
107
|
}
|
|
116
108
|
|
|
117
109
|
private _cancelTimeout() {
|
package/src/lib/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '1.7.
|
|
1
|
+
export const version = '1.7.5'
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { CHANNEL_EVENTS, CHANNEL_STATES } from './lib/constants';
|
|
2
|
-
import Push from './lib/push';
|
|
3
|
-
import RealtimeClient from './RealtimeClient';
|
|
4
|
-
import Timer from './lib/timer';
|
|
5
|
-
import RealtimePresence from './RealtimePresence';
|
|
6
|
-
export default class RealtimeChannel {
|
|
7
|
-
topic: string;
|
|
8
|
-
params: {
|
|
9
|
-
[key: string]: unknown;
|
|
10
|
-
};
|
|
11
|
-
socket: RealtimeClient;
|
|
12
|
-
bindings: any[];
|
|
13
|
-
timeout: number;
|
|
14
|
-
state: CHANNEL_STATES;
|
|
15
|
-
joinedOnce: boolean;
|
|
16
|
-
joinPush: Push;
|
|
17
|
-
rejoinTimer: Timer;
|
|
18
|
-
pushBuffer: Push[];
|
|
19
|
-
presence: RealtimePresence;
|
|
20
|
-
constructor(topic: string, params: {
|
|
21
|
-
[key: string]: unknown;
|
|
22
|
-
}, socket: RealtimeClient);
|
|
23
|
-
list(): any[];
|
|
24
|
-
rejoinUntilConnected(): void;
|
|
25
|
-
subscribe(timeout?: number): Push;
|
|
26
|
-
/**
|
|
27
|
-
* Registers a callback that will be executed when the channel closes.
|
|
28
|
-
*/
|
|
29
|
-
onClose(callback: Function): void;
|
|
30
|
-
/**
|
|
31
|
-
* Registers a callback that will be executed when the channel encounteres an error.
|
|
32
|
-
*/
|
|
33
|
-
onError(callback: Function): void;
|
|
34
|
-
on(type: string, filter?: {
|
|
35
|
-
[key: string]: string;
|
|
36
|
-
}, callback?: Function): void;
|
|
37
|
-
off(type: string, filter: {
|
|
38
|
-
[key: string]: any;
|
|
39
|
-
}): void;
|
|
40
|
-
/**
|
|
41
|
-
* Returns `true` if the socket is connected and the channel has been joined.
|
|
42
|
-
*/
|
|
43
|
-
canPush(): boolean;
|
|
44
|
-
push(event: CHANNEL_EVENTS, payload: any, timeout?: number): Push;
|
|
45
|
-
updateJoinPayload(payload: {
|
|
46
|
-
[key: string]: unknown;
|
|
47
|
-
}): void;
|
|
48
|
-
/**
|
|
49
|
-
* Leaves the channel.
|
|
50
|
-
*
|
|
51
|
-
* Unsubscribes from server events, and instructs channel to terminate on server.
|
|
52
|
-
* Triggers onClose() hooks.
|
|
53
|
-
*
|
|
54
|
-
* To receive leave acknowledgements, use the a `receive` hook to bind to the server ack, ie:
|
|
55
|
-
* channel.unsubscribe().receive("ok", () => alert("left!") )
|
|
56
|
-
*/
|
|
57
|
-
unsubscribe(timeout?: number): Push;
|
|
58
|
-
/**
|
|
59
|
-
* Overridable message hook
|
|
60
|
-
*
|
|
61
|
-
* Receives all events for specialized message handling before dispatching to the channel callbacks.
|
|
62
|
-
* Must return the payload, modified or unmodified.
|
|
63
|
-
*/
|
|
64
|
-
onMessage(event: string, payload: any, ref?: string): any;
|
|
65
|
-
isMember(topic: string): boolean;
|
|
66
|
-
joinRef(): string;
|
|
67
|
-
rejoin(timeout?: number): void;
|
|
68
|
-
trigger(type: string, payload?: any, ref?: string): void;
|
|
69
|
-
send(payload: {
|
|
70
|
-
type: string;
|
|
71
|
-
[key: string]: any;
|
|
72
|
-
}): Promise<unknown>;
|
|
73
|
-
replyEventName(ref: string): string;
|
|
74
|
-
isClosed(): boolean;
|
|
75
|
-
isErrored(): boolean;
|
|
76
|
-
isJoined(): boolean;
|
|
77
|
-
isJoining(): boolean;
|
|
78
|
-
isLeaving(): boolean;
|
|
79
|
-
private static isEqual;
|
|
80
|
-
}
|
|
81
|
-
//# sourceMappingURL=RealtimeChannel.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RealtimeChannel.d.ts","sourceRoot":"","sources":["../../src/RealtimeChannel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChE,OAAO,IAAI,MAAM,YAAY,CAAA;AAC7B,OAAO,cAAc,MAAM,kBAAkB,CAAA;AAC7C,OAAO,KAAK,MAAM,aAAa,CAAA;AAC/B,OAAO,gBAAgB,MAAM,oBAAoB,CAAA;AAEjD,MAAM,CAAC,OAAO,OAAO,eAAe;IAWzB,KAAK,EAAE,MAAM;IACb,MAAM,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE;IAClC,MAAM,EAAE,cAAc;IAZ/B,QAAQ,EAAE,GAAG,EAAE,CAAK;IACpB,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,iBAAwB;IAC7B,UAAU,UAAQ;IAClB,QAAQ,EAAE,IAAI,CAAA;IACd,WAAW,EAAE,KAAK,CAAA;IAClB,UAAU,EAAE,IAAI,EAAE,CAAK;IACvB,QAAQ,EAAE,gBAAgB,CAAA;gBAGjB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAO,EACvC,MAAM,EAAE,cAAc;IA+C/B,IAAI;IAIJ,oBAAoB;IAOpB,SAAS,CAAC,OAAO,SAAe;IAiChC;;OAEG;IACH,OAAO,CAAC,QAAQ,EAAE,QAAQ;IAI1B;;OAEG;IACH,OAAO,CAAC,QAA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,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const constants_1 = require("./lib/constants");
|
|
7
|
-
const push_1 = __importDefault(require("./lib/push"));
|
|
8
|
-
const timer_1 = __importDefault(require("./lib/timer"));
|
|
9
|
-
const RealtimePresence_1 = __importDefault(require("./RealtimePresence"));
|
|
10
|
-
class RealtimeChannel {
|
|
11
|
-
constructor(topic, params = {}, socket) {
|
|
12
|
-
this.topic = topic;
|
|
13
|
-
this.params = params;
|
|
14
|
-
this.socket = socket;
|
|
15
|
-
this.bindings = [];
|
|
16
|
-
this.state = constants_1.CHANNEL_STATES.closed;
|
|
17
|
-
this.joinedOnce = false;
|
|
18
|
-
this.pushBuffer = [];
|
|
19
|
-
this.timeout = this.socket.timeout;
|
|
20
|
-
this.joinPush = new push_1.default(this, constants_1.CHANNEL_EVENTS.join, this.params, this.timeout);
|
|
21
|
-
this.rejoinTimer = new timer_1.default(() => this.rejoinUntilConnected(), this.socket.reconnectAfterMs);
|
|
22
|
-
this.joinPush.receive('ok', () => {
|
|
23
|
-
this.state = constants_1.CHANNEL_STATES.joined;
|
|
24
|
-
this.rejoinTimer.reset();
|
|
25
|
-
this.pushBuffer.forEach((pushEvent) => pushEvent.send());
|
|
26
|
-
this.pushBuffer = [];
|
|
27
|
-
});
|
|
28
|
-
this.onClose(() => {
|
|
29
|
-
this.rejoinTimer.reset();
|
|
30
|
-
this.socket.log('channel', `close ${this.topic} ${this.joinRef()}`);
|
|
31
|
-
this.state = constants_1.CHANNEL_STATES.closed;
|
|
32
|
-
this.socket.remove(this);
|
|
33
|
-
});
|
|
34
|
-
this.onError((reason) => {
|
|
35
|
-
if (this.isLeaving() || this.isClosed()) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
this.socket.log('channel', `error ${this.topic}`, reason);
|
|
39
|
-
this.state = constants_1.CHANNEL_STATES.errored;
|
|
40
|
-
this.rejoinTimer.scheduleTimeout();
|
|
41
|
-
});
|
|
42
|
-
this.joinPush.receive('timeout', () => {
|
|
43
|
-
if (!this.isJoining()) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
this.socket.log('channel', `timeout ${this.topic}`, this.joinPush.timeout);
|
|
47
|
-
this.state = constants_1.CHANNEL_STATES.errored;
|
|
48
|
-
this.rejoinTimer.scheduleTimeout();
|
|
49
|
-
});
|
|
50
|
-
this.on(constants_1.CHANNEL_EVENTS.reply, {}, (payload, ref) => {
|
|
51
|
-
this.trigger(this.replyEventName(ref), payload);
|
|
52
|
-
});
|
|
53
|
-
this.presence = new RealtimePresence_1.default(this);
|
|
54
|
-
}
|
|
55
|
-
list() {
|
|
56
|
-
return this.presence.list();
|
|
57
|
-
}
|
|
58
|
-
rejoinUntilConnected() {
|
|
59
|
-
this.rejoinTimer.scheduleTimeout();
|
|
60
|
-
if (this.socket.isConnected()) {
|
|
61
|
-
this.rejoin();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
subscribe(timeout = this.timeout) {
|
|
65
|
-
if (this.joinedOnce) {
|
|
66
|
-
throw `tried to subscribe multiple times. 'subscribe' can only be called a single time per channel instance`;
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
const configs = this.bindings.reduce((acc, binding) => {
|
|
70
|
-
const { type } = binding;
|
|
71
|
-
if (![
|
|
72
|
-
'phx_close',
|
|
73
|
-
'phx_error',
|
|
74
|
-
'phx_reply',
|
|
75
|
-
'presence_diff',
|
|
76
|
-
'presence_state',
|
|
77
|
-
].includes(type)) {
|
|
78
|
-
acc[type] = binding;
|
|
79
|
-
}
|
|
80
|
-
return acc;
|
|
81
|
-
}, {});
|
|
82
|
-
if (Object.keys(configs).length) {
|
|
83
|
-
this.updateJoinPayload({ configs });
|
|
84
|
-
}
|
|
85
|
-
this.joinedOnce = true;
|
|
86
|
-
this.rejoin(timeout);
|
|
87
|
-
return this.joinPush;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Registers a callback that will be executed when the channel closes.
|
|
92
|
-
*/
|
|
93
|
-
onClose(callback) {
|
|
94
|
-
this.on(constants_1.CHANNEL_EVENTS.close, {}, callback);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Registers a callback that will be executed when the channel encounteres an error.
|
|
98
|
-
*/
|
|
99
|
-
onError(callback) {
|
|
100
|
-
this.on(constants_1.CHANNEL_EVENTS.error, {}, (reason) => callback(reason));
|
|
101
|
-
}
|
|
102
|
-
on(type, filter, callback) {
|
|
103
|
-
this.bindings.push({
|
|
104
|
-
type,
|
|
105
|
-
filter: filter !== null && filter !== void 0 ? filter : {},
|
|
106
|
-
callback: callback !== null && callback !== void 0 ? callback : (() => { }),
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
off(type, filter) {
|
|
110
|
-
this.bindings = this.bindings.filter((bind) => {
|
|
111
|
-
return !(bind.type === type && RealtimeChannel.isEqual(bind.filter, filter));
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Returns `true` if the socket is connected and the channel has been joined.
|
|
116
|
-
*/
|
|
117
|
-
canPush() {
|
|
118
|
-
return this.socket.isConnected() && this.isJoined();
|
|
119
|
-
}
|
|
120
|
-
push(event, payload, timeout = this.timeout) {
|
|
121
|
-
if (!this.joinedOnce) {
|
|
122
|
-
throw `tried to push '${event}' to '${this.topic}' before joining. Use channel.subscribe() before pushing events`;
|
|
123
|
-
}
|
|
124
|
-
let pushEvent = new push_1.default(this, event, payload, timeout);
|
|
125
|
-
if (this.canPush()) {
|
|
126
|
-
pushEvent.send();
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
pushEvent.startTimeout();
|
|
130
|
-
this.pushBuffer.push(pushEvent);
|
|
131
|
-
}
|
|
132
|
-
return pushEvent;
|
|
133
|
-
}
|
|
134
|
-
updateJoinPayload(payload) {
|
|
135
|
-
this.joinPush.updatePayload(payload);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Leaves the channel.
|
|
139
|
-
*
|
|
140
|
-
* Unsubscribes from server events, and instructs channel to terminate on server.
|
|
141
|
-
* Triggers onClose() hooks.
|
|
142
|
-
*
|
|
143
|
-
* To receive leave acknowledgements, use the a `receive` hook to bind to the server ack, ie:
|
|
144
|
-
* channel.unsubscribe().receive("ok", () => alert("left!") )
|
|
145
|
-
*/
|
|
146
|
-
unsubscribe(timeout = this.timeout) {
|
|
147
|
-
this.state = constants_1.CHANNEL_STATES.leaving;
|
|
148
|
-
const onClose = () => {
|
|
149
|
-
this.socket.log('channel', `leave ${this.topic}`);
|
|
150
|
-
this.trigger(constants_1.CHANNEL_EVENTS.close, 'leave', this.joinRef());
|
|
151
|
-
};
|
|
152
|
-
// Destroy joinPush to avoid connection timeouts during unscription phase
|
|
153
|
-
this.joinPush.destroy();
|
|
154
|
-
const leavePush = new push_1.default(this, constants_1.CHANNEL_EVENTS.leave, {}, timeout);
|
|
155
|
-
leavePush.receive('ok', () => onClose()).receive('timeout', () => onClose());
|
|
156
|
-
leavePush.send();
|
|
157
|
-
if (!this.canPush()) {
|
|
158
|
-
leavePush.trigger('ok', {});
|
|
159
|
-
}
|
|
160
|
-
return leavePush;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Overridable message hook
|
|
164
|
-
*
|
|
165
|
-
* Receives all events for specialized message handling before dispatching to the channel callbacks.
|
|
166
|
-
* Must return the payload, modified or unmodified.
|
|
167
|
-
*/
|
|
168
|
-
onMessage(event, payload, ref) {
|
|
169
|
-
return payload;
|
|
170
|
-
}
|
|
171
|
-
isMember(topic) {
|
|
172
|
-
return this.topic === topic;
|
|
173
|
-
}
|
|
174
|
-
joinRef() {
|
|
175
|
-
return this.joinPush.ref;
|
|
176
|
-
}
|
|
177
|
-
rejoin(timeout = this.timeout) {
|
|
178
|
-
if (this.isLeaving()) {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
this.socket.leaveOpenTopic(this.topic);
|
|
182
|
-
this.state = constants_1.CHANNEL_STATES.joining;
|
|
183
|
-
this.joinPush.resend(timeout);
|
|
184
|
-
}
|
|
185
|
-
trigger(type, payload, ref) {
|
|
186
|
-
const { close, error, leave, join } = constants_1.CHANNEL_EVENTS;
|
|
187
|
-
const events = [close, error, leave, join];
|
|
188
|
-
if (ref && events.indexOf(type) >= 0 && ref !== this.joinRef()) {
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
const handledPayload = this.onMessage(type, payload, ref);
|
|
192
|
-
if (payload && !handledPayload) {
|
|
193
|
-
throw 'channel onMessage callbacks must return the payload, modified or unmodified';
|
|
194
|
-
}
|
|
195
|
-
this.bindings
|
|
196
|
-
.filter((bind) => {
|
|
197
|
-
var _a, _b;
|
|
198
|
-
return ((bind === null || bind === void 0 ? void 0 : bind.type) === type &&
|
|
199
|
-
(((_a = bind === null || bind === void 0 ? void 0 : bind.filter) === null || _a === void 0 ? void 0 : _a.event) === '*' ||
|
|
200
|
-
((_b = bind === null || bind === void 0 ? void 0 : bind.filter) === null || _b === void 0 ? void 0 : _b.event) === (payload === null || payload === void 0 ? void 0 : payload.event)));
|
|
201
|
-
})
|
|
202
|
-
.map((bind) => bind.callback(handledPayload, ref));
|
|
203
|
-
}
|
|
204
|
-
send(payload) {
|
|
205
|
-
const push = this.push(payload.type, payload);
|
|
206
|
-
return new Promise((resolve) => {
|
|
207
|
-
push.receive('ok', () => resolve('ok'));
|
|
208
|
-
push.receive('timeout', () => resolve('timeout'));
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
replyEventName(ref) {
|
|
212
|
-
return `chan_reply_${ref}`;
|
|
213
|
-
}
|
|
214
|
-
isClosed() {
|
|
215
|
-
return this.state === constants_1.CHANNEL_STATES.closed;
|
|
216
|
-
}
|
|
217
|
-
isErrored() {
|
|
218
|
-
return this.state === constants_1.CHANNEL_STATES.errored;
|
|
219
|
-
}
|
|
220
|
-
isJoined() {
|
|
221
|
-
return this.state === constants_1.CHANNEL_STATES.joined;
|
|
222
|
-
}
|
|
223
|
-
isJoining() {
|
|
224
|
-
return this.state === constants_1.CHANNEL_STATES.joining;
|
|
225
|
-
}
|
|
226
|
-
isLeaving() {
|
|
227
|
-
return this.state === constants_1.CHANNEL_STATES.leaving;
|
|
228
|
-
}
|
|
229
|
-
static isEqual(obj1, obj2) {
|
|
230
|
-
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
|
|
231
|
-
return false;
|
|
232
|
-
}
|
|
233
|
-
for (const k in obj1) {
|
|
234
|
-
if (obj1[k] !== obj2[k]) {
|
|
235
|
-
return false;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
return true;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
exports.default = RealtimeChannel;
|
|
242
|
-
//# sourceMappingURL=RealtimeChannel.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RealtimeChannel.js","sourceRoot":"","sources":["../../src/RealtimeChannel.ts"],"names":[],"mappings":";;;;;AAAA,+CAAgE;AAChE,sDAA6B;AAE7B,wDAA+B;AAC/B,0EAAiD;AAEjD,MAAqB,eAAe;IAUlC,YACS,KAAa,EACb,SAAqC,EAAE,EACvC,MAAsB;QAFtB,UAAK,GAAL,KAAK,CAAQ;QACb,WAAM,GAAN,MAAM,CAAiC;QACvC,WAAM,GAAN,MAAM,CAAgB;QAZ/B,aAAQ,GAAU,EAAE,CAAA;QAEpB,UAAK,GAAG,0BAAc,CAAC,MAAM,CAAA;QAC7B,eAAU,GAAG,KAAK,CAAA;QAGlB,eAAU,GAAW,EAAE,CAAA;QAQrB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAA;QAClC,IAAI,CAAC,QAAQ,GAAG,IAAI,cAAI,CACtB,IAAI,EACJ,0BA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CAAC,IAAI,CAAC,CAAC,CAAA;YACvC,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAA;QACnD,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,cAAc,CAAC,GAAW;QACxB,OAAO,cAAc,GAAG,EAAE,CAAA;IAC5B,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,KAAK,0BAAc,CAAC,MAAM,CAAA;IAC7C,CAAC;IACD,SAAS;QACP,OAAO,IAAI,CAAC,KAAK,KAAK,0BAAc,CAAC,OAAO,CAAA;IAC9C,CAAC;IACD,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,KAAK,0BAAc,CAAC,MAAM,CAAA;IAC7C,CAAC;IACD,SAAS;QACP,OAAO,IAAI,CAAC,KAAK,KAAK,0BAAc,CAAC,OAAO,CAAA;IAC9C,CAAC;IACD,SAAS;QACP,OAAO,IAAI,CAAC,KAAK,KAAK,0BAAc,CAAC,OAAO,CAAA;IAC9C,CAAC;IAEO,MAAM,CAAC,OAAO,CACpB,IAA+B,EAC/B,IAA+B;QAE/B,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;YACzD,OAAO,KAAK,CAAA;SACb;QAED,KAAK,MAAM,CAAC,IAAI,IAAI,EAAE;YACpB,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE;gBACvB,OAAO,KAAK,CAAA;aACb;SACF;QAED,OAAO,IAAI,CAAA;IACb,CAAC;CACF;AA1RD,kCA0RC"}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { PresenceOpts, PresenceOnJoinCallback, PresenceOnLeaveCallback } from 'phoenix';
|
|
2
|
-
import RealtimeChannel from './RealtimeChannel';
|
|
3
|
-
declare type Presence = {
|
|
4
|
-
presence_id: string;
|
|
5
|
-
[key: string]: any;
|
|
6
|
-
};
|
|
7
|
-
declare type PresenceState = {
|
|
8
|
-
[key: string]: Presence[];
|
|
9
|
-
};
|
|
10
|
-
declare type PresenceDiff = {
|
|
11
|
-
joins: PresenceState;
|
|
12
|
-
leaves: PresenceState;
|
|
13
|
-
};
|
|
14
|
-
declare type RawPresenceState = {
|
|
15
|
-
[key: string]: Record<'metas', {
|
|
16
|
-
phx_ref?: string;
|
|
17
|
-
phx_ref_prev?: string;
|
|
18
|
-
[key: string]: any;
|
|
19
|
-
}[]>;
|
|
20
|
-
};
|
|
21
|
-
declare type RawPresenceDiff = {
|
|
22
|
-
joins: RawPresenceState;
|
|
23
|
-
leaves: RawPresenceState;
|
|
24
|
-
};
|
|
25
|
-
declare type PresenceChooser<T> = (key: string, presences: any) => T;
|
|
26
|
-
export default class RealtimePresence {
|
|
27
|
-
channel: RealtimeChannel;
|
|
28
|
-
state: PresenceState;
|
|
29
|
-
pendingDiffs: RawPresenceDiff[];
|
|
30
|
-
joinRef: string | null;
|
|
31
|
-
caller: {
|
|
32
|
-
onJoin: PresenceOnJoinCallback;
|
|
33
|
-
onLeave: PresenceOnLeaveCallback;
|
|
34
|
-
onSync: () => void;
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* Initializes the Presence.
|
|
38
|
-
*
|
|
39
|
-
* @param channel - The RealtimeSubscription
|
|
40
|
-
* @param opts - The options,
|
|
41
|
-
* for example `{events: {state: 'state', diff: 'diff'}}`
|
|
42
|
-
*/
|
|
43
|
-
constructor(channel: RealtimeChannel, opts?: PresenceOpts);
|
|
44
|
-
/**
|
|
45
|
-
* Used to sync the list of presences on the server with the
|
|
46
|
-
* client's state.
|
|
47
|
-
*
|
|
48
|
-
* An optional `onJoin` and `onLeave` callback can be provided to
|
|
49
|
-
* react to changes in the client's local presences across
|
|
50
|
-
* disconnects and reconnects with the server.
|
|
51
|
-
*/
|
|
52
|
-
static syncState(currentState: PresenceState, newState: RawPresenceState | PresenceState, onJoin: PresenceOnJoinCallback, onLeave: PresenceOnLeaveCallback): PresenceState;
|
|
53
|
-
/**
|
|
54
|
-
* Used to sync a diff of presence join and leave events from the
|
|
55
|
-
* server, as they happen.
|
|
56
|
-
*
|
|
57
|
-
* Like `syncState`, `syncDiff` accepts optional `onJoin` and
|
|
58
|
-
* `onLeave` callbacks to react to a user joining or leaving from a
|
|
59
|
-
* device.
|
|
60
|
-
*/
|
|
61
|
-
static syncDiff(state: PresenceState, diff: RawPresenceDiff | PresenceDiff, onJoin: PresenceOnJoinCallback, onLeave: PresenceOnLeaveCallback): PresenceState;
|
|
62
|
-
/**
|
|
63
|
-
* Returns the array of presences, with selected metadata.
|
|
64
|
-
*/
|
|
65
|
-
static list<T = any>(presences: PresenceState, chooser: PresenceChooser<T> | undefined): T[];
|
|
66
|
-
private static map;
|
|
67
|
-
/**
|
|
68
|
-
* Remove 'metas' key
|
|
69
|
-
* Change 'phx_ref' to 'presence_id'
|
|
70
|
-
* Remove 'phx_ref' and 'phx_ref_prev'
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* // returns {
|
|
74
|
-
* abc123: [
|
|
75
|
-
* { presence_id: '2', user_id: 1 },
|
|
76
|
-
* { presence_id: '3', user_id: 2 }
|
|
77
|
-
* ]
|
|
78
|
-
* }
|
|
79
|
-
* RealtimePresence.transformState({
|
|
80
|
-
* abc123: {
|
|
81
|
-
* metas: [
|
|
82
|
-
* { phx_ref: '2', phx_ref_prev: '1' user_id: 1 },
|
|
83
|
-
* { phx_ref: '3', user_id: 2 }
|
|
84
|
-
* ]
|
|
85
|
-
* }
|
|
86
|
-
* })
|
|
87
|
-
*/
|
|
88
|
-
private static transformState;
|
|
89
|
-
private static cloneDeep;
|
|
90
|
-
onJoin(callback: PresenceOnJoinCallback): void;
|
|
91
|
-
onLeave(callback: PresenceOnLeaveCallback): void;
|
|
92
|
-
onSync(callback: () => void): void;
|
|
93
|
-
list<T = any>(by?: PresenceChooser<T>): T[];
|
|
94
|
-
private inPendingSyncState;
|
|
95
|
-
}
|
|
96
|
-
export {};
|
|
97
|
-
//# sourceMappingURL=RealtimePresence.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RealtimePresence.d.ts","sourceRoot":"","sources":["../../src/RealtimePresence.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,YAAY,EACZ,sBAAsB,EACtB,uBAAuB,EACxB,MAAM,SAAS,CAAA;AAChB,OAAO,eAAe,MAAM,mBAAmB,CAAA;AAE/C,aAAK,QAAQ,GAAG;IACd,WAAW,EAAE,MAAM,CAAA;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CACnB,CAAA;AAED,aAAK,aAAa,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,EAAE,CAAA;CAAE,CAAA;AAElD,aAAK,YAAY,GAAG;IAClB,KAAK,EAAE,aAAa,CAAA;IACpB,MAAM,EAAE,aAAa,CAAA;CACtB,CAAA;AAED,aAAK,gBAAgB,GAAG;IACtB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CACnB,OAAO,EACP;QACE,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,YAAY,CAAC,EAAE,MAAM,CAAA;QACrB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KACnB,EAAE,CACJ,CAAA;CACF,CAAA;AAED,aAAK,eAAe,GAAG;IACrB,KAAK,EAAE,gBAAgB,CAAA;IACvB,MAAM,EAAE,gBAAgB,CAAA;CACzB,CAAA;AAED,aAAK,eAAe,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,KAAK,CAAC,CAAA;AAE5D,MAAM,CAAC,OAAO,OAAO,gBAAgB;IAqBhB,OAAO,EAAE,eAAe;IApB3C,KAAK,EAAE,aAAa,CAAK;IACzB,YAAY,EAAE,eAAe,EAAE,CAAK;IACpC,OAAO,EAAE,MAAM,GA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