literium-runner
Advanced tools
Comparing version 0.2.1 to 0.2.2
import { JsonType } from 'literium-json'; | ||
export interface StoreData<Type> { | ||
$?: boolean; | ||
_?: Type; | ||
export declare const enum StoreType { | ||
Default = 0, | ||
Session = 1, | ||
Persist = 2 | ||
} | ||
export interface StoreCell<Type> { | ||
$: string; | ||
_: Type; | ||
_?: Type; | ||
d: Type; | ||
j: JsonType<Type>; | ||
t: StoreType; | ||
} | ||
export declare function initStore<Type, JType extends Type>(name: string, json: JsonType<JType>, def: Type): StoreCell<Type>; | ||
export declare function storeType<Type>(store: StoreCell<Type>): StoreType; | ||
export declare function loadStore<Type>(store: StoreCell<Type>): Type; | ||
export declare function saveStore<Type>(store: StoreCell<Type>, data?: Type, persist?: boolean): void; | ||
export declare function moveStore<Type>(store: StoreCell<Type>, t: StoreType): void; | ||
export declare function saveStore<Type>(store: StoreCell<Type>, data?: Type): void; |
@@ -5,33 +5,44 @@ import { dummy, is_ok, un_ok } from 'literium'; | ||
export function initStore(name, json, def) { | ||
return { $: name, _: def, j: json }; | ||
var store = { $: name, d: def, j: json, t: 0 /* Default */ }; | ||
_load(store); | ||
return store; | ||
} | ||
export function storeType(store) { | ||
return store.t; | ||
} | ||
export function loadStore(store) { | ||
var cell = _load(store.$, store.j); | ||
return cell ? cell._ : store._; | ||
return store._ || store.d; | ||
} | ||
export function saveStore(store, data, persist) { | ||
var cell = _load(store.$, store.j) || { _: store._ }; | ||
if (persist != undefined) | ||
cell.$ = persist; | ||
cell._ = data; | ||
_save(store.$, store.j, cell); | ||
export function moveStore(store, t) { | ||
if (store.t != t) { | ||
store.t = t; | ||
_save(store); | ||
} | ||
} | ||
function load_(name, json) { | ||
var raw1 = sessionStorage.getItem(name); | ||
var raw2 = localStorage.getItem(name); | ||
export function saveStore(store, data) { | ||
store._ = data; | ||
_save(store); | ||
} | ||
function load_(s) { | ||
var raw1 = sessionStorage.getItem(s.$); | ||
var raw2 = localStorage.getItem(s.$); | ||
var raw = raw1 || raw2; | ||
if (!raw) | ||
return; | ||
var persist = !raw1 && !!raw2; | ||
var res = parse(json, raw); | ||
return is_ok(res) ? { $: persist, _: un_ok(res) } : undefined; | ||
if (raw) { | ||
s.t = !raw1 && !!raw2 ? 2 /* Persist */ : 1 /* Session */; | ||
var res = parse(s.j, raw); | ||
if (is_ok(res)) { | ||
s._ = un_ok(res); | ||
return; | ||
} | ||
} | ||
s.t = 0 /* Default */; | ||
s._ = s.d; | ||
} | ||
function save_(name, json, _a) { | ||
var persist = _a.$, data = _a._; | ||
sessionStorage.removeItem(name); | ||
localStorage.removeItem(name); | ||
if (persist != undefined && data != undefined) { | ||
var res = build(json, data); | ||
function save_(s) { | ||
sessionStorage.removeItem(s.$); | ||
localStorage.removeItem(s.$); | ||
if (s.t != 0 /* Default */ && s._ != undefined) { | ||
var res = build(s.j, s._); | ||
if (is_ok(res)) | ||
(persist ? localStorage : sessionStorage).setItem(name, un_ok(res)); | ||
(s.t == 2 /* Persist */ ? localStorage : sessionStorage).setItem(name, un_ok(res)); | ||
} | ||
@@ -38,0 +49,0 @@ } |
{ | ||
"name": "literium-runner", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Runner module for Literium web-framework.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
import { dummy, is_ok, un_ok } from 'literium'; | ||
import { JsonType, parse, build } from 'literium-json'; | ||
export interface StoreData<Type> { | ||
$?: boolean; // data is persistent | ||
_?: Type; // actual data value | ||
export const enum StoreType { | ||
Default, // not saved default value | ||
Session, // saved into the session storage | ||
Persist, // saved into the local storage | ||
} | ||
@@ -11,43 +12,61 @@ | ||
$: string; // data cell name | ||
_: Type; // default value | ||
_?: Type; // current value | ||
d: Type; // default value | ||
j: JsonType<Type>; // validator | ||
t: StoreType; // holder storage | ||
} | ||
const [_load, _save]: [ | ||
<Type>(name: string, json: JsonType<Type>) => StoreData<Type> | void, | ||
<Type>(name: string, json: JsonType<Type>, data: StoreData<Type>) => void | ||
<Type>(store: StoreCell<Type>) => void, | ||
<Type>(store: StoreCell<Type>) => void | ||
] = check() ? [load_, save_] : [dummy, dummy]; | ||
export function initStore<Type, JType extends Type>(name: string, json: JsonType<JType>, def: Type): StoreCell<Type> { | ||
return { $: name, _: def, j: json }; | ||
const store = { $: name, d: def, j: json, t: StoreType.Default }; | ||
_load(store); | ||
return store; | ||
} | ||
export function storeType<Type>(store: StoreCell<Type>): StoreType { | ||
return store.t; | ||
} | ||
export function loadStore<Type>(store: StoreCell<Type>): Type { | ||
const cell = _load(store.$, store.j); | ||
return cell ? cell._ as Type : store._; | ||
return store._ || store.d; | ||
} | ||
export function saveStore<Type>(store: StoreCell<Type>, data?: Type, persist?: boolean) { | ||
const cell = _load(store.$, store.j) || { _: store._ }; | ||
if (persist != undefined) cell.$ = persist; | ||
cell._ = data; | ||
_save(store.$, store.j, cell); | ||
export function moveStore<Type>(store: StoreCell<Type>, t: StoreType) { | ||
if (store.t != t) { | ||
store.t = t; | ||
_save(store); | ||
} | ||
} | ||
function load_<Type>(name: string, json: JsonType<Type>): StoreData<Type> | void { | ||
const raw1 = sessionStorage.getItem(name); | ||
const raw2 = localStorage.getItem(name); | ||
export function saveStore<Type>(store: StoreCell<Type>, data?: Type) { | ||
store._ = data; | ||
_save(store); | ||
} | ||
function load_<Type>(s: StoreCell<Type>) { | ||
const raw1 = sessionStorage.getItem(s.$); | ||
const raw2 = localStorage.getItem(s.$); | ||
const raw = raw1 || raw2; | ||
if (!raw) return; | ||
const persist = !raw1 && !!raw2; | ||
const res = parse(json, raw); | ||
return is_ok(res) ? { $: persist, _: un_ok(res) } : undefined; | ||
if (raw) { | ||
s.t = !raw1 && !!raw2 ? StoreType.Persist : StoreType.Session; | ||
const res = parse(s.j, raw); | ||
if (is_ok(res)) { | ||
s._ = un_ok(res); | ||
return; | ||
} | ||
} | ||
s.t = StoreType.Default; | ||
s._ = s.d; | ||
} | ||
function save_<Type>(name: string, json: JsonType<Type>, { $: persist, _: data }: StoreData<Type>) { | ||
sessionStorage.removeItem(name); | ||
localStorage.removeItem(name); | ||
if (persist != undefined && data != undefined) { | ||
const res = build(json, data); | ||
if (is_ok(res)) (persist ? localStorage : sessionStorage).setItem(name, un_ok(res)); | ||
function save_<Type>(s: StoreCell<Type>) { | ||
sessionStorage.removeItem(s.$); | ||
localStorage.removeItem(s.$); | ||
if (s.t != StoreType.Default && s._ != undefined) { | ||
const res = build(s.j, s._); | ||
if (is_ok(res)) (s.t == StoreType.Persist ? localStorage : sessionStorage).setItem(name, un_ok(res)); | ||
} | ||
@@ -54,0 +73,0 @@ } |
import { JsonType } from 'literium-json'; | ||
export interface StoreData<Type> { | ||
$?: boolean; | ||
_?: Type; | ||
export declare const enum StoreType { | ||
Default = 0, | ||
Session = 1, | ||
Persist = 2 | ||
} | ||
export interface StoreCell<Type> { | ||
$: string; | ||
_: Type; | ||
_?: Type; | ||
d: Type; | ||
j: JsonType<Type>; | ||
t: StoreType; | ||
} | ||
export declare function initStore<Type, JType extends Type>(name: string, json: JsonType<JType>, def: Type): StoreCell<Type>; | ||
export declare function storeType<Type>(store: StoreCell<Type>): StoreType; | ||
export declare function loadStore<Type>(store: StoreCell<Type>): Type; | ||
export declare function saveStore<Type>(store: StoreCell<Type>, data?: Type, persist?: boolean): void; | ||
export declare function moveStore<Type>(store: StoreCell<Type>, t: StoreType): void; | ||
export declare function saveStore<Type>(store: StoreCell<Type>, data?: Type): void; |
61
store.js
@@ -7,36 +7,49 @@ "use strict"; | ||
function initStore(name, json, def) { | ||
return { $: name, _: def, j: json }; | ||
var store = { $: name, d: def, j: json, t: 0 /* Default */ }; | ||
_load(store); | ||
return store; | ||
} | ||
exports.initStore = initStore; | ||
function storeType(store) { | ||
return store.t; | ||
} | ||
exports.storeType = storeType; | ||
function loadStore(store) { | ||
var cell = _load(store.$, store.j); | ||
return cell ? cell._ : store._; | ||
return store._ || store.d; | ||
} | ||
exports.loadStore = loadStore; | ||
function saveStore(store, data, persist) { | ||
var cell = _load(store.$, store.j) || { _: store._ }; | ||
if (persist != undefined) | ||
cell.$ = persist; | ||
cell._ = data; | ||
_save(store.$, store.j, cell); | ||
function moveStore(store, t) { | ||
if (store.t != t) { | ||
store.t = t; | ||
_save(store); | ||
} | ||
} | ||
exports.moveStore = moveStore; | ||
function saveStore(store, data) { | ||
store._ = data; | ||
_save(store); | ||
} | ||
exports.saveStore = saveStore; | ||
function load_(name, json) { | ||
var raw1 = sessionStorage.getItem(name); | ||
var raw2 = localStorage.getItem(name); | ||
function load_(s) { | ||
var raw1 = sessionStorage.getItem(s.$); | ||
var raw2 = localStorage.getItem(s.$); | ||
var raw = raw1 || raw2; | ||
if (!raw) | ||
return; | ||
var persist = !raw1 && !!raw2; | ||
var res = literium_json_1.parse(json, raw); | ||
return literium_1.is_ok(res) ? { $: persist, _: literium_1.un_ok(res) } : undefined; | ||
if (raw) { | ||
s.t = !raw1 && !!raw2 ? 2 /* Persist */ : 1 /* Session */; | ||
var res = literium_json_1.parse(s.j, raw); | ||
if (literium_1.is_ok(res)) { | ||
s._ = literium_1.un_ok(res); | ||
return; | ||
} | ||
} | ||
s.t = 0 /* Default */; | ||
s._ = s.d; | ||
} | ||
function save_(name, json, _a) { | ||
var persist = _a.$, data = _a._; | ||
sessionStorage.removeItem(name); | ||
localStorage.removeItem(name); | ||
if (persist != undefined && data != undefined) { | ||
var res = literium_json_1.build(json, data); | ||
function save_(s) { | ||
sessionStorage.removeItem(s.$); | ||
localStorage.removeItem(s.$); | ||
if (s.t != 0 /* Default */ && s._ != undefined) { | ||
var res = literium_json_1.build(s.j, s._); | ||
if (literium_1.is_ok(res)) | ||
(persist ? localStorage : sessionStorage).setItem(name, literium_1.un_ok(res)); | ||
(s.t == 2 /* Persist */ ? localStorage : sessionStorage).setItem(name, literium_1.un_ok(res)); | ||
} | ||
@@ -43,0 +56,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74703
1307