-
Notifications
You must be signed in to change notification settings - Fork 109
/
deepstream-client.ts
177 lines (158 loc) · 6.2 KB
/
deepstream-client.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { DefaultOptions, Options } from './client-options'
import * as C from './constants'
import { CONNECTION_STATE, RecordData, Message, JSONObject } from './constants'
import { Logger } from './util/logger'
import { TimeoutRegistry } from './util/timeout-registry'
import { IntervalTimerRegistry } from './util/interval-timer-registry'
import { NativeTimerRegistry } from './util/native-timer-registry'
import { Connection, AuthenticationCallback, ResumeCallback } from './connection/connection'
import { socketFactory, SocketFactory} from './connection/socket-factory'
import { EventHandler } from './event/event-handler'
import { RPCHandler } from './rpc/rpc-handler'
import { RecordHandler} from './record/record-handler'
import { PresenceHandler } from './presence/presence-handler'
import { Emitter } from './util/emitter'
import { IndexdbStorage } from './storage/indexdb-storage-service'
import { LocalstorageStorage } from './storage/localstorage-storage-service'
import { NoopStorage } from './storage/noop-storage-service'
export type offlineStoreWriteResponse = ((error: string | null, recordName: string) => void)
export interface RecordOfflineStore {
get: (recordName: string, callback: ((recordName: string, version: number, data: RecordData) => void)) => void
set: (recordName: string, version: number, data: RecordData, callback: offlineStoreWriteResponse) => void
delete: (recordName: string, callback: offlineStoreWriteResponse) => void
reset: (callback: (error: string | null) => void) => void
}
export type TimerRef = number
export interface Timeout {
callback: Function,
duration: number,
context: any,
data?: any
}
export interface TimerRegistry {
close (): void
has (timerId: TimerRef): boolean
add (timeout: Timeout): TimerRef
remove (timerId: TimerRef): boolean
requestIdleCallback (callback: Function): void
}
export interface Socket {
close: () => void
onparsedmessages: (messages: Message[]) => void
onclosed: () => void
onopened: () => void
onerror: (error: any) => void
sendParsedMessage: (message: Message) => void
getTimeSinceLastMessage: () => number
}
export interface Services {
logger: Logger
connection: Connection
timeoutRegistry: TimeoutRegistry
timerRegistry: TimerRegistry
socketFactory: SocketFactory
storage: RecordOfflineStore
}
export {
DeepstreamClient,
C,
DefaultOptions
}
class DeepstreamClient extends Emitter {
public event: EventHandler
public rpc: RPCHandler
public record: RecordHandler
public presence: PresenceHandler
private services: Services
private options: Options
constructor (url: string, options: Partial<Options> = {}) {
super()
this.options = { ...DefaultOptions, ...options } as Options
const services: Partial<Services> = {}
services.logger = new Logger(this)
if (options.nativeTimerRegistry) {
services.timerRegistry = new NativeTimerRegistry()
} else {
services.timerRegistry = new IntervalTimerRegistry(this.options.intervalTimerResolution)
}
services.timeoutRegistry = new TimeoutRegistry(services as Services, this.options)
services.socketFactory = this.options.socketFactory || socketFactory
services.connection = new Connection(services as Services, this.options, url, this)
if (this.options.offlineEnabled) {
if (this.options.storageName === 'indexdb') services.storage = new IndexdbStorage(this.options)
if (this.options.storageName === 'localstorage') services.storage = new LocalstorageStorage(this.options)
if (this.options.storage) services.storage = this.options.storage
} else {
services.storage = new NoopStorage()
}
this.services = services as Services
this.services.connection.onLost(
services.timeoutRegistry.onConnectionLost.bind(services.timeoutRegistry)
)
this.event = new EventHandler(this.services, this.options)
this.rpc = new RPCHandler(this.services, this.options)
this.record = new RecordHandler(this.services, this.options)
this.presence = new PresenceHandler(this.services, this.options)
}
public login (): Promise<JSONObject>
public login (callback: AuthenticationCallback): void
public login (details: JSONObject): Promise<JSONObject>
public login (details: JSONObject, callback: AuthenticationCallback): void
public login (detailsOrCallback?: JSONObject | AuthenticationCallback, callback?: AuthenticationCallback): void | Promise<JSONObject | null> {
if (detailsOrCallback && typeof detailsOrCallback === 'object') {
if (callback) {
this.services.connection.authenticate(detailsOrCallback, callback)
} else {
return new Promise((resolve, reject) => {
this.services.connection.authenticate(detailsOrCallback, (success, data) => {
success ? resolve(data) : reject(data)
})
})
}
} else {
if (typeof detailsOrCallback === 'function') {
this.services.connection.authenticate({}, detailsOrCallback)
} else {
return new Promise((resolve, reject) => {
this.services.connection.authenticate({}, (success, data) => {
success ? resolve(data) : reject(data)
})
})
}
}
}
public getConnectionState (): CONNECTION_STATE {
return this.services.connection.getConnectionState()
}
public close (): void {
Object.keys(this.services).forEach(serviceName => {
if ((this.services as any)[serviceName].close) {
(this.services as any)[serviceName].close()
}
})
}
public pause (): void {
this.services.connection.pause()
}
public resume (callback?: ResumeCallback): void | Promise<void> {
if (callback) {
this.services.connection.resume(callback)
return
}
return new Promise<void>((resolve, reject) => {
this.services.connection.resume(error => {
error ? reject(error) : resolve()
})
})
}
/**
* Returns a random string. The first block of characters
* is a timestamp, in order to allow databases to optimize for semi-
* sequential numberings
*/
public getUid (): string {
const timestamp = (new Date()).getTime().toString(36)
const randomString = (Math.random() * 10000000000000000).toString(36).replace('.', '')
return `${timestamp}-${randomString}`
}
}