Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cfcs/core

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cfcs/core - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

1

declaration/core/utils.d.ts

@@ -8,3 +8,4 @@ /**

export declare function camelize(str: string): string;
export declare function isString(val: any): val is string;
export declare function isObject(val: any): val is object;
export declare function isFunction(val: any): val is Function;

@@ -0,1 +1,3 @@

import { Ref } from "../core";
export declare function findTarget<Target extends Element = Element>(target: string | Target | Ref<Target> | null): Target | null;
export declare function withClassMethods(methods: readonly string[]): (prototype: any, memberName: string) => void;

6

declaration/reactive/ReactiveSubscribe.d.ts
export declare function injectReactiveSubscribe(object: Record<string, any>): void;
export declare function ReactiveSubscribe(Constructor: any): void;
export interface ReactiveSubscribe<Properties extends Record<string, any>> {
subscribe<Name extends keyof Properties = keyof Properties>(name: Name, callback: (value: Properties[Name]) => void): void;
unsubscribe<Name extends keyof Properties = keyof Properties>(name?: Name, callback?: (value: Properties[Name]) => void): void;
export interface ReactiveSubscribe<State extends Record<string, any>> {
subscribe<Name extends keyof State = keyof State>(name: Name, callback: (value: State[Name]) => void): void;
unsubscribe<Name extends keyof State = keyof State>(name?: Name, callback?: (value: State[Name]) => void): void;
}
import { ComponentEvent } from "@egjs/component";
import { Observer } from "./Observer";
import { ReactiveSubscribe } from "./ReactiveSubscribe";
declare type AnyFunction = (...args: any[]) => any;

@@ -11,2 +13,6 @@ declare type NoArguments = undefined | null | void | never;

};
export interface GetReactiveValue {
<Type>(observer: Observer<Type>): Type;
<Inst extends ReactiveSubscribe<object>, Name = (Inst extends ReactiveSubscribe<infer State> ? keyof State : unknown)>(inst: Inst, name: any): Name;
}
export {};

@@ -6,2 +6,3 @@ import { Observer } from "./Observer";

export declare function observe<Type>(defaultValue?: Type): Observer<Type>;
export declare function defineObservers(instance: any): Record<string, Observer<any>>;
export declare function getObservers(instance: any): Record<string, Observer<any>>;

@@ -8,0 +9,0 @@ export declare function getObserver(instance: any, name: string, defaultValue?: any): Observer<any>;

@@ -7,3 +7,3 @@ /*

repository: https://github.com/naver/cfcs
version: 0.0.4
version: 0.0.5
*/

