@nikandlv/fox
Advanced tools
Comparing version 0.0.1 to 0.0.2
107
index.js
import undefsafe from "undefsafe"; | ||
export var fox = { | ||
onListeners: [], | ||
undefsafe | ||
}; | ||
let onPublishListeners = []; | ||
let onUnpublishListeners = []; | ||
let onUpdateListeners = []; | ||
var fox = {}; | ||
function log(str) { | ||
console.log(str) | ||
console.log("🦊 "+str) | ||
} | ||
export var manager = { | ||
set:(keys,obj,callback=()=>{})=> { | ||
var dummy = fox; | ||
var manager = { | ||
publish: (keys, fun, callback = ()=>{}) => { | ||
let dummy = fox; | ||
const keylist = keys.split("."); | ||
@@ -20,36 +19,24 @@ const key = keylist.pop(); | ||
}, dummy); | ||
pointer[key] = obj; | ||
fox = dummy | ||
callback() | ||
}, | ||
get: (item,def)=> { | ||
let res = undefsafe(fox,item); | ||
if(typeof res === 'undefined') { | ||
return def | ||
} | ||
return res; | ||
}, | ||
register: (keys, fun, callback = ()=>{}) => { | ||
var dummy = fox; | ||
const keylist = keys.split("."); | ||
const key = keylist.pop(); | ||
const pointer = keylist.reduce((accumulator, currentValue) => { | ||
if (accumulator[currentValue] === undefined) | ||
accumulator[currentValue] = {}; | ||
return accumulator[currentValue]; | ||
}, dummy); | ||
const is_first = typeof pointer[key] === 'undefined'; | ||
pointer[key] = fun; | ||
fox = dummy | ||
log("🦊 " + keys + " registered"); | ||
if (typeof fox.onListeners[keys] !== "undefined") { | ||
fox.onListeners[keys].map((callback) => { | ||
return callback(fun) | ||
}); | ||
fox.onListeners[keys] = [] | ||
fox = dummy; | ||
if(!is_first) { | ||
log("Updated:"+keys); | ||
if (typeof onUpdateListeners[keys] !== "undefined") { | ||
onUpdateListeners[keys].map((changeCallback) => { | ||
return changeCallback(fun) | ||
}); | ||
} | ||
} else { | ||
log("Published:"+keys); | ||
if (typeof onPublishListeners[keys] !== "undefined") { | ||
onPublishListeners[keys].map((callback) => { | ||
return callback(fun) | ||
}); | ||
onPublishListeners[keys] = [] | ||
} | ||
} | ||
callback() | ||
}, | ||
unRegister: keys => { | ||
unpublish: (keys,callback = ()=>{}) => { | ||
const dummy = fox; | ||
@@ -65,18 +52,42 @@ const keyList = keys.split("."); | ||
fox = dummy; | ||
log("🦊 " + keys + " unregistered"); | ||
log("Unpublished:"+keys); | ||
if (typeof onUnpublishListeners[keys] !== "undefined") { | ||
onUnpublishListeners[keys].map((callback) => { | ||
return callback(fun) | ||
}); | ||
onUnpublishListeners[keys] = [] | ||
} | ||
callback() | ||
}, | ||
onRegister: (item, callback) => { | ||
var target = item.split(".").reduce((p, c) => (p && p[c]) || null, fox) | ||
get: (item,default_value)=> { | ||
let res = undefsafe(fox,item); | ||
if(typeof res === 'undefined') { | ||
return default_value | ||
} | ||
return res; | ||
}, | ||
onPublish: (item, callback) => { | ||
const target = item.split(".").reduce((p, c) => (p && p[c]) || null, fox); | ||
if (target !== null) { | ||
callback(target); | ||
log("got " + item); | ||
} else { | ||
if (typeof fox.onListeners[item] === "undefined") { | ||
fox.onListeners[item] = []; | ||
fox.onListeners[item].push(callback) | ||
} else { | ||
fox.onListeners[item].push(callback) | ||
if (typeof onPublishListeners[item] === "undefined") { | ||
onPublishListeners[item] = []; | ||
} | ||
onPublishListeners[item].push(callback) | ||
} | ||
}, | ||
onUnpublish: (item, callback) => { | ||
if (typeof onUnpublishListeners[item] === "undefined") { | ||
onUnpublishListeners[item] = []; | ||
} | ||
onUnpublishListeners[item].push(callback) | ||
}, | ||
onUpdate: (item, callback) => { | ||
if (typeof onUpdateListeners[item] === "undefined") { | ||
onUpdateListeners[item] = []; | ||
} | ||
onUpdateListeners[item].push(callback) | ||
} | ||
}; | ||
export {fox,manager}; |
{ | ||
"name": "@nikandlv/fox", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A Simple, Easy and fast state manager", | ||
@@ -15,3 +15,6 @@ "license": "MIT", | ||
"fox" | ||
] | ||
], | ||
"dependencies": { | ||
"undefsafe": "^2.0.2" | ||
} | ||
} |
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
38298
8
90
1
+ Addedundefsafe@^2.0.2
+ Addedundefsafe@2.0.5(transitive)