@cfcs/core
Version:
Write once, create framework components that supports React, Vue, Svelte, and more.
11 lines (10 loc) • 9.5 kB
JavaScript
/*
Copyright (c) 2022-present NAVER Corp.
name: @cfcs/core
license: MIT
author: NAVER Crop.
repository: https://github.com/naver/cfcs/tree/main/packages/core
version: 0.1.0
*/
!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 g(t){return"function"==typeof t}var r="__observers__",i="__computed__",o=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 m(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[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 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}(),O=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.4",t}(),v=function(){function t(t){this._emitter=new O,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 x(this,e).current},set:function(t){x(this,e,t).current=t}}),e!==n&&Object.defineProperty(t,e,{configurable:!0,get:function(){return x(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],x(this,t).subscribe(n)},t.unsubscribe=function(t,n){var e=this;t?t in this&&x(this,t).unsubscribe(n):_(S(this)).forEach(function(t){e.unsubscribe(t)})}}function t(t){var e=g(t)?t():t,r={};return w(r),_(e).forEach(function(t){var n=e[t];P(n)?H(r,t,n):H(r,t,E(n)),b(t)(r,t)}),y(r),r}function E(t){return new v(t)}function j(t){return new p(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 w(t){var n={};return Object.defineProperty(t,r,{get:function(){return n}}),n}function S(n,t){n[r]||w(n);var e=n[r];return t||(t=null==(t=null==(t=null==n?void 0:n.constructor)?void 0:t.prototype)?void 0:t[i])&&t.forEach(function(t){!(t in e)&&t in n&&n[t]}),e}function x(t,n,e){t=S(t);return t[n]||(t[n]=E(e)),t[n]}function H(t,n,e){S(t)[n]=e}function P(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:g,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:w,getObservers:S,getObserver:x,setObserver:H,isObserver:P,isReactive:function(t){return t&&!P(t)&&"subscribe"in t&&"unsubscribe"in t},Observer:v,ComputedObserver:p,reactive:t,partialReactive:t,observe:E,computed:j,adaptReactive:function(t,n){var i=g(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){s.push(t)}function o(t){d=t}function u(t){b=t}function c(t){y=t}var f=new O,s=[],a=[],l=[],h=[],v={current:null},p=[],d=null,b=[],y=[];return i.setup&&(v.current=i.setup({getProps:r,setInitialState:o,setEvents:u,setMethods:c,onMounted:e,onDestroy:function(t){l.push(t)},onInit:function(t){a.push(t)},emit:function(t){for(var n=[],e=1;e<arguments.length;e++)n[e-1]=arguments[e];f.trigger.apply(f,m([t],n,!1))},on:function(t){h.push(t)}})||null),i.created&&(v.current=i.created(r())||null),i.events&&u(i.events),i.state&&o(i.state),i.methods&&c(i.methods),i.mounted&&e(i.mounted),i.destroy&&l.push(i.destroy),i.init&&a.push(i.init),i.on&&h.push(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 b},state:function(){var e,t=v.current;return d||(t&&(e=S(t),o(_(e).reduce(function(t,n){return t[n]=e[n].current,t},{}))),d||{})},instance:function(){return v.current},mounted:function(){var n=r();s.forEach(function(t){v.current=t(n,v.current)||v.current})},init:function(){var n=v.current,e=r();p=b.map(function(e){function n(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];f.trigger.apply(f,m([e],t,!1))}var r=v.current;return h.map(function(t){return t(r,e,n)}).filter(Boolean)}),a.forEach(function(t){t(n,e)})},destroy:function(){p.forEach(function(t){t.forEach(function(t){t()})}),f.off();var n=v.current,e=r();l.forEach(function(t){t(n,e)})},methods:function(){return C(v,y)},on:function(t,n){f.on(t,n)},off:function(t,n){f.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,e={configurable:!0,get:function(){var t=S(this,!0);return n in t||(t[n]=j(r.bind(this))),x(this,n).current}},o=(t[i]||(t[i]=[]),t[i]);return-1===o.indexOf(n)&&o.push(n),Object.defineProperty(t,n,e),e},injectReactiveSubscribe:y,ReactiveSubscribe:function(t){y(t.prototype)}}});
//# sourceMappingURL=cfcs.min.js.map