@cfcs/core
Advanced tools
Comparing version 0.0.15 to 0.0.17
import { ReactiveAdapter } from "./ReactiveAdapter"; | ||
import { ReactiveSubscribe } from "./decorators/ReactiveSubscribe"; | ||
import { ReactiveEventCallback } from "./types"; | ||
export declare function adaptReactive<Instance extends ReactiveSubscribe<Record<string, any>>, State extends Record<string, any> = {}, Methods extends keyof Partial<Instance> = any, Props = any, Events extends Record<string, any> = {}>(adapter: ReactiveAdapter<Instance, State, Methods, Props, Events>): { | ||
import { ReactiveEventCallback, ReactiveState } from "./types"; | ||
export declare function adaptReactive<Instance extends ReactiveSubscribe<Record<string, any>>, State extends Record<string, any> = ReactiveState<Instance>, Methods extends keyof Partial<Instance> = any, Props = any, Events extends Record<string, any> = {}>(adapter: ReactiveAdapter<Instance, State, Methods, Props, Events>): { | ||
state(): State; | ||
@@ -6,0 +6,0 @@ instance(): Instance; |
import { EventKey, EventTriggerParams } from "@egjs/component/declaration/types"; | ||
import { ReactiveEventCallback } from "./types"; | ||
import { ReactiveEventCallback, ReactiveState } from "./types"; | ||
import { ReactiveSubscribe } from "./decorators/ReactiveSubscribe"; | ||
export interface ReactiveLifecycle<Instance extends ReactiveSubscribe<Record<string, any>>, Props = any, Events extends Record<string, any> = {}> { | ||
export interface ReacitveSetup<Instance extends ReactiveSubscribe<Record<string, any>>, State extends Record<string, any> = ReactiveState<Instance>, Methods extends keyof Partial<Instance> = never, Props = any, Events extends Record<string, any> = {}> { | ||
/** | ||
* Set the first Initial state. | ||
* Do not set the return value if it exists. | ||
*/ | ||
setInitialState(state: State): void; | ||
/** | ||
* Sets the functions to be exposed in the instance. | ||
*/ | ||
setMethods(methods: readonly Methods[]): void; | ||
/** | ||
* Set the events to be exposed. | ||
*/ | ||
setEvents(events: readonly (keyof Events)[]): void; | ||
onMounted: (callback: (props: Props, instance?: Instance | null) => Instance | void) => void; | ||
@@ -11,3 +24,5 @@ onInit: (callback: (instance: Instance, props: Props) => void) => void; | ||
} | ||
export interface ReactiveAdapter<Instance extends ReactiveSubscribe<Record<string, any>>, State extends Record<string, any> = {}, Methods extends keyof Partial<Instance> = never, Props = any, Events extends Record<string, any> = {}> { | ||
export declare type ReactiveAdapter<Instance extends ReactiveSubscribe<Record<string, any>>, State extends Record<string, any> = ReactiveState<Instance>, Methods extends keyof Partial<Instance> = never, Props = any, Events extends Record<string, any> = {}> = ReactiveObjectAdapter<Instance, State, Methods, Props, Events>; | ||
export declare type ReactiveFunctionAdapter<Instance extends ReactiveSubscribe<Record<string, any>>, State extends Record<string, any> = ReactiveState<Instance>, Methods extends keyof Partial<Instance> = never, Props = any, Events extends Record<string, any> = {}> = (props: Props, setup: ReacitveSetup<Instance, State, Methods, Props, Events>) => Instance | undefined | void; | ||
export interface ReactiveObjectAdapter<Instance extends ReactiveSubscribe<Record<string, any>>, State extends Record<string, any> = ReactiveState<Instance>, Methods extends keyof Partial<Instance> = never, Props = any, Events extends Record<string, any> = {}> { | ||
/** | ||
@@ -19,2 +34,4 @@ * Specify if you want to use the framework's state. If an instance is created through created, it can be omitted. | ||
* If you want to use an instance's method, write the method names. | ||
* | ||
* @default [] | ||
*/ | ||
@@ -40,3 +57,3 @@ methods?: readonly Methods[]; | ||
*/ | ||
created?: (props: Props, lifecycle: ReactiveLifecycle<Instance, Props, Events>) => Instance | undefined | void; | ||
created?: (props: Props, setup: ReacitveSetup<Instance, State, Methods, Props, Events>) => Instance | undefined | void; | ||
/** | ||
@@ -43,0 +60,0 @@ * Occurs when a component is mounted. |
@@ -13,6 +13,7 @@ import { ComponentEvent } from "@egjs/component"; | ||
}; | ||
export declare type ReactiveState<Instance extends ReactiveSubscribe<object>> = Instance extends ReactiveSubscribe<infer State> ? State : 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; | ||
<Inst extends ReactiveSubscribe<object>, Name = keyof ReactiveState<Inst>>(inst: Inst, name: any): Name; | ||
} | ||
export {}; |
@@ -7,3 +7,3 @@ /* | ||
repository: https://github.com/naver/cfcs | ||
version: 0.0.15 | ||
version: 0.0.17 | ||
*/ | ||
@@ -470,7 +470,8 @@ 'use strict'; | ||
function getProps() { | ||
var _a, _b; | ||
var _a, _b, _c, _d; | ||
return (_b = (_a = adapter.props) === null || _a === void 0 ? void 0 : _a.call(adapter)) !== null && _b !== void 0 ? _b : {}; | ||
return (_d = (_b = (_a = adapter.props) === null || _a === void 0 ? void 0 : _a.call(adapter)) !== null && _b !== void 0 ? _b : (_c = adapter.data) === null || _c === void 0 ? void 0 : _c.call(adapter)) !== null && _d !== void 0 ? _d : {}; | ||
} | ||
var eventEmitter = new Component(); | ||
var mountedHooks = []; | ||
@@ -481,3 +482,3 @@ var initHooks = []; | ||
var offHooksList = []; | ||
var eventEmitter = new Component(); | ||
var initialState = null; | ||
@@ -510,4 +511,17 @@ var onMounted = function (callback) { | ||
var setInitialState = function (state) { | ||
initialState = state; | ||
}; | ||
var setEvents = function (events) { | ||
}; | ||
var setMethods = function (methods) { | ||
}; | ||
var instanceRef = { | ||
current: ((_a = adapter.created) === null || _a === void 0 ? void 0 : _a.call(adapter, getProps(), { | ||
setInitialState: setInitialState, | ||
setEvents: setEvents, | ||
setMethods: setMethods, | ||
onMounted: onMounted, | ||
@@ -520,4 +534,15 @@ onDestroy: onDestroy, | ||
}; | ||
var firstState = null; | ||
if (adapter.events) { | ||
setEvents(adapter.events); | ||
} | ||
if (adapter.state) { | ||
setInitialState(adapter.state); | ||
} | ||
if (adapter.methods) { | ||
setMethods(adapter.methods); | ||
} | ||
if (adapter.mounted) { | ||
@@ -550,17 +575,15 @@ onMounted(adapter.mounted); | ||
if (firstState) { | ||
return firstState; | ||
if (initialState) { | ||
return initialState; | ||
} | ||
if (adapter.state) { | ||
firstState = adapter.state; | ||
} else if (inst) { | ||
if (inst) { | ||
var observers_1 = getObservers(inst); | ||
firstState = keys(observers_1).reduce(function (prev, cur) { | ||
setInitialState(keys(observers_1).reduce(function (prev, cur) { | ||
prev[cur] = observers_1[cur].current; | ||
return prev; | ||
}, {}); | ||
}, {})); | ||
} | ||
return firstState || {}; | ||
return initialState || {}; | ||
}, | ||
@@ -567,0 +590,0 @@ instance: function () { |
@@ -7,3 +7,3 @@ /* | ||
repository: https://github.com/naver/cfcs | ||
version: 0.0.15 | ||
version: 0.0.17 | ||
*/ | ||
@@ -468,7 +468,8 @@ import Component from '@egjs/component'; | ||
function getProps() { | ||
var _a, _b; | ||
var _a, _b, _c, _d; | ||
return (_b = (_a = adapter.props) === null || _a === void 0 ? void 0 : _a.call(adapter)) !== null && _b !== void 0 ? _b : {}; | ||
return (_d = (_b = (_a = adapter.props) === null || _a === void 0 ? void 0 : _a.call(adapter)) !== null && _b !== void 0 ? _b : (_c = adapter.data) === null || _c === void 0 ? void 0 : _c.call(adapter)) !== null && _d !== void 0 ? _d : {}; | ||
} | ||
var eventEmitter = new Component(); | ||
var mountedHooks = []; | ||
@@ -479,3 +480,3 @@ var initHooks = []; | ||
var offHooksList = []; | ||
var eventEmitter = new Component(); | ||
var initialState = null; | ||
@@ -508,4 +509,17 @@ var onMounted = function (callback) { | ||
var setInitialState = function (state) { | ||
initialState = state; | ||
}; | ||
var setEvents = function (events) { | ||
}; | ||
var setMethods = function (methods) { | ||
}; | ||
var instanceRef = { | ||
current: ((_a = adapter.created) === null || _a === void 0 ? void 0 : _a.call(adapter, getProps(), { | ||
setInitialState: setInitialState, | ||
setEvents: setEvents, | ||
setMethods: setMethods, | ||
onMounted: onMounted, | ||
@@ -518,4 +532,15 @@ onDestroy: onDestroy, | ||
}; | ||
var firstState = null; | ||
if (adapter.events) { | ||
setEvents(adapter.events); | ||
} | ||
if (adapter.state) { | ||
setInitialState(adapter.state); | ||
} | ||
if (adapter.methods) { | ||
setMethods(adapter.methods); | ||
} | ||
if (adapter.mounted) { | ||
@@ -548,17 +573,15 @@ onMounted(adapter.mounted); | ||
if (firstState) { | ||
return firstState; | ||
if (initialState) { | ||
return initialState; | ||
} | ||
if (adapter.state) { | ||
firstState = adapter.state; | ||
} else if (inst) { | ||
if (inst) { | ||
var observers_1 = getObservers(inst); | ||
firstState = keys(observers_1).reduce(function (prev, cur) { | ||
setInitialState(keys(observers_1).reduce(function (prev, cur) { | ||
prev[cur] = observers_1[cur].current; | ||
return prev; | ||
}, {}); | ||
}, {})); | ||
} | ||
return firstState || {}; | ||
return initialState || {}; | ||
}, | ||
@@ -565,0 +588,0 @@ instance: function () { |
@@ -7,3 +7,3 @@ /* | ||
repository: https://github.com/naver/cfcs | ||
version: 0.0.15 | ||
version: 0.0.17 | ||
*/ | ||
@@ -872,7 +872,8 @@ (function (global, factory) { | ||
function getProps() { | ||
var _a, _b; | ||
var _a, _b, _c, _d; | ||
return (_b = (_a = adapter.props) === null || _a === void 0 ? void 0 : _a.call(adapter)) !== null && _b !== void 0 ? _b : {}; | ||
return (_d = (_b = (_a = adapter.props) === null || _a === void 0 ? void 0 : _a.call(adapter)) !== null && _b !== void 0 ? _b : (_c = adapter.data) === null || _c === void 0 ? void 0 : _c.call(adapter)) !== null && _d !== void 0 ? _d : {}; | ||
} | ||
var eventEmitter = new Component(); | ||
var mountedHooks = []; | ||
@@ -883,3 +884,3 @@ var initHooks = []; | ||
var offHooksList = []; | ||
var eventEmitter = new Component(); | ||
var initialState = null; | ||
@@ -912,4 +913,17 @@ var onMounted = function (callback) { | ||
var setInitialState = function (state) { | ||
initialState = state; | ||
}; | ||
var setEvents = function (events) { | ||
}; | ||
var setMethods = function (methods) { | ||
}; | ||
var instanceRef = { | ||
current: ((_a = adapter.created) === null || _a === void 0 ? void 0 : _a.call(adapter, getProps(), { | ||
setInitialState: setInitialState, | ||
setEvents: setEvents, | ||
setMethods: setMethods, | ||
onMounted: onMounted, | ||
@@ -922,4 +936,15 @@ onDestroy: onDestroy, | ||
}; | ||
var firstState = null; | ||
if (adapter.events) { | ||
setEvents(adapter.events); | ||
} | ||
if (adapter.state) { | ||
setInitialState(adapter.state); | ||
} | ||
if (adapter.methods) { | ||
setMethods(adapter.methods); | ||
} | ||
if (adapter.mounted) { | ||
@@ -952,17 +977,15 @@ onMounted(adapter.mounted); | ||
if (firstState) { | ||
return firstState; | ||
if (initialState) { | ||
return initialState; | ||
} | ||
if (adapter.state) { | ||
firstState = adapter.state; | ||
} else if (inst) { | ||
if (inst) { | ||
var observers_1 = getObservers(inst); | ||
firstState = keys(observers_1).reduce(function (prev, cur) { | ||
setInitialState(keys(observers_1).reduce(function (prev, cur) { | ||
prev[cur] = observers_1[cur].current; | ||
return prev; | ||
}, {}); | ||
}, {})); | ||
} | ||
return firstState || {}; | ||
return initialState || {}; | ||
}, | ||
@@ -969,0 +992,0 @@ instance: function () { |
@@ -7,5 +7,5 @@ /* | ||
repository: https://github.com/naver/cfcs | ||
version: 0.0.15 | ||
version: 0.0.17 | ||
*/ | ||
!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 d(t){return Object.keys(t)}function e(t){return"string"==typeof t}function n(t){return"object"==typeof t}function o(t){return"function"==typeof t}var r="__observers__",i=1,u="__CFCS_DETECTED_DEPENDENCIES__",c=function(t,n){return(c=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var e in n)Object.prototype.hasOwnProperty.call(n,e)&&(t[e]=n[e])})(t,n)};function b(t,n,e){if(e||2===arguments.length)for(var r,o=0,i=n.length;o<i;o++)!r&&o in n||((r=r||Array.prototype.slice.call(n,0,o))[o]=n[o]);return t.concat(r||Array.prototype.slice.call(n))}function f(){Object[u]=Object[u]||{};var t=Object[u];return t[i]=t[i]||[],t[i]}function a(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 s(){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,o,i=e.call(t),u=[];try{for(;(void 0===n||0<n--)&&!(r=i.next()).done;)u.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(e=i.return)&&e.call(i)}finally{if(o)throw o.error}}return u}(arguments[n]));return t}function l(t){return void 0===t}var h=function(){function t(t,n){var e,r;if(this._canceled=!1,n)try{for(var o=a(Object.keys(n)),i=o.next();!i.done;i=o.next()){var u=i.value;this[u]=n[u]}}catch(t){e={error:t}}finally{try{i&&!i.done&&(r=o.return)&&r.call(o)}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}(),y=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 h?n.eventType:n,r=s(this._eventHandler[r]||[]);return r.length<=0||(n instanceof h?(n.currentTarget=this,r.forEach(function(t){t(n)})):r.forEach(function(t){t.apply(void 0,s(e))})),this},n.once=function(e,r){var o,i=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&&(o=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];r.apply(void 0,s(t)),i.off(e,o)},this.on(e,o));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 o;"string"==typeof t&&"function"==typeof n&&(o=this._eventHandler[t],l(o)&&(this._eventHandler[t]=[],o=this._eventHandler[t]),o.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 o,i=t;for(o in i)this.off(o,i[o])}else{var u=this._eventHandler[t];if(u){var c=0;try{for(var f=a(u),s=f.next();!s.done;s=f.next()){if(s.value===n){u.splice(c,1),u.length<=0&&delete this._eventHandler[t];break}c++}}catch(t){e={error:t}}finally{try{s&&!s.done&&(r=f.return)&&r.call(f)}finally{if(e)throw e.error}}}}return this},t.VERSION="3.0.3",t}(),v=function(){function t(t){this._emitter=new y,this._current=t}var n=t.prototype;return Object.defineProperty(n,"current",{get:function(){var t=(t=f())[t.length-1];return null!=t&&t.push(this),this._current},set:function(t){this._setCurrent(t)},enumerable:!1,configurable:!0}),n.subscribe=function(t){return this.current,this._emitter.on("update",t),this},n.unsubscribe=function(t){return this._emitter.off("update",t),this},n._setCurrent=function(t){var n=this._current,e=t!==n;this._current=t,e&&this._emitter.trigger("update",t,n)},n.toString=function(){return"".concat(this.current)},n.valueOf=function(){return this.current},t}(),p=function(e){var t=o,n=e;if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=t}function o(t){var n=e.call(this)||this;return n._computedCallback=t,n._registered=[],n._onCheckUpdate=function(){n._setCurrent(n.current)},n._current=n.current,n}return c(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r),Object.defineProperty(o.prototype,"current",{get:function(){var n,e,r=this,t=(n=this,t=f(),o={host:n,observers:e=[],push:function(t){n!==t&&-1===e.indexOf(t)&&e.push(t)}},t.push(o),this._computedCallback()),o=f().pop();return this._registered.forEach(function(t){t.unsubscribe(r._onCheckUpdate)}),o.observers.forEach(function(t){t.subscribe(r._onCheckUpdate)}),this._registered=o.observers,t},enumerable:!1,configurable:!0}),o}(v);function _(t,n,e){void 0===e&&(e=n),Object.defineProperty(t,n,{configurable:!0,get:function(){return S(this,e).current},set:function(t){S(this,e,t).current=t}}),e!==n&&Object.defineProperty(t,e,{configurable:!0,get:function(){return S(this,e).current}})}function g(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return 1<e.length?_(e[0],e[1]):function(t,n){return _(t,n,e[0])}}function m(t){t.subscribe=function(t,n){this[t],S(this,t).subscribe(n)},t.unsubscribe=function(t,n){var e=this;t?t in this&&S(this,t).unsubscribe(n):d(w(this)).forEach(function(t){e.unsubscribe(t)})}}function t(t){var e=o(t)?t():t,r={};return C(r),d(e).forEach(function(t){var n=e[t];H(n)?x(r,t,n):x(r,t,E(n)),g(t)(r,t)}),m(r),r}function O(t){return new p(t)}function E(t){return new v(t)}function j(o,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=o.current||o.value;return e[r].apply(e,t)}}),n}function C(t){var n={};return Object.defineProperty(t,r,{get:function(){return n}}),n}function w(t){return t[r]||C(t),t[r]}function S(t,n,e){t=w(t);return t[n]||(t[n]=E(e)),t[n]}function x(t,n,e){w(t)[n]=e}function H(t){return t&&n(t)&&"current"in t&&"subscribe"in t&&"unsubscribe"in t}return{__proto__:null,keys:d,camelize:function(t){return t.replace(/[\s-_]([a-z])/g,function(t,n){return n.toUpperCase()})},isString:e,isObject:n,isFunction:o,findTarget:function(t){var n;return t?(e(t)?n=document.querySelector(t):t instanceof Element?n=t:("value"in t||"current"in t)&&(n=t.value||t.current),n):null},withClassMethods:function(n){return function(t,o){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[o])[r].apply(e,t);return e===this[o]?this:e})})}},withReactiveMethods:j,defineObservers:C,getObservers:w,getObserver:S,setObserver:x,isObserver:H,Observer:v,ComputedObserver:p,reactive:t,reactiveAll:t,computed:O,observe:E,adaptReactive:function(o){var t;function r(){var t;return null!=(t=null==(t=o.props)?void 0:t.call(o))?t:{}}function n(t){c.push(t)}function e(t){f.push(t)}function i(t){s.push(t)}function u(t){a.push(t)}var c=[],f=[],s=[],a=[],l=[],h=new y,v={current:(null==(t=o.created)?void 0:t.call(o,r(),{onMounted:n,onDestroy:i,onInit:e,emit:function(t){for(var n=[],e=1;e<arguments.length;e++)n[e-1]=arguments[e];h.trigger.apply(h,b([t],n,!1))},on:u}))||null},p=null;return o.mounted&&n(o.mounted),o.destroy&&i(o.destroy),o.init&&e(o.init),o.on&&u(function(n,e,r){return o.on(n,e,r),function(){var t;null!=(t=o.off)&&t.call(o,n,e,r)}}),{state:function(){var e,t=v.current;return p||(o.state?p=o.state:t&&(e=w(t),p=d(e).reduce(function(t,n){return t[n]=e[n].current,t},{})),p||{})},instance:function(){return v.current},mounted:function(){var n=r();c.forEach(function(t){v.current=t(n,v.current)||v.current})},init:function(){var t=o.events||[],n=v.current,e=r();l=t.map(function(e){function n(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];h.trigger.apply(h,b([e],t,!1))}var r=v.current;return a.map(function(t){return t(r,e,n)}).filter(Boolean)}),f.forEach(function(t){t(n,e)})},destroy:function(){l.forEach(function(t){t.forEach(function(t){t()})}),h.off();var n=v.current,e=r();s.forEach(function(t){t(n,e)})},methods:function(){return j(v,o.methods)},on:function(t,n){h.on(t,n)},off:function(t,n){h.off(t,n)}}},Observe:g,Reactive:function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return g.apply(void 0,t)},Computed:function(t,n,e){var r=e.get;return e={configurable:!0,get:function(){var t=w(this);return n in t||(t[n]=O(r.bind(this))),S(this,n).current}},Object.defineProperty(t,n,e),e},injectReactiveSubscribe:m,ReactiveSubscribe:function(t){m(t.prototype)}}}); | ||
!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 _(t){return Object.keys(t)}function e(t){return"string"==typeof t}function n(t){return"object"==typeof t}function o(t){return"function"==typeof t}var r="__observers__",i=1,u="__CFCS_DETECTED_DEPENDENCIES__",c=function(t,n){return(c=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var e in n)Object.prototype.hasOwnProperty.call(n,e)&&(t[e]=n[e])})(t,n)};function g(t,n,e){if(e||2===arguments.length)for(var r,o=0,i=n.length;o<i;o++)!r&&o in n||((r=r||Array.prototype.slice.call(n,0,o))[o]=n[o]);return t.concat(r||Array.prototype.slice.call(n))}function f(){Object[u]=Object[u]||{};var t=Object[u];return t[i]=t[i]||[],t[i]}function a(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 s(){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,o,i=e.call(t),u=[];try{for(;(void 0===n||0<n--)&&!(r=i.next()).done;)u.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(e=i.return)&&e.call(i)}finally{if(o)throw o.error}}return u}(arguments[n]));return t}function l(t){return void 0===t}var h=function(){function t(t,n){var e,r;if(this._canceled=!1,n)try{for(var o=a(Object.keys(n)),i=o.next();!i.done;i=o.next()){var u=i.value;this[u]=n[u]}}catch(t){e={error:t}}finally{try{i&&!i.done&&(r=o.return)&&r.call(o)}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}(),m=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 h?n.eventType:n,r=s(this._eventHandler[r]||[]);return r.length<=0||(n instanceof h?(n.currentTarget=this,r.forEach(function(t){t(n)})):r.forEach(function(t){t.apply(void 0,s(e))})),this},n.once=function(e,r){var o,i=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&&(o=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];r.apply(void 0,s(t)),i.off(e,o)},this.on(e,o));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 o;"string"==typeof t&&"function"==typeof n&&(o=this._eventHandler[t],l(o)&&(this._eventHandler[t]=[],o=this._eventHandler[t]),o.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 o,i=t;for(o in i)this.off(o,i[o])}else{var u=this._eventHandler[t];if(u){var c=0;try{for(var f=a(u),s=f.next();!s.done;s=f.next()){if(s.value===n){u.splice(c,1),u.length<=0&&delete this._eventHandler[t];break}c++}}catch(t){e={error:t}}finally{try{s&&!s.done&&(r=f.return)&&r.call(f)}finally{if(e)throw e.error}}}}return this},t.VERSION="3.0.3",t}(),v=function(){function t(t){this._emitter=new m,this._current=t}var n=t.prototype;return Object.defineProperty(n,"current",{get:function(){var t=(t=f())[t.length-1];return null!=t&&t.push(this),this._current},set:function(t){this._setCurrent(t)},enumerable:!1,configurable:!0}),n.subscribe=function(t){return this.current,this._emitter.on("update",t),this},n.unsubscribe=function(t){return this._emitter.off("update",t),this},n._setCurrent=function(t){var n=this._current,e=t!==n;this._current=t,e&&this._emitter.trigger("update",t,n)},n.toString=function(){return"".concat(this.current)},n.valueOf=function(){return this.current},t}(),p=function(e){var t=o,n=e;if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=t}function o(t){var n=e.call(this)||this;return n._computedCallback=t,n._registered=[],n._onCheckUpdate=function(){n._setCurrent(n.current)},n._current=n.current,n}return c(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r),Object.defineProperty(o.prototype,"current",{get:function(){var n,e,r=this,t=(n=this,t=f(),o={host:n,observers:e=[],push:function(t){n!==t&&-1===e.indexOf(t)&&e.push(t)}},t.push(o),this._computedCallback()),o=f().pop();return this._registered.forEach(function(t){t.unsubscribe(r._onCheckUpdate)}),o.observers.forEach(function(t){t.subscribe(r._onCheckUpdate)}),this._registered=o.observers,t},enumerable:!1,configurable:!0}),o}(v);function d(t,n,e){void 0===e&&(e=n),Object.defineProperty(t,n,{configurable:!0,get:function(){return S(this,e).current},set:function(t){S(this,e,t).current=t}}),e!==n&&Object.defineProperty(t,e,{configurable:!0,get:function(){return S(this,e).current}})}function b(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return 1<e.length?d(e[0],e[1]):function(t,n){return d(t,n,e[0])}}function y(t){t.subscribe=function(t,n){this[t],S(this,t).subscribe(n)},t.unsubscribe=function(t,n){var e=this;t?t in this&&S(this,t).unsubscribe(n):_(w(this)).forEach(function(t){e.unsubscribe(t)})}}function t(t){var e=o(t)?t():t,r={};return C(r),_(e).forEach(function(t){var n=e[t];H(n)?x(r,t,n):x(r,t,E(n)),b(t)(r,t)}),y(r),r}function O(t){return new p(t)}function E(t){return new v(t)}function j(o,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=o.current||o.value;return e[r].apply(e,t)}}),n}function C(t){var n={};return Object.defineProperty(t,r,{get:function(){return n}}),n}function w(t){return t[r]||C(t),t[r]}function S(t,n,e){t=w(t);return t[n]||(t[n]=E(e)),t[n]}function x(t,n,e){w(t)[n]=e}function H(t){return t&&n(t)&&"current"in t&&"subscribe"in t&&"unsubscribe"in t}return{__proto__:null,keys:_,camelize:function(t){return t.replace(/[\s-_]([a-z])/g,function(t,n){return n.toUpperCase()})},isString:e,isObject:n,isFunction:o,findTarget:function(t){var n;return t?(e(t)?n=document.querySelector(t):t instanceof Element?n=t:("value"in t||"current"in t)&&(n=t.value||t.current),n):null},withClassMethods:function(n){return function(t,o){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[o])[r].apply(e,t);return e===this[o]?this:e})})}},withReactiveMethods:j,defineObservers:C,getObservers:w,getObserver:S,setObserver:x,isObserver:H,Observer:v,ComputedObserver:p,reactive:t,reactiveAll:t,computed:O,observe:E,adaptReactive:function(o){var t;function r(){var t;return null!=(t=null!=(t=null==(t=o.props)?void 0:t.call(o))?t:null==(t=o.data)?void 0:t.call(o))?t:{}}function n(t){l.push(t)}function e(t){h.push(t)}function i(t){v.push(t)}function u(t){p.push(t)}function c(t){b=t}function f(t){}function s(t){}var a=new m,l=[],h=[],v=[],p=[],d=[],b=null,y={current:(null==(t=o.created)?void 0:t.call(o,r(),{setInitialState:c,setEvents:f,setMethods:s,onMounted:n,onDestroy:i,onInit:e,emit:function(t){for(var n=[],e=1;e<arguments.length;e++)n[e-1]=arguments[e];a.trigger.apply(a,g([t],n,!1))},on:u}))||null};return o.events&&o.events,o.state&&c(o.state),o.methods&&o.methods,o.mounted&&n(o.mounted),o.destroy&&i(o.destroy),o.init&&e(o.init),o.on&&u(function(n,e,r){return o.on(n,e,r),function(){var t;null!=(t=o.off)&&t.call(o,n,e,r)}}),{state:function(){var e,t=y.current;return b||(t&&(e=w(t),c(_(e).reduce(function(t,n){return t[n]=e[n].current,t},{}))),b||{})},instance:function(){return y.current},mounted:function(){var n=r();l.forEach(function(t){y.current=t(n,y.current)||y.current})},init:function(){var t=o.events||[],n=y.current,e=r();d=t.map(function(e){function n(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];a.trigger.apply(a,g([e],t,!1))}var r=y.current;return p.map(function(t){return t(r,e,n)}).filter(Boolean)}),h.forEach(function(t){t(n,e)})},destroy:function(){d.forEach(function(t){t.forEach(function(t){t()})}),a.off();var n=y.current,e=r();v.forEach(function(t){t(n,e)})},methods:function(){return j(y,o.methods)},on:function(t,n){a.on(t,n)},off:function(t,n){a.off(t,n)}}},Observe:b,Reactive:function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return b.apply(void 0,t)},Computed:function(t,n,e){var r=e.get;return e={configurable:!0,get:function(){var t=w(this);return n in t||(t[n]=O(r.bind(this))),S(this,n).current}},Object.defineProperty(t,n,e),e},injectReactiveSubscribe:y,ReactiveSubscribe:function(t){y(t.prototype)}}}); | ||
//# sourceMappingURL=cfcs.min.js.map |
{ | ||
"name": "@cfcs/core", | ||
"version": "0.0.15", | ||
"version": "0.0.17", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/cfcs.cjs.js", |
@@ -5,3 +5,3 @@ import { keys } from "../core"; | ||
import { ReactiveSubscribe } from "./decorators/ReactiveSubscribe"; | ||
import { ReactiveEventCallback } from "./types"; | ||
import { ReactiveEventCallback, ReactiveState } from "./types"; | ||
import { getObservers, withReactiveMethods } from "./utils"; | ||
@@ -11,6 +11,5 @@ import Component from "@egjs/component"; | ||
export function adaptReactive< | ||
Instance extends ReactiveSubscribe<Record<string, any>>, | ||
State extends Record<string, any> = {}, | ||
State extends Record<string, any> = ReactiveState<Instance>, | ||
Methods extends keyof Partial<Instance> = any, | ||
@@ -22,5 +21,6 @@ Props = any, | ||
function getProps(): Props { | ||
return adapter.props?.() ?? {} as Props; | ||
return adapter.props?.() ?? adapter.data?.() ?? {} as Props; | ||
} | ||
const eventEmitter = new Component<Events>(); | ||
const mountedHooks: Array<(props: Props, instance?: Instance | null) => Instance | void> = []; | ||
@@ -31,4 +31,6 @@ const initHooks: Array<(instance: Instance, props: Props) => void> = []; | ||
let offHooksList: Array<Array<() => void>> = []; | ||
let initialState: State | null = null; | ||
let eventNames: readonly (keyof Events)[] = []; | ||
let methodNames: readonly Methods[] = []; | ||
const eventEmitter = new Component<Events>(); | ||
@@ -51,7 +53,33 @@ const onMounted = (callback: (props: Props, instance?: Instance | null) => Instance | void) => { | ||
const instanceRef: Ref<Instance> = { current: adapter.created?.(getProps(), { onMounted, onDestroy, onInit, emit, on }) || null }; | ||
let firstState: State | null = null; | ||
const setInitialState = (state: State) => { | ||
initialState = state; | ||
}; | ||
const setEvents = (events: readonly (keyof Events)[]) => { | ||
eventNames = events; | ||
} | ||
const setMethods = (methods: readonly Methods[]) => { | ||
methodNames = methods; | ||
} | ||
const instanceRef: Ref<Instance> = { current: adapter.created?.(getProps(), { | ||
setInitialState, | ||
setEvents, | ||
setMethods, | ||
onMounted, | ||
onDestroy, | ||
onInit, | ||
emit, | ||
on, | ||
}) || null }; | ||
if (adapter.events) { | ||
setEvents(adapter.events); | ||
} | ||
if (adapter.state) { | ||
setInitialState(adapter.state); | ||
} | ||
if (adapter.methods) { | ||
setMethods(adapter.methods); | ||
} | ||
if (adapter.mounted) { | ||
@@ -80,16 +108,14 @@ onMounted(adapter.mounted); | ||
if (firstState) { | ||
return firstState; | ||
if (initialState) { | ||
return initialState; | ||
} | ||
if (adapter.state) { | ||
firstState = adapter.state; | ||
} else if (inst) { | ||
if (inst) { | ||
const observers = getObservers(inst); | ||
firstState = keys(observers).reduce((prev, cur) => { | ||
setInitialState(keys(observers).reduce((prev, cur) => { | ||
prev[cur] = observers[cur].current; | ||
return prev; | ||
}, {} as any); | ||
}, {} as any)); | ||
} | ||
return firstState || {} as State; | ||
return initialState || {} as State; | ||
}, | ||
@@ -112,2 +138,3 @@ instance() { | ||
const props = getProps(); | ||
offHooksList = events.map(eventName => { | ||
@@ -114,0 +141,0 @@ const listener = (...params: any[]) => { |
import { EventKey, EventTriggerParams } from "@egjs/component/declaration/types"; | ||
import { ReactiveEventCallback } from "./types"; | ||
import { ReactiveEventCallback, ReactiveState } from "./types"; | ||
import { ReactiveSubscribe } from "./decorators/ReactiveSubscribe"; | ||
export interface ReactiveLifecycle< | ||
export interface ReacitveSetup< | ||
Instance extends ReactiveSubscribe<Record<string, any>>, | ||
State extends Record<string, any> = ReactiveState<Instance>, | ||
Methods extends keyof Partial<Instance> = never, | ||
Props = any, | ||
Events extends Record<string, any> = {}, | ||
> { | ||
/** | ||
* Set the first Initial state. | ||
* Do not set the return value if it exists. | ||
*/ | ||
setInitialState(state: State): void; | ||
/** | ||
* Sets the functions to be exposed in the instance. | ||
*/ | ||
setMethods(methods: readonly Methods[]): void; | ||
/** | ||
* Set the events to be exposed. | ||
*/ | ||
setEvents(events: readonly (keyof Events)[]): void; | ||
onMounted: (callback: (props: Props, instance?: Instance | null) => Instance | void) => void; | ||
@@ -16,8 +31,24 @@ onInit: (callback: (instance: Instance, props: Props) => void) => void; | ||
} | ||
export interface ReactiveAdapter< | ||
export type ReactiveAdapter< | ||
Instance extends ReactiveSubscribe<Record<string, any>>, | ||
State extends Record<string, any> = {}, | ||
State extends Record<string, any> = ReactiveState<Instance>, | ||
Methods extends keyof Partial<Instance> = never, | ||
Props = any, | ||
Events extends Record<string, any> = {} | ||
> = ReactiveObjectAdapter<Instance, State, Methods, Props, Events>; | ||
export type ReactiveFunctionAdapter< | ||
Instance extends ReactiveSubscribe<Record<string, any>>, | ||
State extends Record<string, any> = ReactiveState<Instance>, | ||
Methods extends keyof Partial<Instance> = never, | ||
Props = any, | ||
Events extends Record<string, any> = {}, | ||
> = (props: Props, setup: ReacitveSetup<Instance, State, Methods, Props, Events>) => Instance | undefined | void; | ||
export interface ReactiveObjectAdapter< | ||
Instance extends ReactiveSubscribe<Record<string, any>>, | ||
State extends Record<string, any> = ReactiveState<Instance>, | ||
Methods extends keyof Partial<Instance> = never, | ||
Props = any, | ||
Events extends Record<string, any> = {}, | ||
> { | ||
@@ -30,2 +61,4 @@ /** | ||
* If you want to use an instance's method, write the method names. | ||
* | ||
* @default [] | ||
*/ | ||
@@ -51,3 +84,3 @@ methods?: readonly Methods[]; | ||
*/ | ||
created?: (props: Props, lifecycle: ReactiveLifecycle<Instance, Props, Events>) => Instance | undefined | void; | ||
created?: (props: Props, setup: ReacitveSetup<Instance, State, Methods, Props, Events>) => Instance | undefined | void; | ||
/** | ||
@@ -54,0 +87,0 @@ * Occurs when a component is mounted. |
@@ -29,6 +29,7 @@ import { ComponentEvent } from "@egjs/component"; | ||
export type ReactiveState<Instance extends ReactiveSubscribe<object>> = Instance extends ReactiveSubscribe<infer State> ? State : 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; | ||
<Inst extends ReactiveSubscribe<object>, Name = keyof ReactiveState<Inst>>(inst: Inst, name: any): Name; | ||
} |
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
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
284377
3415