@toa.io/bridges.node 0.8.0-dev.1 → 0.8.0-dev.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/bridges.node",
3
- "version": "0.8.0-dev.1",
3
+ "version": "0.8.0-dev.3",
4
4
  "description": "Toa Node Bridge (inproc)",
5
5
  "homepage": "https://toa.io",
6
6
  "author": {
@@ -26,13 +26,13 @@
26
26
  "test": "echo \"Error: run tests from root\" && exit 1"
27
27
  },
28
28
  "dependencies": {
29
- "@toa.io/core": "0.8.0-dev.1",
30
- "@toa.io/filesystem": "0.7.1-dev.1",
31
- "@toa.io/generic": "0.8.0-dev.1",
29
+ "@toa.io/core": "0.8.0-dev.3",
30
+ "@toa.io/filesystem": "0.7.1-dev.3",
31
+ "@toa.io/generic": "0.8.0-dev.3",
32
32
  "fast-glob": "3.2.7"
33
33
  },
34
34
  "devDependencies": {
35
35
  "clone-deep": "4.0.1"
36
36
  },
37
- "gitHead": "9bb2f372c556440ee3e2a42e10eb415220ba1cb0"
37
+ "gitHead": "d72b4c2362e928dba56f4743cc956aa5486f82a4"
38
38
  }
@@ -17,13 +17,14 @@ class Func {
17
17
  this.#func = func
18
18
  }
19
19
 
20
- execute (input, state) {
21
- return this.#func(input, state, this.#context)
22
- }
23
-
24
20
  mount (context) {
25
21
  this.#context = context
26
22
  }
23
+
24
+ execute (input, state) {
25
+ if (state === undefined) return this.#func(input, this.#context)
26
+ else return this.#func(input, state, this.#context)
27
+ }
27
28
  }
28
29
 
29
30
  /** @type {toa.node.define.algorithms.Constructor} */
@@ -1,4 +1,4 @@
1
1
  'use strict'
2
2
 
3
- exports.types = ['transition', 'observation', 'assignment']
4
- exports.scopes = ['object', 'objects', 'changeset', 'none']
3
+ exports.types = ['transition', 'observation', 'assignment', 'computation', 'effect']
4
+ exports.scopes = ['object', 'objects', 'changeset']
@@ -1,3 +1,5 @@
1
+ // noinspection JSValidateTypes
2
+
1
3
  'use strict'
2
4
 
3
5
  /** @type {toa.node.define.operations.Define} */
@@ -7,8 +9,8 @@ const define = (descriptor) => {
7
9
  /** @type {toa.node.define.operations.Definition} */
8
10
  const definition = {}
9
11
 
10
- definition.type = /** @type {typeof toa.norm.component.Operation.scope} */ match[2].toLowerCase()
11
- definition.scope = /** @type {typeof toa.norm.component.Operation.type} */ match[1].toLowerCase()
12
+ definition.type = match.groups.type.toLowerCase()
13
+ definition.scope = match.groups.scope?.toLowerCase()
12
14
 
13
15
  return definition
14
16
  }
@@ -21,7 +23,7 @@ const test = (statement, name) => {
21
23
  return declaration && match
22
24
  }
23
25
 
24
- const pattern = new RegExp('^(Objects?|Changeset|None)(Transition|Observation|Assignment)Factory$')
26
+ const pattern = new RegExp('^(?<scope>Objects?|Changeset)?(?<type>Transition|Observation|Assignment|Computation|Effect)Factory$')
25
27
 
26
28
  exports.define = define
27
29
  exports.test = test
