@cfcs/core
Advanced tools
Comparing version 0.0.17 to 0.0.18
import { ReactiveAdapter } from "./ReactiveAdapter"; | ||
import { ReactiveSubscribe } from "./decorators/ReactiveSubscribe"; | ||
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>): { | ||
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>, props?: () => Props): { | ||
events: () => readonly (keyof Events)[]; | ||
state(): State; | ||
@@ -6,0 +7,0 @@ instance(): Instance; |
@@ -6,2 +6,6 @@ import { EventKey, EventTriggerParams } from "@egjs/component/declaration/types"; | ||
/** | ||
* Get external props. | ||
*/ | ||
getProps(): Props; | ||
/** | ||
* Set the first Initial state. | ||
@@ -25,4 +29,4 @@ * Do not set the return value if it exists. | ||
} | ||
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 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> | ReactiveFunctionAdapter<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> = {}> = (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> = {}> { | ||
@@ -53,2 +57,7 @@ /** | ||
/** | ||
* You can use lifecycle hooks functions for setup. | ||
* You can omit created, mounted, and init. | ||
*/ | ||
setup?: (setup: ReacitveSetup<Instance, State, Methods, Props, Events>) => Instance | undefined | void; | ||
/** | ||
* Occurs when a component is created. | ||
@@ -58,3 +67,3 @@ * If an instance is created and returned, the state specification can be omitted. | ||
*/ | ||
created?: (props: Props, setup: ReacitveSetup<Instance, State, Methods, Props, Events>) => Instance | undefined | void; | ||
created?: (props: Props) => Instance | undefined | void; | ||
/** | ||
@@ -79,4 +88,5 @@ * Occurs when a component is mounted. | ||
* If you want to use events, write the events property and a way to register/unregister events. | ||
* @deprecated | ||
*/ | ||
off?: <EventName extends EventKey<Events>>(instance: Instance, eventName: EventName, listener: ReactiveEventCallback<Events, EventName>) => void; | ||
} |
@@ -7,3 +7,3 @@ /* | ||
repository: https://github.com/naver/cfcs | ||
version: 0.0.17 | ||
version: 0.0.18 | ||
*/ | ||
@@ -466,9 +466,11 @@ 'use strict'; | ||
function adaptReactive(adapter) { | ||
var _a; | ||
function adaptReactive(adapter, props) { | ||
var objectAdapter = isFunction(adapter) ? { | ||
setup: adapter | ||
} : adapter; | ||
function getProps() { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c, _d, _e; | ||
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 : {}; | ||
return (_e = (_c = (_a = props === null || props === void 0 ? void 0 : props()) !== null && _a !== void 0 ? _a : (_b = objectAdapter.props) === null || _b === void 0 ? void 0 : _b.call(objectAdapter)) !== null && _c !== void 0 ? _c : (_d = objectAdapter.data) === null || _d === void 0 ? void 0 : _d.call(objectAdapter)) !== null && _e !== void 0 ? _e : {}; | ||
} | ||
@@ -481,4 +483,8 @@ | ||
var onHooks = []; | ||
var instanceRef = { | ||
current: null | ||
}; | ||
var offHooksList = []; | ||
var initialState = null; | ||
var eventNames = []; | ||
@@ -516,2 +522,3 @@ var onMounted = function (callback) { | ||
var setEvents = function (events) { | ||
eventNames = events; | ||
}; | ||
@@ -522,4 +529,5 @@ | ||
var instanceRef = { | ||
current: ((_a = adapter.created) === null || _a === void 0 ? void 0 : _a.call(adapter, getProps(), { | ||
if (objectAdapter.setup) { | ||
instanceRef.current = objectAdapter.setup({ | ||
getProps: getProps, | ||
setInitialState: setInitialState, | ||
@@ -533,36 +541,41 @@ setEvents: setEvents, | ||
on: on | ||
})) || null | ||
}; | ||
}) || null; | ||
} | ||
if (adapter.events) { | ||
setEvents(adapter.events); | ||
if (objectAdapter.created) { | ||
instanceRef.current = objectAdapter.created(getProps()) || null; | ||
} | ||
if (adapter.state) { | ||
setInitialState(adapter.state); | ||
if (objectAdapter.events) { | ||
setEvents(objectAdapter.events); | ||
} | ||
if (adapter.methods) { | ||
setMethods(adapter.methods); | ||
if (objectAdapter.state) { | ||
setInitialState(objectAdapter.state); | ||
} | ||
if (adapter.mounted) { | ||
onMounted(adapter.mounted); | ||
if (objectAdapter.methods) { | ||
setMethods(objectAdapter.methods); | ||
} | ||
if (adapter.destroy) { | ||
onDestroy(adapter.destroy); | ||
if (objectAdapter.mounted) { | ||
onMounted(objectAdapter.mounted); | ||
} | ||
if (adapter.init) { | ||
onInit(adapter.init); | ||
if (objectAdapter.destroy) { | ||
onDestroy(objectAdapter.destroy); | ||
} | ||
if (adapter.on) { | ||
if (objectAdapter.init) { | ||
onInit(objectAdapter.init); | ||
} | ||
if (objectAdapter.on) { | ||
on(function (instance, eventName, listener) { | ||
adapter.on(instance, eventName, listener); | ||
var off = objectAdapter.on(instance, eventName, listener); | ||
return function () { | ||
var _a; | ||
(_a = adapter.off) === null || _a === void 0 ? void 0 : _a.call(adapter, instance, eventName, listener); | ||
off && off(); | ||
(_a = objectAdapter.off) === null || _a === void 0 ? void 0 : _a.call(objectAdapter, instance, eventName, listener); | ||
}; | ||
@@ -573,2 +586,5 @@ }); | ||
return { | ||
events: function () { | ||
return eventNames; | ||
}, | ||
state: function () { | ||
@@ -602,6 +618,5 @@ var inst = instanceRef.current; | ||
// on events | ||
var events = adapter.events || []; | ||
var instance = instanceRef.current; | ||
var props = getProps(); | ||
offHooksList = events.map(function (eventName) { | ||
offHooksList = eventNames.map(function (eventName) { | ||
var listener = function () { | ||
@@ -645,3 +660,3 @@ var _a; | ||
methods: function () { | ||
return withReactiveMethods(instanceRef, adapter.methods); | ||
return withReactiveMethods(instanceRef, objectAdapter.methods); | ||
}, | ||
@@ -648,0 +663,0 @@ on: function (eventName, listener) { |
@@ -7,3 +7,3 @@ /* | ||
repository: https://github.com/naver/cfcs | ||
version: 0.0.17 | ||
version: 0.0.18 | ||
*/ | ||
@@ -464,9 +464,11 @@ import Component from '@egjs/component'; | ||
function adaptReactive(adapter) { | ||
var _a; | ||
function adaptReactive(adapter, props) { | ||
var objectAdapter = isFunction(adapter) ? { | ||
setup: adapter | ||
} : adapter; | ||
function getProps() { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c, _d, _e; | ||
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 : {}; | ||
return (_e = (_c = (_a = props === null || props === void 0 ? void 0 : props()) !== null && _a !== void 0 ? _a : (_b = objectAdapter.props) === null || _b === void 0 ? void 0 : _b.call(objectAdapter)) !== null && _c !== void 0 ? _c : (_d = objectAdapter.data) === null || _d === void 0 ? void 0 : _d.call(objectAdapter)) !== null && _e !== void 0 ? _e : {}; | ||
} | ||
@@ -479,4 +481,8 @@ | ||
var onHooks = []; | ||
var instanceRef = { | ||
current: null | ||
}; | ||
var offHooksList = []; | ||
var initialState = null; | ||
var eventNames = []; | ||
@@ -514,2 +520,3 @@ var onMounted = function (callback) { | ||
var setEvents = function (events) { | ||
eventNames = events; | ||
}; | ||
@@ -520,4 +527,5 @@ | ||
var instanceRef = { | ||
current: ((_a = adapter.created) === null || _a === void 0 ? void 0 : _a.call(adapter, getProps(), { | ||
if (objectAdapter.setup) { | ||
instanceRef.current = objectAdapter.setup({ | ||
getProps: getProps, | ||
setInitialState: setInitialState, | ||
@@ -531,36 +539,41 @@ setEvents: setEvents, | ||
on: on | ||
})) || null | ||
}; | ||
}) || null; | ||
} | ||
if (adapter.events) { | ||
setEvents(adapter.events); | ||
if (objectAdapter.created) { | ||
instanceRef.current = objectAdapter.created(getProps()) || null; | ||
} | ||
if (adapter.state) { | ||
setInitialState(adapter.state); | ||
if (objectAdapter.events) { | ||
setEvents(objectAdapter.events); | ||
} | ||
if (adapter.methods) { | ||
setMethods(adapter.methods); | ||
if (objectAdapter.state) { | ||
setInitialState(objectAdapter.state); | ||
} | ||
if (adapter.mounted) { | ||
onMounted(adapter.mounted); | ||
if (objectAdapter.methods) { | ||
setMethods(objectAdapter.methods); | ||
} | ||
if (adapter.destroy) { | ||
onDestroy(adapter.destroy); | ||
if (objectAdapter.mounted) { | ||
onMounted(objectAdapter.mounted); | ||
} | ||
if (adapter.init) { | ||
onInit(adapter.init); | ||
if (objectAdapter.destroy) { | ||
onDestroy(objectAdapter.destroy); | ||
} | ||
if (adapter.on) { | ||
if (objectAdapter.init) { | ||
onInit(objectAdapter.init); | ||
} | ||
if (objectAdapter.on) { | ||
on(function (instance, eventName, listener) { | ||
adapter.on(instance, eventName, listener); | ||
var off = objectAdapter.on(instance, eventName, listener); | ||
return function () { | ||
var _a; | ||
(_a = adapter.off) === null || _a === void 0 ? void 0 : _a.call(adapter, instance, eventName, listener); | ||
off && off(); | ||
(_a = objectAdapter.off) === null || _a === void 0 ? void 0 : _a.call(objectAdapter, instance, eventName, listener); | ||
}; | ||
@@ -571,2 +584,5 @@ }); | ||
return { | ||
events: function () { | ||
return eventNames; | ||
}, | ||
state: function () { | ||
@@ -600,6 +616,5 @@ var inst = instanceRef.current; | ||
// on events | ||
var events = adapter.events || []; | ||
var instance = instanceRef.current; | ||
var props = getProps(); | ||
offHooksList = events.map(function (eventName) { | ||
offHooksList = eventNames.map(function (eventName) { | ||
var listener = function () { | ||
@@ -643,3 +658,3 @@ var _a; | ||
methods: function () { | ||
return withReactiveMethods(instanceRef, adapter.methods); | ||
return withReactiveMethods(instanceRef, objectAdapter.methods); | ||
}, | ||
@@ -646,0 +661,0 @@ on: function (eventName, listener) { |
@@ -7,3 +7,3 @@ /* | ||
repository: https://github.com/naver/cfcs | ||
version: 0.0.17 | ||
version: 0.0.18 | ||
*/ | ||
@@ -868,9 +868,11 @@ (function (global, factory) { | ||
function adaptReactive(adapter) { | ||
var _a; | ||
function adaptReactive(adapter, props) { | ||
var objectAdapter = isFunction(adapter) ? { | ||
setup: adapter | ||
} : adapter; | ||
function getProps() { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c, _d, _e; | ||
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 : {}; | ||
return (_e = (_c = (_a = props === null || props === void 0 ? void 0 : props()) !== null && _a !== void 0 ? _a : (_b = objectAdapter.props) === null || _b === void 0 ? void 0 : _b.call(objectAdapter)) !== null && _c !== void 0 ? _c : (_d = objectAdapter.data) === null || _d === void 0 ? void 0 : _d.call(objectAdapter)) !== null && _e !== void 0 ? _e : {}; | ||
} | ||
@@ -883,4 +885,8 @@ | ||
var onHooks = []; | ||
var instanceRef = { | ||
current: null | ||
}; | ||
var offHooksList = []; | ||
var initialState = null; | ||
var eventNames = []; | ||
@@ -918,2 +924,3 @@ var onMounted = function (callback) { | ||
var setEvents = function (events) { | ||
eventNames = events; | ||
}; | ||
@@ -924,4 +931,5 @@ | ||
var instanceRef = { | ||
current: ((_a = adapter.created) === null || _a === void 0 ? void 0 : _a.call(adapter, getProps(), { | ||
if (objectAdapter.setup) { | ||
instanceRef.current = objectAdapter.setup({ | ||
getProps: getProps, | ||
setInitialState: setInitialState, | ||
@@ -935,36 +943,41 @@ setEvents: setEvents, | ||
on: on | ||
})) || null | ||
}; | ||
}) || null; | ||
} | ||
if (adapter.events) { | ||
setEvents(adapter.events); | ||
if (objectAdapter.created) { | ||
instanceRef.current = objectAdapter.created(getProps()) || null; | ||
} | ||
if (adapter.state) { | ||
setInitialState(adapter.state); | ||
if (objectAdapter.events) { | ||
setEvents(objectAdapter.events); | ||
} | ||
if (adapter.methods) { | ||
setMethods(adapter.methods); | ||
if (objectAdapter.state) { | ||
setInitialState(objectAdapter.state); | ||
} | ||
if (adapter.mounted) { | ||
onMounted(adapter.mounted); | ||
if (objectAdapter.methods) { | ||
setMethods(objectAdapter.methods); | ||
} | ||
if (adapter.destroy) { | ||
onDestroy(adapter.destroy); | ||
if (objectAdapter.mounted) { | ||
onMounted(objectAdapter.mounted); | ||
} | ||
if (adapter.init) { | ||
onInit(adapter.init); | ||
if (objectAdapter.destroy) { | ||
onDestroy(objectAdapter.destroy); | ||
} | ||
if (adapter.on) { | ||
if (objectAdapter.init) { | ||
onInit(objectAdapter.init); | ||
} | ||
if (objectAdapter.on) { | ||
on(function (instance, eventName, listener) { | ||
adapter.on(instance, eventName, listener); | ||
var off = objectAdapter.on(instance, eventName, listener); | ||
return function () { | ||
var _a; | ||
(_a = adapter.off) === null || _a === void 0 ? void 0 : _a.call(adapter, instance, eventName, listener); | ||
off && off(); | ||
(_a = objectAdapter.off) === null || _a === void 0 ? void 0 : _a.call(objectAdapter, instance, eventName, listener); | ||
}; | ||
@@ -975,2 +988,5 @@ }); | ||
return { | ||
events: function () { | ||
return eventNames; | ||
}, | ||
state: function () { | ||
@@ -1004,6 +1020,5 @@ var inst = instanceRef.current; | ||
// on events | ||
var events = adapter.events || []; | ||
var instance = instanceRef.current; | ||
var props = getProps(); | ||
offHooksList = events.map(function (eventName) { | ||
offHooksList = eventNames.map(function (eventName) { | ||
var listener = function () { | ||
@@ -1047,3 +1062,3 @@ var _a; | ||
methods: function () { | ||
return withReactiveMethods(instanceRef, adapter.methods); | ||
return withReactiveMethods(instanceRef, objectAdapter.methods); | ||
}, | ||
@@ -1050,0 +1065,0 @@ on: function (eventName, listener) { |
@@ -7,5 +7,5 @@ /* | ||
repository: https://github.com/naver/cfcs | ||
version: 0.0.17 | ||
version: 0.0.18 | ||
*/ | ||
!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)}}}); | ||
!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 m(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__",o=1,i="__CFCS_DETECTED_DEPENDENCIES__",u=function(t,n){return(u=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 E(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 c(){Object[i]=Object[i]||{};var t=Object[i];return t[o]=t[o]||[],t[o]}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 f(){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 s=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}(),j=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 s?n.eventType:n,r=f(this._eventHandler[r]||[]);return r.length<=0||(n instanceof s?(n.currentTarget=this,r.forEach(function(t){t(n)})):r.forEach(function(t){t.apply(void 0,f(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,f(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}(),h=function(){function t(t){this._emitter=new j,this._current=t}var n=t.prototype;return Object.defineProperty(n,"current",{get:function(){var t=(t=c())[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}(),v=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 u(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=c(),o={host:n,observers:e=[],push:function(t){n!==t&&-1===e.indexOf(t)&&e.push(t)}},t.push(o),this._computedCallback()),o=c().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}(h);function p(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 d(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return 1<e.length?p(e[0],e[1]):function(t,n){return p(t,n,e[0])}}function b(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):m(w(this)).forEach(function(t){e.unsubscribe(t)})}}function t(t){var e=O(t)?t():t,r={};return g(r),m(e).forEach(function(t){var n=e[t];H(n)?x(r,t,n):x(r,t,_(n)),d(t)(r,t)}),b(r),r}function y(t){return new v(t)}function _(t){return new h(t)}function C(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 g(t){var n={};return Object.defineProperty(t,r,{get:function(){return n}}),n}function w(t){return t[r]||g(t),t[r]}function S(t,n,e){t=w(t);return t[n]||(t[n]=_(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:m,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:C,defineObservers:g,getObservers:w,getObserver:S,setObserver:x,isObserver:H,Observer:h,ComputedObserver:v,reactive:t,reactiveAll:t,computed:y,observe:_,adaptReactive:function(t,n){var i=O(t)?{setup:t}:t;function r(){var t;return null!=(t=null!=(t=null!=(t=null==n?void 0:n())?t:null==(t=i.props)?void 0:t.call(i))?t:null==(t=i.data)?void 0:t.call(i))?t:{}}function e(t){h.push(t)}function o(t){v.push(t)}function u(t){p.push(t)}function c(t){d.push(t)}function f(t){_=t}function s(t){g=t}function a(t){}var l=new j,h=[],v=[],p=[],d=[],b={current:null},y=[],_=null,g=[];return i.setup&&(b.current=i.setup({getProps:r,setInitialState:f,setEvents:s,setMethods:a,onMounted:e,onDestroy:u,onInit:o,emit:function(t){for(var n=[],e=1;e<arguments.length;e++)n[e-1]=arguments[e];l.trigger.apply(l,E([t],n,!1))},on:c})||null),i.created&&(b.current=i.created(r())||null),i.events&&s(i.events),i.state&&f(i.state),i.methods&&i.methods,i.mounted&&e(i.mounted),i.destroy&&u(i.destroy),i.init&&o(i.init),i.on&&c(function(n,e,r){var o=i.on(n,e,r);return function(){var t;o&&o(),null!=(t=i.off)&&t.call(i,n,e,r)}}),{events:function(){return g},state:function(){var e,t=b.current;return _||(t&&(e=w(t),f(m(e).reduce(function(t,n){return t[n]=e[n].current,t},{}))),_||{})},instance:function(){return b.current},mounted:function(){var n=r();h.forEach(function(t){b.current=t(n,b.current)||b.current})},init:function(){var n=b.current,e=r();y=g.map(function(e){function n(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];l.trigger.apply(l,E([e],t,!1))}var r=b.current;return d.map(function(t){return t(r,e,n)}).filter(Boolean)}),v.forEach(function(t){t(n,e)})},destroy:function(){y.forEach(function(t){t.forEach(function(t){t()})}),l.off();var n=b.current,e=r();p.forEach(function(t){t(n,e)})},methods:function(){return C(b,i.methods)},on:function(t,n){l.on(t,n)},off:function(t,n){l.off(t,n)}}},Observe:d,Reactive:function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return d.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]=y(r.bind(this))),S(this,n).current}},Object.defineProperty(t,n,e),e},injectReactiveSubscribe:b,ReactiveSubscribe:function(t){b(t.prototype)}}}); | ||
//# sourceMappingURL=cfcs.min.js.map |
{ | ||
"name": "@cfcs/core", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/cfcs.cjs.js", |
@@ -1,4 +0,4 @@ | ||
import { keys } from "../core"; | ||
import { isFunction, keys } from "../core"; | ||
import { Ref } from "../core/types"; | ||
import { ReactiveAdapter } from "./ReactiveAdapter"; | ||
import { ReactiveAdapter, ReactiveObjectAdapter } from "./ReactiveAdapter"; | ||
import { ReactiveSubscribe } from "./decorators/ReactiveSubscribe"; | ||
@@ -16,6 +16,9 @@ import { ReactiveEventCallback, ReactiveState } from "./types"; | ||
Events extends Record<string, any> = {}, | ||
>(adapter: ReactiveAdapter<Instance, State, Methods, Props, Events>) { | ||
>(adapter: ReactiveAdapter<Instance, State, Methods, Props, Events>, props?: () => Props) { | ||
const objectAdapter: ReactiveObjectAdapter<Instance, State, Methods, Props, Events> = isFunction(adapter) ? { | ||
setup: adapter, | ||
} : adapter; | ||
function getProps(): Props { | ||
return adapter.props?.() ?? adapter.data?.() ?? {} as Props; | ||
return props?.() ?? objectAdapter.props?.() ?? objectAdapter.data?.() ?? {} as Props; | ||
} | ||
@@ -28,2 +31,3 @@ | ||
const onHooks: Array<<EventName extends EventKey<Events>>(instance: Instance, eventName: EventName, listener: ReactiveEventCallback<Events, EventName>) => void | (() => void)> = []; | ||
const instanceRef: Ref<Instance> = { current: null }; | ||
let offHooksList: Array<Array<() => void>> = []; | ||
@@ -61,38 +65,45 @@ let initialState: State | null = null; | ||
const instanceRef: Ref<Instance> = { current: adapter.created?.(getProps(), { | ||
setInitialState, | ||
setEvents, | ||
setMethods, | ||
onMounted, | ||
onDestroy, | ||
onInit, | ||
emit, | ||
on, | ||
}) || null }; | ||
if (objectAdapter.setup) { | ||
instanceRef.current = objectAdapter.setup({ | ||
getProps, | ||
setInitialState, | ||
setEvents, | ||
setMethods, | ||
onMounted, | ||
onDestroy, | ||
onInit, | ||
emit, | ||
on, | ||
}) || null; | ||
} | ||
if (objectAdapter.created) { | ||
instanceRef.current = objectAdapter.created(getProps()) || null; | ||
} | ||
if (adapter.events) { | ||
setEvents(adapter.events); | ||
if (objectAdapter.events) { | ||
setEvents(objectAdapter.events); | ||
} | ||
if (adapter.state) { | ||
setInitialState(adapter.state); | ||
if (objectAdapter.state) { | ||
setInitialState(objectAdapter.state); | ||
} | ||
if (adapter.methods) { | ||
setMethods(adapter.methods); | ||
if (objectAdapter.methods) { | ||
setMethods(objectAdapter.methods); | ||
} | ||
if (adapter.mounted) { | ||
onMounted(adapter.mounted); | ||
if (objectAdapter.mounted) { | ||
onMounted(objectAdapter.mounted); | ||
} | ||
if (adapter.destroy) { | ||
onDestroy(adapter.destroy); | ||
if (objectAdapter.destroy) { | ||
onDestroy(objectAdapter.destroy); | ||
} | ||
if (adapter.init) { | ||
onInit(adapter.init); | ||
if (objectAdapter.init) { | ||
onInit(objectAdapter.init); | ||
} | ||
if (adapter.on) { | ||
if (objectAdapter.on) { | ||
on((instance, eventName, listener) => { | ||
adapter.on!(instance, eventName, listener); | ||
const off = objectAdapter.on!(instance, eventName, listener); | ||
return () => { | ||
adapter.off?.(instance, eventName, listener); | ||
off && off(); | ||
objectAdapter.off?.(instance, eventName, listener); | ||
}; | ||
@@ -103,2 +114,3 @@ }); | ||
return { | ||
events: () => eventNames, | ||
state(): State { | ||
@@ -133,7 +145,6 @@ const inst = instanceRef.current; | ||
// on events | ||
const events = (adapter.events || []) as string[]; | ||
const instance = instanceRef.current!; | ||
const props = getProps(); | ||
offHooksList = events.map(eventName => { | ||
offHooksList = (eventNames as string[]).map(eventName => { | ||
const listener = (...params: any[]) => { | ||
@@ -171,3 +182,3 @@ (eventEmitter as any).trigger(eventName, ...params); | ||
methods() { | ||
return withReactiveMethods<any, any, any>(instanceRef, adapter.methods); | ||
return withReactiveMethods<any, any, any>(instanceRef, objectAdapter.methods); | ||
}, | ||
@@ -174,0 +185,0 @@ on(eventName: string, listener: ReactiveEventCallback<any, any>) { |
@@ -13,2 +13,6 @@ import { EventKey, EventTriggerParams } from "@egjs/component/declaration/types"; | ||
/** | ||
* Get external props. | ||
*/ | ||
getProps(): Props; | ||
/** | ||
* Set the first Initial state. | ||
@@ -38,3 +42,4 @@ * Do not set the return value if it exists. | ||
Events extends Record<string, any> = {} | ||
> = ReactiveObjectAdapter<Instance, State, Methods, Props, Events>; | ||
> = ReactiveObjectAdapter<Instance, State, Methods, Props, Events> | ||
| ReactiveFunctionAdapter<Instance, State, Methods, Props, Events>; | ||
@@ -47,3 +52,3 @@ export type ReactiveFunctionAdapter< | ||
Events extends Record<string, any> = {}, | ||
> = (props: Props, setup: ReacitveSetup<Instance, State, Methods, Props, Events>) => Instance | undefined | void; | ||
> = (setup: ReacitveSetup<Instance, State, Methods, Props, Events>) => Instance | undefined | void; | ||
@@ -81,2 +86,7 @@ export interface ReactiveObjectAdapter< | ||
/** | ||
* You can use lifecycle hooks functions for setup. | ||
* You can omit created, mounted, and init. | ||
*/ | ||
setup?: (setup: ReacitveSetup<Instance, State, Methods, Props, Events>) => Instance | undefined | void; | ||
/** | ||
* Occurs when a component is created. | ||
@@ -86,3 +96,3 @@ * If an instance is created and returned, the state specification can be omitted. | ||
*/ | ||
created?: (props: Props, setup: ReacitveSetup<Instance, State, Methods, Props, Events>) => Instance | undefined | void; | ||
created?: (props: Props) => Instance | undefined | void; | ||
/** | ||
@@ -107,4 +117,5 @@ * Occurs when a component is mounted. | ||
* If you want to use events, write the events property and a way to register/unregister events. | ||
* @deprecated | ||
*/ | ||
off?: <EventName extends EventKey<Events>>(instance: Instance, eventName: EventName, listener: ReactiveEventCallback<Events, EventName>) => void; | ||
} |
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
291444
3494