Skip to content

Commit

Permalink
Update example.js (#2)
Browse files Browse the repository at this point in the history
* Update example.js

* Update example.js

Tiny change to path
  • Loading branch information
supersuryaansh authored Jan 31, 2025
1 parent 445d951 commit a7b3af9
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
const Hyperschema = require('hyperschema')
const Hyperdispatch = require('.')

const SCHEMA_DIR = './output/hyperschema'
const DISPATCH_DIR = './output/hyperdispatch'
const SCHEMA_DIR = './spec/hyperschema'

const schema = Hyperschema.from(SCHEMA_DIR)
const ns1 = schema.namespace('example')

ns1.register({
name: 'request1',
fields: [
{
name: 'field1',
type: 'uint'
},
{
name: 'field2',
type: 'string'
}
{ name: 'field1', type: 'uint' },
{ name: 'field2', type: 'string' }
]
})

ns1.register({
name: 'request2',
fields: [
{
name: 'field1',
type: 'string'
},
{
name: 'field2',
type: 'uint'
}
{ name: 'field1', type: 'string' },
{ name: 'field2', type: 'uint' }
]
})

// Write the schema to disk
Hyperschema.toDisk(schema)

const Hyperdispatch = require('hyperdispatch')

const DISPATCH_DIR = './spec/hyperdispatch'
const hyperdispatch = Hyperdispatch.from(SCHEMA_DIR, DISPATCH_DIR)

const ns2 = hyperdispatch.namespace('example')

// Define commands and associate them with requests
ns2.register({
name: 'command1',
requestType: '@example/request1'
Expand All @@ -52,4 +45,35 @@ ns2.register({
requestType: '@example/request2'
})

// Write the hyperdispatch configuration to disk
Hyperdispatch.toDisk(hyperdispatch)

const { Router, dispatch } = require('./spec/hyperdispatch')

const router = new Router()

// Register handlers for commands
router.add('@example/command1', (data, context) => {
console.log('Handler for command1 executed:', data, context)
return { success: true }
})

router.add('@example/command2', (data, context) => {
console.log('Handler for command2 executed:', data, context)
return { success: true }
})

router.add('@example/command3', (data, context) => {
console.log('Handler for command3 executed:', data, context)
return { success: true }
})

const context = { user: 'exampleUser' }

// Dispatch a command1 message
const encodedMessage1 = dispatch('@example/command1', { field1: 42, field2: 'hello' })
router.dispatch(encodedMessage1, context)

// Dispatch a command3 message
const encodedMessage2 = dispatch('@example/command3', { field1: 'world', field2: 99 })
router.dispatch(encodedMessage2, context)

0 comments on commit a7b3af9

Please sign in to comment.