@@ -13,8 +13,6 @@ const define = (descriptor) => {
13
13
  definition.type = /** @type {typeof toa.norm.component.Operation.type} */ name
14
14
 
15
15
  if (node.params.length > 1) definition.scope = scope(node.params[1].name)
16
- else definition.scope = 'none'
17
-
18
16
  if (node.params.length === 0) definition.input = null
19
17
 
20
18
  return definition
@@ -34,7 +34,7 @@ it('should throw if no function exported', () => {
34
34
  })
35
35
 
36
36
  describe('function', () => {
37
- it('should parse declaration', () => {
37
+ it('should parse transition declaration', () => {
38
38
  function transition (input, object) {}
39
39
 
40
40
  const module = { transition }
@@ -43,6 +43,42 @@ describe('function', () => {
43
43
  expect(definition).toMatchObject({ type: 'transition', scope: 'object' })
44
44
  })
45
45
 
46
+ it('should parse observation declaration', () => {
47
+ function observation (input, object) {}
48
+
49
+ const module = { observation }
50
+ const definition = define(module)
51
+
52
+ expect(definition).toMatchObject({ type: 'observation', scope: 'object' })
53
+ })
54
+
55
+ it('should parse assignment declaration', () => {
56
+ function assignment (input, changeset) {}
57
+
58
+ const module = { assignment }
59
+ const definition = define(module)
60
+
61
+ expect(definition).toMatchObject({ type: 'assignment', scope: 'changeset' })
62
+ })
63
+
64
+ it('should parse computation declaration', () => {
65
+ function computation (input, context) {}
66
+
67
+ const module = { computation }
68
+ const definition = define(module)
69
+
70
+ expect(definition).toMatchObject({ type: 'computation', scope: undefined })
71
+ })
72
+
73
+ it('should parse effect declaration', () => {
74
+ function effect (input, context) {}
75
+
76
+ const module = { effect }
77
+ const definition = define(module)
78
+
79
+ expect(definition).toMatchObject({ type: 'effect', scope: undefined })
80
+ })
81
+
46
82
  it('should parse expression', () => {
47
83
  const observation = (input, objects) => null
48
84
  const module = { observation }
@@ -67,20 +103,12 @@ describe('function', () => {
67
103
  expect(definition.scope).toStrictEqual(undefined)
68
104
  })
69
105
 
70
- it('should define none scope', async () => {
106
+ it('should not define scope', async () => {
71
107
  const observation = (input) => null
72
108
  const module = { observation }
73
109
  const definition = define(module)
74
110
 
75
- expect(definition.scope).toStrictEqual('none')
76
- })
77
-
78
- it('should define none scope for _', async () => {
79
- const observation = (input, none, context) => null
80
- const module = { observation }
81
- const definition = define(module)
82
-
83
- expect(definition.scope).toStrictEqual('none')
111
+ expect(definition.scope).toBeUndefined()
84
112
  })
85
113
 
86
114
  it('should define null input', async () => {
@@ -140,7 +168,7 @@ describe('class', () => {
140
168
  expect(() => define(module)).toThrow('does not match conventions')
141
169
  })
142
170
 
143
- it('should define none scope', async () => {
171
+ it('should define not define default scope', async () => {
144
172
  class Observation {
145
173
  execute (input) {}
146
174
  }
@@ -148,7 +176,7 @@ describe('class', () => {
148
176
  const module = { Observation }
149
177
  const definition = define(module)
150
178
 
151
- expect(definition.scope).toStrictEqual('none')
179
+ expect(definition.scope).toBeUndefined()
152
180
  })
153
181
 
154
182
  it('should define null input', async () => {
@@ -161,6 +189,28 @@ describe('class', () => {
161
189
 
162
190
  expect(definition.input).toStrictEqual(null)
163
191
  })
192
+
193
+ it('should parse Computation', async () => {
194
+ class Computation {
195
+ execute () {}
196
+ }
197
+
198
+ const module = { Computation }
199
+ const definition = define(module)
200
+
201
+ expect(definition.type).toStrictEqual('computation')
202
+ })
203
+
204
+ it('should parse Effect', async () => {
205
+ class Effect {
206
+ execute () {}
207
+ }
208
+
209
+ const module = { Effect }
210
+ const definition = define(module)
211
+
212
+ expect(definition.type).toStrictEqual('effect')
213
+ })
164
214
  })
165
215
 
166
216
  describe('factory', () => {
@@ -182,14 +232,33 @@ describe('factory', () => {
182
232
  expect(definition.scope).toStrictEqual('object')
183
233
  })
184
234
 
185
- it('should define none scope', async () => {
235
+ it('should throw if not follows convention', async () => {
186
236
  class NoneObservationFactory {
187
237
  create () {}
188
238
  }
189
239
 
190
240
  const module = { NoneObservationFactory }
241
+
242
+ expect(() => define(module)).toThrow('does not match conventions')
243
+ })
244
+
245
+ it('should parse ComputationFactory', () => {
246
+ class ComputationFactory {
247
+ }
248
+
249
+ const module = { ComputationFactory }
250
+ const definition = define(module)
251
+
252
+ expect(definition.type).toStrictEqual('computation')
253
+ })
254
+
255
+ it('should parse EffectFactory', () => {
256
+ class EffectFactory {
257
+ }
258
+
259
+ const module = { EffectFactory }
191
260
  const definition = define(module)
192
261
 
193
- expect(definition.scope).toStrictEqual('none')
262
+ expect(definition.type).toStrictEqual('effect')
194
263
  })
195
264
  })
@@ -0,0 +1,18 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * @implements {toa.core.bridges.Algorithm}
5
+ */
6
+ class Computation {
7
+ #context
8
+
9
+ async mount (context) {
10
+ this.#context = context
11
+ }
12
+
13
+ async execute (input) {
14
+ return { output: { input, context: this.#context !== undefined } }
15
+ }
16
+ }
17
+
18
+ exports.Computation = Computation
@@ -0,0 +1,20 @@
1
+ 'use strict'
2
+
3
+ const { Computation } = require('./cls')
4
+
5
+ /**
6
+ * @implements {toa.node.algorithms.Factory}
7
+ */
8
+ class ComputationFactory {
9
+ #context
10
+
11
+ constructor (context) {
12
+ this.#context = context
13
+ }
14
+
15
+ create () {
16
+ return new Computation()
17
+ }
18
+ }
19
+
20
+ exports.ComputationFactory = ComputationFactory
@@ -0,0 +1,7 @@
1
+ 'use strict'
2
+
3
+ async function computation (input, object, context) {
4
+ return { output: { input, state: object, context: context !== undefined } }
5
+ }
6
+
7
+ exports.computation = computation
@@ -15,8 +15,12 @@ declare namespace toa.node {
15
15
  type func = (input?: any, scope?: object | object[], context?: _context.Context) => Promise<_core.Reply>
16
16
  }
17
17
 
18
- interface Algorithm extends bridges.Algorithm {
19
- mount?(context?: _context.Context): Promise<void> | void
18
+ interface Algorithm {
19
+ mount?(context: _context.Context): Promise<void> | void
20
+
21
+ execute(input: any, scope: object | object[]): Promise<_core.Reply>
22
+
23
+ execute(input: any): Promise<_core.Reply>
20
24
  }
21
25
 
22
26
  }