Skip to content

Commit b46f7af

Browse files
committed
Adds the ability to subscribe and unsubscribe
1 parent fe5e093 commit b46f7af

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

src/Client.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { ClientConfig } from './Client.types'
1+
import { ClientConfig, Subscription } from './Client.types'
2+
import { uuid } from './lib/helpers'
23

34
export default class Client {
45
url: string
56
headers: ClientConfig['headers'] = {}
7+
stateChangeEmmitters: Map<string, Subscription> = new Map()
68

79
/**
810
* Creates a GoTrue instance for admin interactions.
@@ -16,4 +18,41 @@ export default class Client {
1618
this.headers = { ...this.headers, ...options.headers }
1719
}
1820
}
21+
22+
/**
23+
* Creates a new user account for your business or project.
24+
*/
25+
signup() {
26+
return null
27+
}
28+
29+
/**
30+
* Allows existing users to log into your system.
31+
*/
32+
login() {
33+
return null
34+
}
35+
36+
/**
37+
* Sends a temporary password to a user's email address.
38+
*/
39+
forgotPassword() {
40+
return null
41+
}
42+
43+
/**
44+
* Register a single .
45+
* @returns {Subscription} A subscription object which can be used to unsubcribe itself.
46+
*/
47+
onAuthStateChange(callback: Function): Subscription {
48+
const id: string = uuid()
49+
let self = this
50+
const subscription: Subscription = {
51+
id,
52+
callback,
53+
unsubscribe: () => self.stateChangeEmmitters.delete(id),
54+
}
55+
this.stateChangeEmmitters.set(id, subscription)
56+
return subscription
57+
}
1958
}

src/Client.types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ export interface ClientConfig {
33
[key: string]: string
44
}
55
}
6+
7+
export interface Subscription {
8+
id: string
9+
callback: Function
10+
unsubscribe: Function
11+
}
File renamed without changes.

src/lib/helpers.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function uuid() {
2+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
3+
var r = (Math.random() * 16) | 0,
4+
v = c == 'x' ? r : (r & 0x3) | 0x8
5+
return v.toString(16)
6+
})
7+
}

test/__snapshots__/basic.test.ts.snap

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Developers can subscribe and unsubscribe Subscribe a listener 1`] = `1`;
4+
5+
exports[`Developers can subscribe and unsubscribe Unsubscribe a listener 1`] = `0`;
6+
37
exports[`Init 1`] = `"http://localhost:3000"`;
48

59
exports[`Init 2`] = `"http://localhost:3000"`;

test/basic.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ test('Init', async () => {
88
expect(gotrue.url).toMatchSnapshot()
99
expect(admin.url).toMatchSnapshot()
1010
})
11+
12+
13+
describe('Developers can subscribe and unsubscribe', () => {
14+
const subscription = gotrue.onAuthStateChange(() => console.log('called'))
15+
test('Subscribe a listener', async () => {
16+
expect(gotrue.stateChangeEmmitters.size).toMatchSnapshot()
17+
})
18+
test('Unsubscribe a listener', async () => {
19+
subscription.unsubscribe()
20+
expect(gotrue.stateChangeEmmitters.size).toMatchSnapshot()
21+
})
22+
})
23+
24+

0 commit comments

Comments
 (0)