Skip to content

Commit 068b598

Browse files
committed
event emitter implementation
1 parent c3e2e80 commit 068b598

5 files changed

Lines changed: 21 additions & 2 deletions

File tree

packages/ocap-js/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"debug": "^3.1.0",
2222
"graphql": "^0.13.2",
2323
"phoenix": "^1.3.4",
24-
"phoenix-channels": "^1.0.0"
24+
"phoenix-channels": "^1.0.0",
25+
"wolfy87-eventemitter": "^5.2.5"
2526
},
2627
"devDependencies": {
2728
"babel-cli": "^6.26.0",

packages/ocap-js/src/base.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const axios = require('axios');
2-
const EventEmitter = require('events');
32
const { print, parse } = require('graphql');
43
const {
54
getQueryBuilders,
@@ -57,6 +56,10 @@ class OCAPClientBase {
5756
throw new Error('_getSocketImplementation must be implemented in sub class');
5857
}
5958

59+
_getEventImplementation() {
60+
throw new Error('_getEventImplementation must be implemented in sub class');
61+
}
62+
6063
_getQueryId() {
6164
throw new Error('_getQueryId must be implemented in sub class');
6265
}
@@ -143,6 +146,7 @@ class OCAPClientBase {
143146
debug('subscription success', { queryId, res });
144147

145148
// create a new EventEmitter for each subscription
149+
const EventEmitter = this._getEventImplementation();
146150
this.subscriptions[queryId] = new EventEmitter();
147151
this.subscriptions[queryId].subscriptionId = res.subscriptionId;
148152

packages/ocap-js/src/browser.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const schemas = require('@arcblock/ocap-schema');
22
const md5 = require('blueimp-md5');
3+
const EventEmitter = require('wolfy87-eventemitter');
34
const { Socket } = require('phoenix');
45
const OCAPClientBase = require('./base');
56

@@ -16,6 +17,10 @@ class OCAPBrowserClient extends OCAPClientBase {
1617
return Socket;
1718
}
1819

20+
_getEventImplementation() {
21+
return EventEmitter;
22+
}
23+
1924
/**
2025
* Get ignored field lists that need to be excluded when build graphql queries
2126
* These fields are ignored because they may cause query failure

packages/ocap-js/src/node.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const schemas = require('@arcblock/ocap-schema');
22
const md5 = require('blueimp-md5');
3+
const EventEmitter = require('events');
34
const { Socket } = require('phoenix-channels');
45
const OCAPClientBase = require('./base');
56

@@ -16,6 +17,10 @@ class OCAPClient extends OCAPClientBase {
1617
return Socket;
1718
}
1819

20+
_getEventImplementation() {
21+
return EventEmitter;
22+
}
23+
1924
/**
2025
* Get ignored field lists that need to be excluded when build graphql queries
2126
* These fields are ignored because they may cause query failure

packages/ocap-js/yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4060,6 +4060,10 @@ window-size@0.1.0:
40604060
version "0.1.0"
40614061
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"
40624062

4063+
wolfy87-eventemitter@^5.2.5:
4064+
version "5.2.5"
4065+
resolved "https://registry.yarnpkg.com/wolfy87-eventemitter/-/wolfy87-eventemitter-5.2.5.tgz#e7af2adbb84e481c65edeb2a2e01032c8ff1b88f"
4066+
40634067
wordwrap@0.0.2:
40644068
version "0.0.2"
40654069
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"

0 commit comments

Comments
 (0)