Skip to content

Commit 93e37e7

Browse files
committed
feat(streamsql): add template funcs for identifier listeners
1 parent db25e73 commit 93e37e7

File tree

6 files changed

+88
-11
lines changed

6 files changed

+88
-11
lines changed

.size-snapshot.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
{
22
"dist/streamsql.esm.js": {
3-
"bundled": 7741,
4-
"minified": 3828,
5-
"gzipped": 1575,
3+
"bundled": 8133,
4+
"minified": 3944,
5+
"gzipped": 1606,
66
"treeshaked": {
77
"rollup": {
88
"code": 75,
99
"import_statements": 0
1010
},
1111
"webpack": {
12-
"code": 4442
12+
"code": 4558
1313
}
1414
}
1515
},
1616
"dist/streamsql.cjs.js": {
17-
"bundled": 9122,
18-
"minified": 4326,
19-
"gzipped": 1641
17+
"bundled": 9592,
18+
"minified": 4470,
19+
"gzipped": 1679
2020
},
2121
"dist/streamsql.min.js": {
22-
"bundled": 10015,
23-
"minified": 4153,
24-
"gzipped": 1615
22+
"bundled": 10483,
23+
"minified": 4297,
24+
"gzipped": 1655
2525
}
2626
}

integration/fixtures/streamsql.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<script type="text/javascript" src="./streamsql.min.js"></script>
7+
<title>Test Template Functions</title>
8+
</head>
9+
<body>
10+
<h1>Alt Page</h1>
11+
<input id="input-user-id" type="text" />
12+
<button id="login-button">Login</button>
13+
<button id="logout-button">Logout</button>
14+
<div id="identify-response">
15+
<!-- Plug/unplug user id here -->
16+
</div>
17+
18+
19+
<script type="application/javascript">
20+
streamsql.init('0000-0000-0000-0000'); // create client with apiKey
21+
// These should be called on (un)identify.
22+
// Test by checking the text of user-id response
23+
streamsql.onIdentify = function(userId) {
24+
document.getElementById('identify-response').innerText = userId;
25+
}
26+
streamsql.onUnidentify = function() {
27+
document.getElementById('identify-response').innerText = '';
28+
}
29+
document.getElementById('login-button').addEventListener('click', function () {
30+
var userId = document.getElementById('input-user-id').value;
31+
streamsql.identify(userId);
32+
})
33+
document.getElementById('logout-button').addEventListener('click', function () {
34+
streamsql.unidentify() // clear the userid
35+
})
36+
</script>
37+
</body>
38+
</html>

integration/index.umd.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const home = SERVER_URL + '/index.html'
22
const alt = SERVER_URL + '/alt.html'
3+
const templatePage = SERVER_URL + '/template-fns.html'
34

45
describe('integration', () => {
56
let page
@@ -155,4 +156,19 @@ describe('integration', () => {
155156
postData = postData && JSON.parse(postData)
156157
expect(postData.user).toMatchObject({ id: userId })
157158
}, 8000)
159+
160+
it('calls template functions onIdentify and onUnidentify', async () => {
161+
const userId = 'user-id'
162+
const responseDivSelector = 'div#identify-response'
163+
// simulate a login that should store id to send w request
164+
await page.goto(templatePage)
165+
await page.type('#input-user-id', userId)
166+
await page.click('button#login-button')
167+
const identifyResponse = await page.$(responseDivSelector)
168+
expect(await identifyResponse.evaluate(node => node.innerText)).toBe(userId);
169+
170+
await page.click('button#logout-button')
171+
const unidentifyResponse = await page.$(responseDivSelector)
172+
expect(await unidentifyResponse.evaluate(node => node.innerText)).toBe('');
173+
})
158174
})

src/StreamSQL.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ export default class StreamSQLClient implements CoreAPI {
3636
public identify(userId: string): StreamSQLClient {
3737
if (!this.identifier) this.throwNoInitError()
3838
this.identifier.setUser(userId)
39+
this.onIdentify(userId) // call template listener
3940
return this
4041
}
4142

4243
public unidentify(): StreamSQLClient {
4344
if (!this.identifier) this.throwNoInitError()
4445
this.identifier.deleteUser()
46+
this.onUnidentify() // call template listener
4547
return this
4648
}
4749

@@ -76,4 +78,9 @@ export default class StreamSQLClient implements CoreAPI {
7678
private throwNoInitError(): never {
7779
throw streamsqlErr(`api key must be set first: streamsql.init(apiKey)`)
7880
}
81+
82+
// Template functions. Originally built to allow pixel to listen for events.
83+
// @ts-ignore
84+
public onIdentify(userId: string) {}
85+
public onUnidentify() {}
7986
}

src/test/streamsql.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,20 @@ describe('StreamSQL Core', () => {
132132
})
133133
expect(() => streamsql.sendEvent('mystream')).not.toThrow()
134134
})
135+
136+
it('calls template listeners for identify, unidentify', () => {
137+
streamsql.init(apiKey)
138+
const onIdentify = jest.fn((userId: string) => userId)
139+
const onUnidentify = jest.fn()
140+
141+
streamsql.onIdentify = onIdentify
142+
streamsql.onUnidentify = onUnidentify
143+
144+
streamsql.identify('user-id')
145+
expect(onIdentify).toHaveBeenCalledTimes(1)
146+
expect(onIdentify).toHaveBeenCalledWith('user-id')
147+
148+
streamsql.unidentify()
149+
expect(onUnidentify).toHaveBeenCalledTimes(1)
150+
})
135151
})

0 commit comments

Comments
 (0)