@@ -27,2 +27,5 @@ 'use strict';

}
function isString(val) {
return typeof val === "string";
}
function isObject(val) {

@@ -35,2 +38,19 @@ return typeof val === "object";

function findTarget(target) {
var el;
if (!target) {
return null;
}
if (isString(target)) {
el = document.querySelector(target);
} else if (target instanceof Element) {
el = target;
} else if ("value" in target || "current" in target) {
el = target.value || target.current;
}
return el;
}
function withClassMethods(methods) {

@@ -127,5 +147,14 @@ return function (prototype, memberName) {

}
function defineObservers(instance) {
var observers = {};
Object.defineProperty(instance, OBSERVERS_PATH, {
get: function () {
return observers;
}
});
return observers;
}
function getObservers(instance) {
if (!instance[OBSERVERS_PATH]) {
instance[OBSERVERS_PATH] = {};
defineObservers(instance);
}

@@ -204,2 +233,3 @@

var reactiveObject = {};
defineObservers(reactiveObject);
keys(result).forEach(function (name) {

@@ -290,7 +320,10 @@ var value = result[name];

camelize: camelize,
isString: isString,
isObject: isObject,
isFunction: isFunction,
findTarget: findTarget,
withClassMethods: withClassMethods,
withReactiveMethods: withReactiveMethods,
observe: observe,
defineObservers: defineObservers,
getObservers: getObservers,

@@ -297,0 +330,0 @@ getObserver: getObserver,

@@ -7,3 +7,3 @@ /*

repository: https://github.com/naver/cfcs
version: 0.0.4
version: 0.0.5
*/

@@ -25,2 +25,5 @@ import Component from '@egjs/component';

}
function isString(val) {
return typeof val === "string";
}
function isObject(val) {

@@ -33,2 +36,19 @@ return typeof val === "object";

function findTarget(target) {
var el;
if (!target) {
return null;
}
if (isString(target)) {
el = document.querySelector(target);
} else if (target instanceof Element) {
el = target;
} else if ("value" in target || "current" in target) {
el = target.value || target.current;
}
return el;
}
function withClassMethods(methods) {

@@ -125,5 +145,14 @@ return function (prototype, memberName) {

}
function defineObservers(instance) {
var observers = {};
Object.defineProperty(instance, OBSERVERS_PATH, {
get: function () {
return observers;
}
});
return observers;
}
function getObservers(instance) {
if (!instance[OBSERVERS_PATH]) {
instance[OBSERVERS_PATH] = {};
defineObservers(instance);
}

@@ -202,2 +231,3 @@

var reactiveObject = {};
defineObservers(reactiveObject);
keys(result).forEach(function (name) {

@@ -284,3 +314,3 @@ var value = result[name];

export { Observer, Reactive, ReactiveSubscribe, adaptReactive, camelize, getObserver, getObservers, injectReactiveSubscribe, isFunction, isObject, isObserver, keys, observe, reactive, setObserver, withClassMethods, withReactiveMethods };
export { Observer, Reactive, ReactiveSubscribe, adaptReactive, camelize, defineObservers, findTarget, getObserver, getObservers, injectReactiveSubscribe, isFunction, isObject, isObserver, isString, keys, observe, reactive, setObserver, withClassMethods, withReactiveMethods };
//# sourceMappingURL=cfcs.esm.js.map

@@ -7,3 +7,3 @@ /*

repository: https://github.com/naver/cfcs
version: 0.0.4
version: 0.0.5
*/

@@ -29,2 +29,5 @@ (function (global, factory) {

}
function isString(val) {
return typeof val === "string";
}
function isObject(val) {

@@ -37,2 +40,19 @@ return typeof val === "object";

function findTarget(target) {
var el;
if (!target) {
return null;
}
if (isString(target)) {
el = document.querySelector(target);
} else if (target instanceof Element) {
el = target;
} else if ("value" in target || "current" in target) {
el = target.value || target.current;
}
return el;
}
function withClassMethods(methods) {

@@ -590,5 +610,14 @@ return function (prototype, memberName) {

}
function defineObservers(instance) {
var observers = {};
Object.defineProperty(instance, OBSERVERS_PATH, {
get: function () {
return observers;
}
});
return observers;
}
function getObservers(instance) {
if (!instance[OBSERVERS_PATH]) {
instance[OBSERVERS_PATH] = {};
defineObservers(instance);
}

@@ -667,2 +696,3 @@

var reactiveObject = {};
defineObservers(reactiveObject);
keys(result).forEach(function (name) {

@@ -753,7 +783,10 @@ var value = result[name];

camelize: camelize,
isString: isString,
isObject: isObject,
isFunction: isFunction,
findTarget: findTarget,
withClassMethods: withClassMethods,
withReactiveMethods: withReactiveMethods,
observe: observe,
defineObservers: defineObservers,
getObservers: getObservers,

@@ -760,0 +793,0 @@ getObserver: getObserver,

@@ -7,5 +7,5 @@ /*

repository: https://github.com/naver/cfcs
version: 0.0.4
version: 0.0.5
*/
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(t="undefined"!=typeof globalThis?globalThis:t||self).cfcs=n()}(this,function(){"use strict";function u(t){return Object.keys(t)}function n(t){return"function"==typeof t}var e="__observers__";function s(t){var n="function"==typeof Symbol&&Symbol.iterator,e=n&&t[n],r=0;if(e)return e.call(t);if(t&&"number"==typeof t.length)return{next:function(){return{value:(t=t&&r>=t.length?void 0:t)&&t[r++],done:!t}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}function c(){for(var t=[],n=0;n<arguments.length;n++)t=t.concat(function(t,n){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var r,i,o=e.call(t),u=[];try{for(;(void 0===n||0<n--)&&!(r=o.next()).done;)u.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return u}(arguments[n]));return t}function l(t){return void 0===t}var i=function(){function t(t,n){var e,r;if(this._canceled=!1,n)try{for(var i=s(Object.keys(n)),o=i.next();!o.done;o=i.next()){var u=o.value;this[u]=n[u]}}catch(t){e={error:t}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}this.eventType=t}var n=t.prototype;return n.stop=function(){this._canceled=!0},n.isCanceled=function(){return this._canceled},t}(),r=function(){function t(){this._eventHandler={}}var n=t.prototype;return n.trigger=function(n){for(var e=[],t=1;t<arguments.length;t++)e[t-1]=arguments[t];var r=n instanceof i?n.eventType:n,r=c(this._eventHandler[r]||[]);return r.length<=0||(n instanceof i?(n.currentTarget=this,r.forEach(function(t){t(n)})):r.forEach(function(t){t.apply(void 0,c(e))})),this},n.once=function(e,r){var i,o=this;if("object"==typeof e&&l(r)){var t,n=e;for(t in n)this.once(t,n[t])}else"string"==typeof e&&"function"==typeof r&&(i=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];r.apply(void 0,c(t)),o.off(e,i)},this.on(e,i));return this},n.hasOn=function(t){return!!this._eventHandler[t]},n.on=function(t,n){if("object"==typeof t&&l(n)){var e,r=t;for(e in r)this.on(e,r[e])}else{var i;"string"==typeof t&&"function"==typeof n&&(i=this._eventHandler[t],l(i)&&(this._eventHandler[t]=[],i=this._eventHandler[t]),i.push(n))}return this},n.off=function(t,n){var e,r;if(l(t))return this._eventHandler={},this;if(l(n)){if("string"==typeof t)return delete this._eventHandler[t],this;var i,o=t;for(i in o)this.off(i,o[i])}else{var u=this._eventHandler[t];if(u){var c=0;try{for(var f=s(u),a=f.next();!a.done;a=f.next()){if(a.value===n){u.splice(c,1),u.length<=0&&delete this._eventHandler[t];break}c++}}catch(t){e={error:t}}finally{try{a&&!a.done&&(r=f.return)&&r.call(f)}finally{if(e)throw e.error}}}}return this},t.VERSION="3.0.2",t}(),o=function(){function t(t){this._emitter=new r,this._current=t}var n=t.prototype;return Object.defineProperty(n,"current",{get:function(){return this._current},set:function(t){var n=t!==this._current;this._current=t,n&&this._emitter.trigger("update",t)},enumerable:!1,configurable:!0}),n.subscribe=function(t){this._emitter.on("update",t)},n.unsubscribe=function(t){this._emitter.off("update",t)},t}();function f(i,t){var n={};return t&&t.forEach(function(r){n[r]=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var e=i.current||i.value;return e[r].apply(e,t)}}),n}function a(t){return new o(t)}function v(t){return t[e]||(t[e]={}),t[e]}function h(t,n,e){t=v(t);return t[n]||(t[n]=a(e)),t[n]}function d(t,n,e){v(t)[n]=e}function b(t){return t&&"current"in t&&"subscribe"in t&&"unsubscribe"in t}function p(r){return function(t,n){var e=r||n;Object.defineProperty(t,n,{get:function(){return h(this,e).current},set:function(t){h(this,e,t).current=t}}),e!==n&&Object.defineProperty(t,e,{get:function(){return h(this,e).current}})}}function y(t){t.subscribe=function(t,n){h(this,t).subscribe(n)},t.unsubscribe=function(t,n){var e=this;t?t in this&&h(this,t).unsubscribe(n):u(v(this)).forEach(function(t){e.unsubscribe(t)})}}return{__proto__:null,keys:u,camelize:function(t){return t.replace(/[\s-_]([a-z])/g,function(t,n){return n.toUpperCase()})},isObject:function(t){return"object"==typeof t},isFunction:n,withClassMethods:function(n){return function(t,i){n.forEach(function(r){r in t||(t[r]=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var e=(e=this[i])[r].apply(e,t);return e===this[i]?this:e})})}},withReactiveMethods:f,observe:a,getObservers:v,getObserver:h,setObserver:d,isObserver:b,Observer:o,reactive:function(t){var e=n(t)?t():t,r={};return u(e).forEach(function(t){var n=e[t];b(n)?(d(r,t,n),p(t)(r,t)):r[t]=n}),y(r),r},Reactive:p,injectReactiveSubscribe:y,ReactiveSubscribe:function(t){y(t.prototype)},adaptReactive:function(r){var t;function n(){var t;return null!=(t=null==(t=r.data)?void 0:t.call(r))?t:{}}var i={current:(null==(t=r.created)?void 0:t.call(r,n()))||null},o=null;return{state:function(){var e,t=i.current;return o||(r.state?o=r.state:t&&(e=v(t),o=u(e).reduce(function(t,n){return t[n]=e[n].current,t},{})),o||{})},instance:function(){return i.current},mounted:function(){var t;i.current=(null==(t=r.mounted)?void 0:t.call(r,n()))||i.current},init:function(){var t;null!=(t=r.init)&&t.call(r,i.current,n())},destroy:function(){var t;null!=(t=r.destroy)&&t.call(r,i.current,n())},methods:function(){return f(i,r.methods)},on:function(t,n){var e;null!=(e=r.on)&&e.call(r,i.current,t,n)},off:function(t,n){var e;null!=(e=r.off)&&e.call(r,i.current,t,n)}}}}});
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).cfcs=t()}(this,function(){"use strict";function u(n){return Object.keys(n)}function e(n){return"string"==typeof n}function t(n){return"function"==typeof n}var r="__observers__";function s(n){var t="function"==typeof Symbol&&Symbol.iterator,e=t&&n[t],r=0;if(e)return e.call(n);if(n&&"number"==typeof n.length)return{next:function(){return{value:(n=n&&r>=n.length?void 0:n)&&n[r++],done:!n}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function c(){for(var n=[],t=0;t<arguments.length;t++)n=n.concat(function(n,t){var e="function"==typeof Symbol&&n[Symbol.iterator];if(!e)return n;var r,i,o=e.call(n),u=[];try{for(;(void 0===t||0<t--)&&!(r=o.next()).done;)u.push(r.value)}catch(n){i={error:n}}finally{try{r&&!r.done&&(e=o.return)&&e.call(o)}finally{if(i)throw i.error}}return u}(arguments[t]));return n}function l(n){return void 0===n}var i=function(){function n(n,t){var e,r;if(this._canceled=!1,t)try{for(var i=s(Object.keys(t)),o=i.next();!o.done;o=i.next()){var u=o.value;this[u]=t[u]}}catch(n){e={error:n}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}this.eventType=n}var t=n.prototype;return t.stop=function(){this._canceled=!0},t.isCanceled=function(){return this._canceled},n}(),o=function(){function n(){this._eventHandler={}}var t=n.prototype;return t.trigger=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var r=t instanceof i?t.eventType:t,r=c(this._eventHandler[r]||[]);return r.length<=0||(t instanceof i?(t.currentTarget=this,r.forEach(function(n){n(t)})):r.forEach(function(n){n.apply(void 0,c(e))})),this},t.once=function(e,r){var i,o=this;if("object"==typeof e&&l(r)){var n,t=e;for(n in t)this.once(n,t[n])}else"string"==typeof e&&"function"==typeof r&&(i=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];r.apply(void 0,c(n)),o.off(e,i)},this.on(e,i));return this},t.hasOn=function(n){return!!this._eventHandler[n]},t.on=function(n,t){if("object"==typeof n&&l(t)){var e,r=n;for(e in r)this.on(e,r[e])}else{var i;"string"==typeof n&&"function"==typeof t&&(i=this._eventHandler[n],l(i)&&(this._eventHandler[n]=[],i=this._eventHandler[n]),i.push(t))}return this},t.off=function(n,t){var e,r;if(l(n))return this._eventHandler={},this;if(l(t)){if("string"==typeof n)return delete this._eventHandler[n],this;var i,o=n;for(i in o)this.off(i,o[i])}else{var u=this._eventHandler[n];if(u){var c=0;try{for(var f=s(u),a=f.next();!a.done;a=f.next()){if(a.value===t){u.splice(c,1),u.length<=0&&delete this._eventHandler[n];break}c++}}catch(n){e={error:n}}finally{try{a&&!a.done&&(r=f.return)&&r.call(f)}finally{if(e)throw e.error}}}}return this},n.VERSION="3.0.2",n}(),f=function(){function n(n){this._emitter=new o,this._current=n}var t=n.prototype;return Object.defineProperty(t,"current",{get:function(){return this._current},set:function(n){var t=n!==this._current;this._current=n,t&&this._emitter.trigger("update",n)},enumerable:!1,configurable:!0}),t.subscribe=function(n){this._emitter.on("update",n)},t.unsubscribe=function(n){this._emitter.off("update",n)},n}();function a(i,n){var t={};return n&&n.forEach(function(r){t[r]=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var e=i.current||i.value;return e[r].apply(e,n)}}),t}function v(n){return new f(n)}function h(n){var t={};return Object.defineProperty(n,r,{get:function(){return t}}),t}function d(n){return n[r]||h(n),n[r]}function y(n,t,e){n=d(n);return n[t]||(n[t]=v(e)),n[t]}function b(n,t,e){d(n)[t]=e}function p(n){return n&&"current"in n&&"subscribe"in n&&"unsubscribe"in n}function g(r){return function(n,t){var e=r||t;Object.defineProperty(n,t,{get:function(){return y(this,e).current},set:function(n){y(this,e,n).current=n}}),e!==t&&Object.defineProperty(n,e,{get:function(){return y(this,e).current}})}}function _(n){n.subscribe=function(n,t){y(this,n).subscribe(t)},n.unsubscribe=function(n,t){var e=this;n?n in this&&y(this,n).unsubscribe(t):u(d(this)).forEach(function(n){e.unsubscribe(n)})}}return{__proto__:null,keys:u,camelize:function(n){return n.replace(/[\s-_]([a-z])/g,function(n,t){return t.toUpperCase()})},isString:e,isObject:function(n){return"object"==typeof n},isFunction:t,findTarget:function(n){var t;return n?(e(n)?t=document.querySelector(n):n instanceof Element?t=n:("value"in n||"current"in n)&&(t=n.value||n.current),t):null},withClassMethods:function(t){return function(n,i){t.forEach(function(r){r in n||(n[r]=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var e=(e=this[i])[r].apply(e,n);return e===this[i]?this:e})})}},withReactiveMethods:a,observe:v,defineObservers:h,getObservers:d,getObserver:y,setObserver:b,isObserver:p,Observer:f,reactive:function(n){var e=t(n)?n():n,r={};return h(r),u(e).forEach(function(n){var t=e[n];p(t)?(b(r,n,t),g(n)(r,n)):r[n]=t}),_(r),r},Reactive:g,injectReactiveSubscribe:_,ReactiveSubscribe:function(n){_(n.prototype)},adaptReactive:function(r){var n;function t(){var n;return null!=(n=null==(n=r.data)?void 0:n.call(r))?n:{}}var i={current:(null==(n=r.created)?void 0:n.call(r,t()))||null},o=null;return{state:function(){var e,n=i.current;return o||(r.state?o=r.state:n&&(e=d(n),o=u(e).reduce(function(n,t){return n[t]=e[t].current,n},{})),o||{})},instance:function(){return i.current},mounted:function(){var n;i.current=(null==(n=r.mounted)?void 0:n.call(r,t()))||i.current},init:function(){var n;null!=(n=r.init)&&n.call(r,i.current,t())},destroy:function(){var n;null!=(n=r.destroy)&&n.call(r,i.current,t())},methods:function(){return a(i,r.methods)},on:function(n,t){var e;null!=(e=r.on)&&e.call(r,i.current,n,t)},off:function(n,t){var e;null!=(e=r.off)&&e.call(r,i.current,n,t)}}}}});
//# sourceMappingURL=cfcs.min.js.map
{
"name": "@cfcs/core",
"version": "0.0.4",
"version": "0.0.5",
"description": "",

@@ -5,0 +5,0 @@ "main": "dist/cfcs.cjs.js",

@@ -14,7 +14,12 @@ /**

export function isString(val: any): val is string {
return typeof val === "string";
}
export function isObject(val: any): val is object {
return typeof val === "object";
}
export function isFunction(val: any): val is Function {
return typeof val === "function";
}
}

@@ -0,2 +1,19 @@

import { isString, Ref } from "../core";
export function findTarget<Target extends Element = Element>(target: string | Target | Ref<Target> | null): Target | null {
let el!: Target;
if (!target) {
return null;
} if (isString(target)) {
el = document.querySelector<Target>(target)!;
} else if (target instanceof Element) {
el = target;
} else if ("value" in target || "current" in target) {
el = target.value! || target.current!;
}
return el;
}
export function withClassMethods(methods: readonly string[]) {

@@ -3,0 +20,0 @@ return function (prototype: any, memberName: string) {

@@ -9,57 +9,57 @@ import { keys } from "src/core";

export function adaptReactive<
Instance extends ReactiveSubscribe<Record<string, any>>,
State extends Record<string, any> = {},
Methods extends keyof Partial<Instance> = any,
Data = any,
Events extends Record<string, any> = {},
>(adapter: ReactiveAdapter<Instance, State, Methods, Data, Events>) {
Instance extends ReactiveSubscribe<Record<string, any>>,
State extends Record<string, any> = {},
Methods extends keyof Partial<Instance> = any,
Data = any,
Events extends Record<string, any> = {},
>(adapter: ReactiveAdapter<Instance, State, Methods, Data, Events>) {
function data(): Data {
return adapter.data?.() ?? {} as Data;
}
function data(): Data {
return adapter.data?.() ?? {} as Data;
}
const instanceRef: Ref<Instance> = { current: adapter.created?.(data()) || null };
let firstState: State | null = null;
return {
state(): State {
const inst = instanceRef.current;
const instanceRef: Ref<Instance> = { current: adapter.created?.(data()) || null };
let firstState: State | null = null;
if (firstState) {
return firstState;
}
if (adapter.state) {
firstState = adapter.state;
} else if (inst) {
const observers = getObservers(inst);
return {
state(): State {
const inst = instanceRef.current;
firstState = keys(observers).reduce((prev, cur) => {
prev[cur] = observers[cur].current;
return prev;
}, {} as any);
}
return firstState || {} as State;
},
instance() {
return instanceRef.current;
},
mounted(): void {
instanceRef.current = adapter.mounted?.(data()) || instanceRef.current;
},
init(): void {
adapter.init?.(instanceRef.current!, data());
},
destroy(): void {
adapter.destroy?.(instanceRef.current!, data());
},
methods() {
return withReactiveMethods<any, any, any>(instanceRef, adapter.methods);
},
on(eventName: string, listener: ReactiveEventCallback<any, any>) {
adapter.on?.(instanceRef.current!, eventName as never, listener);
},
off(eventName: string, listener: ReactiveEventCallback<any, any>) {
adapter.off?.(instanceRef.current!, eventName as never, listener);
},
};
}
if (firstState) {
return firstState;
}
if (adapter.state) {
firstState = adapter.state;
} else if (inst) {
const observers = getObservers(inst);
firstState = keys(observers).reduce((prev, cur) => {
prev[cur] = observers[cur].current;
return prev;
}, {} as any);
}
return firstState || {} as State;
},
instance() {
return instanceRef.current;
},
mounted(): void {
instanceRef.current = adapter.mounted?.(data()) || instanceRef.current;
},
init(): void {
adapter.init?.(instanceRef.current!, data());
},
destroy(): void {
adapter.destroy?.(instanceRef.current!, data());
},
methods() {
return withReactiveMethods<any, any, any>(instanceRef, adapter.methods);
},
on(eventName: string, listener: ReactiveEventCallback<any, any>) {
adapter.on?.(instanceRef.current!, eventName as never, listener);
},
off(eventName: string, listener: ReactiveEventCallback<any, any>) {
adapter.off?.(instanceRef.current!, eventName as never, listener);
},
};
}
import { ExtractNever, isFunction, keys } from "../core";
import { OBSERVERS_PATH } from "./const";
import { Observer } from "./Observer";
import { Reactive } from "./Reactive";
import { injectReactiveSubscribe, ReactiveSubscribe } from "./ReactiveSubscribe";
import { isObserver, setObserver } from "./utils";
import { defineObservers, isObserver, setObserver } from "./utils";
type ConvertValue<Object extends Record<string, any>> = {
[Key in keyof Object]: Object[Key] extends Observer<infer Type> ? Type : Object[Key];
[Key in keyof Object]: Object[Key] extends Observer<infer Type> ? Type : Object[Key];
}
type PickObverser<Object extends Record<string, any>> = ExtractNever<{
[Key in keyof Object]: Object[Key] extends Observer<infer Type> ? Type : never;
[Key in keyof Object]: Object[Key] extends Observer<infer Type> ? Type : never;
}>;
export type ReactiveObject<Object extends Record<string, any>>
= ConvertValue<Object> & ReactiveSubscribe<PickObverser<Object>>;
= ConvertValue<Object> & ReactiveSubscribe<PickObverser<Object>>;
export function reactive<Object extends Record<string, any>>(
setup: Readonly<Object> | (() => Readonly<Object>),
setup: Readonly<Object> | (() => Readonly<Object>),
): ReactiveObject<Object> {
const result = isFunction(setup) ? setup() : setup;
const reactiveObject: Record<string, any> = {};
const result = isFunction(setup) ? setup() : setup;
const reactiveObject: Record<string, any> = {};
keys(result).forEach((name: any) => {
const value = result[name];
if (isObserver(value)) {
setObserver(reactiveObject, name, value);
Reactive(name)(reactiveObject, name);
} else {
reactiveObject[name] = value;
}
});
defineObservers(reactiveObject);
keys(result).forEach((name: any) => {
const value = result[name];
if (isObserver(value)) {
setObserver(reactiveObject, name, value);
Reactive(name)(reactiveObject, name);
} else {
reactiveObject[name] = value;
}
});
injectReactiveSubscribe(reactiveObject);
return reactiveObject as ReactiveObject<Object>;
injectReactiveSubscribe(reactiveObject);
return reactiveObject as ReactiveObject<Object>;
}
import { getObserver } from "./utils";
export function Reactive(name?: string) {
return function (prototype: any, memberName: string) {
const publicName = name || memberName;
return function (prototype: any, memberName: string) {
const publicName = name || memberName;
Object.defineProperty(prototype, memberName, {
get: function () {
return getObserver(this, publicName).current;
},
set: function (value: any) {
getObserver(this, publicName, value).current = value;
},
});
if (publicName !== memberName) {
Object.defineProperty(prototype, publicName, {
get: function () {
return getObserver(this, publicName).current;
},
});
}
};
Object.defineProperty(prototype, memberName, {
get: function () {
return getObserver(this, publicName).current;
},
set: function (value: any) {
getObserver(this, publicName, value).current = value;
},
});
if (publicName !== memberName) {
Object.defineProperty(prototype, publicName, {
get: function () {
return getObserver(this, publicName).current;
},
});
}
};
}
import { keys } from "../core";
import { getObserver, getObservers } from "./utils";
export function injectReactiveSubscribe(object: Record<string, any>, ) {
object["subscribe"] = function (name: string, callback: (value: any) => void) {
getObserver(this, name).subscribe(callback);
};
object["unsubscribe"] = function (name?: string, callback?: (value: any) => void) {
if (!name) {
keys(getObservers(this)).forEach((observerName) => {
this.unsubscribe(observerName);
});
return;
}
if (!(name in this)) {
return;
}
getObserver(this, name).unsubscribe(callback);
};
export function injectReactiveSubscribe(object: Record<string, any>,) {
object["subscribe"] = function (name: string, callback: (value: any) => void) {
getObserver(this, name).subscribe(callback);
};
object["unsubscribe"] = function (name?: string, callback?: (value: any) => void) {
if (!name) {
keys(getObservers(this)).forEach((observerName) => {
this.unsubscribe(observerName);
});
return;
}
if (!(name in this)) {
return;
}
getObserver(this, name).unsubscribe(callback);
};
}
export function ReactiveSubscribe(Constructor: any) {
const prototype = Constructor.prototype;
const prototype = Constructor.prototype;
injectReactiveSubscribe(prototype);
injectReactiveSubscribe(prototype);
}
export interface ReactiveSubscribe<Properties extends Record<string, any>> {
subscribe<Name extends keyof Properties = keyof Properties>(
name: Name, callback: (value: Properties[Name]) => void): void;
unsubscribe<Name extends keyof Properties = keyof Properties>(
name?: Name, callback?: (value: Properties[Name]) => void): void;
}
export interface ReactiveSubscribe<State extends Record<string, any>> {
subscribe<Name extends keyof State = keyof State>(
name: Name, callback: (value: State[Name]) => void): void;
unsubscribe<Name extends keyof State = keyof State>(
name?: Name, callback?: (value: State[Name]) => void): void;
}
import { ComponentEvent } from "@egjs/component";
import { Observer } from "./Observer";
import { ReactiveSubscribe } from "./ReactiveSubscribe";

@@ -26,1 +28,7 @@ type AnyFunction = (...args: any[]) => any;

};
export interface GetReactiveValue {
<Type>(observer: Observer<Type>): Type;
<Inst extends ReactiveSubscribe<object>, Name = (Inst extends ReactiveSubscribe<infer State> ? keyof State : unknown)>(inst: Inst, name: any): Name;
}
import { OBSERVERS_PATH } from "./const";
import { Observer } from "./Observer";
import { ReactiveMethods } from "./types";
import { GetReactiveValue, ReactiveMethods } from "./types";
import { Ref } from "../core";

@@ -33,5 +33,17 @@

export function defineObservers(instance: any) {
const observers: Record<string, Observer<any>> = {};
Object.defineProperty(instance, OBSERVERS_PATH, {
get() {
return observers;
},
});
return observers;
}
export function getObservers(instance: any): Record<string, Observer<any>> {
if (!instance[OBSERVERS_PATH]) {
instance[OBSERVERS_PATH] = {};
defineObservers(instance);
}

@@ -58,2 +70,2 @@ return instance[OBSERVERS_PATH];

return val && ("current" in val && "subscribe" in val && "unsubscribe" in val);
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc