1
1
// MIT © 2017 azu
2
2
"use strict" ;
3
3
const debug = require ( "debug" ) ( "textlint-rule-proofdict" ) ;
4
- const { createLocalStorage } = require ( "localstorage-ponyfill" ) ;
5
4
const { RuleHelper } = require ( "textlint-rule-helper" ) ;
6
- import { createTester } from "./create-tester" ;
5
+ import { createTester , getDictionary } from "./create-tester" ;
7
6
import { fetchProofdict } from "./fetch-proofdict" ;
8
7
import { getDictJSONURL , getRuleURL } from "./proofdict-repo-util" ;
8
+ import { MODE } from "./mode" ;
9
+ import { storage } from "./dictionary-storage" ;
9
10
10
11
const DefaultOptions = {
11
12
// If you want to use live-proofdict
@@ -28,16 +29,11 @@ const DefaultOptions = {
28
29
"blacklistTags" : [ ] ,
29
30
// For testing
30
31
// set you proofdict json object
31
- "proofdict" : undefined
32
+ "proofdict" : undefined ,
33
+ // Disable cache for tester
34
+ "disableProofdictTesterCache" : false
32
35
} ;
33
36
34
- /**
35
- * @type {{LOCAL: string, NETWORK: string} }
36
- */
37
- const MODE = {
38
- LOCAL : "LOCAL" ,
39
- NETWORK : "NETWORK"
40
- } ;
41
37
const reporter = ( context , options = DefaultOptions ) => {
42
38
const helper = new RuleHelper ( context ) ;
43
39
const { Syntax, RuleError, report, getSource, fixer } = context ;
@@ -52,24 +48,28 @@ Please set dictURL or dictPath to .textlintrc.`))
52
48
const mode = options . dictURL ? MODE . NETWORK : MODE . LOCAL ;
53
49
const whitelistTags = Array . isArray ( options . whitelistTags ) ? options . whitelistTags : DefaultOptions . whitelistTags ;
54
50
const blacklistTags = Array . isArray ( options . blacklistTags ) ? options . blacklistTags : DefaultOptions . blacklistTags ;
51
+ const disableTesterCache = options . disableProofdictTesterCache !== undefined
52
+ ? options . disableProofdictTesterCache
53
+ : DefaultOptions . disableProofdictTesterCache ;
55
54
const autoUpdateInterval = options . autoUpdateInterval !== undefined
56
- ? options . autoUpdateInterval
57
- : DefaultOptions . autoUpdateInterval ;
58
- const localStorage = createLocalStorage ( ) ;
55
+ ? options . autoUpdateInterval
56
+ : DefaultOptions . autoUpdateInterval ;
59
57
const targetNodes = [ ] ;
60
58
const addQueue = node => targetNodes . push ( node ) ;
61
59
let promiseQueue = null ;
62
60
return {
63
61
[ Syntax . Document ] ( ) {
64
62
// default: 0
65
- const lastUpdated = Number ( localStorage . getItem ( "proofdict-lastUpdated" , "0" ) ) ;
66
- const isExpired = lastUpdated + autoUpdateInterval < Date . now ( ) ;
63
+ const lastUpdated = Number ( storage . getItem ( "proofdict-lastUpdated" , "-1" ) ) ;
64
+ const isExpired = lastUpdated <= 0
65
+ ? true
66
+ : Date . now ( ) - lastUpdated > autoUpdateInterval ;
67
67
if ( mode === MODE . NETWORK && isExpired ) {
68
68
const jsonAPIURL = getDictJSONURL ( options ) ;
69
69
promiseQueue = fetchProofdict ( { URL : jsonAPIURL } )
70
70
. then ( dictionary => {
71
- localStorage . setItem ( "proofdict" , JSON . stringify ( dictionary ) ) ;
72
- localStorage . setItem ( "proofdict-lastUpdated" , Date . now ( ) ) ;
71
+ storage . setItem ( "proofdict" , JSON . stringify ( dictionary ) ) ;
72
+ storage . setItem ( "proofdict-lastUpdated" , Date . now ( ) ) ;
73
73
} )
74
74
. catch ( error => {
75
75
debug ( "Fetch is failed" , error ) ;
@@ -84,34 +84,14 @@ Please set dictURL or dictPath to .textlintrc.`))
84
84
} ,
85
85
[ `${ Syntax . Document } :exit` ] ( ) {
86
86
return promiseQueue . then ( ( ) => {
87
- const getDict = ( options ) => {
88
- // prefer `dictionary` option
89
- if ( options . proofdict !== undefined ) {
90
- return options . proofdict ;
91
- }
92
- let proofDictData ;
93
- // NETWORK
94
- if ( options . dictURL ) {
95
- try {
96
- const cachedProofdict = localStorage . getItem ( "proofdict" ) ;
97
- proofDictData = JSON . parse ( cachedProofdict ) ;
98
- } catch ( error ) {
99
- localStorage . removeItem ( "proofdict" ) ;
100
- }
101
- }
102
- // LOCAL
103
- if ( options . dictPath ) {
104
- // TODO: not implemented
105
- }
106
- return proofDictData ;
107
- } ;
108
- const dictionary = getDict ( options ) ;
109
- const lastUpdated = Number ( localStorage . getItem ( "proofdict-lastUpdated" , "0" ) ) ;
87
+ const dictionary = getDictionary ( options , mode ) ;
88
+ const lastUpdated = Number ( storage . getItem ( "proofdict-lastUpdated" , "0" ) ) ;
110
89
const tester = createTester ( {
111
90
dictionary,
112
91
lastUpdated,
113
92
whitelistTags,
114
- blacklistTags
93
+ blacklistTags,
94
+ disableTesterCache
115
95
} ) ;
116
96
// check
117
97
const promises = targetNodes . map ( node => {
0 commit comments