-
Notifications
You must be signed in to change notification settings - Fork 2
/
MyDDPClient.ts
executable file
·126 lines (105 loc) · 2.98 KB
/
MyDDPClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// For this example use a mock WebSocket.
// in React Native or Angular we can use the global WebSocket or SockJs
import { WebSocket } from 'mock-socket';
// Change this to from 'rxjs-ddp-client';
import { DDPClient, DDPCacheEngine } from '../';
// Cache storage implementation (using LocalStorage)
import { MyDDPCacheEngine } from './MyDDPCacheEngine';
// Define your collections names
export enum MY_DDP_COLLECTIONS {
USERS = 'users',
CHATS = 'chats',
}
// This is how a user is represented on the server
export interface IUser {
_id: string;
full_name: string;
email: string;
}
/**
* DDP Client implementation
* Handle connection, login and collections subscriptions
*/
export class MyDDPClient extends DDPClient {
ddpStatus: {
isConnected: boolean;
isDisconnected: boolean;
};
constructor() {
super({
socketConstructor: WebSocket,
});
this.ddpStatus = {
isConnected: false,
isDisconnected: true,
};
this.initCacheStorage(new MyDDPCacheEngine());
}
initCacheStorage(cacheEngine: DDPCacheEngine) {
this.ddpStorage.setCacheEngine(cacheEngine);
this.ddpStorage.loadFromCache([
MY_DDP_COLLECTIONS.USERS,
MY_DDP_COLLECTIONS.CHATS,
]);
}
connect() {
const ddpServerUrl = 'ws://localhost:8080';
return super.connect(ddpServerUrl);
}
login() {
return this.callWithPromise('login', {
username: 'xxx',
password: 'xxx',
});
}
logout() {
this.ddpStorage.clearCache([
MY_DDP_COLLECTIONS.USERS,
MY_DDP_COLLECTIONS.CHATS,
]);
super.close();
}
// Events called by DDPClient
onConnected() {
// DDP connected, now we can login and subscribe to the publications on the server
this.ddpStatus.isConnected = true;
this.ddpStatus.isDisconnected = false;
// Example: Login automatically when WebSocket is connected
this.login().then(() => {
this.subscribePublications();
this.observeCollections();
});
}
onDisconnected() {
// DDP disconnected, notify user
this.ddpStatus.isConnected = true;
this.ddpStatus.isDisconnected = false;
}
onSocketError(error) {
// Custom code on Socket error
}
onSocketClosed() {
// Custom code on Socket closed
}
onMessage(data) {
// Custom code for handle special DDP server messages
}
// Custom utility methods
subscribePublications() {
const since = this.ddpStorage.lastSyncTime;
this.subscribe(MY_DDP_COLLECTIONS.USERS, [since]);
this.subscribe(MY_DDP_COLLECTIONS.CHATS, [since]);
}
observeCollections() {
this.observeCollection<IUser[]>(MY_DDP_COLLECTIONS.USERS).subscribe(
items => {
// NOTE: here you can do some actions when the server send us some new users data
// for example automatic logout
},
);
}
getCollectionData$(collectionName: MY_DDP_COLLECTIONS) {
// To access data directly from the collection you can use the ddpStorage methods
return this.ddpStorage.getObservable(collectionName);
}
}