From eb76861fadd0139a499d7e8bb7766c1d1884d866 Mon Sep 17 00:00:00 2001 From: eight Date: Sat, 1 Sep 2018 18:20:12 +0800 Subject: [PATCH] Add: use cacheFn --- edit/linter-csslint.js | 43 ++++++++++++++------------------ edit/linter-stylelint.js | 53 +++++++++++++++++----------------------- 2 files changed, 40 insertions(+), 56 deletions(-) diff --git a/edit/linter-csslint.js b/edit/linter-csslint.js index 95090640..e8a79b4d 100644 --- a/edit/linter-csslint.js +++ b/edit/linter-csslint.js @@ -1,4 +1,4 @@ -/* global linter editorWorker */ +/* global linter editorWorker cacheFn */ 'use strict'; var csslint = (() => { // eslint-disable-line @@ -47,7 +47,23 @@ var csslint = (() => { // eslint-disable-line 'zero-units': 0 }; let config; - let preparing; + + const prepareConfig = cacheFn(() => { + chrome.storage.onChanged.addListener((changes, area) => { + if (area !== 'sync' || !changes.hasOwnProperty('editorCSSLintConfig')) { + return; + } + getNewValue().then(linter.refresh); + }); + return getNewValue(); + + function getNewValue() { + return chromeSync.getLZValue('editorCSSLintConfig') + .then(newConfig => { + config = Object.assign({}, DEFAULT, newConfig); + }); + } + }); linter.register((text, options, cm) => { if (prefs.get('editor.linter') !== 'csslint' || cm.getOption('mode') !== 'css') { @@ -69,27 +85,4 @@ var csslint = (() => { // eslint-disable-line }); return {DEFAULT}; - - function prepareConfig() { - if (config) { - return Promise.resolve(); - } - if (!preparing) { - chrome.storage.onChanged.addListener((changes, area) => { - if (area !== 'sync' || !changes.editorCSSLintConfig) { - return; - } - getNewValue().then(newConfig => { - config = newConfig; - }); - }); - preparing = getNewValue(); - } - return preparing; - } - - function getNewValue() { - return chromeSync.getLZValue('editorCSSLintConfig') - .then(newConfig => Object.assign({}, DEFAULT, newConfig)); - } })(); diff --git a/edit/linter-stylelint.js b/edit/linter-stylelint.js index 1546515d..df3a86a4 100644 --- a/edit/linter-stylelint.js +++ b/edit/linter-stylelint.js @@ -1,4 +1,4 @@ -/* global linter editorWorker */ +/* global linter editorWorker cacheFn */ 'use strict'; var stylelint = (() => { // eslint-disable-line no-var @@ -6,7 +6,7 @@ var stylelint = (() => { // eslint-disable-line no-var const DEFAULT = { // 'sugarss' is a indent-based syntax like Sass or Stylus // ref: https://github.com/postcss/postcss#syntaxes - syntax: 'sugarss', + // syntax: 'sugarss', // ** recommended rules ** // ref: https://github.com/stylelint/stylelint-config-recommended/blob/master/index.js rules: { @@ -172,7 +172,26 @@ var stylelint = (() => { // eslint-disable-line no-var } }; let config; - let preparing; + + const prepareConfig = cacheFn(() => { + chrome.storage.onChanged.addListener((changes, area) => { + if (area !== 'sync' || !changes.hasOwnProperty('editorStylelintConfig')) { + return; + } + getNewValue().then(linter.refresh); + }); + return getNewValue(); + + function getNewValue() { + return chromeSync.getLZValue('editorStylelintConfig') + .then(newConfig => { + const output = {}; + output.rules = Object.assign({}, DEFAULT.rules, newConfig && newConfig.rules); + output.syntax = 'sugarss'; + config = output; + }); + } + }); linter.register((text, options, cm) => { if ( @@ -207,32 +226,4 @@ var stylelint = (() => { // eslint-disable-line no-var }); return {DEFAULT}; - - function prepareConfig() { - if (config) { - return Promise.resolve(); - } - if (!preparing) { - chrome.storage.onChanged.addListener((changes, area) => { - if (area !== 'sync' || !changes.editorStylelintConfig) { - return; - } - getNewValue().then(newConfig => { - config = newConfig; - }); - }); - preparing = getNewValue(); - } - return preparing; - } - - function getNewValue() { - return chromeSync.getLZValue('editorStylelintConfig') - .then(newConfig => { - const output = {}; - output.rules = Object.assign({}, DEFAULT.rules, newConfig && newConfig.rules); - output.syntax = 'sugarss'; - config = output; - }); - } })();