1
1
import { HumanActivity , Resolution , StartOptions } from "../../index" ;
2
- import { pluginDb } from "../../../persistence/plugin-db" ;
3
- import { recognizersStateModel } from "./model" ;
2
+ import { Couchbase } from "nativescript-couchbase-plugin" ;
4
3
5
4
export interface RecognizerStateStore {
6
5
isActive ( recognizer : Resolution ) : Promise < boolean > ;
@@ -17,8 +16,14 @@ export interface RecognizerStateStore {
17
16
) : Promise < void > ;
18
17
}
19
18
19
+ const DATABASE_NAME = "recognizers-state" ;
20
+
20
21
class RecognizersStateStoreDb implements RecognizerStateStore {
21
- private tableName = recognizersStateModel . name ;
22
+ private database : Couchbase ;
23
+
24
+ constructor ( ) {
25
+ this . database = new Couchbase ( DATABASE_NAME ) ;
26
+ }
22
27
23
28
async isActive ( recognizer : Resolution ) : Promise < boolean > {
24
29
const recognizerData = await this . getRecognizerData ( recognizer ) ;
@@ -38,7 +43,7 @@ class RecognizersStateStoreDb implements RecognizerStateStore {
38
43
if ( ! recognizerData ) {
39
44
return null ;
40
45
}
41
- return JSON . parse ( recognizerData . startOptions ) ;
46
+ return recognizerData . startOptions ;
42
47
}
43
48
44
49
async markAsActive (
@@ -53,15 +58,12 @@ class RecognizersStateStoreDb implements RecognizerStateStore {
53
58
}
54
59
55
60
async getLastActivity ( recognizer : Resolution ) : Promise < HumanActivity > {
56
- const instance = await this . db ( ) ;
57
- const rows = await instance
58
- . query ( "select" )
59
- . where ( [ "id" , "=" , recognizer ] )
60
- . exec ( ) ;
61
- if ( rows . length === 0 ) {
61
+ const recognizerData = this . database . getDocument ( recognizer ) ;
62
+
63
+ if ( ! recognizerData ) {
62
64
return null ;
63
65
}
64
- const lastActivity = rows [ 0 ] . lastActivity ;
66
+ const lastActivity = recognizerData . lastActivity ;
65
67
return lastActivity ? lastActivity : null ;
66
68
}
67
69
@@ -73,45 +75,37 @@ class RecognizersStateStoreDb implements RecognizerStateStore {
73
75
if ( ! isActive ) {
74
76
return null ;
75
77
}
76
- const instance = await this . db ( `${ this . tableName } .lastActivity` ) ;
77
- await instance
78
- . query ( "upsert" , activity )
79
- . where ( [ "id" , "=" , recognizer ] )
80
- . exec ( ) ;
78
+
79
+ this . database . updateDocument ( recognizer , { lastActivity : activity } ) ;
81
80
}
82
81
83
82
private async updateStatus (
84
83
recognizer : Resolution ,
85
84
active : boolean ,
86
85
startOptions : StartOptions = { }
87
86
) {
88
- const instance = await this . db ( ) ;
89
- await instance
90
- . query ( "upsert" , {
91
- id : recognizer ,
92
- active,
93
- startOptions : JSON . stringify ( startOptions ) ,
94
- lastActivity : null ,
95
- } )
96
- . exec ( ) ;
87
+ const newData = {
88
+ active,
89
+ startOptions,
90
+ lastActivity : null ,
91
+ } ;
92
+
93
+ const prevData = await this . getRecognizerData ( recognizer ) ;
94
+ if ( ! prevData ) {
95
+ this . database . createDocument ( newData , recognizer ) ;
96
+ return ;
97
+ }
98
+ this . database . updateDocument ( recognizer , newData ) ;
97
99
}
98
100
99
101
private async getRecognizerData (
100
102
recognizer : Resolution
101
103
) : Promise < { [ key : string ] : any } > {
102
- const instance = await this . db ( ) ;
103
- const rows = await instance
104
- . query ( "select" )
105
- . where ( [ "id" , "=" , recognizer ] )
106
- . exec ( ) ;
107
- if ( rows . length === 0 ) {
104
+ const recognizerData = this . database . getDocument ( recognizer ) ;
105
+ if ( ! recognizerData ) {
108
106
return null ;
109
107
}
110
- return rows [ 0 ] ;
111
- }
112
-
113
- private db ( tableName = this . tableName ) {
114
- return pluginDb . instance ( tableName ) ;
108
+ return recognizerData ;
115
109
}
116
110
}
117
111
0 commit comments