Comparing version 0.0.17 to 0.0.18
@@ -37,3 +37,7 @@ | ||
try { | ||
this.dispatchEvent(new CustomEvent(eventType, { detail: data })); | ||
if (data.isSuiEvent) { | ||
this.dispatchEvent(data); | ||
} else { | ||
this.dispatchEvent(new CustomEvent(eventType, { detail: data })); | ||
} | ||
} catch (e) { | ||
@@ -40,0 +44,0 @@ console.error(e); |
const SuiCommonMethods = require('./SuiCommonMethods.js'); | ||
class SuiEvent extends SuiCommonMethods { | ||
class SuiEvent extends Event { | ||
constructor(params = {}) { | ||
super(params); | ||
const typeName = params.data ? (params.data.type.split('::').pop()) : null; | ||
super(typeName, {}); | ||
this._debug = !!params.debug; | ||
this._suiMaster = params.suiMaster; | ||
@@ -13,4 +15,23 @@ if (!this._suiMaster) { | ||
this._data = params.data || {}; | ||
this.detail = this; // quick backward support as this is the instance of CustomEvent | ||
} | ||
log(...args) { | ||
if (!this._debug) { | ||
return; | ||
} | ||
let prefix = (this._suiMaster ? (''+this._suiMaster.instanceN+' |') : (this.instanceN ? ''+this.instanceN+' |' : '') ); | ||
// prefix += this.constructor.name+' | '; | ||
args.unshift(this.constructor.name+' |'); | ||
args.unshift(prefix); | ||
console.info.apply(null, args); | ||
} | ||
get isSuiEvent() { | ||
return true; | ||
} | ||
/** | ||
@@ -17,0 +38,0 @@ * In module type name, without package and module prefix |
@@ -108,3 +108,2 @@ // const { spawn } = require('child_process'); | ||
process.on('exit', ()=>{ | ||
console.log('this._testFallbackEnabled', this._testFallbackEnabled); | ||
if (this._child) { | ||
@@ -111,0 +110,0 @@ this._child.kill(); |
@@ -6,2 +6,3 @@ const sui = require('@mysten/sui.js'); | ||
const SuiPaginatedResponse = require('./SuiPaginatedResponse.js'); | ||
const SuiEvent = require('./SuiEvent.js'); | ||
// fromB64, toB64 | ||
@@ -32,4 +33,41 @@ | ||
this._normalizedMoveModule = {}; | ||
this._unsubscribeFunction = null; | ||
} | ||
async subscribeEvents() { | ||
this.log('subscribing to events of module', this._moduleName); | ||
// we need very first package version's id here. So we are getting it from normalized data | ||
const normalizedPackageAddress = await this.getNormalizedPackageAddress(); | ||
const onMessage = (rawEvent) => { | ||
const suiEvent = new SuiEvent({ | ||
suiMaster: this._suiMaster, | ||
debug: this._debug, | ||
data: rawEvent, | ||
}); | ||
const eventTypeName = suiEvent.typeName; | ||
this.log('got event', eventTypeName); | ||
this.emit(eventTypeName, suiEvent); | ||
}; | ||
this._unsubscribeFunction = await this._suiMaster._provider.subscribeEvent({ | ||
filter: {"MoveModule": {"package": normalizedPackageAddress, "module": this._moduleName} }, | ||
onMessage: onMessage, | ||
}); | ||
} | ||
async unsubscribeEvents() { | ||
if (this._unsubscribeFunction) { | ||
await this._unsubscribeFunction(); | ||
this._unsubscribeFunction = null; | ||
return true; | ||
} | ||
return false; | ||
} | ||
get objectStorage() { | ||
@@ -186,4 +224,11 @@ return this._suiMaster.objectStorage; | ||
const eventData = event.parsedJson; | ||
this.emit(eventTypeName, eventData); | ||
// const eventData = event.parsedJson; | ||
const suiEvent = new SuiEvent({ | ||
suiMaster: this._suiMaster, | ||
debug: this._debug, | ||
data: event, | ||
}); | ||
this.emit(eventTypeName, suiEvent); | ||
} | ||
@@ -190,0 +235,0 @@ } |
{ | ||
"name": "suidouble", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"description": "Set of provider, package and object classes for javascript representation of Sui Move smart contracts. Use same code for publishing, upgrading, integration testing, interaction with smart contracts and integration in browser web3 dapps", | ||
@@ -24,3 +24,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"@mysten/sui.js": "^0.34.0", | ||
"@mysten/sui.js": "^0.35.1", | ||
"@wallet-standard/core": "^1.0.3" | ||
@@ -27,0 +27,0 @@ }, |
@@ -12,2 +12,3 @@ # suidouble | ||
- [Fetching Events](#fetching-events) | ||
- [Subscribe to Events](#subscribing-to-events) | ||
- [Executing smart contract method](#executing-smart-contract-method) | ||
@@ -142,2 +143,31 @@ - [Fetching objects](#fetching-objects) | ||
##### subscribing to events | ||
You can subscribe to Sui's contract events on package's module level. No types-etc filters for now ( @todo? ) | ||
```javascript | ||
const module = await contract.getModule('suidouble_chat'); | ||
await module.subscribeEvents(); | ||
module.addEventListener('ChatResponseCreated', (suiEvent)=>{ | ||
// received message emited by | ||
// emit(ChatResponseCreated { id: object::uid_to_inner(&chat_response_id), top_message_id: object::uid_to_inner(&id), seq_n: 0 }); | ||
// in suidouble_chat 's smart contract | ||
console.log(suiEvent.typeName); // == 'ChatResponseCreated' | ||
console.log(suiEvent.parsedJson); | ||
}); | ||
module.addEventListener('ChatTopMessageCreated', (suiEvent)=>{ | ||
// received message emited by | ||
// emit(ChatTopMessageCreated { id: object::uid_to_inner(&id), top_response_id: object::uid_to_inner(&chat_response_id), }); | ||
// in suidouble_chat 's smart contract | ||
console.log(suiEvent.typeName); // == 'ChatTopMessageCreated' | ||
console.log(suiEvent.parsedJson); | ||
}); | ||
``` | ||
Don't forget to unsubscribe from events when you don't need them anymore: | ||
```javascript | ||
await module.unsubscribeEvents(); | ||
``` | ||
##### executing smart contract method | ||
@@ -144,0 +174,0 @@ |
@@ -28,3 +28,3 @@ 'use strict' | ||
test('init suiMaster and connect it to local test validator', async t => { | ||
suiMaster = new SuiMaster({provider: suiLocalTestValidator, as: 'somebody', debug: false}); | ||
suiMaster = new SuiMaster({provider: suiLocalTestValidator, as: 'somebody', debug: true}); | ||
await suiMaster.initialize(); | ||
@@ -154,2 +154,30 @@ | ||
test('subscribe to module events', async t => { | ||
const module = await contract.getModule('suidouble_chat'); | ||
await module.subscribeEvents(); | ||
let gotEventChatTopMessageCreated = false; | ||
let gotEventChatResponseCreated = false; | ||
module.addEventListener('ChatTopMessageCreated', (event)=>{ | ||
gotEventChatTopMessageCreated = event; | ||
}); | ||
module.addEventListener('ChatResponseCreated', (event)=>{ | ||
gotEventChatResponseCreated = event.detail; // .detail is reference to event itself. To support CustomEvent pattern | ||
}); | ||
await contract.moveCall('suidouble_chat', 'post', [chatShopObjectId, 'the message', 'metadata']); | ||
await new Promise((res)=>setTimeout(res, 300)); // got events without timeout, but just to be sure. | ||
t.ok(gotEventChatTopMessageCreated); | ||
t.ok(gotEventChatResponseCreated); | ||
// just some checks that events have data by contract's architecture | ||
t.ok(gotEventChatResponseCreated.parsedJson.top_message_id == gotEventChatTopMessageCreated.parsedJson.id); | ||
t.ok(gotEventChatTopMessageCreated.parsedJson.top_response_id == gotEventChatResponseCreated.parsedJson.id); | ||
// unsubscribing from events, to close websocket | ||
await module.unsubscribeEvents(); | ||
}); | ||
test('execute contract methods', async t => { | ||
@@ -295,3 +323,3 @@ const moveCallResult = await contract.moveCall('suidouble_chat', 'post', [chatShopObjectId, 'the message', 'metadata']); | ||
test('stops local test node', async t => { | ||
SuiLocalTestValidator.stop(); | ||
await SuiLocalTestValidator.stop(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
178681
2542
353
68
6
21
1
+ Added@mysten/sui.js@0.35.1(transitive)
+ Added@open-rpc/client-js@1.8.1(transitive)
+ Addedisomorphic-fetch@3.0.0(transitive)
+ Addedisomorphic-ws@5.0.0(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addedstrict-event-emitter-types@2.0.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
- Removed@mysten/sui.js@0.34.1(transitive)
- Removed@types/connect@3.4.38(transitive)
- Removed@types/node@12.20.55(transitive)
- Removed@types/ws@7.4.7(transitive)
- RemovedJSONStream@1.3.5(transitive)
- Removedbufferutil@4.0.8(transitive)
- Removedcommander@2.20.3(transitive)
- Removeddelay@5.0.0(transitive)
- Removedes6-promise@4.2.8(transitive)
- Removedes6-promisify@5.0.0(transitive)
- Removedeventemitter3@4.0.7(transitive)
- Removedeyes@0.1.8(transitive)
- Removedisomorphic-ws@4.0.1(transitive)
- Removedjayson@4.1.2(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsonparse@1.3.1(transitive)
- Removednode-gyp-build@4.8.2(transitive)
- Removedrpc-websockets@7.11.2(transitive)
- Removedthrough@2.3.8(transitive)
- Removedutf-8-validate@5.0.10(transitive)
- Removeduuid@8.3.2(transitive)
- Removedws@8.18.0(transitive)
Updated@mysten/sui.js@^0.35.1