Skip to content

Commit ab3eae4

Browse files
committed
improve eventhandler to detect changes automatically, return bool is no longer required
1 parent 3b5b2a0 commit ab3eae4

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

js/mokapi/on.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package mokapi
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
57
"github.com/dop251/goja"
68
"mokapi/js/util"
@@ -18,7 +20,13 @@ func (m *Module) On(event string, do goja.Value, vArgs goja.Value) {
1820
}
1921

2022
f := func(args ...interface{}) (bool, error) {
21-
r, err := m.loop.RunAsync(func(vm *goja.Runtime) (goja.Value, error) {
23+
origin, err := getHashes(args...)
24+
if err != nil {
25+
return false, err
26+
}
27+
28+
var r goja.Value
29+
r, err = m.loop.RunAsync(func(vm *goja.Runtime) (goja.Value, error) {
2230
call, _ := goja.AssertFunction(do)
2331
var params []goja.Value
2432
for _, v := range args {
@@ -35,7 +43,16 @@ func (m *Module) On(event string, do goja.Value, vArgs goja.Value) {
3543
return false, err
3644
}
3745

38-
return r.ToBoolean(), nil
46+
if r != goja.Undefined() {
47+
return r.ToBoolean(), nil
48+
}
49+
50+
newHashes, err := getHashes(args...)
51+
if err != nil {
52+
return false, err
53+
}
54+
55+
return haveChanges(origin, newHashes), nil
3956
}
4057

4158
m.host.On(event, f, args.tags)
@@ -69,3 +86,25 @@ func getOnArgs(vm *goja.Runtime, args goja.Value) (onArgs, error) {
6986
}
7087
return onArgs{}, nil
7188
}
89+
90+
func getHashes(args ...any) ([][]byte, error) {
91+
var result [][]byte
92+
for _, arg := range args {
93+
b, err := json.Marshal(arg)
94+
if err != nil {
95+
return nil, fmt.Errorf("unable to marshal arg")
96+
}
97+
result = append(result, b)
98+
}
99+
return result, nil
100+
}
101+
102+
func haveChanges(origin [][]byte, new [][]byte) bool {
103+
for i, o := range origin {
104+
n := new[i]
105+
if !bytes.Equal(o, n) {
106+
return true
107+
}
108+
}
109+
return false
110+
}

js/mokapi/on_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ func TestModule_On(t *testing.T) {
5757
r.Equal(t, true, b)
5858
},
5959
},
60+
{
61+
name: "event handler changes params but returns void",
62+
test: func(t *testing.T, vm *goja.Runtime, host *enginetest.Host) {
63+
var handler func(args ...interface{}) (bool, error)
64+
host.OnFunc = func(evt string, do func(args ...interface{}) (bool, error), tags map[string]string) {
65+
handler = do
66+
}
67+
68+
_, err := vm.RunString(`
69+
const m = require('mokapi')
70+
m.on('http', (param) => { param['foo'] = false })
71+
`)
72+
r.NoError(t, err)
73+
b, err := handler(map[string]bool{"foo": true})
74+
r.NoError(t, err)
75+
r.Equal(t, true, b)
76+
},
77+
},
6078
{
6179
name: "event handler throws error",
6280
test: func(t *testing.T, vm *goja.Runtime, host *enginetest.Host) {

npm/types/index.d.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ export interface EventHandler {
108108
smtp: SmtpEventHandler;
109109
}
110110

111+
export type ChangeResult = boolean | void;
112+
113+
export type EventHandlerResult = ChangeResult | Promise<ChangeResult>;
114+
111115
/**
112116
* HttpEventHandler is a function that is executed when an HTTP event is triggered.
113117
* https://mokapi.io/docs/javascript-api/mokapi/eventhandler/httpeventhandler
@@ -122,7 +126,7 @@ export interface EventHandler {
122126
* })
123127
* }
124128
*/
125-
export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => boolean | Promise<boolean>;
129+
export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => EventHandlerResult;
126130

127131
/**
128132
* HttpRequest is an object used by HttpEventHandler that contains request-specific
@@ -136,7 +140,7 @@ export interface HttpRequest {
136140
/** Represents a parsed URL. */
137141
readonly url: Url;
138142

139-
/** Body contains request body specified by OpenAPI request body. */
143+
/** Body contains the request body specified by OpenAPI request body. */
140144
readonly body: any;
141145

142146
/** Object contains path parameters specified by OpenAPI path parameters. */
@@ -204,7 +208,7 @@ export interface Url {
204208
* })
205209
* }
206210
*/
207-
export type KafkaEventHandler = (message: KafkaEventMessage) => boolean | Promise<boolean>;
211+
export type KafkaEventHandler = (message: KafkaEventMessage) => EventHandlerResult;
208212

209213
/**
210214
* KafkaEventMessage is an object used by KafkaEventHandler that contains Kafka-specific message data.
@@ -243,7 +247,7 @@ export interface KafkaEventMessage {
243247
* })
244248
* }
245249
*/
246-
export type LdapEventHandler = (request: LdapSearchRequest, response: LdapSearchResponse) => boolean | Promise<boolean>;
250+
export type LdapEventHandler = (request: LdapSearchRequest, response: LdapSearchResponse) => EventHandlerResult;
247251

248252
/**
249253
* LdapSearchRequest is an object used by LdapEventHandler that contains request-specific data.
@@ -343,7 +347,7 @@ export enum LdapResultStatus {
343347
SizeLimitExceeded = 4,
344348
}
345349

346-
export type SmtpEventHandler = (record: SmtpEventMessage) => boolean | Promise<boolean>;
350+
export type SmtpEventHandler = (record: SmtpEventMessage) => EventHandlerResult;
347351

348352
export interface SmtpEventMessage {
349353
server: string;

0 commit comments

Comments
 (0)