@toa.io/core 1.0.0-alpha.7 → 1.0.0-alpha.9

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/core",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-alpha.9",
4
4
  "description": "Toa Core",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -21,13 +21,13 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@rsql/parser": "1.2.4",
24
- "@toa.io/console": "1.0.0-alpha.7",
25
- "@toa.io/generic": "1.0.0-alpha.7",
26
- "@toa.io/yaml": "1.0.0-alpha.7",
24
+ "@toa.io/console": "1.0.0-alpha.9",
25
+ "@toa.io/generic": "1.0.0-alpha.9",
26
+ "@toa.io/yaml": "1.0.0-alpha.9",
27
27
  "error-value": "0.3.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "clone-deep": "4.0.1"
31
31
  },
32
- "gitHead": "4f5ac0bc342d4b7bd469fbe5c74266f050b55c9f"
32
+ "gitHead": "017a3fa22e8c60654c240f8e55908773d44d4ed1"
33
33
  }
package/src/assignment.js CHANGED
@@ -9,13 +9,22 @@ class Assignment extends Operation {
9
9
  }
10
10
 
11
11
  async commit (store) {
12
- const { scope, state, reply } = store
12
+ const {
13
+ scope,
14
+ state,
15
+ reply
16
+ } = store
13
17
 
14
18
  if (reply.error !== undefined) return
15
19
 
16
20
  scope.set(state)
17
21
 
18
- await this.scope.apply(scope)
22
+ const output = await this.scope.apply(scope)
23
+
24
+ // assignment returns new state by default
25
+ if (store.reply.output === undefined) {
26
+ store.reply.output = output
27
+ }
19
28
  }
20
29
  }
21
30
 
package/src/exceptions.js CHANGED
@@ -19,6 +19,7 @@ const codes = {
19
19
  StatePrecondition: 303,
20
20
  StateConcurrency: 304,
21
21
  StateInitialization: 305,
22
+ Duplicate: 306,
22
23
 
23
24
  Communication: 400,
24
25
  Transmission: 401
package/src/state.js CHANGED
@@ -4,21 +4,14 @@ const { empty } = require('@toa.io/generic')
4
4
 
5
5
  const {
6
6
  StatePreconditionException,
7
- StateNotFoundException,
8
- StateInitializationException
7
+ StateNotFoundException
9
8
  } = require('./exceptions')
10
9
 
11
- /**
12
- * @implements {toa.core.State}
13
- */
14
10
  class State {
15
- /** @type {toa.core.Storage} */
11
+ #dependent
16
12
  #storage
17
-
18
- /** @type {toa.core.entity.Factory} */
19
13
  #entity
20
14
  #emission
21
- #dependent
22
15
 
23
16
  constructor (storage, entity, emission, dependent) {
24
17
  this.#storage = storage
@@ -35,12 +28,16 @@ class State {
35
28
  const record = await this.#storage.get(query)
36
29
 
37
30
  if (record === null) {
38
- if (this.#dependent && query.id !== undefined && query.version === undefined) return this.init(query.id)
39
- else if (query.version !== undefined) throw new StatePreconditionException()
31
+ if (this.#dependent && query.id !== undefined && query.version === undefined) {
32
+ return this.init(query.id)
33
+ } else if (query.version !== undefined) throw new StatePreconditionException()
40
34
  }
41
35
 
42
- if (record === null) return null
43
- else return this.#entity.object(record)
36
+ if (record === null) {
37
+ return null
38
+ } else {
39
+ return this.#entity.object(record)
40
+ }
44
41
  }
45
42
 
46
43
  async objects (query) {
@@ -75,7 +72,10 @@ class State {
75
72
  }
76
73
 
77
74
  async apply (state) {
78
- const { changeset, insert } = state.export()
75
+ const {
76
+ changeset,
77
+ insert
78
+ } = state.export()
79
79
 
80
80
  let upsert
81
81
 
@@ -86,12 +86,20 @@ class State {
86
86
  const result = await this.#storage.upsert(state.query, changeset, upsert)
87
87
 
88
88
  if (result === null) {
89
- if (state.query.version !== undefined) throw new StatePreconditionException()
90
- else throw new StateNotFoundException()
89
+ if (state.query.version !== undefined) {
90
+ throw new StatePreconditionException()
91
+ } else {
92
+ throw new StateNotFoundException()
93
+ }
94
+ } else {
95
+ // same as above
96
+ await this.#emission.emit({
97
+ changeset,
98
+ state: result
99
+ })
91
100
  }
92
101
 
93
- // same as above
94
- await this.#emission.emit({ changeset, state: result })
102
+ return result
95
103
  }
96
104
  }
97
105
 
package/src/transition.js CHANGED
@@ -3,7 +3,12 @@
3
3
  const { retry } = require('@toa.io/generic')
4
4
 
5
5
  const { Operation } = require('./operation')
6
- const { StateConcurrencyException, StateNotFoundException } = require('./exceptions')
6
+ const {
7
+ StateConcurrencyException,
8
+ StateNotFoundException,
9
+ DuplicateException,
10
+ Exception
11
+ } = require('./exceptions')
7
12
 
8
13
  class Transition extends Operation {
9
14
  #concurrency
@@ -29,18 +34,29 @@ class Transition extends Operation {
29
34
  }
30
35
 
31
36
  async commit (store) {
32
- const { scope, state, reply, retry } = store
37
+ const {
38
+ scope,
39
+ state,
40
+ reply,
41
+ retry
42
+ } = store
33
43
 
34
44
  if (reply.error !== undefined) return
35
45
 
36
46
  scope.set(state)
37
47
 
38
- const ok = await this.scope.commit(scope)
48
+ const result = await this.scope.commit(scope)
39
49
 
40
- if (ok !== true) {
41
- if (this.#concurrency === 'retry') retry()
42
- else throw new StateConcurrencyException()
50
+ if (result === false ||
51
+ (result instanceof DuplicateException && result.message.includes('_id'))) {
52
+ if (this.#concurrency === 'retry') {
53
+ return retry()
54
+ } else {
55
+ throw new StateConcurrencyException()
56
+ }
43
57
  }
58
+
59
+ if (result instanceof Exception) throw result
44
60
  }
45
61
 
46
62
  async #retry (store, retry) {