@toa.io/bridges.node 0.6.0-dev.3 → 0.6.0-dev.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/readme.md +9 -5
- package/src/algorithms/class.js +1 -1
- package/src/algorithms/factory.js +2 -2
- package/src/algorithms/function.js +26 -4
- package/src/algorithms/runner.js +12 -10
- package/src/define/.operations/syntaxes/class.js +1 -1
- package/src/factory.js +2 -2
- package/test/algorithms.runner.test.js +35 -9
- package/test/define.algorithms.test.js +1 -1
- package/test/define.operations.define.test.js +8 -8
- package/test/dummies/one/operations/cls.js +3 -3
- package/test/factory.algorithm.test.js +1 -1
- package/types/algorithms.d.ts +10 -0
- package/types/context.d.ts +1 -0
- package/types/define.d.ts +4 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toa.io/bridges.node",
|
|
3
|
-
"version": "0.6.0-dev.
|
|
3
|
+
"version": "0.6.0-dev.4",
|
|
4
4
|
"description": "Toa Node Bridge (inproc)",
|
|
5
5
|
"homepage": "https://toa.io",
|
|
6
6
|
"author": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@toa.io/core": "0.6.0-dev.
|
|
29
|
+
"@toa.io/core": "0.6.0-dev.4",
|
|
30
30
|
"@toa.io/filesystem": "0.6.0-dev.1",
|
|
31
31
|
"@toa.io/generic": "0.6.0-dev.1",
|
|
32
32
|
"fast-glob": "3.2.7"
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"clone-deep": "4.0.1"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "64ed74a84a57955b905fdd331b655bf4bd9bf3b2"
|
|
38
38
|
}
|
package/readme.md
CHANGED
|
@@ -41,12 +41,18 @@ See [Operation properties](#).
|
|
|
41
41
|
|
|
42
42
|
### Class
|
|
43
43
|
|
|
44
|
+
#### Example
|
|
45
|
+
|
|
44
46
|
```javascript
|
|
45
47
|
// operations/transit.js
|
|
46
48
|
|
|
47
49
|
class Transition {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
#context
|
|
51
|
+
|
|
52
|
+
async mount(context) {
|
|
53
|
+
this.#context = context
|
|
54
|
+
}
|
|
55
|
+
|
|
50
56
|
execute (input, object) {
|
|
51
57
|
// ...
|
|
52
58
|
|
|
@@ -66,8 +72,6 @@ it defines operation's `scope`.
|
|
|
66
72
|
|
|
67
73
|
```javascript
|
|
68
74
|
class ObjectTransitionFactory {
|
|
69
|
-
constructor (context) {}
|
|
70
|
-
|
|
71
75
|
create () {
|
|
72
76
|
// ...
|
|
73
77
|
}
|
|
@@ -100,4 +104,4 @@ return { output: { ok: 1 } }
|
|
|
100
104
|
### Storing Context
|
|
101
105
|
|
|
102
106
|
> Algorithm definition should store reference to the `context` object without copying its value
|
|
103
|
-
> type variables as they may change over
|
|
107
|
+
> type variables as they may change over operation lifetime.
|
package/src/algorithms/class.js
CHANGED
|
@@ -1,10 +1,32 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @implements {toa.node.Algorithm}
|
|
5
|
+
*/
|
|
6
|
+
class Func {
|
|
7
|
+
/** @type {toa.node.algorithms.func} */
|
|
8
|
+
#func
|
|
9
|
+
|
|
10
|
+
/** @type {toa.node.Context} */
|
|
11
|
+
#context
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {toa.node.algorithms.func} func
|
|
15
|
+
*/
|
|
16
|
+
constructor (func) {
|
|
17
|
+
this.#func = func
|
|
18
|
+
}
|
|
6
19
|
|
|
7
|
-
|
|
20
|
+
execute (input, state) {
|
|
21
|
+
return this.#func(input, state, this.#context)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
mount (context) {
|
|
25
|
+
this.#context = context
|
|
26
|
+
}
|
|
8
27
|
}
|
|
9
28
|
|
|
29
|
+
/** @type {toa.node.define.algorithms.Constructor} */
|
|
30
|
+
const create = (func) => new Func(func)
|
|
31
|
+
|
|
10
32
|
exports.create = create
|
package/src/algorithms/runner.js
CHANGED
|
@@ -6,32 +6,34 @@ const { Connector } = require('@toa.io/core')
|
|
|
6
6
|
* @implements {toa.core.bridges.Algorithm}
|
|
7
7
|
*/
|
|
8
8
|
class Runner extends Connector {
|
|
9
|
-
/** @type {toa.node.
|
|
10
|
-
#
|
|
9
|
+
/** @type {toa.node.Algorithm} */
|
|
10
|
+
#algorithm
|
|
11
11
|
|
|
12
|
-
/** @type {toa.
|
|
13
|
-
#
|
|
12
|
+
/** @type {toa.node.Context} */
|
|
13
|
+
#context
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* @param {toa.node.
|
|
16
|
+
* @param {toa.node.Algorithm} algorithm
|
|
17
17
|
* @param {toa.node.Context} context
|
|
18
18
|
*/
|
|
19
|
-
constructor (
|
|
19
|
+
constructor (algorithm, context) {
|
|
20
20
|
super()
|
|
21
21
|
|
|
22
|
-
this.#
|
|
22
|
+
this.#algorithm = algorithm
|
|
23
|
+
this.#context = context
|
|
23
24
|
|
|
24
25
|
this.depends(context)
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
async open () {
|
|
28
|
-
this.#
|
|
29
|
+
await this.#algorithm.mount?.(this.#context)
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
async
|
|
32
|
-
let reply = await this.#
|
|
32
|
+
async execute (input, state) {
|
|
33
|
+
let reply = await this.#algorithm.execute(input, state)
|
|
33
34
|
|
|
34
35
|
if (reply !== undefined) reply = normalize(reply)
|
|
36
|
+
else reply = {}
|
|
35
37
|
|
|
36
38
|
return reply
|
|
37
39
|
}
|
|
@@ -9,7 +9,7 @@ const define = (descriptor) => {
|
|
|
9
9
|
const declaration = /** @type {import('@babel/types').ClassDeclaration} */ descriptor.statement
|
|
10
10
|
|
|
11
11
|
descriptor.name = descriptor.name.toLowerCase()
|
|
12
|
-
descriptor.statement = method(declaration, '
|
|
12
|
+
descriptor.statement = method(declaration, 'execute')
|
|
13
13
|
|
|
14
14
|
return func.define(descriptor)
|
|
15
15
|
}
|
package/src/factory.js
CHANGED
|
@@ -40,9 +40,9 @@ function runner (module, context) {
|
|
|
40
40
|
const descriptor = extract(module)
|
|
41
41
|
const func = module[descriptor.name]
|
|
42
42
|
const factory = require('./algorithms/' + descriptor.syntax)
|
|
43
|
-
const
|
|
43
|
+
const instance = factory.create(func)
|
|
44
44
|
|
|
45
|
-
return new Runner(
|
|
45
|
+
return new Runner(instance, context)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
exports.Factory = Factory
|
|
@@ -11,30 +11,56 @@ it('should be', () => {
|
|
|
11
11
|
|
|
12
12
|
const context = /** @type {toa.node.Context} */ new Connector()
|
|
13
13
|
|
|
14
|
+
/** @type {Runner} */
|
|
15
|
+
let runner
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
const execute = () => undefined
|
|
19
|
+
const algorithm = /** @type {toa.node.Algorithm} */ ({ execute })
|
|
20
|
+
|
|
21
|
+
runner = new Runner(algorithm, context)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should be instance of Connector', async () => {
|
|
25
|
+
expect(runner).toBeInstanceOf(Connector)
|
|
26
|
+
})
|
|
27
|
+
|
|
14
28
|
it('should return output', async () => {
|
|
15
29
|
const values = [{ [generate()]: generate() }, generate()]
|
|
16
30
|
|
|
17
31
|
for (const value of values) {
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
|
|
32
|
+
const execute = () => value
|
|
33
|
+
const algorithm = /** @type {toa.node.Algorithm} */ ({ execute })
|
|
34
|
+
|
|
35
|
+
runner = new Runner(algorithm, context)
|
|
21
36
|
|
|
22
37
|
await runner.connect()
|
|
23
38
|
|
|
24
|
-
const reply = await runner.
|
|
39
|
+
const reply = await runner.execute()
|
|
25
40
|
|
|
26
41
|
expect(reply.output).toStrictEqual(value)
|
|
27
42
|
}
|
|
28
43
|
})
|
|
29
44
|
|
|
30
45
|
it('should not return undefined output', async () => {
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
const runner = new Runner(
|
|
46
|
+
const execute = () => undefined
|
|
47
|
+
const algorithm = /** @type {toa.node.Algorithm} */ { execute }
|
|
48
|
+
const runner = new Runner(algorithm, context)
|
|
34
49
|
|
|
35
50
|
await runner.connect()
|
|
36
51
|
|
|
37
|
-
const reply = await runner.
|
|
52
|
+
const reply = await runner.execute()
|
|
53
|
+
|
|
54
|
+
expect(reply).not.toStrictEqual(undefined)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('should mount', async () => {
|
|
58
|
+
const execute = () => undefined
|
|
59
|
+
const mount = jest.fn(() => undefined)
|
|
60
|
+
const algorithm = /** @type {toa.node.Algorithm} */ { execute, mount }
|
|
61
|
+
const runner = new Runner(algorithm, context)
|
|
62
|
+
|
|
63
|
+
await runner.connect()
|
|
38
64
|
|
|
39
|
-
expect(
|
|
65
|
+
expect(mount).toHaveBeenCalledWith(context)
|
|
40
66
|
})
|
|
@@ -94,7 +94,7 @@ describe('function', () => {
|
|
|
94
94
|
|
|
95
95
|
describe('class', () => {
|
|
96
96
|
class Transition {
|
|
97
|
-
|
|
97
|
+
execute (input, object) {}
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
const module = { Transition }
|
|
@@ -111,11 +111,11 @@ describe('class', () => {
|
|
|
111
111
|
expect(definition.scope).toStrictEqual('object')
|
|
112
112
|
})
|
|
113
113
|
|
|
114
|
-
it('should find
|
|
114
|
+
it('should find execute method', () => {
|
|
115
115
|
class Assignment {
|
|
116
|
-
execute (input,
|
|
116
|
+
execute (input, object) {}
|
|
117
117
|
|
|
118
|
-
run (input,
|
|
118
|
+
run (input, objects) {}
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
const module = { Assignment }
|
|
@@ -124,12 +124,12 @@ describe('class', () => {
|
|
|
124
124
|
expect(definition).toMatchObject({ type: 'assignment', scope: 'object' })
|
|
125
125
|
})
|
|
126
126
|
|
|
127
|
-
it('should throw if no
|
|
127
|
+
it('should throw if no execute method found', () => {
|
|
128
128
|
class Observation {}
|
|
129
129
|
|
|
130
130
|
const module = { Observation }
|
|
131
131
|
|
|
132
|
-
expect(() => define(module)).toThrow('Method \'
|
|
132
|
+
expect(() => define(module)).toThrow('Method \'execute\' not found')
|
|
133
133
|
})
|
|
134
134
|
|
|
135
135
|
it('should throw if function is not a class', () => {
|
|
@@ -142,7 +142,7 @@ describe('class', () => {
|
|
|
142
142
|
|
|
143
143
|
it('should define none scope', async () => {
|
|
144
144
|
class Observation {
|
|
145
|
-
|
|
145
|
+
execute (input) {}
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
const module = { Observation }
|
|
@@ -153,7 +153,7 @@ describe('class', () => {
|
|
|
153
153
|
|
|
154
154
|
it('should define null input', async () => {
|
|
155
155
|
class Observation {
|
|
156
|
-
|
|
156
|
+
execute () {}
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
const module = { Observation }
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @implements {toa.
|
|
4
|
+
* @implements {toa.node.Algorithm}
|
|
5
5
|
*/
|
|
6
6
|
class Transition {
|
|
7
7
|
#context
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
async mount (context) {
|
|
10
10
|
this.#context = context
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
async
|
|
13
|
+
async execute (input, object) {
|
|
14
14
|
return { output: { input, state: object, context: this.#context !== undefined } }
|
|
15
15
|
}
|
|
16
16
|
}
|
package/types/algorithms.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { bridges } from '@toa.io/core/types'
|
|
2
|
+
import * as _context from './context'
|
|
3
|
+
import * as _core from '@toa.io/core/types';
|
|
2
4
|
|
|
3
5
|
declare namespace toa.node {
|
|
4
6
|
|
|
@@ -10,6 +12,14 @@ declare namespace toa.node {
|
|
|
10
12
|
create: Constructor
|
|
11
13
|
}
|
|
12
14
|
|
|
15
|
+
type func = (input?: any, scope?: object | object[], context?: _context.Context) => Promise<_core.Reply>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface Algorithm extends bridges.Algorithm {
|
|
19
|
+
mount?(context?: _context.Context): Promise<void> | void
|
|
13
20
|
}
|
|
14
21
|
|
|
15
22
|
}
|
|
23
|
+
|
|
24
|
+
export type Algorithm = toa.node.Algorithm
|
|
25
|
+
export type func = toa.node.algorithms.func
|
package/types/context.d.ts
CHANGED
package/types/define.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Operation } from '@toa.io/norm/types'
|
|
2
2
|
import { Node, Statement } from '@babel/types'
|
|
3
|
-
import
|
|
4
|
-
import * as
|
|
3
|
+
import * as _bridges from '@toa.io/core/types/bridges'
|
|
4
|
+
import * as _context from './context'
|
|
5
|
+
import * as _algorithm from './algorithms'
|
|
5
6
|
|
|
6
7
|
declare namespace toa.node.define {
|
|
7
8
|
|
|
@@ -22,7 +23,7 @@ declare namespace toa.node.define {
|
|
|
22
23
|
|
|
23
24
|
type Test = (node: Node, type: string) => boolean
|
|
24
25
|
|
|
25
|
-
type Constructor = (func:
|
|
26
|
+
type Constructor = (func: _algorithm.func, context: _context.Context) => _algorithm.Algorithm
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
type Algorithms = (root: string) => Promise<algorithms.List>
|