Skip to content
This repository was archived by the owner on Mar 23, 2018. It is now read-only.

Commit 0ce4bed

Browse files
committed
fix(rule): fix cache mechanism
1 parent ce0aa4e commit 0ce4bed

9 files changed

+135
-61
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"dependencies": {
3838
"@types/url-join": "^0.8.2",
3939
"debug": "^3.1.0",
40-
"fetch-ponyfill": "^5.0.1",
40+
"fetch-ponyfill": "^6.0.0",
4141
"localstorage-ponyfill": "^1.0.1",
4242
"proofdict": "^1.2.1",
4343
"proofdict-tester": "^1.0.0",

src/create-tester.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// MIT © 2017 azu
22
"use strict";
3+
import { MODE } from "./mode";
4+
import { storage } from "./dictionary-storage";
5+
36
const { ProofdictTester } = require("proofdict-tester");
47
let currentTester = null;
58
let checkedLastTime = -1;
@@ -8,10 +11,11 @@ let checkedLastTime = -1;
811
* @param {*} dictionary
912
* @param {string[]} whitelistTags
1013
* @param {string[]} blacklistTags
14+
* @param {boolean} disableTesterCache
1115
* @returns {ProofdictTester}
1216
*/
13-
export const createTester = ({ lastUpdated, dictionary, whitelistTags, blacklistTags }) => {
14-
if (currentTester === null && checkedLastTime < lastUpdated) {
17+
export const createTester = ({ lastUpdated, dictionary, whitelistTags, blacklistTags, disableTesterCache }) => {
18+
if (disableTesterCache || (currentTester === null && checkedLastTime < lastUpdated)) {
1519
checkedLastTime = lastUpdated;
1620
currentTester = new ProofdictTester({
1721
dictionary,
@@ -22,3 +26,31 @@ export const createTester = ({ lastUpdated, dictionary, whitelistTags, blacklist
2226
}
2327
return currentTester;
2428
};
29+
30+
31+
/**
32+
* @param options
33+
* @param {string} mode
34+
* @returns {*}
35+
*/
36+
export const getDictionary = (options, mode) => {
37+
// prefer `dictionary` option
38+
if (options.proofdict !== undefined) {
39+
return options.proofdict;
40+
}
41+
let proofDictData;
42+
// NETWORK
43+
if (mode === MODE.NETWORK) {
44+
try {
45+
const cachedProofdict = storage.getItem("proofdict");
46+
proofDictData = JSON.parse(cachedProofdict);
47+
} catch (error) {
48+
storage.removeItem("proofdict");
49+
}
50+
}
51+
// LOCAL
52+
if (mode === MODE.LOCAL) {
53+
// TODO: not implemented
54+
}
55+
return proofDictData;
56+
};

src/dictionary-storage.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// MIT © 2018 azu
2+
"use strict";
3+
const { createLocalStorage } = require("localstorage-ponyfill");
4+
5+
6+
class Storage {
7+
constructor() {
8+
this.localStorage = createLocalStorage();
9+
}
10+
11+
getItem(name, defaultValue) {
12+
return this.localStorage.getItem(name, defaultValue);
13+
}
14+
15+
setItem(name, value) {
16+
return this.localStorage.setItem(name, value);
17+
};
18+
19+
removeItem(name) {
20+
return this.localStorage.removeItem(name);
21+
}
22+
}
23+
24+
export const storage = new Storage();

src/mode.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// MIT © 2018 azu
2+
"use strict";
3+
/**
4+
* @type {{LOCAL: string, NETWORK: string}}
5+
*/
6+
export const MODE = {
7+
LOCAL: "LOCAL",
8+
NETWORK: "NETWORK"
9+
};

src/proofdict-repo-util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function getDictJSONURL(options) {
1010
if (typeof options.dictURL === "object" && typeof options.dictURL.jsonAPI === "string") {
1111
return options.dictURL.jsonAPI;
1212
}
13-
return urlJoin(options.dictURL, "dict.json");
13+
return urlJoin(options.dictURL, "dictionary.json");
1414
}
1515

1616
/**

src/textlint-rule-proofdict.js

+21-41
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// MIT © 2017 azu
22
"use strict";
33
const debug = require("debug")("textlint-rule-proofdict");
4-
const { createLocalStorage } = require("localstorage-ponyfill");
54
const { RuleHelper } = require("textlint-rule-helper");
6-
import { createTester } from "./create-tester";
5+
import { createTester, getDictionary } from "./create-tester";
76
import { fetchProofdict } from "./fetch-proofdict";
87
import { getDictJSONURL, getRuleURL } from "./proofdict-repo-util";
8+
import { MODE } from "./mode";
9+
import { storage } from "./dictionary-storage";
910

1011
const DefaultOptions = {
1112
// If you want to use live-proofdict
@@ -28,16 +29,11 @@ const DefaultOptions = {
2829
"blacklistTags": [],
2930
// For testing
3031
// set you proofdict json object
31-
"proofdict": undefined
32+
"proofdict": undefined,
33+
// Disable cache for tester
34+
"disableProofdictTesterCache": false
3235
};
3336

34-
/**
35-
* @type {{LOCAL: string, NETWORK: string}}
36-
*/
37-
const MODE = {
38-
LOCAL: "LOCAL",
39-
NETWORK: "NETWORK"
40-
};
4137
const reporter = (context, options = DefaultOptions) => {
4238
const helper = new RuleHelper(context);
4339
const { Syntax, RuleError, report, getSource, fixer } = context;
@@ -52,24 +48,28 @@ Please set dictURL or dictPath to .textlintrc.`))
5248
const mode = options.dictURL ? MODE.NETWORK : MODE.LOCAL;
5349
const whitelistTags = Array.isArray(options.whitelistTags) ? options.whitelistTags : DefaultOptions.whitelistTags;
5450
const blacklistTags = Array.isArray(options.blacklistTags) ? options.blacklistTags : DefaultOptions.blacklistTags;
51+
const disableTesterCache = options.disableProofdictTesterCache !== undefined
52+
? options.disableProofdictTesterCache
53+
: DefaultOptions.disableProofdictTesterCache;
5554
const autoUpdateInterval = options.autoUpdateInterval !== undefined
56-
? options.autoUpdateInterval
57-
: DefaultOptions.autoUpdateInterval;
58-
const localStorage = createLocalStorage();
55+
? options.autoUpdateInterval
56+
: DefaultOptions.autoUpdateInterval;
5957
const targetNodes = [];
6058
const addQueue = node => targetNodes.push(node);
6159
let promiseQueue = null;
6260
return {
6361
[Syntax.Document]() {
6462
// 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;
6767
if (mode === MODE.NETWORK && isExpired) {
6868
const jsonAPIURL = getDictJSONURL(options);
6969
promiseQueue = fetchProofdict({ URL: jsonAPIURL })
7070
.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());
7373
})
7474
.catch(error => {
7575
debug("Fetch is failed", error);
@@ -84,34 +84,14 @@ Please set dictURL or dictPath to .textlintrc.`))
8484
},
8585
[`${Syntax.Document}:exit`]() {
8686
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"));
11089
const tester = createTester({
11190
dictionary,
11291
lastUpdated,
11392
whitelistTags,
114-
blacklistTags
93+
blacklistTags,
94+
disableTesterCache
11595
});
11696
// check
11797
const promises = targetNodes.map(node => {

test/fixtures/proofdict.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
"noun"
2525
]
2626
}
27-
]
27+
]

test/textlint-rule-proofdict-test.js

+41-12
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,35 @@ const TextLintTester = require("textlint-tester");
22
const tester = new TextLintTester();
33
// rule
44
import rule from "../src/textlint-rule-proofdict";
5+
// IN testing , disable tester cache
6+
const disableProofdictTesterCache = true;
7+
const defaultOptions = {
8+
disableProofdictTesterCache,
9+
proofdict: require("./fixtures/proofdict.json")
10+
};
511
// ruleName, rule, { valid, invalid }
612
tester.run("proofdict", rule, {
713
valid: [
814
{
915
text: "jQuery",
10-
options: {
11-
proofdict: require("./fixtures/proofdict.json")
12-
}
16+
options: defaultOptions
1317
},
1418
{
1519
text: "WebKit",
16-
options: {
17-
proofdict: require("./fixtures/proofdict.json")
18-
}
20+
options: defaultOptions
1921
},
2022
{
2123
text: "WebKit",
22-
options: {
23-
proofdict: require("./fixtures/proofdict.json")
24-
}
24+
options: defaultOptions
2525
},
2626
],
2727
invalid: [
2828
{
2929
text: "texlint check your texts.\n" + "jquery is libray.\n",
3030
output: "texlint check your texts.\n" + "jQuery is libray.\n",
3131
options: {
32-
dictURL: "https://proofdict.github.io/proof-dictionary/"
32+
dictURL: "https://proofdict.github.io/proof-dictionary/",
33+
disableProofdictTesterCache
3334
},
3435
errors: [
3536
{
@@ -44,7 +45,8 @@ tester.run("proofdict", rule, {
4445
text: "jquery",
4546
output: "jQuery",
4647
options: {
47-
dictURL: "https://proofdict.github.io/proof-dictionary/"
48+
dictURL: "https://proofdict.github.io/proof-dictionary/",
49+
disableProofdictTesterCache
4850
},
4951
errors: [
5052
{
@@ -59,7 +61,34 @@ tester.run("proofdict", rule, {
5961
text: "This is webkit.",
6062
output: "This is WebKit.",
6163
options: {
62-
proofdict: require("./fixtures/proofdict.json")
64+
proofdict: [
65+
{
66+
"id": "01BQ92YZ6QR8RJKA5Y8W2F9NMY",
67+
"description": "Reference https://webkit.org/",
68+
"expected": "WebKit",
69+
"patterns": [
70+
"/webkit/i"
71+
],
72+
"specs": [
73+
{
74+
"from": "これはwebkitです",
75+
"to": "これはWebKitです"
76+
},
77+
{
78+
"from": "XXXwebkit",
79+
"to": "XXXwebkit"
80+
},
81+
{
82+
"from": "node-webkit",
83+
"to": "node-webkit"
84+
}
85+
],
86+
"tags": [
87+
"noun"
88+
]
89+
}
90+
],
91+
disableProofdictTesterCache
6392
},
6493
errors: [
6594
{

yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -1139,9 +1139,9 @@ fetch-ponyfill@^4.1.0:
11391139
dependencies:
11401140
node-fetch "~1.7.1"
11411141

1142-
fetch-ponyfill@^5.0.1:
1143-
version "5.0.1"
1144-
resolved "https://registry.yarnpkg.com/fetch-ponyfill/-/fetch-ponyfill-5.0.1.tgz#2789ec179c1de357f60f9fd49e154a8b3fd8cd1b"
1142+
fetch-ponyfill@^6.0.0:
1143+
version "6.0.0"
1144+
resolved "https://registry.yarnpkg.com/fetch-ponyfill/-/fetch-ponyfill-6.0.0.tgz#1809503d4e3f966920708bb296114e1b69c0c477"
11451145
dependencies:
11461146
node-fetch "~2.0.0"
11471147

0 commit comments

Comments
 (0)