Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add HashPersist #68

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ try {
}
```

### Used in Hash
Basic Persist supports only url excluding hash. If you want to use hash, use the code below.
```js
import { HashPersist, registerHashPersist } from "@egjs/persist";

// Register globally to use HashPersist.
registerHashPersist();


// Use HashPersist
const persist = new HashPersist();

// set
persist.set("a", "A");

// get
persist.get("a");
```

### Used in SPA

Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ declare module "@egjs/persist" {
public remove(path: string): this;
}

export class HashPersist extends Persist {
}

export class PersistQuotaExceededError extends Error {
public name: string;
public storageType: "SessionStorage" | "LocalStorage" | "History" | "None";
public key: string;
public size: number;
}
export function releaseEvent(): void;
export function updateDepth(type?: number): void;
export function replaceDepth(): void;
export default Persist;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@egjs/persist",
"version": "2.6.0-snapshot",
"version": "2.7.0-beta.0",
"description": "Provide cache interface to handle persisted data among history navigation.",
"main": "dist/persist.js",
"module": "dist/persist.esm.js",
"es2015": "dist/persist.esm.js",
"types": "./index.d.ts",
"sideEffects": false,
"scripts": {
"start": "webpack-dev-server --open",
"start": "rollup -c -w",
"build": "rollup -c",
"build:webpack": "webpack --env production",
"test": "karma start",
Expand Down
61 changes: 61 additions & 0 deletions src/HashPersist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2015 NAVER Corp.
* egjs projects are licensed under the MIT license
*/
/* eslint-disable class-methods-use-this */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All files are missing the license notification, could you please add it?

/* eslint-disable no-use-before-define */
import {location} from "./browser";
import {CONST_HASH} from "./consts";
import Persist from "./Persist";

/**
* Get or store the current state of the web page using JSON according to hash.
* @ko 웹 페이지의 현재 상태를 hash에 따라 JSON 형식으로 저장하거나 읽는다.
* @memberof eg.Persist
* @alias eg.Persist.HashPersist
* @extends eg.Persist
*
* @support {"ie": "9+", "ch" : "latest", "ff" : "latest", "sf" : "latest" , "edge" : "latest", "ios" : "7+", "an" : "2.3+ (except 3.x)"}
*/
class HashPersist extends Persist {
/**
* Read value
* @param {String|Array} [path] - target path
* @return {String|Number|Boolean|Object|Array}
*/
get(path) {
return this._get(this._getKey(), this._getPath(path));
}
/**
* Save value
* @param {String|Array} path - target path
* @param {String|Number|Boolean|Object|Array} value - value to save
* @return {Persist}
*/
set(path, value) {
return this._set(this._getKey(), this._getPath(path), value);
}
/**
* Remove value
* @param {String|Array} [path] - target path
* @return {Persist}
*/
remove(path) {
return this._remove(this._getKey(), this._getPath(path));
}
_getKey() {
return `${CONST_HASH}${location.hash}`;
}
_getPath(path) {
let nextPath = path;

if (Array.isArray(nextPath)) {
nextPath = [this.key, ...nextPath];
} else {
nextPath = `${this.key}.${nextPath}`;
}
return nextPath;
}
}

export default HashPersist;
Loading