Skip to content

Commit

Permalink
feat(core): Add sessionStorage and coopreate decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Genuifx committed Jun 29, 2020
1 parent 284537d commit 5cf9961
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 9 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module.exports = {
'<rootDir>/packages/wxa-core/src/utils/deep-merge.js',
'<rootDir>/packages/wxa-core/src/polyfill/*',
'<rootDir>/packages/wxa-core/src/test/setup.js',
'<rootDir>/packages/wxa-core/dist/*',
'<rootDir>/packages/wxa-mobx/',
'<rootDir>/scripts/setupJest.js',
'<rootDir>/packages/wxa-validate/test/helpers/*',
'<rootDir>/packages/wxa-compiler-babel/src/fs-cache.js',
Expand Down
31 changes: 22 additions & 9 deletions packages/wxa-core/src/decorators/class.decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {eventbus} from '../utils/eventbus';
import {router} from '../utils/router';
import wxapi from '../utils/wxapi';
import {storage} from '../utils/storage';
import {sessionStorage} from '../utils/sessionStorage';
import debounce from 'lodash/debounce';
import throttle from 'lodash/throttle';
import * as helpers from '../utils/helpers';
Expand All @@ -15,6 +16,7 @@ const Eventbus = classFactory('$eventbus', eventbus);
const Router = classFactory('$router', router);
const Wxapi = classFactory('$wxapi', wxapi(wx));
const Storage = classFactory('$storage', storage);
const SessionStorage = classFactory('$sessionStorage', sessionStorage);
const Utils = classFactory('$utils', {
debounce,
promisify,
Expand All @@ -25,39 +27,50 @@ const Fetch = classFactory('$fetch', fetch);
const Mixins = (...args)=>classFactory('mixins', [mixin({mixins: args})]);

// Page and App level class Decorators.
const Page = (classDescriptor)=>{
const Page = (classDescriptor) => {
classDescriptor = Utils(classDescriptor);
classDescriptor = Storage(classDescriptor);
classDescriptor = SessionStorage(classDescriptor);
classDescriptor = Wxapi(classDescriptor);
classDescriptor = Router(classDescriptor);
classDescriptor = Eventbus(classDescriptor);
classDescriptor = Fetch(classDescriptor);

let {elements} = classDescriptor;
// let {elements} = classDescriptor;

// 兼容wxa1.0 还是挂载一个app对象到每个页面实例
return {
...classDescriptor,
elements,
};
return classDescriptor;
};

const Component = (classDescriptor) => {
classDescriptor = Utils(classDescriptor);
classDescriptor = Storage(classDescriptor);
classDescriptor = SessionStorage(classDescriptor);
classDescriptor = Wxapi(classDescriptor);
classDescriptor = Router(classDescriptor);
classDescriptor = Eventbus(classDescriptor);
classDescriptor = Fetch(classDescriptor);

return classDescriptor;
};

const App = (classDescriptor)=> {
const App = (classDescriptor) => {
classDescriptor = Utils(classDescriptor);
classDescriptor = Storage(classDescriptor);
classDescriptor = SessionStorage(classDescriptor);
classDescriptor = Wxapi(classDescriptor);
classDescriptor = Router(classDescriptor);
classDescriptor = Eventbus(classDescriptor);
classDescriptor = Fetch(classDescriptor);

// console.log(classDescriptor);
return classDescriptor;
};

export {
Page,
App,
Component,
Storage,
SessionStorage,
Wxapi,
Router,
Eventbus,
Expand Down
2 changes: 2 additions & 0 deletions packages/wxa-core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {wxa} from './wxa';
import {default as wxapiFn} from './utils/wxapi';
import {storage} from './utils/storage';
import {sessionStorage, SessionStorage} from './utils/sessionStorage';
import {router} from './utils/router';
import promisify from './utils/promisify';
import {eventbus, Eventbus} from './utils/eventbus';
Expand All @@ -20,6 +21,7 @@ export * from './decorators/index';
export const wxapi = wxapiFn(wx);
export {
storage,
sessionStorage, SessionStorage,
router,
promisify,
eventbus, Eventbus,
Expand Down
41 changes: 41 additions & 0 deletions packages/wxa-core/src/utils/sessionStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const Global = 'global';
export class SessionStorage {
constructor() {
let _store = new Map();
this._scope = new Map();
this._scope.set(Global, _store);
}

set(key, value, scope = Global) {
if (!this._scope.has(scope)) this._scope.set(scope, new Map());

let store = this._scope.get(scope);
store.set(key, value);
}

get(key, scope = Global) {
if (!this._scope.has(scope)) return null;

let store = this._scope.get(scope);
return store.get(key);
}

remove(key, scope = Global) {
if (!this._scope.has(scope)) return null;

let store = this._scope.get(scope);
store.delete(key);
}

clear(scope = Global) {
if (scope === '*') this._scope.clear();

if (!this._scope.has(scope)) return void(0);

let store = this._scope.get(scope);
store.clear();
}
}

export const sessionStorage = new SessionStorage();

46 changes: 46 additions & 0 deletions packages/wxa-core/test/sessionStorage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
SessionStorage,
} from '../src/utils/sessionStorage';

import 'jest-plugin-console-matchers/setup';

describe('Session Storage', () => {
let map = new Map([]);
let val = {type: 'navigate', value: 'pages/index'};
let key = 'APP_REDIRECT';

test('Basic Usage', ()=>{
let store = new SessionStorage();

store.set(key, val);
expect(store.get(key)).toBe(val);

store.set(key + key, map);
expect(store.get(key + key)).toBe(map);

store.remove(key);
expect(store.get(key)).toBeFalsy();

store.set(key, val);
store.clear();
expect(store.get(key)).toBeFalsy();
expect(store.get(key + key)).toBeFalsy();
});

test('with scope', () => {
let store = new SessionStorage();

store.set(key, val, 'app');

expect(store.get(key)).toBeFalsy();
expect(store.get(key, 'app')).toBe(val);

store.remove(key, 'app');
expect(store.get(key, 'app')).toBeFalsy();

expect(store._scope.get('app')).not.toBeFalsy();

store.clear('*');
expect(store._scope.get('app')).toBeFalsy();
});
});

0 comments on commit 5cf9961

Please sign in to comment.