Comparing version 0.4.0-beta.1 to 0.4.0
118
index.d.ts
@@ -20,95 +20,43 @@ declare global { | ||
new (): { | ||
event: { | ||
[key: string]: ArkflowsEvent; | ||
[x: string]: any; | ||
event: Record<string, ArkflowsEvent>; | ||
store: Record<string, any>; | ||
withDevtools: boolean | undefined; | ||
middleware: Function[]; | ||
create<T = string | number | object | {}>(name: string, initStore: T): T; | ||
get<T_1 extends string>(name: T_1): any; | ||
set<T_2>(name: string, value: T_2): T_2; | ||
update<T_3>(name: string, value: T_3): T_3; | ||
subscribe<T_4 = string | number | object>(name: string | string[], callback: (value: T_4, name: string, model: any) => any): { | ||
unsubscribe: () => void; | ||
}; | ||
store: { | ||
[key: string]: any; | ||
}; | ||
list(): string[]; | ||
model(): { | ||
name: string; | ||
store: any; | ||
}[]; | ||
applyMiddleware(...callbacks: ((store: any, process: ArkflowsProcess, name: string) => any)[]): void; | ||
useMiddleware(store: any, process: ArkflowsProcess, name: string): any; | ||
enableDevtools(option?: boolean): void; | ||
}; | ||
[x: string]: any; | ||
} | { | ||
new (): { | ||
event: Record<string, ArkflowsEvent>; | ||
store: Record<string, any>; | ||
withDevtools: boolean | undefined; | ||
middleware: Function[]; | ||
withDevtools: boolean | undefined; | ||
/** | ||
* Create store. | ||
* Create a collection of storage which can be used in various collection | ||
* @param {string} name - Store Name | ||
* @param initStore - Initial storage value | ||
* @returns {object} Storage value | ||
*/ | ||
create<T = any>(name: string, initStore?: {} | T): any; | ||
/** | ||
* Get existing store value with given store name. | ||
* @param {string} name - Store Name | ||
* @returns {object} Storage value | ||
*/ | ||
get(name: string): any; | ||
/** | ||
* Set store. | ||
* Overwrite a storage. Store's value will be overwriten. | ||
* @param {string} name - Store Name | ||
* @param {object} value - Value to change or update | ||
* @returns {object} Storage value | ||
*/ | ||
set<T_1 = Object>(name: string, value: T_1): any; | ||
/** | ||
* Update store. | ||
* Mutate storage data, doesn't overwrite existed value if new value is not provided. | ||
* @param {string} name - Store Name | ||
* @param value - Value to change or update | ||
* @returns Storage value | ||
*/ | ||
update<T_2 = any>(name: string, value: T_2): any; | ||
/** | ||
* | ||
* Middleware callback | ||
* @callback MiddlewareCallback | ||
* @param store - Collection of existing store. | ||
* @param process - Process name which flow through middleware. | ||
*/ | ||
/** | ||
* Subscribe store. | ||
* Listen to a storage update, invoke callback when update. | ||
* @param {string|string[]} name - Store Name | ||
* @param {object} initStore - Initial storage value | ||
*/ | ||
subscribe<T_3 = Object>(name: string | string[], callback: (value: T_3, name: string, model: any) => any): { | ||
create<T_5 = string | number | object | {}>(name: string, initStore: T_5): T_5; | ||
get<T_6 extends string>(name: T_6): any; | ||
set<T_7>(name: string, value: T_7): T_7; | ||
update<T_8>(name: string, value: T_8): T_8; | ||
subscribe<T_9 = string | number | object>(name: string | string[], callback: (value: T_9, name: string, model: any) => any): { | ||
unsubscribe: () => void; | ||
}; | ||
/** | ||
* Get every store name. | ||
* @returns {string[]} name - Store's name. | ||
*/ | ||
list(): string[]; | ||
/** | ||
* Single store model | ||
* @typedef StoreModel | ||
* @type {object} | ||
* @returns {string} name - Store's name. | ||
* @returns {object} store - Store's value. | ||
*/ | ||
/** | ||
* Retreive every store collection's model. | ||
* @returns {StoreModel[]} Store Model - Collection of store's model | ||
*/ | ||
model(): { | ||
name: string | number; | ||
name: string; | ||
store: any; | ||
}[]; | ||
/** | ||
* Middleware callback | ||
* | ||
* @callback MiddlewareCallback | ||
* @param store - Collection of existing store. | ||
* @param process - Process name which flow through middleware. | ||
* @param { string } name - Store name. | ||
*/ | ||
/** | ||
* Add middleware to store. | ||
* @param {MiddlewareCallback} callback - Callback function to manipulate data from middleware. | ||
*/ | ||
applyMiddleware(...callbacks: ((store: { | ||
[key: string]: any; | ||
}, process: ArkflowsProcess, name: string) => any)[]): void; | ||
/** | ||
* Enable Arkflows devtools in browser developer's tool. | ||
* @param {boolean} option - Enable devtools. | ||
*/ | ||
applyMiddleware(...callbacks: ((store: any, process: ArkflowsProcess, name: string) => any)[]): void; | ||
enableDevtools(option?: boolean): void; | ||
@@ -115,0 +63,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const validate=(e,t)=>{if(!e)throw"type is required.";if("string"!=typeof e)throw"type must be string.";if(void 0===t[e])throw`Store: ${e} isn't existed. Please create it with create("${e}")`},isObject=e=>"object"==typeof e,isServer="undefined"==typeof window;var EventTargetClass="undefined"!=typeof navigator&&/^((?!chrome|android).)*safari/i.test(navigator.userAgent)?require("./polyfill"):isServer?null:class extends EventTarget{};exports.default=isServer?class{constructor(){this.event={},this.store={},this.middleware=[],this.withDevtools=void 0}create(e,t={}){return t}get(e){return{}}set(e,t){return{}}update(e,t){return{}}subscribe(e,t){return{unsubscribe:()=>null}}list(){return[]}model(){return[]}applyMiddleware(...e){}enableDevtools(e=!0){}}:class extends EventTargetClass{constructor(){super(),this.event={},this.store={},this.middleware=[],this.withDevtools=void 0}create(e,t={}){if(void 0!==this.store[e])throw`${e} is already existed.`;return this.event[e]=new Event(e),this.store[e]=this.useMiddleware(void 0!==t?t:{},"create",e),this.store[e]}get(e){return validate(e,this.event),Object.freeze(this.useMiddleware(this.store[e],"get",e))}set(e,t){validate(e,this.event);let s=this.event[e];return s.detail=t,this.store[e]=this.useMiddleware(t,"set",e),this.dispatchEvent(s),this.store[e]}update(e,t){validate(e,this.event);let s=this.event[e];return s.detail=t,this.store[e]=this.useMiddleware(isObject(t)?Object.assign(Object.assign({},this.store[e]),t):t,"update",e),this.dispatchEvent(s),this.store[e]}subscribe(e,t){"*"===e&&(e=[...this.list()]||[]),"string"==typeof e&&(e=new Array(e));let s=[];return e.forEach(e=>{validate(e,this.event);let i=()=>t(this.useMiddleware(this.store[e],"subscribe",e),e,this.model());return s.push(i),this.addEventListener(e,()=>i())}),{unsubscribe:()=>Object.entries(this.event).forEach(([e,t],i)=>this.removeEventListener(e,s[i]))}}list(){return Object.keys(this.store)}model(){let e=[];return Object.entries(this.store).map(([t,s])=>{e.push(JSON.parse(`{"name":"${t}","store":${JSON.stringify(this.useMiddleware(s,"get",t))}}`))}),e}applyMiddleware(...e){e.forEach(e=>this.middleware.push(e))}useMiddleware(e=this.store,t,s){let i=Object.freeze(e);return this.middleware.map(e=>i=e(i,t,s)),this.withDevtools&&void 0===window.__arkflows__&&(window.__arkflows__={version:"0.4.0",listener:e=>this.subscribe("*",t=>e(t)),process:[]},"subscribe"!==t&&window.__arkflows__.process.push({name:s,process:t,store:e})),i}enableDevtools(e=!0){if(void 0!==this.withDevtools)throw"Devtools can only figure at once.";e&&(window.__arkflows__={version:"0.4.0",listener:(e=(()=>null))=>this.subscribe("*",(t,s)=>e(t,s,this.model())),process:[]},this.withDevtools=!0)}}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const validate=(e,t)=>{if(!e)throw"type is required.";if("string"!=typeof e)throw"type must be string.";if(void 0===t[e])throw`Store: ${e} isn't existed. Please create it with create("${e}")`},isObject=e=>"object"==typeof e,isServer="undefined"==typeof window;var EventTargetClass="undefined"!=typeof navigator&&/^((?!chrome|android).)*safari/i.test(navigator.userAgent)?require("./polyfill"):isServer?null:class extends EventTarget{};exports.default=isServer?class{constructor(){this.event={},this.store={},this.middleware=[],this.withDevtools=void 0}create(e,t){return t}get(e){return this.store[e]}set(e,t){return t}update(e,t){return t}subscribe(e,t){return{unsubscribe:()=>null}}list(){return[]}model(){return[]}applyMiddleware(...e){}enableDevtools(e=!0){}}:class extends EventTargetClass{constructor(){super(),this.event={},this.store={},this.middleware=[],this.withDevtools=void 0}create(e,t){if(void 0!==this.store[e])throw`${e} is already existed.`;return this.event[e]=new Event(e),this.store[e]=this.useMiddleware(void 0!==t?t:{},"create",e),this.store[e]}get(e){return validate(e,this.event),Object.freeze(this.useMiddleware(this.store[e],"get",e))}set(e,t){validate(e,this.event);let s=this.event[e];return s.detail=t,this.store[e]=this.useMiddleware(t,"set",e),this.dispatchEvent(s),this.store[e]}update(e,t){validate(e,this.event);let s=this.event[e];return s.detail=t,this.store[e]=this.useMiddleware(isObject(t)?Object.assign(Object.assign({},this.store[e]),t):t,"update",e),this.dispatchEvent(s),this.store[e]}subscribe(e,t){"*"===e&&(e=[...this.list()]||[]),"string"==typeof e&&(e=new Array(e));let s=[];return e.forEach(e=>{validate(e,this.event);let i=()=>t(this.useMiddleware(this.store[e],"subscribe",e),e,this.model());return s.push(i),this.addEventListener(e,()=>i())}),{unsubscribe:()=>Object.entries(this.event).forEach(([e,t],i)=>this.removeEventListener(e,s[i]))}}list(){return Object.keys(this.store)}model(){let e=[];return Object.entries(this.store).map(([t,s])=>{e.push(JSON.parse(`{"name":"${t}","store":${JSON.stringify(this.useMiddleware(s,"get",t))}}`))}),e}applyMiddleware(...e){e.forEach(e=>this.middleware.push(e))}useMiddleware(e,t,s){let i=Object.freeze(e);return this.middleware.map(e=>i=e(i,t,s)),this.withDevtools&&void 0===window.__arkflows__&&(window.__arkflows__={version:"0.4.0",listener:e=>this.subscribe("*",t=>e(t)),process:[]},"subscribe"!==t&&window.__arkflows__.process.push({name:s,process:t,store:e})),i}enableDevtools(e=!0){if(void 0!==this.withDevtools)throw"Devtools can only figure at once.";e&&(window.__arkflows__={version:"0.4.0",listener:(e=(()=>null))=>this.subscribe("*",(t,s)=>e(t,s,this.model())),process:[]},this.withDevtools=!0)}}; |
{ | ||
"name": "arkflows", | ||
"version": "0.4.0-beta.1", | ||
"version": "0.4.0", | ||
"description": "Immutable state container comes in light-weight", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -22,8 +22,8 @@ # Arkflows | ||
```javascript | ||
let tea = new Store() | ||
let store = new Store() | ||
// Create "sugar" store with initial value of { amount: 0 } | ||
tea.create("sugar", { amount: 0 }) // { amount: 0 } | ||
tea.create("sugar", { amount: 0 }) // sugar is already existed. | ||
store.create("sugar", { amount: 0 }) // { amount: 0 } | ||
store.create("sugar", { amount: 0 }) // sugar is already existed. | ||
``` | ||
@@ -37,11 +37,11 @@ ## A simple way to create store | ||
```javascript | ||
import Store from "purple-tea" | ||
import Store from "arkflows" | ||
let tea = new Store() | ||
let store = new Store() | ||
``` | ||
`create()` will handle storage creation. It require `name` and `initial storage value`. | ||
```javascript | ||
let tea = new Store() | ||
let store = new Store() | ||
tea.create("sugar", { amount: 0 }) | ||
store.create("sugar", { amount: 0 }) | ||
``` | ||
@@ -53,19 +53,19 @@ This will create a store name "sugar" with initial value of `{ amount: 0 }`. | ||
```javascript | ||
let tea = new Store() | ||
let store = new Store() | ||
tea.create("sugar", { amount: 0 }) | ||
store.create("sugar", { amount: 0 }) | ||
tea.get("sugar") // { amount: 0 } | ||
tea.get("sugar").amount // 0 | ||
store.get("sugar") // { amount: 0 } | ||
store.get("sugar").amount // 0 | ||
``` | ||
If you get data from storage which isn't existed, it'll return error. | ||
```javascript | ||
let tea = new Store() | ||
let store = new Store() | ||
tea.get("sugar") // sugar isn't existed. Please create it with create("sugar") | ||
store.get("sugar") // sugar isn't existed. Please create it with create("sugar") | ||
tea.create("sugar", { amount: 0 }) | ||
tea.get("sugar") // { amount: 0 } | ||
store.create("sugar", { amount: 0 }) | ||
store.get("sugar") // { amount: 0 } | ||
tea.get("salt") // salt isn't existed. Please create it with create("sugar") | ||
store.get("salt") // salt isn't existed. Please create it with create("sugar") | ||
``` | ||
@@ -76,9 +76,9 @@ | ||
```javascript | ||
let tea = new Store() | ||
let store = new Store() | ||
tea.create("sugar", { amount: 0 }) | ||
tea.get("sugar") // { amount: 0 } | ||
store.create("sugar", { amount: 0 }) | ||
store.get("sugar") // { amount: 0 } | ||
tea.update("sugar", { amount: 1 }) | ||
tea.get("sugar") // { amount: 1 } | ||
store.update("sugar", { amount: 1 }) | ||
store.get("sugar") // { amount: 1 } | ||
``` | ||
@@ -89,9 +89,9 @@ | ||
```javascript | ||
let tea = new Store() | ||
let store = new Store() | ||
tea.create("sugar", { ingredient: "sugar", amount: 0 }) | ||
tea.get("sugar") // { ingredient: "sugar", amount: 0 } | ||
store.create("sugar", { ingredient: "sugar", amount: 0 }) | ||
store.get("sugar") // { ingredient: "sugar", amount: 0 } | ||
tea.set("sugar", { amount: 0 }) | ||
tea.get("sugar") // { amount: 0 } | ||
store.set("sugar", { amount: 0 }) | ||
store.get("sugar") // { amount: 0 } | ||
``` | ||
@@ -102,17 +102,17 @@ | ||
```javascript | ||
import Store from "./purple.ts" | ||
import Store from "arkflows" | ||
let tea = new Store() | ||
let store = new Store() | ||
tea.create("sugar", { amount: 0 }) | ||
store.create("sugar", { amount: 0 }) | ||
// Trigger when store is updated. | ||
tea.subscribe("sugar", data => { | ||
store.subscribe("sugar", data => { | ||
console.log(data) // Get current value of sugar. eg. { amount: 1 } | ||
}) | ||
tea.update("sugar", { amount: 1 }) | ||
store.update("sugar", { amount: 1 }) | ||
// Support multiple listener at once. | ||
tea.subscribe(["sugar", "milk"], data => { | ||
store.subscribe(["sugar", "milk"], data => { | ||
console.log(data) // Get current value of sugar. eg. { amount: 1 } | ||
@@ -128,5 +128,5 @@ }) | ||
let tea = new Store() | ||
let store = new Store() | ||
tea.applyMiddleware((store, process) => { | ||
store.applyMiddleware((store, process) => { | ||
console.log(store, `Process: ${process}`) // { amount: 0 } Process: create( | ||
@@ -136,3 +136,3 @@ return store | ||
tea.create("sugar", { amount: 0 }) | ||
store.create("sugar", { amount: 0 }) | ||
``` | ||
@@ -157,2 +157,2 @@ | ||
] | ||
``` | ||
``` |
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
22337
64
149