Comparing version 0.9.0 to 1.0.0
33
index.js
@@ -6,2 +6,3 @@ import 'core-js/modules/es.array.iterator.js'; | ||
import { useState, useRef, useEffect } from 'react'; | ||
import 'core-js/modules/es.object.from-entries.js'; | ||
import 'core-js/modules/es.weak-map.js'; | ||
@@ -36,5 +37,6 @@ | ||
} | ||
subscribe(key, listener = () => {}) { | ||
if (typeof listener !== "function") { | ||
warn(["listener must be a function"]); | ||
subscribe(key, listener) { | ||
if (typeof listener !== 'function') { | ||
warn(['listener must be a function']); | ||
return () => false; | ||
} | ||
@@ -47,3 +49,3 @@ __classPrivateFieldGet(this, _Observer_instances, "m", _Observer_createCache).call(this, key); | ||
set(object) { | ||
if (typeof object !== "object") return; | ||
if (typeof object !== 'object') return; | ||
Object.entries(object).forEach(([key, value]) => { | ||
@@ -57,6 +59,6 @@ __classPrivateFieldGet(this, _Observer_instances, "m", _Observer_createCache).call(this, key); | ||
var _a, _b; | ||
return (_b = (_a = __classPrivateFieldGet(this, _Observer_cache, "f")) === null || _a === void 0 ? void 0 : _a[key]) === null || _b === void 0 ? void 0 : _b.value; | ||
return typeof key === 'string' ? (_b = (_a = __classPrivateFieldGet(this, _Observer_cache, "f")) === null || _a === void 0 ? void 0 : _a[key]) === null || _b === void 0 ? void 0 : _b.value : Array.isArray(key) ? Object.fromEntries(key.map(key => [key, observer.get(key)])) : undefined; | ||
} | ||
isValid(key) { | ||
return typeof __classPrivateFieldGet(this, _Observer_cache, "f")[key] !== "undefined"; | ||
return typeof __classPrivateFieldGet(this, _Observer_cache, "f")[key] !== 'undefined'; | ||
} | ||
@@ -70,12 +72,12 @@ } | ||
const listeners = __classPrivateFieldGet(this, _Observer_cache, "f")[key].listeners; | ||
const state = __classPrivateFieldGet(this, _Observer_cache, "f")[key].value; | ||
listeners.forEach(listener => listener(state)); | ||
const value = __classPrivateFieldGet(this, _Observer_cache, "f")[key].value; | ||
listeners.forEach(listener => listener(value)); | ||
}; | ||
const observer = new Observer(); | ||
function warn(message) { | ||
console.warn(message.join("\n")); | ||
console.warn(message.join('\n')); | ||
} | ||
function useObserver(initialValue = undefined) { | ||
const state = useState(); | ||
const state = useState({}); | ||
const ref = useRef({ | ||
@@ -90,3 +92,8 @@ keys: new Set() | ||
const array = []; | ||
keys.forEach(key => array.push(observer.subscribe(key, state[1]))); | ||
keys.forEach(key => array.push(observer.subscribe(key, listener))); | ||
function listener(value) { | ||
state[1]({ | ||
value | ||
}); | ||
} | ||
return () => { | ||
@@ -104,3 +111,3 @@ array.forEach(unsubscribe => unsubscribe()); | ||
const key = prop.replace(/^set./, w => w.charAt(3).toLowerCase()); | ||
setValue(key); | ||
if (typeof initialValue !== 'undefined') setValue(key); | ||
return value => { | ||
@@ -111,3 +118,3 @@ observer.set({ | ||
}; | ||
} else if (/^withchange$/.test(prop)) { | ||
} else if (/^withstate$/.test(prop)) { | ||
return function (callback) { | ||
@@ -114,0 +121,0 @@ return function Element() { |
{ | ||
"name": "rosma", | ||
"version": "0.9.0", | ||
"version": "1.0.0", | ||
"repository": { | ||
@@ -9,3 +9,3 @@ "type": "git", | ||
"author": "Shahab Yazdi <sh.yazdi@gmail.com>", | ||
"homepage": "https://shahabyazdi.github.io/rosma/", | ||
"homepage": "https://github.com/shahabyazdi/rosma/", | ||
"description": "combined javascript proxy and observer pattern to optimize your react component.", | ||
@@ -12,0 +12,0 @@ "keywords": [ |
import { ElementType, ReactNode } from 'react'; | ||
export declare type State<T> = { | ||
withChange: (callback: (state: T & State<T>) => ReactNode) => ElementType; | ||
withState: (callback: (state: T & State<T>) => ReactNode) => ElementType; | ||
} & Record<string, any>; | ||
export declare function useObserver<T>(initialValue?: any): T & State<T>; |
@@ -1,7 +0,13 @@ | ||
declare type Listener = (state: any) => void; | ||
export declare type Listener = (value: any) => void; | ||
export declare type CacheData = { | ||
setter?: string; | ||
getter?: string; | ||
value?: any; | ||
listeners?: Set<Listener>; | ||
}; | ||
declare class Observer { | ||
#private; | ||
subscribe(key: string, listener?: Listener): () => boolean; | ||
set(object: Record<string, any>): void; | ||
get(key: string): any; | ||
subscribe(key: string, listener: Listener): () => boolean; | ||
set<T>(object: T | Record<string, any>): void; | ||
get(key: string | string[]): any; | ||
isValid(key: string): boolean; | ||
@@ -8,0 +14,0 @@ } |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
12299
288
0