@@ -6,25 +6,24 @@ import "./../JS/PinyinPro.mjs" as Pinyin
66QtObject {
77 property list< QtObject> children
88 readonly property var context: Pinyin
9- property int patternValue: 500 // In "size" mode, number of patterns per build; in "group" mode, number of groups to divide patterns into
109 property string patternBuildMode: " size" // size, group
11- property alias patternBuildInterval: patternTimer .interval
10+ property int patternValue: 500 // In "size" mode, number of patterns per build; in "group" mode, number of groups to divide patterns into
11+ property int patternBuildInterval: 1500
1212 readonly property alias isPatternBuilt: d .isPatternBuilt
1313 readonly property var outputFormat: Pinyin .OutputFormat
1414 function buildPattern (buildAllAtOnce = true ) {
15- if (d .isPatternBuilt || patternTimer .running ) {
15+ const builder = context .getPatternsNormalBuilder (patternValue,
16+ patternBuildMode)
17+ if (! builder || d .isPatternBuilt || (d .patternTimer && d .patternTimer .running )) {
1618 return
1719 }
18- d .builder = context .getPatternsNormalBuilder (patternValue,
19- patternBuildMode)
2020 if (buildAllAtOnce) {
21- while (! d . buildNext () ) {
21+ while (! builder . next (). done ) {
2222
2323 }
24- d .builder = null
2524 d .isPatternBuilt = true
2625 } else {
27- patternTimer . start ( )
26+ d . patternTimer = d . stepRunner (builder, () => d . isPatternBuilt = true , patternBuildInterval )
2827 }
2928 }
3029 function addDict (dict , options ) {
@@ -54,26 +53,54 @@ QtObject {
5453 function segment (word , options ) {
5554 return context .segment (word, options)
5655 }
56+ function createCache (data , options , buildCache = true ) {
57+ return context .createCache (data, options, buildCache)
58+ }
59+ function rebuildCache (cache , data , asyncArg ) {
60+ cache .data = data
61+ const arg = Object .assign ({
62+ " async" : true ,
63+ " chunkSize" : 50 ,
64+ " interval" : 500 ,
65+ " triggeredOnStart" : true
66+ }, asyncArg || {})
67+ if (arg .async ) {
68+ const timer = d .stepRunner (cache .buildGenerator (arg .chunkSize , true , arg .interval , arg .triggeredOnStart ))
69+ if (cache .data .length > arg .chunkSize ) {
70+ cache .generatorDeleter = timer .destroy
71+ }
72+ } else {
73+ cache .build (true )
74+ }
75+ }
76+ function findMatches (cache , pinyin ) {
77+ return context .findMatches (cache, pinyin)
78+ }
5779 children: [
5880 QtObject {
5981 id: d
82+ property Timer patternTimer: null
6083 property bool isPatternBuilt: false
61- property var builder: null
62- function buildNext () {
63- return d .builder .next ().done
64- }
65- },
66- Timer {
67- id: patternTimer
68- interval: 1500
69- repeat: true
70- triggeredOnStart: true
71- onTriggered: {
72- if (d .buildNext ()) {
73- d .builder = null
74- d .isPatternBuilt = true
75- stop ()
84+ function stepRunner (generator , doneCallback , interval = 500 , triggeredOnStart = true ) {
85+ const timer = Qt .createQmlObject (" import QtQuick 2.15; Timer {}" , Qt .application )
86+ timer .interval = interval
87+ timer .triggered .connect (function () {
88+ const result = generator .next ()
89+ if (result .done ) {
90+ timer .destroy ()
91+ if (typeof doneCallback === " function" ) {
92+ doneCallback ()
93+ }
94+ } else {
95+ timer .start ()
96+ }
97+ })
98+ timer .Component .onDestruction .connect (() => generator = null )
99+ if (triggeredOnStart) {
100+ generator .next ()
76101 }
102+ timer .start ()
103+ return timer
77104 }
78105 }
79106 ]
0 commit comments