Skip to content

Commit

Permalink
Merge pull request #3996 from SukkaW/utilize-hexo-util-cache
Browse files Browse the repository at this point in the history
refactor: utilize hexo-util
  • Loading branch information
SukkaW committed Dec 21, 2019
2 parents a4a3f82 + aed8240 commit 3d80619
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
21 changes: 9 additions & 12 deletions lib/hexo/locals.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
'use strict';

const { Cache } = require('hexo-util');

class Locals {
constructor() {
this.cache = {};
this.cache = new Cache();
this.getters = {};
}

get(name) {
if (typeof name !== 'string') throw new TypeError('name must be a string!');

let cache = this.cache[name];

if (cache == null) {
return this.cache.apply(name, () => {
const getter = this.getters[name];
if (!getter) return;

cache = getter();
this.cache[name] = cache;
}

return cache;
return getter();
});
}

set(name, value) {
Expand All @@ -29,7 +26,7 @@ class Locals {
const getter = typeof value === 'function' ? value : () => value;

this.getters[name] = getter;
this.cache[name] = null;
this.cache.del(name);

return this;
}
Expand All @@ -38,13 +35,13 @@ class Locals {
if (typeof name !== 'string') throw new TypeError('name must be a string!');

this.getters[name] = null;
this.cache[name] = null;
this.cache.del(name);

return this;
}

invalidate() {
this.cache = {};
this.cache.flush();

return this;
}
Expand Down
28 changes: 2 additions & 26 deletions lib/plugins/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const Promise = require('bluebird');
const prettyHrtime = require('pretty-hrtime');
const { cyan, magenta } = require('chalk');
const tildify = require('tildify');
const { Transform, PassThrough } = require('stream');
const { HashStream } = require('hexo-util');
const { PassThrough } = require('stream');
const { CacheStream, HashStream } = require('hexo-util');

function generateConsole(args = {}) {
const force = args.f || args.force;
Expand Down Expand Up @@ -178,28 +178,4 @@ function pipeStream(...args) {
});
}

function CacheStream() {
Reflect.apply(Transform, this, []);

this._cache = [];
}

require('util').inherits(CacheStream, Transform);

CacheStream.prototype._transform = function(chunk, enc, callback) {
const buf = chunk instanceof Buffer ? chunk : Buffer.from(chunk, enc);

this._cache.push(buf);
this.push(buf);
callback();
};

CacheStream.prototype.destroy = function() {
this._cache.length = 0;
};

CacheStream.prototype.getCache = function() {
return Buffer.concat(this._cache);
};

module.exports = generateConsole;
8 changes: 4 additions & 4 deletions test/scripts/hexo/locals.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe('Locals', () => {
locals.set('foo', () => 'foo');

// cache should be clear after new data is set
should.not.exist(locals.cache.foo);
locals.cache.has('foo').should.eql(false);
locals.get('foo').should.eql('foo');
// cache should be saved once it's get
locals.cache.foo.should.eql('foo');
locals.cache.get('foo').should.eql('foo');
});

it('set() - not function', () => {
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('Locals', () => {
locals.remove('foo');

should.not.exist(locals.getters.foo);
should.not.exist(locals.cache.foo);
locals.cache.has('foo').should.eql(false);
});

it('remove() - name must be a string', () => {
Expand Down Expand Up @@ -100,6 +100,6 @@ describe('Locals', () => {
locals.get('foo');
locals.invalidate();

should.not.exist(locals.cache.foo);
locals.cache.has('foo').should.eql(false);
});
});

0 comments on commit 3d80619

Please sign in to comment.