rumious 0.0.13-beta.2 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/dist/app/app.d.ts +13 -0
- package/dist/app/index.d.ts +2 -0
- package/dist/app/module.d.ts +6 -0
- package/dist/component/component.d.ts +29 -0
- package/dist/component/element.d.ts +17 -0
- package/dist/component/index.d.ts +2 -0
- package/dist/context/context.d.ts +12 -0
- package/dist/context/index.d.ts +1 -0
- package/dist/index.cjs +1100 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.esm.js +1072 -0
- package/dist/index.global.d.ts +41 -0
- package/dist/index.min.js +2658 -1
- package/dist/jsx/index.d.ts +3 -0
- package/dist/performance/index.d.ts +5 -0
- package/dist/ref/children.d.ts +23 -0
- package/dist/ref/element.d.ts +49 -0
- package/dist/ref/index.d.ts +2 -0
- package/dist/render/array.d.ts +20 -0
- package/dist/render/context.d.ts +11 -0
- package/dist/render/directives.d.ts +1 -0
- package/dist/render/dynamic.d.ts +2 -0
- package/dist/render/index.d.ts +3 -0
- package/dist/render/injector.d.ts +14 -0
- package/dist/render/render.d.ts +3 -0
- package/dist/render/template.d.ts +5 -0
- package/dist/state/array.d.ts +21 -0
- package/dist/state/index.d.ts +3 -0
- package/dist/state/object.d.ts +31 -0
- package/dist/state/reactor.d.ts +19 -0
- package/dist/state/state.d.ts +15 -0
- package/dist/types/jsx.d.ts +8 -0
- package/dist/types/render.d.ts +5 -0
- package/dist/types/utils.d.ts +1 -0
- package/dist/utils/checkers.d.ts +1 -0
- package/dist/utils/name.d.ts +2 -0
- package/dist/utils/oberve.d.ts +1 -0
- package/package.json +49 -31
- package/dist/index.cjs.min.js +0 -1
- package/dist/index.esm.min.js +0 -1
- package/index.js +0 -3
package/dist/index.min.js
CHANGED
@@ -1 +1,2658 @@
|
|
1
|
-
var Rumious=function(e){"use strict";class t{constructor(e){this.target=e,this.app=e.app,this.cleans=[],this.hooks={onRendered:[],onBeforeRender:[]}}find(e){return this.target[e]}get(e){return this.target[e]}addHook(e,t){if(!this.hooks[e])throw new Error(`Hook type "${e}" is not defined!`);this.hooks[e].push(t)}async runHooks(e,...t){if(this.hooks[e])for(const n of this.hooks[e])await n(...t)}}class n{constructor(){this.renderOptions={mode:"linear"},this.element=null,this.app=null,this.props={},this.forwardRefs={},this.renderContext=new t(this),this.renderer=null,this.wrapped=null}prepare(e,t,n={},r=null){this.element=e,this.props=t,this.renderer=r,this.wrapped=n,this.renderContext.app=this.app}template(){return{}}render(e){return this.renderer(e,document.createDocumentFragment(),this.renderContext)}async requestRender(){await this.onBeforeRender();let e=this.template(),t=this.renderer(e,document.createDocumentFragment(),this.renderContext);this.element.appendChild(t),this.onRender()}async requestCleanUp(){if(this.renderContext.cleans.forEach((e=>e())),this.element){let e=this.element.cloneNode(!0);for(this.element.replaceWith(e),this.element=e;this.element.firstChild;)this.element.removeChild(this.element.firstChild)}}onInit(){}onCreate(){}async onBeforeRender(){}onRender(){}onUpdate(){}onDestroy(){}}function r(e){return Object.getPrototypeOf(e)===n}class i{constructor(e,t,n=[]){this.type=e,this.props=t,this.children=n}}class s{constructor(e){this.type="ELEMENT_LIST",this.children=e}forEach(e){this.children.forEach(e)}}function o(e){return"function"==typeof e}class a{constructor(e,t,n){this.type=e,this.name=t,this.value=n}init(){console.warn("Directive haven't handler !")}}class l extends a{async init(e,t){let n=null;const r=e=>{if(!t.find(e))return console.warn(`Missing event handler for event: ${this.name}`),null;const n=t.get(e);if(!o(n))throw new Error(`${e} is not a function!`);return n.bind(t.target)};if("string"===this.value.type)n=r(this.value.value);else if("expression"===this.value.type){if(n=this.value.value,!o(n))throw new Error("Event handler requires a function!")}else"dynamic_value"===this.value.type&&(n=r(this.value.value.objectName));n&&e.addEventListener(this.name,n)}}class c extends a{async init(e,t){if("expression"===this.value.type){let t=this.value.value;if(!t)throw new Error("Invalid props value: Directive bind requires a RumiousState Object!");t.reactor.addBinding((()=>{let n=t.get();this.bindAttr(e,n)})),this.bindAttr(e,t.get())}else if("dynamic_value"===this.value.type){let n=this.value.evaluator?.(t.target),r=t.find(this.value.value.objectName);if(!r)throw new Error("Invalid props value: Directive bind requires a RumiousState Object!");r.reactor.addBinding((()=>{let n=this.value.evaluator?.(t.target);this.bindAttr(e,n)})),this.bindAttr(e,n)}}normalizeValue(e){return"object"==typeof e?e.toString?.()??"":e}async bindAttr(e,t){switch(this.name){case"html":e.innerHTML=this.normalizeValue(t);break;case"text":e.textContent=this.normalizeValue(t);break;case"show":e.style.display=this.normalizeValue(t)?"block":"none";break;case"hide":e.style.display=this.normalizeValue(t)?"none":"block";break;case"className":e.className=this.normalizeValue(t);break;case"style":Object.assign(e.style,t);break;case"disabled":e.disabled=!!this.normalizeValue(t);break;case"visible":e.style.visibility=this.normalizeValue(t)?"visible":"hidden";break;case"value":e.value=this.normalizeValue(t);break;case"checked":e.checked=this.normalizeValue(t);break;default:e.setAttribute(this.name,this.normalizeValue(t))}}}class u extends a{async init(e,t){if("expression"===this.value.type){let t=this.value.value;if(!t)throw new Error("Invalid props value: Directive ref requires a RumiousRef Object!");t.set(e)}else if("dynamic_value"===this.value.type){let n=t.find(this.value.value.objectName);if(!n)throw new Error("Invalid props value: Directive ref requires a RumiousRef Object!");n.set(e)}}}class h extends a{async init(e,t){let n;"expression"===this.value.type?n=this.value.value:"dynamic_value"===this.value.type&&(n=t.find(this.value.value.objectName));const r=()=>{"checkbox"===e.type?n.set(e.checked):"radio"===e.type?e.checked&&n.set(e.value):n.set(e.value)};e.addEventListener("input",r),e.addEventListener("change",r)}}class p extends a{async init(e,t){if("expression"===this.value.type){let t=this.value.value;if(!t)throw new Error("Invalid props value: Directive childsRef requires a RumiousChildRef Object!");t.set(e)}else if("dynamic_value"===this.value.type){let n=t.find(this.value.value.objectName);if(!n)throw new Error("Invalid props value: Directive childsRef requires a RumiousChildRef Object!");n.set(e)}}}const f={on:(e,t)=>new l("on",e,t),bind:(e,t)=>new c("bind",e,t),ref:(e,t)=>new u("ref",e,t),childsRef:(e,t)=>new p("childsRef",e,t),model:(e,t)=>new h("model",e,t)};function d(e,t,n){return function(e,t,n){return f[e]?.(t,n)}(e,t,n)}function y(e,t,...n){return r(e)?g(e,t,n):e===m?m(...n):new i(e,t||{},b(n))}function v(e){return new i("TEXT_ELEMENT",{nodeValue:e},[])}function g(e,t,n){let r=new i("COMPONENT",{...t},new s(b(n)));return r.component=e,r}function m(...e){return new i("FRAGMENT",{},b(e))}function b(e){return e.map((e=>"object"==typeof e?e:v(e)))}window.RUMIOUS_JSX_SUPPORT={createElement:y,createFragment:m,createDirective:d};class w extends HTMLElement{constructor(){super(),this.instance=null,this.ref=null}cleanUp(){Object.setPrototypeOf(this,HTMLUnknownElement.prototype),this.remove()}init(e,t,n={},r,i){this.instance=new e,this.instance.element=this,this.instance.app=i,this.instance.prepare(this,t,n,r),this.instance.onInit()}connectedCallback(){switch(this.instance.renderOptions.mode){case"linear":this.instance.onCreate(),this.instance.requestRender();break;case"async":(async()=>{this.instance.onCreate(),this.instance.requestRender()})();break;case"animate":requestAnimationFrame((async()=>{this.instance.onCreate(),this.instance.requestRender()}));break;case"idle":requestIdleCallback((async()=>{this.instance.onCreate(),this.instance.requestRender()}));break;case"defer":setTimeout((async()=>{this.instance.onCreate(),this.instance.requestRender()}),0);break;case"delay":setTimeout((async()=>{this.instance.onCreate(),this.instance.requestRender()}),this.instance.renderOptions.time);break;case"visible":new IntersectionObserver(((e,t)=>{for(const n of e)n.isIntersecting&&(t.disconnect(),this.instance.onCreate(),this.instance.requestRender())})).observe(this)}}disconnectedCallback(){this.instance.onDestroy(),this.instance.requestCleanUp(),this.cleanUp()}}function E(e="r-component"){return window.customElements.get(e)||window.customElements.define(e,class extends w{static tag=e}),document.createElement(e)}class O{constructor(){this.contents=[],this.targets=[],this.observers=new WeakMap}commit(e=[]){this.contents=e}setTarget(e,t,n){this.targets.push({element:e,context:n,renderer:t}),this.observeTarget(e)}observeTarget(e){if(this.observers.has(e))return;const t=new MutationObserver((()=>{e.parentNode||this.removeTarget(e)}));t.observe(document.body,{childList:!0}),this.observers.set(e,t)}removeTarget(e){const t=this.observers.get(e);t&&(t.disconnect(),this.observers.delete(e)),this.targets=this.targets.filter((t=>t.element!==e))}inject(e=!1){for(let t of this.targets)e&&(t.element.textContent=""),this.contents.forEach((e=>{"html"===e.type?t.element.innerHTML=e.value:"text"===e.type?t.element.textContent=e.value:"component"===e.type&&t.renderer(y(e.value,e.props??{},e.child??{}),t.element,t.context)}))}}class k{constructor(e,t){this.state=e,this.template=t,this.target=null,this._id=Date.now()}setTarget(e,t,n){this.target={element:e,context:n,renderer:t}}clean(){this.target&&delete this.state.reactor.priorityBinding[this._id]}sync(){this.target&&(this.renderAll(),this.state.reactor.priorityBinding[this._id]=this.onChange.bind(this))}renderAll(){this.target.element.textContent="",this.state.value.forEach(((e,t)=>this.appendElement(e,t)))}appendElement(e,t){let n=this.template(e,t),r=this.target.renderer(n,document.createDocumentFragment(),this.target.context);this.target.element.appendChild(r)}onChange(e){if(this.target)switch(e.type){case"REMOVE_ELEMENT":{let t=this.target.element.children[e.index];t&&this.target.element.removeChild(t);break}case"SET_VALUE":this.renderAll();break;case"ADD_ELEMENT":this.appendElement(e.value,e.index);break;case"SET_ELEMENT":{let t=this.template(e.value,e.index),n=this.target.renderer(t,document.createDocumentFragment(),this.target.context);!function(e,t,n){let r=e.children;t>=0&&t<r.length?e.replaceChild(n,r[t]):e.appendChild(n)}(this.target.element,e.index,n);break}case"INSERT_ELEMENT":{let t=this.template(e.value,e.index),n=this.target.renderer(t,document.createDocumentFragment(),this.target.context),r=this.target.element.children;e.index>=r.length?this.target.element.appendChild(n):this.target.element.insertBefore(n.children[0],r[e.index]);break}}}}function M(e,t,n={}){if(!e)return t;if(null===(r=e)||"object"!=typeof r&&"function"!=typeof r)return M(v(e),t,n);var r;if(Array.isArray(e)){for(const r of e)M(r,t,n);return t}if(e instanceof i){let r;switch(e.type){case"COMPONENT":return function(e,t,n,r){const i=function(e,t,n,r,i=null){const s=E(e.tag??"r-component");return s.init(e,t,n,r,i?.app),s}(e.component,e.props,e.children,n,r);return t.appendChild(i),t}(e,t,M,n);case"FRAGMENT":case"ELEMENT_LIST":for(const r of e.children)M(r,t,n);return t;case"TEXT_ELEMENT":r=function(e){return document.createTextNode(e.props.nodeValue)}(e);break;default:r=function(e,t){const n=document.createElement(e.type);!function(e,t,n){for(const[r,i]of Object.entries(t||{}))i instanceof a?i.init(e,n):r.startsWith("on")&&/^[a-zA-Z]+([A-Z][a-z]*)*$/.test(r)?e.addEventListener(r.substring(2).toLowerCase(),i):e.setAttribute(r,i)}(n,e.props,t);for(const r of e.children)M(r,n,t);return n}(e,n)}return t.appendChild(r),t}if(e instanceof HTMLElement)return t.appendChild(e),t;if(e instanceof O){if(t instanceof HTMLDocument)throw"Rumious Render: Unsupported inject content in HTMLDocument!";return e.setTarget(t,M,n),e.inject(!0),t}if(e instanceof k){if(t instanceof HTMLDocument)throw"Rumious Render: Unsupported sync data of array in HTMLDocument!";return n.cleans.push((()=>{e.clean()})),e.setTarget(t,M,n),e.sync(),t}return M(v(JSON.stringify(e)),t,n)}class R{constructor(e=null,t=[]){this.target=e,this.bindings=t,this.priorityBinding={}}react(e){Object.keys(this.priorityBinding).forEach((t=>this.priorityBinding[t]?.(e))),this.bindings.forEach((t=>t?.(e)))}removeBinding(e){for(let t=this.bindings.length-1;t>=0;t--)this.bindings[t]===e&&this.bindings.splice(t,1)}addBinding(e){this.bindings.push(e)}}class j{constructor(e,t="",n){this.state=e,this.path=t,this.fn=n}trigger(...e){let t=this.fn(...e);this.state.set(this.path,t)}}const S="remove",x="replace",T="add",C=Symbol.for("__MUTATIVE_PROXY_DRAFT__"),A=Symbol("__MUTATIVE_RAW_RETURN_SYMBOL__"),D=Symbol.iterator,P={mutable:"mutable",immutable:"immutable"},z={};function L(e,t){return e instanceof Map?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function _(e,t){if(t in e){let n=Reflect.getPrototypeOf(e);for(;n;){const e=Reflect.getOwnPropertyDescriptor(n,t);if(e)return e;n=Reflect.getPrototypeOf(n)}}}function N(e){return Object.getPrototypeOf(e)===Set.prototype}function I(e){return Object.getPrototypeOf(e)===Map.prototype}function V(e){var t;return null!==(t=e.copy)&&void 0!==t?t:e.original}function U(e){return!!q(e)}function q(e){return"object"!=typeof e?null:null==e?void 0:e[C]}function F(e){var t;const n=q(e);return n?null!==(t=n.copy)&&void 0!==t?t:n.original:e}function B(e,t){if(!e||"object"!=typeof e)return!1;let n;return Object.getPrototypeOf(e)===Object.prototype||Array.isArray(e)||e instanceof Map||e instanceof Set||!!(null==t?void 0:t.mark)&&((n=t.mark(e,P))===P.immutable||"function"==typeof n)}function H(e,t=[]){if(Object.hasOwnProperty.call(e,"key")){const n=e.parent.copy,r=q(W(n,e.key));if(null!==r&&(null==r?void 0:r.original)!==e.original)return null;const i=3===e.parent.type,s=i?Array.from(e.parent.setMap.keys()).indexOf(e.key):e.key;if(!(i&&n.size>s||L(n,s)))return null;t.push(s)}if(e.parent)return H(e.parent,t);t.reverse();try{!function(e,t){for(let n=0;n<t.length-1;n+=1){const r=t[n];if("object"!=typeof(e=W(3===$(e)?Array.from(e):e,r)))throw new Error(`Cannot resolve patch at '${t.join("/")}'.`)}}(e.copy,t)}catch(e){return null}return t}function $(e){return Array.isArray(e)?1:e instanceof Map?2:e instanceof Set?3:0}function W(e,t){return 2===$(e)?e.get(t):e[t]}function X(e,t,n){2===$(e)?e.set(t,n):e[t]=n}function K(e,t){const n=q(e);return(n?V(n):e)[t]}function G(e,t){return e===t?0!==e||1/e==1/t:e!=e&&t!=t}function Y(e){if(e)for(;e.finalities.revoke.length>0;)e.finalities.revoke.pop()()}function Z(e,t){return t?e:[""].concat(e).map((e=>{const t=`${e}`;return-1===t.indexOf("/")&&-1===t.indexOf("~")?t:t.replace(/~/g,"~0").replace(/\//g,"~1")})).join("/")}const J=Object.prototype.propertyIsEnumerable;function Q(e,t){let n;if(Array.isArray(e))return Array.prototype.concat.call(e);if(e instanceof Set)return N(e)?Set.prototype.difference?Set.prototype.difference.call(e,new Set):new Set(e.values()):new(0,Object.getPrototypeOf(e).constructor)(e.values());if(e instanceof Map)return I(e)?new Map(e):new(0,Object.getPrototypeOf(e).constructor)(e);if((null==t?void 0:t.mark)&&(n=t.mark(e,P),void 0!==n)&&n!==P.mutable){if(n===P.immutable)return function(e){const t=Object.create(Object.getPrototypeOf(e));return Reflect.ownKeys(e).forEach((n=>{let r=Reflect.getOwnPropertyDescriptor(e,n);r.enumerable&&r.configurable&&r.writable?t[n]=e[n]:(r.writable||(r.writable=!0,r.configurable=!0),(r.get||r.set)&&(r={configurable:!0,writable:!0,enumerable:r.enumerable,value:e[n]}),Reflect.defineProperty(t,n,r))})),t}(e);if("function"==typeof n){if(t.enablePatches||t.enableAutoFreeze)throw new Error("You can't use mark and patches or auto freeze together.");return n()}throw new Error(`Unsupported mark result: ${n}`)}if("object"==typeof e&&Object.getPrototypeOf(e)===Object.prototype){const t={};return Object.keys(e).forEach((n=>{t[n]=e[n]})),Object.getOwnPropertySymbols(e).forEach((n=>{J.call(e,n)&&(t[n]=e[n])})),t}throw new Error("Please check mark() to ensure that it is a stable marker draftable function.")}function ee(e){e.copy||(e.copy=Q(e.original,e.options))}function te(e){if(!B(e))return F(e);if(Array.isArray(e))return e.map(te);if(e instanceof Map){const t=Array.from(e.entries()).map((([e,t])=>[e,te(t)]));return I(e)?new Map(t):new(0,Object.getPrototypeOf(e).constructor)(t)}if(e instanceof Set){const t=Array.from(e).map(te);return N(e)?new Set(t):new(0,Object.getPrototypeOf(e).constructor)(t)}const t=Object.create(Object.getPrototypeOf(e));for(const n in e)t[n]=te(e[n]);return t}function ne(e){return U(e)?te(e):e}function re(e){var t;e.assignedMap=null!==(t=e.assignedMap)&&void 0!==t?t:new Map,e.operated||(e.operated=!0,e.parent&&re(e.parent))}function ie(){throw new Error("Cannot modify frozen object")}function se(e,t,n,r,i){{n=null!=n?n:new WeakMap,r=null!=r?r:[],i=null!=i?i:[];const s=n.has(e)?n.get(e):e;if(r.length>0){const e=r.indexOf(s);if(s&&"object"==typeof s&&-1!==e){if(r[0]===s)throw new Error("Forbids circular reference");throw new Error(`Forbids circular reference: ~/${i.slice(0,e).map(((e,t)=>{if("symbol"==typeof e)return`[${e.toString()}]`;const n=r[t];return"object"==typeof e&&(n instanceof Map||n instanceof Set)?Array.from(n.keys()).indexOf(e):e})).join("/")}`)}r.push(s),i.push(t)}else r.push(s)}if(Object.isFrozen(e)||U(e))return r.pop(),void i.pop();switch($(e)){case 2:for(const[t,s]of e)se(t,t,n,r,i),se(s,t,n,r,i);e.set=e.clear=e.delete=ie;break;case 3:for(const t of e)se(t,t,n,r,i);e.add=e.clear=e.delete=ie;break;case 1:Object.freeze(e);let t=0;for(const s of e)se(s,t,n,r,i),t+=1;break;default:Object.freeze(e),Object.keys(e).forEach((t=>{se(e[t],t,n,r,i)}))}r.pop(),i.pop()}function oe(e,t){const n=$(e);if(0===n)Reflect.ownKeys(e).forEach((n=>{t(n,e[n],e)}));else if(1===n){let n=0;for(const r of e)t(n,r,e),n+=1}else e.forEach(((n,r)=>t(r,n,e)))}function ae(e,t,n){if(U(e)||!B(e,n)||t.has(e)||Object.isFrozen(e))return;const r=e instanceof Set,i=r?new Map:void 0;if(t.add(e),oe(e,((s,o)=>{var a;if(U(o)){const t=q(o);ee(t);const n=(null===(a=t.assignedMap)||void 0===a?void 0:a.size)||t.operated?t.copy:t.original;X(r?i:e,s,n)}else ae(o,t,n)})),i){const t=e,n=Array.from(t);t.clear(),n.forEach((e=>{t.add(i.has(e)?i.get(e):e)}))}}function le(e,t){const n=3===e.type?e.setMap:e.copy;e.finalities.revoke.length>1&&e.assignedMap.get(t)&&n&&ae(W(n,t),e.finalities.handledSet,e.options)}function ce(e){3===e.type&&e.copy&&(e.copy.clear(),e.setMap.forEach((t=>{e.copy.add(F(t))})))}function ue(e,t,n,r){if(e.operated&&e.assignedMap&&e.assignedMap.size>0&&!e.finalized){if(n&&r){const i=H(e);i&&t(e,i,n,r)}e.finalized=!0}}function he(e,t,n,r){const i=q(n);i&&(i.callbacks||(i.callbacks=[]),i.callbacks.push(((s,o)=>{var a;const l=3===e.type?e.setMap:e.copy;if(G(W(l,t),n)){let n=i.original;i.copy&&(n=i.copy),ce(e),ue(e,r,s,o),e.options.enableAutoFreeze&&(e.options.updatedValues=null!==(a=e.options.updatedValues)&&void 0!==a?a:new WeakMap,e.options.updatedValues.set(n,i.original)),X(l,t,n)}})),e.options.enableAutoFreeze&&i.finalities!==e.finalities&&(e.options.enableAutoFreeze=!1)),B(n,e.options)&&e.finalities.draft.push((()=>{G(W(3===e.type?e.setMap:e.copy,t),n)&&le(e,t)}))}function pe(e,t,n,r){const{pathAsArray:i=!0}=e.options.enablePatches;switch(e.type){case 0:case 2:return function({original:e,copy:t,assignedMap:n},r,i,s,o){n.forEach(((n,a)=>{const l=W(e,a),c=ne(W(t,a)),u=n?L(e,a)?x:T:S;if(G(l,c)&&u===x)return;const h=Z(r.concat(a),o);i.push(u===S?{op:u,path:h}:{op:u,path:h,value:c}),s.push(u===T?{op:S,path:h}:u===S?{op:T,path:h,value:l}:{op:x,path:h,value:l})}))}(e,t,n,r,i);case 1:return function(e,t,n,r,i){let{original:s,assignedMap:o,options:a}=e,l=e.copy;l.length<s.length&&([s,l]=[l,s],[n,r]=[r,n]);for(let e=0;e<s.length;e+=1)if(o.get(e.toString())&&l[e]!==s[e]){const o=Z(t.concat([e]),i);n.push({op:x,path:o,value:ne(l[e])}),r.push({op:x,path:o,value:ne(s[e])})}for(let e=s.length;e<l.length;e+=1){const r=Z(t.concat([e]),i);n.push({op:T,path:r,value:ne(l[e])})}if(s.length<l.length){const{arrayLengthAssignment:e=!0}=a.enablePatches;if(e){const e=Z(t.concat(["length"]),i);r.push({op:x,path:e,value:s.length})}else for(let e=l.length;s.length<e;e-=1){const n=Z(t.concat([e-1]),i);r.push({op:S,path:n})}}}(e,t,n,r,i);case 3:return function({original:e,copy:t},n,r,i,s){let o=0;e.forEach((e=>{if(!t.has(e)){const t=Z(n.concat([o]),s);r.push({op:S,path:t,value:e}),i.unshift({op:T,path:t,value:e})}o+=1})),o=0,t.forEach((t=>{if(!e.has(t)){const e=Z(n.concat([o]),s);r.push({op:T,path:e,value:t}),i.unshift({op:S,path:e,value:t})}o+=1}))}(e,t,n,r,i)}}const fe=(e,t,n=!1)=>{if("object"==typeof e&&null!==e&&(!B(e,t)||n))throw new Error("Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap.")},de={get size(){return V(q(this)).size},has(e){return V(q(this)).has(e)},set(e,t){const n=q(this),r=V(n);return r.has(e)&&G(r.get(e),t)||(ee(n),re(n),n.assignedMap.set(e,!0),n.copy.set(e,t),he(n,e,t,pe)),this},delete(e){if(!this.has(e))return!1;const t=q(this);return ee(t),re(t),t.original.has(e)?t.assignedMap.set(e,!1):t.assignedMap.delete(e),t.copy.delete(e),!0},clear(){const e=q(this);if(this.size){ee(e),re(e),e.assignedMap=new Map;for(const[t]of e.original)e.assignedMap.set(t,!1);e.copy.clear()}},forEach(e,t){V(q(this)).forEach(((n,r)=>{e.call(t,this.get(r),r,this)}))},get(e){var t,n;const r=q(this),i=V(r).get(e),s=(null===(n=(t=r.options).mark)||void 0===n?void 0:n.call(t,i,P))===P.mutable;if(r.options.strict&&fe(i,r.options,s),s)return i;if(r.finalized||!B(i,r.options))return i;if(i!==r.original.get(e))return i;const o=z.createDraft({original:i,parentDraft:r,key:e,finalities:r.finalities,options:r.options});return ee(r),r.copy.set(e,o),o},keys(){return V(q(this)).keys()},values(){const e=this.keys();return{[D]:()=>this.values(),next:()=>{const t=e.next();return t.done?t:{done:!1,value:this.get(t.value)}}}},entries(){const e=this.keys();return{[D]:()=>this.entries(),next:()=>{const t=e.next();if(t.done)return t;const n=this.get(t.value);return{done:!1,value:[t.value,n]}}}},[D](){return this.entries()}},ye=Reflect.ownKeys(de),ve=(e,t,{isValuesIterator:n})=>()=>{var r,i;const s=t.next();if(s.done)return s;const o=s.value;let a=e.setMap.get(o);const l=q(a),c=(null===(i=(r=e.options).mark)||void 0===i?void 0:i.call(r,a,P))===P.mutable;if(e.options.strict&&fe(o,e.options,c),c||l||!B(o,e.options)||e.finalized||!e.original.has(o))l&&(a=l.proxy);else{const t=z.createDraft({original:o,parentDraft:e,key:o,finalities:e.finalities,options:e.options});e.setMap.set(o,t),a=t}return{done:!1,value:n?a:[a,a]}},ge={get size(){return q(this).setMap.size},has(e){const t=q(this);if(t.setMap.has(e))return!0;ee(t);const n=q(e);return!(!n||!t.setMap.has(n.original))},add(e){const t=q(this);return this.has(e)||(ee(t),re(t),t.assignedMap.set(e,!0),t.setMap.set(e,e),he(t,e,e,pe)),this},delete(e){if(!this.has(e))return!1;const t=q(this);ee(t),re(t);const n=q(e);return n&&t.setMap.has(n.original)?(t.assignedMap.set(n.original,!1),t.setMap.delete(n.original)):(!n&&t.setMap.has(e)?t.assignedMap.set(e,!1):t.assignedMap.delete(e),t.setMap.delete(e))},clear(){if(!this.size)return;const e=q(this);ee(e),re(e);for(const t of e.original)e.assignedMap.set(t,!1);e.setMap.clear()},values(){const e=q(this);ee(e);const t=e.setMap.keys();return{[Symbol.iterator]:()=>this.values(),next:ve(e,t,{isValuesIterator:!0})}},entries(){const e=q(this);ee(e);const t=e.setMap.keys();return{[Symbol.iterator]:()=>this.entries(),next:ve(e,t,{isValuesIterator:!1})}},keys(){return this.values()},[D](){return this.values()},forEach(e,t){const n=this.values();let r=n.next();for(;!r.done;)e.call(t,r.value,r.value,this),r=n.next()}};Set.prototype.difference&&Object.assign(ge,{intersection(e){return Set.prototype.intersection.call(new Set(this.values()),e)},union(e){return Set.prototype.union.call(new Set(this.values()),e)},difference(e){return Set.prototype.difference.call(new Set(this.values()),e)},symmetricDifference(e){return Set.prototype.symmetricDifference.call(new Set(this.values()),e)},isSubsetOf(e){return Set.prototype.isSubsetOf.call(new Set(this.values()),e)},isSupersetOf(e){return Set.prototype.isSupersetOf.call(new Set(this.values()),e)},isDisjointFrom(e){return Set.prototype.isDisjointFrom.call(new Set(this.values()),e)}});const me=Reflect.ownKeys(ge),be=new WeakSet,we={get(e,t,n){var r,i;const s=null===(r=e.copy)||void 0===r?void 0:r[t];if(s&&be.has(s))return s;if(t===C)return e;let o;if(e.options.mark){const r="size"===t&&(e.original instanceof Map||e.original instanceof Set)?Reflect.get(e.original,t):Reflect.get(e.original,t,n);if(o=e.options.mark(r,P),o===P.mutable)return e.options.strict&&fe(r,e.options,!0),r}const a=V(e);if(a instanceof Map&&ye.includes(t)){if("size"===t)return Object.getOwnPropertyDescriptor(de,"size").get.call(e.proxy);const n=de[t];if(n)return n.bind(e.proxy)}if(a instanceof Set&&me.includes(t)){if("size"===t)return Object.getOwnPropertyDescriptor(ge,"size").get.call(e.proxy);const n=ge[t];if(n)return n.bind(e.proxy)}if(!L(a,t)){const n=_(a,t);return n?"value"in n?n.value:null===(i=n.get)||void 0===i?void 0:i.call(e.proxy):void 0}const l=a[t];if(e.options.strict&&fe(l,e.options),e.finalized||!B(l,e.options))return l;if(l===K(e.original,t)){if(ee(e),e.copy[t]=Ee({original:e.original[t],parentDraft:e,key:1===e.type?Number(t):t,finalities:e.finalities,options:e.options}),"function"==typeof o){const n=q(e.copy[t]);return ee(n),re(n),n.copy}return e.copy[t]}return l},set(e,t,n){var r;if(3===e.type||2===e.type)throw new Error("Map/Set draft does not support any property assignment.");let i;if(1===e.type&&"length"!==t&&(!(Number.isInteger(i=Number(t))&&i>=0)||0!==t&&0!==i&&String(i)!==String(t)))throw new Error("Only supports setting array indices and the 'length' property.");const s=_(V(e),t);if(null==s?void 0:s.set)return s.set.call(e.proxy,n),!0;const o=K(V(e),t),a=q(o);return a&&G(a.original,n)?(e.copy[t]=n,e.assignedMap=null!==(r=e.assignedMap)&&void 0!==r?r:new Map,e.assignedMap.set(t,!1),!0):(G(n,o)&&(void 0!==n||L(e.original,t))||(ee(e),re(e),L(e.original,t)&&G(n,e.original[t])?e.assignedMap.delete(t):e.assignedMap.set(t,!0),e.copy[t]=n,he(e,t,n,pe)),!0)},has:(e,t)=>t in V(e),ownKeys:e=>Reflect.ownKeys(V(e)),getOwnPropertyDescriptor(e,t){const n=V(e),r=Reflect.getOwnPropertyDescriptor(n,t);return r?{writable:!0,configurable:1!==e.type||"length"!==t,enumerable:r.enumerable,value:n[t]}:r},getPrototypeOf:e=>Reflect.getPrototypeOf(e.original),setPrototypeOf(){throw new Error("Cannot call 'setPrototypeOf()' on drafts")},defineProperty(){throw new Error("Cannot call 'defineProperty()' on drafts")},deleteProperty(e,t){var n;return 1===e.type?we.set.call(this,e,t,void 0,e.proxy):(void 0!==K(e.original,t)||t in e.original?(ee(e),re(e),e.assignedMap.set(t,!1)):(e.assignedMap=null!==(n=e.assignedMap)&&void 0!==n?n:new Map,e.assignedMap.delete(t)),e.copy&&delete e.copy[t],!0)}};function Ee(e){const{original:t,parentDraft:n,key:r,finalities:i,options:s}=e,o=$(t),a={type:o,finalized:!1,parent:n,original:t,copy:null,proxy:null,finalities:i,options:s,setMap:3===o?new Map(t.entries()):void 0};(r||"key"in e)&&(a.key=r);const{proxy:l,revoke:c}=Proxy.revocable(1===o?Object.assign([],a):a,we);if(i.revoke.push(c),be.add(l),a.proxy=l,n){const e=n;e.finalities.draft.push(((t,n)=>{var i,s;const o=q(l);let a=3===e.type?e.setMap:e.copy;const c=W(a,r),u=q(c);if(u){let s=u.original;u.operated&&(s=F(c)),ce(u),ue(u,pe,t,n),e.options.enableAutoFreeze&&(e.options.updatedValues=null!==(i=e.options.updatedValues)&&void 0!==i?i:new WeakMap,e.options.updatedValues.set(s,u.original)),X(a,r,s)}null===(s=o.callbacks)||void 0===s||s.forEach((e=>{e(t,n)}))}))}else{const e=q(l);e.finalities.draft.push(((t,n)=>{ce(e),ue(e,pe,t,n)}))}return l}function Oe(e,t,n,r,i){var s;const o=q(e),a=null!==(s=null==o?void 0:o.original)&&void 0!==s?s:e,l=!!t.length;if(null==o?void 0:o.operated)for(;o.finalities.draft.length>0;)o.finalities.draft.pop()(n,r);const c=l?t[0]:o?o.operated?o.copy:o.original:e;return o&&Y(o),i&&se(c,c,null==o?void 0:o.options.updatedValues),[c,n&&l?[{op:x,path:[],value:t[0]}]:n,r&&l?[{op:x,path:[],value:a}]:r]}function ke(e){const{rootDraft:t,value:n,useRawReturn:r=!1,isRoot:i=!0}=e;oe(n,((n,r,i)=>{const s=q(r);if(s&&t&&s.finalities===t.finalities){e.isContainDraft=!0;const t=s.original;if(i instanceof Set){const e=Array.from(i);i.clear(),e.forEach((e=>i.add(n===e?t:e)))}else X(i,n,t)}else"object"==typeof r&&null!==r&&(e.value=r,e.isRoot=!1,ke(e))})),i&&(e.isContainDraft||console.warn("The return value does not contain any draft, please use 'rawReturn()' to wrap the return value to improve performance."),r&&console.warn("The return value contains drafts, please don't use 'rawReturn()' to wrap the return value."))}function Me(e){var t;const n=q(e);if(!B(e,null==n?void 0:n.options))return e;const r=$(e);if(n&&!n.operated)return n.original;let i;function s(){i=2===r?I(e)?new Map(e):new(Object.getPrototypeOf(e).constructor)(e):3===r?Array.from(n.setMap.values()):Q(e,null==n?void 0:n.options)}if(n){n.finalized=!0;try{s()}finally{n.finalized=!1}}else i=e;if(oe(i,((t,r)=>{if(n&&G(W(n.original,t),r))return;const o=Me(r);o!==r&&(i===e&&s(),X(i,t,o))})),3===r){const e=null!==(t=null==n?void 0:n.original)&&void 0!==t?t:i;return N(e)?new Set(i):new(Object.getPrototypeOf(e).constructor)(i)}return i}function Re(e){if(!U(e))throw new Error(`current() is only used for Draft, parameter: ${e}`);return Me(e)}z.createDraft=Ee;const je=function e(t,n,r){var i,s,o;if("function"==typeof t&&"function"!=typeof n)return function(r,...i){return e(r,(e=>t.call(this,e,...i)),n)};const a=t,l=n;let c=r;if("function"!=typeof n&&(c=n),void 0!==c&&"[object Object]"!==Object.prototype.toString.call(c))throw new Error(`Invalid options: ${c}, 'options' should be an object.`);c=Object.assign(Object.assign({},void 0),c);const u=U(a)?Re(a):a,h=Array.isArray(c.mark)?(e,t)=>{for(const n of c.mark){if("function"!=typeof n)throw new Error(`Invalid mark: ${n}, 'mark' should be a function.`);const r=n(e,t);if(r)return r}}:c.mark,p=null!==(i=c.enablePatches)&&void 0!==i&&i,f=null!==(s=c.strict)&&void 0!==s&&s,d={enableAutoFreeze:null!==(o=c.enableAutoFreeze)&&void 0!==o&&o,mark:h,strict:f,enablePatches:p};if(!B(u,d)&&"object"==typeof u&&null!==u)throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.");const[y,v]=function(e,t){var n;const r={draft:[],revoke:[],handledSet:new WeakSet};let i,s;t.enablePatches&&(i=[],s=[]);const o=(null===(n=t.mark)||void 0===n?void 0:n.call(t,e,P))!==P.mutable&&B(e,t)?Ee({original:e,parentDraft:null,finalities:r,options:t}):e;return[o,(e=[])=>{const[n,r,a]=Oe(o,e,i,s,t.enableAutoFreeze);return t.enablePatches?[n,r,a]:n}]}(u,d);if("function"!=typeof n){if(!B(u,d))throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.");return[y,v]}let g;try{g=l(y)}catch(e){throw Y(q(y)),e}const m=e=>{const t=q(y);if(!U(e)){if(void 0!==e&&!G(e,y)&&(null==t?void 0:t.operated))throw new Error("Either the value is returned as a new non-draft value, or only the draft is modified without returning any value.");const n=null==e?void 0:e[A];if(n){const r=n[0];return d.strict&&"object"==typeof e&&null!==e&&ke({rootDraft:t,value:e,useRawReturn:!0}),v([r])}if(void 0!==e)return"object"==typeof e&&null!==e&&ke({rootDraft:t,value:e}),v([e])}if(e===y||void 0===e)return v([]);const n=q(e);if(d===n.options){if(n.operated)throw new Error("Cannot return a modified child draft.");return v([Re(e)])}return v([e])};return g instanceof Promise?g.then(m,(e=>{throw Y(q(y)),e})):m(g)};function Se(e,t){e.set(je(e.get(),t))}Object.prototype.constructor.toString();class xe{constructor(e,t=null){this.value=e,this.reactor=t??new R(this,[])}produce(e){return Se(this,e)}reducer(...e){let t;return t=1===e.length?new j(this,"",e[0]):new j(this,e[0],e[1]),t.trigger.bind(t)}setObjectByPath(e="",t){if(0===e.length)return void(this.value=t);if("string"!=typeof e||!e)throw new Error("Invalid path");let n=e.split("."),r=this.value;for(let e=0;e<n.length-1;e++){let t=n[e];t in r||(r[t]={}),r=r[t]}r[n[n.length-1]]=t}set(...e){if(1===e.length)this.value=e[0],this.reactor.react({type:"SET_VALUE",value:e[0]});else{let t=e[0],n=e[1];this.setObjectByPath(t,n),this.reactor.react({type:"SET_VALUE",path:t,value:n})}}get(){return this.value}}class Te extends xe{constructor(e,t=null){if(!(e instanceof Array))throw Error("RumiousArrayState: The initial value must be an array.");super(e,t)}set(...e){if(1===e.length)this.value=e[0],this.reactor.react({type:"SET_VALUE",value:e[0]});else{let t=e[0],n=e[1];this.value[t]=n,this.reactor.react({type:"SET_ELEMENT",index:t,value:n})}}append(e){this.value.push(e),this.reactor.react({type:"ADD_ELEMENT",value:e,index:this.value.length-1})}index(e){return this.value[e]}remove(e){e>=0&&e<this.value.length&&(this.value.splice(e,1),this.reactor.react({type:"REMOVE_ELEMENT",index:e}))}insert(e,t){e>=0&&e<=this.value.length&&(this.value.splice(e,0,t),this.reactor.react({type:"INSERT_ELEMENT",index:e,value:t}))}find(e){return this.value.find(e)}}class Ce extends xe{constructor(e,t=null){if(!(e instanceof Object))throw Error("RumiousObjectState: The initial value must be an object.");super(e,t)}set(...e){if(1===e.length)this.value=e[0],this.reactor.react({type:"SET_VALUE",value:e[0]});else{let t=e[0],n=e[1];this.value[t]=n,this.reactor.react({type:"SET_VALUE",index:t,value:n})}}keys(){return Object.keys(this.value)}values(){return Object.values(this.value)}each(e){for(let[t,n]of Object.entries(this.value))e(n,t)}has(e){return void 0!==this.value[e]}remove(e){delete this.value[e]}entries(){return Object.entries(this.value)}clear(){this.value={},this.reactor.react({type:"CLEAR"})}merge(e){e instanceof Object&&(Object.assign(this.value,e),this.reactor.react({type:"MERGE",value:e}))}map(e){return Object.entries(this.value).map((([t,n],r)=>e(n,t,r)))}}class Ae{constructor(e){this.target=e}set html(e){this.target.innerHTML=e}query(e){return this.target.querySelectorAll(e)}set text(e){this.target.textContent=e}on(e,t){this.target.addEventListener(e,t)}off(e,t){this.target.removeEventListener(e,t)}remove(){this.target.remove()}addChild(e){this.target.appendChild(e)}set(e){this.target=e}}class De{constructor(e){this.target=e}query(e){return this.target.querySelectorAll(e)}index(e){return Array.from(this.target.children)[e]}list(){return Array.from(this.target.children)}get parent(){return this.target}remove(){this.target.remove()}addChild(e){this.target.appendChild(e)}set(e){this.target=e}}class Pe{constructor(e){this.target=e}set(e){this.target=e.forwardRefs}}window.RUMIOUS_CONTEXTS||(window.RUMIOUS_CONTEXTS={});class ze{constructor(e={}){this.data=e,this.events={}}on(e,t){this.events[e]||(this.events[e]=[]),this.events[e].push(t)}off(e,t){this.events[e]&&(this.events[e]=this.events[e].filter((e=>e!==t)))}emit(e,...t){this.events[e]&&this.events[e].forEach((e=>e(...t)))}set(e,t){return this.data[e]=t,t}get(e){return this.data[e]}}class Le{constructor(e,t={}){this.hasExecuted=!1,this.result=null,this.callback=e,this.dependents=[],this.options=t}onLoad(e){this.hasExecuted?e(this.result):this.dependents.push(e)}execute(){return new Promise(((e,t)=>{if(this.hasExecuted)return e(this.result);this.hasExecuted=!0;const n=async()=>{try{this.result=await this.callback(),e(this.result),this.dependents.forEach((e=>e(this.result)))}catch(e){t(e)}};this.options.idle&&"requestIdleCallback"in window?requestIdleCallback(n):n()}))}}const _e=new Map;return e.Pending=class extends n{static tag="r-pending";constructor(){super(),this.asynchronousRender=!0}async setLoader(e){this.loader=e;try{this.result=await this.loader.execute(),this.renderResult()}catch(e){this.error=e,this.renderError()}}renderResult(){if(!this.result)throw"Rumious Render: Cannot find component when lazy loading!";this.element.before(this.render(y(this.result,{}))),this.element.remove()}renderError(){this.element.appendChild(this.render(this.props.errorComponent??this.error))}onRender(){let{loader:e}=this.props;this.setLoader(e)}template(){return this.props.fallback}},e.RumiousApp=class{constructor(e=document.createElement("span"),n={}){this.root=e,this.app=this,this.modules=[],this.configs=n,this.renderContext=new t(this)}render(e){this.renderContext.runHooks("onBeforeRender",this.renderContext),M(e,this.root,this.renderContext),this.renderContext.runHooks("onRendered",this.renderContext)}addModule(e,t={}){return e.init(this,t)}},e.RumiousArraySync=k,e.RumiousChildrensRef=De,e.RumiousComponent=n,e.RumiousComponentElement=w,e.RumiousComponentRef=Pe,e.RumiousContext=ze,e.RumiousDymanicInjector=O,e.RumiousElementRef=Ae,e.RumiousLazyLoader=Le,e.RumiousReactor=R,e.RumiousReducer=j,e.RumiousState=xe,e.createArrayState=function(e=[]){return new Te(e)},e.createChildrensRef=function(e){return new De(e)},e.createComponent=g,e.createComponentElement=E,e.createComponentRef=function(e){return new Pe(e)},e.createContext=function(e="",t={}){if(!t||"object"!=typeof t)throw new Error("Rumious context: Initial value must be an object!");return window.RUMIOUS_CONTEXTS[e]||(window.RUMIOUS_CONTEXTS[e]=new ze(t)),window.RUMIOUS_CONTEXTS[e]},e.createDirective=d,e.createElement=y,e.createElementRef=function(e){return new Ae(e)},e.createObjectState=function(e={}){return new Ce(e)},e.createState=function(e){return new xe(e)},e.createTextElement=v,e.denounce=function(e,t){let n;return function(...r){clearTimeout(n),n=setTimeout((()=>e.apply(this,r)),t)}},e.injectHTML=function(e){let t=new O;return t.commit([{type:"html",value:e}]),t},e.injectText=function(e){let t=new O;return t.commit([{type:"text",value:e}]),t},e.isComponent=r,e.isState=function(e){return e instanceof xe},e.lazyLoad=function(e,t={}){const n=t.key||e.toString();return _e.has(n)||_e.set(n,new Le(e,t)),_e.get(n)},e.leadingTrailingDebounce=function(e,t){let n,r=!0;return function(...i){r&&(e.apply(this,i),r=!1),clearTimeout(n),n=setTimeout((()=>{e.apply(this,i),r=!0}),t)}},e.produceState=Se,e.rafThrottle=function(e){let t=!1;return function(...n){t||(t=!0,requestAnimationFrame((()=>{e.apply(this,n),t=!1})))}},e.styleHelper=function(e){let t="";for(const n in e)if(Object.prototype.hasOwnProperty.call(e,n)){t+=`${n} {`;const r=e[n];for(const e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t+=`${e.replace(/[A-Z]/g,(e=>`-${e.toLowerCase()}`))}: ${r[e]};`);t+="} "}return t.trim()},e.syncArray=function(e,t){return new k(e,t)},e.tholle=function(e,t){let n=0;return function(...r){const i=Date.now();i-n>=t&&(n=i,e.apply(this,r))}},e.trailingThrottle=function(e,t){let n=0,r=null,i=null;function s(){n=Date.now(),e.apply(this,r),r=null}return function(...e){const o=Date.now();r=e,o-n>=t?s():i||(i=setTimeout((()=>{i=null,s()}),t-(o-n)))}},e.unwatch=function(e,t){e.reactor.removeBinding(t)},e.watch=function(e,t){e.reactor.addBinding(t)},e}({});
|
1
|
+
var Rumious = (function () {
|
2
|
+
'use strict';
|
3
|
+
|
4
|
+
class RumiousRenderContext {
|
5
|
+
constructor(target, app) {
|
6
|
+
this.target = target;
|
7
|
+
this.app = app;
|
8
|
+
this.onRenderFinished = [];
|
9
|
+
}
|
10
|
+
findName(name) {
|
11
|
+
return this.target[name];
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
function render(context, template, target) {
|
16
|
+
let generator = template.generator.bind(context.target);
|
17
|
+
context.renderHelper = render;
|
18
|
+
generator(target, context);
|
19
|
+
setTimeout(async () => {
|
20
|
+
const callbacks = [...context.onRenderFinished];
|
21
|
+
context.onRenderFinished = [];
|
22
|
+
for (const callback of callbacks) {
|
23
|
+
await callback();
|
24
|
+
}
|
25
|
+
}, 0);
|
26
|
+
}
|
27
|
+
|
28
|
+
class RumiousComponent {
|
29
|
+
static classNames = "";
|
30
|
+
static tagName = "rumious-component";
|
31
|
+
constructor() {
|
32
|
+
this.renderOptions = {
|
33
|
+
mode: "idle"
|
34
|
+
};
|
35
|
+
}
|
36
|
+
onCreate() {}
|
37
|
+
onRender() {}
|
38
|
+
onDestroy() {}
|
39
|
+
async onBeforeRender() {}
|
40
|
+
prepare(currentContext, props) {
|
41
|
+
this.props = props;
|
42
|
+
this.context = new RumiousRenderContext(this, currentContext.app);
|
43
|
+
}
|
44
|
+
async requestRender() {
|
45
|
+
await this.onBeforeRender();
|
46
|
+
let template = this.template();
|
47
|
+
render(this.context, template, this.element);
|
48
|
+
this.onRender();
|
49
|
+
}
|
50
|
+
requestCleanup() {}
|
51
|
+
}
|
52
|
+
|
53
|
+
class RumiousComponentElement extends HTMLElement {
|
54
|
+
constructor() {
|
55
|
+
super();
|
56
|
+
this.props = {};
|
57
|
+
}
|
58
|
+
setup(context, componentConstructor) {
|
59
|
+
this.context = context;
|
60
|
+
this.componentConstructor = componentConstructor;
|
61
|
+
}
|
62
|
+
connectedCallback() {
|
63
|
+
if (!this.componentConstructor) {
|
64
|
+
console.warn("Rumious: Cannot find matching component constructor.");
|
65
|
+
return;
|
66
|
+
}
|
67
|
+
this.componentInstance = new this.componentConstructor();
|
68
|
+
this.componentInstance.element = this;
|
69
|
+
if (this.componentConstructor.classNames) {
|
70
|
+
this.className = this.componentConstructor.classNames;
|
71
|
+
}
|
72
|
+
this.componentInstance.prepare(this.context, this.props);
|
73
|
+
this.componentInstance.onCreate();
|
74
|
+
this.componentInstance.requestRender();
|
75
|
+
}
|
76
|
+
disconnectedCallback() {
|
77
|
+
this.componentInstance?.onDestroy();
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
var Component = /*#__PURE__*/Object.freeze({
|
82
|
+
__proto__: null,
|
83
|
+
RumiousComponent: RumiousComponent,
|
84
|
+
RumiousComponentElement: RumiousComponentElement
|
85
|
+
});
|
86
|
+
|
87
|
+
class RumiousApp {
|
88
|
+
constructor(root, options = {}) {
|
89
|
+
this.root = root;
|
90
|
+
this.options = options;
|
91
|
+
this.modules = [];
|
92
|
+
this.context = new RumiousRenderContext(this, this);
|
93
|
+
}
|
94
|
+
addModule(module, options) {
|
95
|
+
let instance = module.init(this, options);
|
96
|
+
this.modules.push(instance);
|
97
|
+
return instance;
|
98
|
+
}
|
99
|
+
render(template) {
|
100
|
+
render(this.context, template, this.root);
|
101
|
+
}
|
102
|
+
}
|
103
|
+
function createApp(root, options) {
|
104
|
+
return new RumiousApp(root, options);
|
105
|
+
}
|
106
|
+
|
107
|
+
var App = /*#__PURE__*/Object.freeze({
|
108
|
+
__proto__: null,
|
109
|
+
RumiousApp: RumiousApp,
|
110
|
+
createApp: createApp
|
111
|
+
});
|
112
|
+
|
113
|
+
class RumiousRenderTemplate {
|
114
|
+
constructor(generator) {
|
115
|
+
this.generator = generator;
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
class RumiousDymanicInjector {
|
120
|
+
constructor(contents) {
|
121
|
+
this.contents = contents;
|
122
|
+
this.targets = new Map();
|
123
|
+
if (!contents || contents.length === 0) {
|
124
|
+
throw new Error("Injector must be initialized with non-empty content");
|
125
|
+
}
|
126
|
+
const first = contents[0];
|
127
|
+
this.type = typeof first === "string" ? "string" : "element";
|
128
|
+
}
|
129
|
+
addTarget(element) {
|
130
|
+
this.targets.set(element, 1);
|
131
|
+
}
|
132
|
+
inject(element) {
|
133
|
+
if (!this.targets.has(element)) return;
|
134
|
+
if (!this.contents) return;
|
135
|
+
element.innerHTML = "";
|
136
|
+
if (this.type === "string") {
|
137
|
+
for (const content of this.contents) {
|
138
|
+
element.insertAdjacentHTML("beforeend", content);
|
139
|
+
}
|
140
|
+
} else {
|
141
|
+
for (const content of this.contents) {
|
142
|
+
element.appendChild(content);
|
143
|
+
}
|
144
|
+
}
|
145
|
+
}
|
146
|
+
injectAll() {
|
147
|
+
for (const target of this.targets.keys()) {
|
148
|
+
this.inject(target);
|
149
|
+
}
|
150
|
+
}
|
151
|
+
removeTarget(element) {
|
152
|
+
this.targets.delete(element);
|
153
|
+
}
|
154
|
+
clear() {
|
155
|
+
this.targets.clear();
|
156
|
+
}
|
157
|
+
}
|
158
|
+
function createHTMLInjector(html) {
|
159
|
+
return new RumiousDymanicInjector([html]);
|
160
|
+
}
|
161
|
+
|
162
|
+
class RumiousElementRef {
|
163
|
+
constructor(target) {
|
164
|
+
this.target = target;
|
165
|
+
}
|
166
|
+
getElement() {
|
167
|
+
return this.target;
|
168
|
+
}
|
169
|
+
remove() {
|
170
|
+
this.target.remove();
|
171
|
+
}
|
172
|
+
addChild(child) {
|
173
|
+
this.target.appendChild(child);
|
174
|
+
}
|
175
|
+
listChild() {
|
176
|
+
return this.target.childNodes;
|
177
|
+
}
|
178
|
+
querySelector(query) {
|
179
|
+
return this.target.querySelector(query);
|
180
|
+
}
|
181
|
+
querySelectorAll(query) {
|
182
|
+
return this.target.querySelectorAll(query);
|
183
|
+
}
|
184
|
+
set text(t) {
|
185
|
+
this.target.textContent = t;
|
186
|
+
}
|
187
|
+
get text() {
|
188
|
+
return this.target.textContent;
|
189
|
+
}
|
190
|
+
set value(v) {
|
191
|
+
if (this.target instanceof HTMLInputElement || this.target instanceof HTMLTextAreaElement) {
|
192
|
+
this.target.value = v;
|
193
|
+
}
|
194
|
+
}
|
195
|
+
get value() {
|
196
|
+
if (this.target instanceof HTMLInputElement || this.target instanceof HTMLTextAreaElement) {
|
197
|
+
return this.target.value;
|
198
|
+
}
|
199
|
+
return undefined;
|
200
|
+
}
|
201
|
+
addClassName(className) {
|
202
|
+
this.target.classList.add(className);
|
203
|
+
}
|
204
|
+
removeClassName(className) {
|
205
|
+
this.target.classList.remove(className);
|
206
|
+
}
|
207
|
+
hasClassName(className) {
|
208
|
+
return this.target.classList.contains(className);
|
209
|
+
}
|
210
|
+
toggleClass(className, force) {
|
211
|
+
return this.target.classList.toggle(className, force);
|
212
|
+
}
|
213
|
+
setStyle(styles) {
|
214
|
+
Object.assign(this.target.style, styles);
|
215
|
+
}
|
216
|
+
getStyle(property) {
|
217
|
+
return getComputedStyle(this.target).getPropertyValue(property);
|
218
|
+
}
|
219
|
+
setAttribute(key, value) {
|
220
|
+
this.target.setAttribute(key, value);
|
221
|
+
}
|
222
|
+
getAttribute(key) {
|
223
|
+
return this.target.getAttribute(key);
|
224
|
+
}
|
225
|
+
removeAttribute(key) {
|
226
|
+
this.target.removeAttribute(key);
|
227
|
+
}
|
228
|
+
on(event, callback, options) {
|
229
|
+
this.target.addEventListener(event, callback, options);
|
230
|
+
}
|
231
|
+
off(event, callback, options) {
|
232
|
+
this.target.removeEventListener(event, callback, options);
|
233
|
+
}
|
234
|
+
set html(t) {
|
235
|
+
this.target.innerHTML = t;
|
236
|
+
}
|
237
|
+
get html() {
|
238
|
+
return this.target.innerHTML;
|
239
|
+
}
|
240
|
+
getBoundingRect() {
|
241
|
+
return this.target.getBoundingClientRect();
|
242
|
+
}
|
243
|
+
isInViewport() {
|
244
|
+
const rect = this.target.getBoundingClientRect();
|
245
|
+
return rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth);
|
246
|
+
}
|
247
|
+
prependChild(child) {
|
248
|
+
this.target.prepend(child);
|
249
|
+
}
|
250
|
+
setDisabled(disabled) {
|
251
|
+
if (this.target instanceof HTMLButtonElement || this.target instanceof HTMLInputElement || this.target instanceof HTMLTextAreaElement) {
|
252
|
+
this.target.disabled = disabled;
|
253
|
+
}
|
254
|
+
}
|
255
|
+
addClasses(...classNames) {
|
256
|
+
this.target.classList.add(...classNames);
|
257
|
+
}
|
258
|
+
removeClasses(...classNames) {
|
259
|
+
this.target.classList.remove(...classNames);
|
260
|
+
}
|
261
|
+
replaceClass(oldClass, newClass) {
|
262
|
+
this.target.classList.replace(oldClass, newClass);
|
263
|
+
}
|
264
|
+
moveTo(newParent) {
|
265
|
+
newParent.appendChild(this.target);
|
266
|
+
}
|
267
|
+
getParent() {
|
268
|
+
return this.target.parentElement;
|
269
|
+
}
|
270
|
+
getNextSibling() {
|
271
|
+
return this.target.nextElementSibling;
|
272
|
+
}
|
273
|
+
getPreviousSibling() {
|
274
|
+
return this.target.previousElementSibling;
|
275
|
+
}
|
276
|
+
hide() {
|
277
|
+
this.target.style.display = 'none';
|
278
|
+
}
|
279
|
+
show() {
|
280
|
+
this.target.style.removeProperty('display');
|
281
|
+
}
|
282
|
+
isHidden() {
|
283
|
+
return window.getComputedStyle(this.target).display === 'none';
|
284
|
+
}
|
285
|
+
scrollIntoView(options = {
|
286
|
+
behavior: 'smooth'
|
287
|
+
}) {
|
288
|
+
this.target.scrollIntoView(options);
|
289
|
+
}
|
290
|
+
matches(selector) {
|
291
|
+
return this.target.matches(selector);
|
292
|
+
}
|
293
|
+
getChildren() {
|
294
|
+
return Array.from(this.target.children);
|
295
|
+
}
|
296
|
+
insertAfter(newNode) {
|
297
|
+
if (this.target.parentNode) {
|
298
|
+
this.target.parentNode.insertBefore(newNode, this.target.nextSibling);
|
299
|
+
}
|
300
|
+
}
|
301
|
+
insertBefore(newNode) {
|
302
|
+
if (this.target.parentNode) {
|
303
|
+
this.target.parentNode.insertBefore(newNode, this.target);
|
304
|
+
}
|
305
|
+
}
|
306
|
+
clearChildren() {
|
307
|
+
while (this.target.firstChild) {
|
308
|
+
this.target.removeChild(this.target.firstChild);
|
309
|
+
}
|
310
|
+
}
|
311
|
+
animate(keyframes, options) {
|
312
|
+
return this.target.animate(keyframes, options);
|
313
|
+
}
|
314
|
+
}
|
315
|
+
function createElementRef() {
|
316
|
+
return new RumiousElementRef(document.createElement("span"));
|
317
|
+
}
|
318
|
+
|
319
|
+
function extractName(str) {
|
320
|
+
const index = str.indexOf('$');
|
321
|
+
if (index !== -1) {
|
322
|
+
return str.slice(index + 1);
|
323
|
+
}
|
324
|
+
return '';
|
325
|
+
}
|
326
|
+
function generateName(prefix = "_") {
|
327
|
+
return `${prefix}${(Math.floor(Math.random() * 9999) * Date.now()).toString(32)}`;
|
328
|
+
}
|
329
|
+
|
330
|
+
class RumiousReactor {
|
331
|
+
constructor(target) {
|
332
|
+
this.target = target;
|
333
|
+
this.bindings = [];
|
334
|
+
}
|
335
|
+
addBinding(bind) {
|
336
|
+
this.bindings.push(bind);
|
337
|
+
}
|
338
|
+
removeBinding(bind) {
|
339
|
+
this.bindings = this.bindings.filter(_ => _ !== bind);
|
340
|
+
}
|
341
|
+
async emit(commit) {
|
342
|
+
await Promise.allSettled(this.bindings.map(bind => {
|
343
|
+
try {
|
344
|
+
const result = bind(commit);
|
345
|
+
return result instanceof Promise ? result : Promise.resolve(result);
|
346
|
+
} catch (err) {
|
347
|
+
return Promise.reject(err);
|
348
|
+
}
|
349
|
+
}));
|
350
|
+
}
|
351
|
+
}
|
352
|
+
|
353
|
+
const Operation = {
|
354
|
+
Remove: 'remove',
|
355
|
+
Replace: 'replace',
|
356
|
+
Add: 'add',
|
357
|
+
};
|
358
|
+
|
359
|
+
// Don't use `Symbol()` just for 3rd party access the draft
|
360
|
+
const PROXY_DRAFT = Symbol.for('__MUTATIVE_PROXY_DRAFT__');
|
361
|
+
const RAW_RETURN_SYMBOL = Symbol('__MUTATIVE_RAW_RETURN_SYMBOL__');
|
362
|
+
const iteratorSymbol = Symbol.iterator;
|
363
|
+
const dataTypes = {
|
364
|
+
mutable: 'mutable',
|
365
|
+
immutable: 'immutable',
|
366
|
+
};
|
367
|
+
|
368
|
+
const internal = {};
|
369
|
+
|
370
|
+
function has(target, key) {
|
371
|
+
return target instanceof Map
|
372
|
+
? target.has(key)
|
373
|
+
: Object.prototype.hasOwnProperty.call(target, key);
|
374
|
+
}
|
375
|
+
function getDescriptor(target, key) {
|
376
|
+
if (key in target) {
|
377
|
+
let prototype = Reflect.getPrototypeOf(target);
|
378
|
+
while (prototype) {
|
379
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(prototype, key);
|
380
|
+
if (descriptor)
|
381
|
+
return descriptor;
|
382
|
+
prototype = Reflect.getPrototypeOf(prototype);
|
383
|
+
}
|
384
|
+
}
|
385
|
+
return;
|
386
|
+
}
|
387
|
+
function isBaseSetInstance(obj) {
|
388
|
+
return Object.getPrototypeOf(obj) === Set.prototype;
|
389
|
+
}
|
390
|
+
function isBaseMapInstance(obj) {
|
391
|
+
return Object.getPrototypeOf(obj) === Map.prototype;
|
392
|
+
}
|
393
|
+
|
394
|
+
function latest(proxyDraft) {
|
395
|
+
var _a;
|
396
|
+
return (_a = proxyDraft.copy) !== null && _a !== undefined ? _a : proxyDraft.original;
|
397
|
+
}
|
398
|
+
/**
|
399
|
+
* Check if the value is a draft
|
400
|
+
*/
|
401
|
+
function isDraft(target) {
|
402
|
+
return !!getProxyDraft(target);
|
403
|
+
}
|
404
|
+
function getProxyDraft(value) {
|
405
|
+
if (typeof value !== 'object')
|
406
|
+
return null;
|
407
|
+
return value === null || value === undefined ? undefined : value[PROXY_DRAFT];
|
408
|
+
}
|
409
|
+
function getValue(value) {
|
410
|
+
var _a;
|
411
|
+
const proxyDraft = getProxyDraft(value);
|
412
|
+
return proxyDraft ? (_a = proxyDraft.copy) !== null && _a !== undefined ? _a : proxyDraft.original : value;
|
413
|
+
}
|
414
|
+
/**
|
415
|
+
* Check if a value is draftable
|
416
|
+
*/
|
417
|
+
function isDraftable(value, options) {
|
418
|
+
if (!value || typeof value !== 'object')
|
419
|
+
return false;
|
420
|
+
let markResult;
|
421
|
+
return (Object.getPrototypeOf(value) === Object.prototype ||
|
422
|
+
Array.isArray(value) ||
|
423
|
+
value instanceof Map ||
|
424
|
+
value instanceof Set ||
|
425
|
+
(!!(options === null || options === undefined ? undefined : options.mark) &&
|
426
|
+
((markResult = options.mark(value, dataTypes)) === dataTypes.immutable ||
|
427
|
+
typeof markResult === 'function')));
|
428
|
+
}
|
429
|
+
function getPath(target, path = []) {
|
430
|
+
if (Object.hasOwnProperty.call(target, 'key')) {
|
431
|
+
// check if the parent is a draft and the original value is not equal to the current value
|
432
|
+
const parentCopy = target.parent.copy;
|
433
|
+
const proxyDraft = getProxyDraft(get(parentCopy, target.key));
|
434
|
+
if (proxyDraft !== null && (proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.original) !== target.original) {
|
435
|
+
return null;
|
436
|
+
}
|
437
|
+
const isSet = target.parent.type === 3 /* DraftType.Set */;
|
438
|
+
const key = isSet
|
439
|
+
? Array.from(target.parent.setMap.keys()).indexOf(target.key)
|
440
|
+
: target.key;
|
441
|
+
// check if the key is still in the next state parent
|
442
|
+
if (!((isSet && parentCopy.size > key) || has(parentCopy, key)))
|
443
|
+
return null;
|
444
|
+
path.push(key);
|
445
|
+
}
|
446
|
+
if (target.parent) {
|
447
|
+
return getPath(target.parent, path);
|
448
|
+
}
|
449
|
+
// `target` is root draft.
|
450
|
+
path.reverse();
|
451
|
+
try {
|
452
|
+
// check if the path is valid
|
453
|
+
resolvePath(target.copy, path);
|
454
|
+
}
|
455
|
+
catch (e) {
|
456
|
+
return null;
|
457
|
+
}
|
458
|
+
return path;
|
459
|
+
}
|
460
|
+
function getType(target) {
|
461
|
+
if (Array.isArray(target))
|
462
|
+
return 1 /* DraftType.Array */;
|
463
|
+
if (target instanceof Map)
|
464
|
+
return 2 /* DraftType.Map */;
|
465
|
+
if (target instanceof Set)
|
466
|
+
return 3 /* DraftType.Set */;
|
467
|
+
return 0 /* DraftType.Object */;
|
468
|
+
}
|
469
|
+
function get(target, key) {
|
470
|
+
return getType(target) === 2 /* DraftType.Map */ ? target.get(key) : target[key];
|
471
|
+
}
|
472
|
+
function set(target, key, value) {
|
473
|
+
const type = getType(target);
|
474
|
+
if (type === 2 /* DraftType.Map */) {
|
475
|
+
target.set(key, value);
|
476
|
+
}
|
477
|
+
else {
|
478
|
+
target[key] = value;
|
479
|
+
}
|
480
|
+
}
|
481
|
+
function peek(target, key) {
|
482
|
+
const state = getProxyDraft(target);
|
483
|
+
const source = state ? latest(state) : target;
|
484
|
+
return source[key];
|
485
|
+
}
|
486
|
+
function isEqual(x, y) {
|
487
|
+
if (x === y) {
|
488
|
+
return x !== 0 || 1 / x === 1 / y;
|
489
|
+
}
|
490
|
+
else {
|
491
|
+
return x !== x && y !== y;
|
492
|
+
}
|
493
|
+
}
|
494
|
+
function revokeProxy(proxyDraft) {
|
495
|
+
if (!proxyDraft)
|
496
|
+
return;
|
497
|
+
while (proxyDraft.finalities.revoke.length > 0) {
|
498
|
+
const revoke = proxyDraft.finalities.revoke.pop();
|
499
|
+
revoke();
|
500
|
+
}
|
501
|
+
}
|
502
|
+
// handle JSON Pointer path with spec https://www.rfc-editor.org/rfc/rfc6901
|
503
|
+
function escapePath(path, pathAsArray) {
|
504
|
+
return pathAsArray
|
505
|
+
? path
|
506
|
+
: ['']
|
507
|
+
.concat(path)
|
508
|
+
.map((_item) => {
|
509
|
+
const item = `${_item}`;
|
510
|
+
if (item.indexOf('/') === -1 && item.indexOf('~') === -1)
|
511
|
+
return item;
|
512
|
+
return item.replace(/~/g, '~0').replace(/\//g, '~1');
|
513
|
+
})
|
514
|
+
.join('/');
|
515
|
+
}
|
516
|
+
function resolvePath(base, path) {
|
517
|
+
for (let index = 0; index < path.length - 1; index += 1) {
|
518
|
+
const key = path[index];
|
519
|
+
// use `index` in Set draft
|
520
|
+
base = get(getType(base) === 3 /* DraftType.Set */ ? Array.from(base) : base, key);
|
521
|
+
if (typeof base !== 'object') {
|
522
|
+
throw new Error(`Cannot resolve patch at '${path.join('/')}'.`);
|
523
|
+
}
|
524
|
+
}
|
525
|
+
return base;
|
526
|
+
}
|
527
|
+
|
528
|
+
function strictCopy(target) {
|
529
|
+
const copy = Object.create(Object.getPrototypeOf(target));
|
530
|
+
Reflect.ownKeys(target).forEach((key) => {
|
531
|
+
let desc = Reflect.getOwnPropertyDescriptor(target, key);
|
532
|
+
if (desc.enumerable && desc.configurable && desc.writable) {
|
533
|
+
copy[key] = target[key];
|
534
|
+
return;
|
535
|
+
}
|
536
|
+
// for freeze
|
537
|
+
if (!desc.writable) {
|
538
|
+
desc.writable = true;
|
539
|
+
desc.configurable = true;
|
540
|
+
}
|
541
|
+
if (desc.get || desc.set)
|
542
|
+
desc = {
|
543
|
+
configurable: true,
|
544
|
+
writable: true,
|
545
|
+
enumerable: desc.enumerable,
|
546
|
+
value: target[key],
|
547
|
+
};
|
548
|
+
Reflect.defineProperty(copy, key, desc);
|
549
|
+
});
|
550
|
+
return copy;
|
551
|
+
}
|
552
|
+
const propIsEnum = Object.prototype.propertyIsEnumerable;
|
553
|
+
function shallowCopy(original, options) {
|
554
|
+
let markResult;
|
555
|
+
if (Array.isArray(original)) {
|
556
|
+
return Array.prototype.concat.call(original);
|
557
|
+
}
|
558
|
+
else if (original instanceof Set) {
|
559
|
+
if (!isBaseSetInstance(original)) {
|
560
|
+
const SubClass = Object.getPrototypeOf(original).constructor;
|
561
|
+
return new SubClass(original.values());
|
562
|
+
}
|
563
|
+
return Set.prototype.difference
|
564
|
+
? Set.prototype.difference.call(original, new Set())
|
565
|
+
: new Set(original.values());
|
566
|
+
}
|
567
|
+
else if (original instanceof Map) {
|
568
|
+
if (!isBaseMapInstance(original)) {
|
569
|
+
const SubClass = Object.getPrototypeOf(original).constructor;
|
570
|
+
return new SubClass(original);
|
571
|
+
}
|
572
|
+
return new Map(original);
|
573
|
+
}
|
574
|
+
else if ((options === null || options === undefined ? undefined : options.mark) &&
|
575
|
+
((markResult = options.mark(original, dataTypes)),
|
576
|
+
markResult !== undefined) &&
|
577
|
+
markResult !== dataTypes.mutable) {
|
578
|
+
if (markResult === dataTypes.immutable) {
|
579
|
+
return strictCopy(original);
|
580
|
+
}
|
581
|
+
else if (typeof markResult === 'function') {
|
582
|
+
if ((options.enablePatches || options.enableAutoFreeze)) {
|
583
|
+
throw new Error(`You can't use mark and patches or auto freeze together.`);
|
584
|
+
}
|
585
|
+
return markResult();
|
586
|
+
}
|
587
|
+
throw new Error(`Unsupported mark result: ${markResult}`);
|
588
|
+
}
|
589
|
+
else if (typeof original === 'object' &&
|
590
|
+
Object.getPrototypeOf(original) === Object.prototype) {
|
591
|
+
// For best performance with shallow copies,
|
592
|
+
// don't use `Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));` by default.
|
593
|
+
const copy = {};
|
594
|
+
Object.keys(original).forEach((key) => {
|
595
|
+
copy[key] = original[key];
|
596
|
+
});
|
597
|
+
Object.getOwnPropertySymbols(original).forEach((key) => {
|
598
|
+
if (propIsEnum.call(original, key)) {
|
599
|
+
copy[key] = original[key];
|
600
|
+
}
|
601
|
+
});
|
602
|
+
return copy;
|
603
|
+
}
|
604
|
+
else {
|
605
|
+
throw new Error(`Please check mark() to ensure that it is a stable marker draftable function.`);
|
606
|
+
}
|
607
|
+
}
|
608
|
+
function ensureShallowCopy(target) {
|
609
|
+
if (target.copy)
|
610
|
+
return;
|
611
|
+
target.copy = shallowCopy(target.original, target.options);
|
612
|
+
}
|
613
|
+
function deepClone(target) {
|
614
|
+
if (!isDraftable(target))
|
615
|
+
return getValue(target);
|
616
|
+
if (Array.isArray(target))
|
617
|
+
return target.map(deepClone);
|
618
|
+
if (target instanceof Map) {
|
619
|
+
const iterable = Array.from(target.entries()).map(([k, v]) => [
|
620
|
+
k,
|
621
|
+
deepClone(v),
|
622
|
+
]);
|
623
|
+
if (!isBaseMapInstance(target)) {
|
624
|
+
const SubClass = Object.getPrototypeOf(target).constructor;
|
625
|
+
return new SubClass(iterable);
|
626
|
+
}
|
627
|
+
return new Map(iterable);
|
628
|
+
}
|
629
|
+
if (target instanceof Set) {
|
630
|
+
const iterable = Array.from(target).map(deepClone);
|
631
|
+
if (!isBaseSetInstance(target)) {
|
632
|
+
const SubClass = Object.getPrototypeOf(target).constructor;
|
633
|
+
return new SubClass(iterable);
|
634
|
+
}
|
635
|
+
return new Set(iterable);
|
636
|
+
}
|
637
|
+
const copy = Object.create(Object.getPrototypeOf(target));
|
638
|
+
for (const key in target)
|
639
|
+
copy[key] = deepClone(target[key]);
|
640
|
+
return copy;
|
641
|
+
}
|
642
|
+
function cloneIfNeeded(target) {
|
643
|
+
return isDraft(target) ? deepClone(target) : target;
|
644
|
+
}
|
645
|
+
|
646
|
+
function markChanged(proxyDraft) {
|
647
|
+
var _a;
|
648
|
+
proxyDraft.assignedMap = (_a = proxyDraft.assignedMap) !== null && _a !== undefined ? _a : new Map();
|
649
|
+
if (!proxyDraft.operated) {
|
650
|
+
proxyDraft.operated = true;
|
651
|
+
if (proxyDraft.parent) {
|
652
|
+
markChanged(proxyDraft.parent);
|
653
|
+
}
|
654
|
+
}
|
655
|
+
}
|
656
|
+
|
657
|
+
function throwFrozenError() {
|
658
|
+
throw new Error('Cannot modify frozen object');
|
659
|
+
}
|
660
|
+
function deepFreeze(target, subKey, updatedValues, stack, keys) {
|
661
|
+
{
|
662
|
+
updatedValues = updatedValues !== null && updatedValues !== undefined ? updatedValues : new WeakMap();
|
663
|
+
stack = stack !== null && stack !== undefined ? stack : [];
|
664
|
+
keys = keys !== null && keys !== undefined ? keys : [];
|
665
|
+
const value = updatedValues.has(target)
|
666
|
+
? updatedValues.get(target)
|
667
|
+
: target;
|
668
|
+
if (stack.length > 0) {
|
669
|
+
const index = stack.indexOf(value);
|
670
|
+
if (value && typeof value === 'object' && index !== -1) {
|
671
|
+
if (stack[0] === value) {
|
672
|
+
throw new Error(`Forbids circular reference`);
|
673
|
+
}
|
674
|
+
throw new Error(`Forbids circular reference: ~/${keys
|
675
|
+
.slice(0, index)
|
676
|
+
.map((key, index) => {
|
677
|
+
if (typeof key === 'symbol')
|
678
|
+
return `[${key.toString()}]`;
|
679
|
+
const parent = stack[index];
|
680
|
+
if (typeof key === 'object' &&
|
681
|
+
(parent instanceof Map || parent instanceof Set))
|
682
|
+
return Array.from(parent.keys()).indexOf(key);
|
683
|
+
return key;
|
684
|
+
})
|
685
|
+
.join('/')}`);
|
686
|
+
}
|
687
|
+
stack.push(value);
|
688
|
+
keys.push(subKey);
|
689
|
+
}
|
690
|
+
else {
|
691
|
+
stack.push(value);
|
692
|
+
}
|
693
|
+
}
|
694
|
+
if (Object.isFrozen(target) || isDraft(target)) {
|
695
|
+
{
|
696
|
+
stack.pop();
|
697
|
+
keys.pop();
|
698
|
+
}
|
699
|
+
return;
|
700
|
+
}
|
701
|
+
const type = getType(target);
|
702
|
+
switch (type) {
|
703
|
+
case 2 /* DraftType.Map */:
|
704
|
+
for (const [key, value] of target) {
|
705
|
+
deepFreeze(key, key, updatedValues, stack, keys);
|
706
|
+
deepFreeze(value, key, updatedValues, stack, keys);
|
707
|
+
}
|
708
|
+
target.set = target.clear = target.delete = throwFrozenError;
|
709
|
+
break;
|
710
|
+
case 3 /* DraftType.Set */:
|
711
|
+
for (const value of target) {
|
712
|
+
deepFreeze(value, value, updatedValues, stack, keys);
|
713
|
+
}
|
714
|
+
target.add = target.clear = target.delete = throwFrozenError;
|
715
|
+
break;
|
716
|
+
case 1 /* DraftType.Array */:
|
717
|
+
Object.freeze(target);
|
718
|
+
let index = 0;
|
719
|
+
for (const value of target) {
|
720
|
+
deepFreeze(value, index, updatedValues, stack, keys);
|
721
|
+
index += 1;
|
722
|
+
}
|
723
|
+
break;
|
724
|
+
default:
|
725
|
+
Object.freeze(target);
|
726
|
+
// ignore non-enumerable or symbol properties
|
727
|
+
Object.keys(target).forEach((name) => {
|
728
|
+
const value = target[name];
|
729
|
+
deepFreeze(value, name, updatedValues, stack, keys);
|
730
|
+
});
|
731
|
+
}
|
732
|
+
{
|
733
|
+
stack.pop();
|
734
|
+
keys.pop();
|
735
|
+
}
|
736
|
+
}
|
737
|
+
|
738
|
+
function forEach(target, iter) {
|
739
|
+
const type = getType(target);
|
740
|
+
if (type === 0 /* DraftType.Object */) {
|
741
|
+
Reflect.ownKeys(target).forEach((key) => {
|
742
|
+
iter(key, target[key], target);
|
743
|
+
});
|
744
|
+
}
|
745
|
+
else if (type === 1 /* DraftType.Array */) {
|
746
|
+
let index = 0;
|
747
|
+
for (const entry of target) {
|
748
|
+
iter(index, entry, target);
|
749
|
+
index += 1;
|
750
|
+
}
|
751
|
+
}
|
752
|
+
else {
|
753
|
+
target.forEach((entry, index) => iter(index, entry, target));
|
754
|
+
}
|
755
|
+
}
|
756
|
+
|
757
|
+
function handleValue(target, handledSet, options) {
|
758
|
+
if (isDraft(target) ||
|
759
|
+
!isDraftable(target, options) ||
|
760
|
+
handledSet.has(target) ||
|
761
|
+
Object.isFrozen(target))
|
762
|
+
return;
|
763
|
+
const isSet = target instanceof Set;
|
764
|
+
const setMap = isSet ? new Map() : undefined;
|
765
|
+
handledSet.add(target);
|
766
|
+
forEach(target, (key, value) => {
|
767
|
+
var _a;
|
768
|
+
if (isDraft(value)) {
|
769
|
+
const proxyDraft = getProxyDraft(value);
|
770
|
+
ensureShallowCopy(proxyDraft);
|
771
|
+
// A draft where a child node has been changed, or assigned a value
|
772
|
+
const updatedValue = ((_a = proxyDraft.assignedMap) === null || _a === undefined ? undefined : _a.size) || proxyDraft.operated
|
773
|
+
? proxyDraft.copy
|
774
|
+
: proxyDraft.original;
|
775
|
+
// final update value
|
776
|
+
set(isSet ? setMap : target, key, updatedValue);
|
777
|
+
}
|
778
|
+
else {
|
779
|
+
handleValue(value, handledSet, options);
|
780
|
+
}
|
781
|
+
});
|
782
|
+
if (setMap) {
|
783
|
+
const set = target;
|
784
|
+
const values = Array.from(set);
|
785
|
+
set.clear();
|
786
|
+
values.forEach((value) => {
|
787
|
+
set.add(setMap.has(value) ? setMap.get(value) : value);
|
788
|
+
});
|
789
|
+
}
|
790
|
+
}
|
791
|
+
function finalizeAssigned(proxyDraft, key) {
|
792
|
+
// handle the draftable assigned values, and the value is not a draft
|
793
|
+
const copy = proxyDraft.type === 3 /* DraftType.Set */ ? proxyDraft.setMap : proxyDraft.copy;
|
794
|
+
if (proxyDraft.finalities.revoke.length > 1 &&
|
795
|
+
proxyDraft.assignedMap.get(key) &&
|
796
|
+
copy) {
|
797
|
+
handleValue(get(copy, key), proxyDraft.finalities.handledSet, proxyDraft.options);
|
798
|
+
}
|
799
|
+
}
|
800
|
+
function finalizeSetValue(target) {
|
801
|
+
if (target.type === 3 /* DraftType.Set */ && target.copy) {
|
802
|
+
target.copy.clear();
|
803
|
+
target.setMap.forEach((value) => {
|
804
|
+
target.copy.add(getValue(value));
|
805
|
+
});
|
806
|
+
}
|
807
|
+
}
|
808
|
+
function finalizePatches(target, generatePatches, patches, inversePatches) {
|
809
|
+
const shouldFinalize = target.operated &&
|
810
|
+
target.assignedMap &&
|
811
|
+
target.assignedMap.size > 0 &&
|
812
|
+
!target.finalized;
|
813
|
+
if (shouldFinalize) {
|
814
|
+
if (patches && inversePatches) {
|
815
|
+
const basePath = getPath(target);
|
816
|
+
if (basePath) {
|
817
|
+
generatePatches(target, basePath, patches, inversePatches);
|
818
|
+
}
|
819
|
+
}
|
820
|
+
target.finalized = true;
|
821
|
+
}
|
822
|
+
}
|
823
|
+
function markFinalization(target, key, value, generatePatches) {
|
824
|
+
const proxyDraft = getProxyDraft(value);
|
825
|
+
if (proxyDraft) {
|
826
|
+
// !case: assign the draft value
|
827
|
+
if (!proxyDraft.callbacks) {
|
828
|
+
proxyDraft.callbacks = [];
|
829
|
+
}
|
830
|
+
proxyDraft.callbacks.push((patches, inversePatches) => {
|
831
|
+
var _a;
|
832
|
+
const copy = target.type === 3 /* DraftType.Set */ ? target.setMap : target.copy;
|
833
|
+
if (isEqual(get(copy, key), value)) {
|
834
|
+
let updatedValue = proxyDraft.original;
|
835
|
+
if (proxyDraft.copy) {
|
836
|
+
updatedValue = proxyDraft.copy;
|
837
|
+
}
|
838
|
+
finalizeSetValue(target);
|
839
|
+
finalizePatches(target, generatePatches, patches, inversePatches);
|
840
|
+
if (target.options.enableAutoFreeze) {
|
841
|
+
target.options.updatedValues =
|
842
|
+
(_a = target.options.updatedValues) !== null && _a !== undefined ? _a : new WeakMap();
|
843
|
+
target.options.updatedValues.set(updatedValue, proxyDraft.original);
|
844
|
+
}
|
845
|
+
// final update value
|
846
|
+
set(copy, key, updatedValue);
|
847
|
+
}
|
848
|
+
});
|
849
|
+
if (target.options.enableAutoFreeze) {
|
850
|
+
// !case: assign the draft value in cross draft tree
|
851
|
+
if (proxyDraft.finalities !== target.finalities) {
|
852
|
+
target.options.enableAutoFreeze = false;
|
853
|
+
}
|
854
|
+
}
|
855
|
+
}
|
856
|
+
if (isDraftable(value, target.options)) {
|
857
|
+
// !case: assign the non-draft value
|
858
|
+
target.finalities.draft.push(() => {
|
859
|
+
const copy = target.type === 3 /* DraftType.Set */ ? target.setMap : target.copy;
|
860
|
+
if (isEqual(get(copy, key), value)) {
|
861
|
+
finalizeAssigned(target, key);
|
862
|
+
}
|
863
|
+
});
|
864
|
+
}
|
865
|
+
}
|
866
|
+
|
867
|
+
function generateArrayPatches(proxyState, basePath, patches, inversePatches, pathAsArray) {
|
868
|
+
let { original, assignedMap, options } = proxyState;
|
869
|
+
let copy = proxyState.copy;
|
870
|
+
if (copy.length < original.length) {
|
871
|
+
[original, copy] = [copy, original];
|
872
|
+
[patches, inversePatches] = [inversePatches, patches];
|
873
|
+
}
|
874
|
+
for (let index = 0; index < original.length; index += 1) {
|
875
|
+
if (assignedMap.get(index.toString()) && copy[index] !== original[index]) {
|
876
|
+
const _path = basePath.concat([index]);
|
877
|
+
const path = escapePath(_path, pathAsArray);
|
878
|
+
patches.push({
|
879
|
+
op: Operation.Replace,
|
880
|
+
path,
|
881
|
+
// If it is a draft, it needs to be deep cloned, and it may also be non-draft.
|
882
|
+
value: cloneIfNeeded(copy[index]),
|
883
|
+
});
|
884
|
+
inversePatches.push({
|
885
|
+
op: Operation.Replace,
|
886
|
+
path,
|
887
|
+
// If it is a draft, it needs to be deep cloned, and it may also be non-draft.
|
888
|
+
value: cloneIfNeeded(original[index]),
|
889
|
+
});
|
890
|
+
}
|
891
|
+
}
|
892
|
+
for (let index = original.length; index < copy.length; index += 1) {
|
893
|
+
const _path = basePath.concat([index]);
|
894
|
+
const path = escapePath(_path, pathAsArray);
|
895
|
+
patches.push({
|
896
|
+
op: Operation.Add,
|
897
|
+
path,
|
898
|
+
// If it is a draft, it needs to be deep cloned, and it may also be non-draft.
|
899
|
+
value: cloneIfNeeded(copy[index]),
|
900
|
+
});
|
901
|
+
}
|
902
|
+
if (original.length < copy.length) {
|
903
|
+
// https://www.rfc-editor.org/rfc/rfc6902#appendix-A.4
|
904
|
+
// For performance, here we only generate an operation that replaces the length of the array,
|
905
|
+
// which is inconsistent with JSON Patch specification
|
906
|
+
const { arrayLengthAssignment = true } = options.enablePatches;
|
907
|
+
if (arrayLengthAssignment) {
|
908
|
+
const _path = basePath.concat(['length']);
|
909
|
+
const path = escapePath(_path, pathAsArray);
|
910
|
+
inversePatches.push({
|
911
|
+
op: Operation.Replace,
|
912
|
+
path,
|
913
|
+
value: original.length,
|
914
|
+
});
|
915
|
+
}
|
916
|
+
else {
|
917
|
+
for (let index = copy.length; original.length < index; index -= 1) {
|
918
|
+
const _path = basePath.concat([index - 1]);
|
919
|
+
const path = escapePath(_path, pathAsArray);
|
920
|
+
inversePatches.push({
|
921
|
+
op: Operation.Remove,
|
922
|
+
path,
|
923
|
+
});
|
924
|
+
}
|
925
|
+
}
|
926
|
+
}
|
927
|
+
}
|
928
|
+
function generatePatchesFromAssigned({ original, copy, assignedMap }, basePath, patches, inversePatches, pathAsArray) {
|
929
|
+
assignedMap.forEach((assignedValue, key) => {
|
930
|
+
const originalValue = get(original, key);
|
931
|
+
const value = cloneIfNeeded(get(copy, key));
|
932
|
+
const op = !assignedValue
|
933
|
+
? Operation.Remove
|
934
|
+
: has(original, key)
|
935
|
+
? Operation.Replace
|
936
|
+
: Operation.Add;
|
937
|
+
if (isEqual(originalValue, value) && op === Operation.Replace)
|
938
|
+
return;
|
939
|
+
const _path = basePath.concat(key);
|
940
|
+
const path = escapePath(_path, pathAsArray);
|
941
|
+
patches.push(op === Operation.Remove ? { op, path } : { op, path, value });
|
942
|
+
inversePatches.push(op === Operation.Add
|
943
|
+
? { op: Operation.Remove, path }
|
944
|
+
: op === Operation.Remove
|
945
|
+
? { op: Operation.Add, path, value: originalValue }
|
946
|
+
: { op: Operation.Replace, path, value: originalValue });
|
947
|
+
});
|
948
|
+
}
|
949
|
+
function generateSetPatches({ original, copy }, basePath, patches, inversePatches, pathAsArray) {
|
950
|
+
let index = 0;
|
951
|
+
original.forEach((value) => {
|
952
|
+
if (!copy.has(value)) {
|
953
|
+
const _path = basePath.concat([index]);
|
954
|
+
const path = escapePath(_path, pathAsArray);
|
955
|
+
patches.push({
|
956
|
+
op: Operation.Remove,
|
957
|
+
path,
|
958
|
+
value,
|
959
|
+
});
|
960
|
+
inversePatches.unshift({
|
961
|
+
op: Operation.Add,
|
962
|
+
path,
|
963
|
+
value,
|
964
|
+
});
|
965
|
+
}
|
966
|
+
index += 1;
|
967
|
+
});
|
968
|
+
index = 0;
|
969
|
+
copy.forEach((value) => {
|
970
|
+
if (!original.has(value)) {
|
971
|
+
const _path = basePath.concat([index]);
|
972
|
+
const path = escapePath(_path, pathAsArray);
|
973
|
+
patches.push({
|
974
|
+
op: Operation.Add,
|
975
|
+
path,
|
976
|
+
value,
|
977
|
+
});
|
978
|
+
inversePatches.unshift({
|
979
|
+
op: Operation.Remove,
|
980
|
+
path,
|
981
|
+
value,
|
982
|
+
});
|
983
|
+
}
|
984
|
+
index += 1;
|
985
|
+
});
|
986
|
+
}
|
987
|
+
function generatePatches(proxyState, basePath, patches, inversePatches) {
|
988
|
+
const { pathAsArray = true } = proxyState.options.enablePatches;
|
989
|
+
switch (proxyState.type) {
|
990
|
+
case 0 /* DraftType.Object */:
|
991
|
+
case 2 /* DraftType.Map */:
|
992
|
+
return generatePatchesFromAssigned(proxyState, basePath, patches, inversePatches, pathAsArray);
|
993
|
+
case 1 /* DraftType.Array */:
|
994
|
+
return generateArrayPatches(proxyState, basePath, patches, inversePatches, pathAsArray);
|
995
|
+
case 3 /* DraftType.Set */:
|
996
|
+
return generateSetPatches(proxyState, basePath, patches, inversePatches, pathAsArray);
|
997
|
+
}
|
998
|
+
}
|
999
|
+
const checkReadable = (value, options, ignoreCheckDraftable = false) => {
|
1000
|
+
if (typeof value === 'object' &&
|
1001
|
+
value !== null &&
|
1002
|
+
(!isDraftable(value, options) || ignoreCheckDraftable) &&
|
1003
|
+
true) {
|
1004
|
+
throw new Error(`Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap.`);
|
1005
|
+
}
|
1006
|
+
};
|
1007
|
+
|
1008
|
+
const mapHandler = {
|
1009
|
+
get size() {
|
1010
|
+
const current = latest(getProxyDraft(this));
|
1011
|
+
return current.size;
|
1012
|
+
},
|
1013
|
+
has(key) {
|
1014
|
+
return latest(getProxyDraft(this)).has(key);
|
1015
|
+
},
|
1016
|
+
set(key, value) {
|
1017
|
+
const target = getProxyDraft(this);
|
1018
|
+
const source = latest(target);
|
1019
|
+
if (!source.has(key) || !isEqual(source.get(key), value)) {
|
1020
|
+
ensureShallowCopy(target);
|
1021
|
+
markChanged(target);
|
1022
|
+
target.assignedMap.set(key, true);
|
1023
|
+
target.copy.set(key, value);
|
1024
|
+
markFinalization(target, key, value, generatePatches);
|
1025
|
+
}
|
1026
|
+
return this;
|
1027
|
+
},
|
1028
|
+
delete(key) {
|
1029
|
+
if (!this.has(key)) {
|
1030
|
+
return false;
|
1031
|
+
}
|
1032
|
+
const target = getProxyDraft(this);
|
1033
|
+
ensureShallowCopy(target);
|
1034
|
+
markChanged(target);
|
1035
|
+
if (target.original.has(key)) {
|
1036
|
+
target.assignedMap.set(key, false);
|
1037
|
+
}
|
1038
|
+
else {
|
1039
|
+
target.assignedMap.delete(key);
|
1040
|
+
}
|
1041
|
+
target.copy.delete(key);
|
1042
|
+
return true;
|
1043
|
+
},
|
1044
|
+
clear() {
|
1045
|
+
const target = getProxyDraft(this);
|
1046
|
+
if (!this.size)
|
1047
|
+
return;
|
1048
|
+
ensureShallowCopy(target);
|
1049
|
+
markChanged(target);
|
1050
|
+
target.assignedMap = new Map();
|
1051
|
+
for (const [key] of target.original) {
|
1052
|
+
target.assignedMap.set(key, false);
|
1053
|
+
}
|
1054
|
+
target.copy.clear();
|
1055
|
+
},
|
1056
|
+
forEach(callback, thisArg) {
|
1057
|
+
const target = getProxyDraft(this);
|
1058
|
+
latest(target).forEach((_value, _key) => {
|
1059
|
+
callback.call(thisArg, this.get(_key), _key, this);
|
1060
|
+
});
|
1061
|
+
},
|
1062
|
+
get(key) {
|
1063
|
+
var _a, _b;
|
1064
|
+
const target = getProxyDraft(this);
|
1065
|
+
const value = latest(target).get(key);
|
1066
|
+
const mutable = ((_b = (_a = target.options).mark) === null || _b === undefined ? undefined : _b.call(_a, value, dataTypes)) === dataTypes.mutable;
|
1067
|
+
if (target.options.strict) {
|
1068
|
+
checkReadable(value, target.options, mutable);
|
1069
|
+
}
|
1070
|
+
if (mutable) {
|
1071
|
+
return value;
|
1072
|
+
}
|
1073
|
+
if (target.finalized || !isDraftable(value, target.options)) {
|
1074
|
+
return value;
|
1075
|
+
}
|
1076
|
+
// drafted or reassigned
|
1077
|
+
if (value !== target.original.get(key)) {
|
1078
|
+
return value;
|
1079
|
+
}
|
1080
|
+
const draft = internal.createDraft({
|
1081
|
+
original: value,
|
1082
|
+
parentDraft: target,
|
1083
|
+
key,
|
1084
|
+
finalities: target.finalities,
|
1085
|
+
options: target.options,
|
1086
|
+
});
|
1087
|
+
ensureShallowCopy(target);
|
1088
|
+
target.copy.set(key, draft);
|
1089
|
+
return draft;
|
1090
|
+
},
|
1091
|
+
keys() {
|
1092
|
+
return latest(getProxyDraft(this)).keys();
|
1093
|
+
},
|
1094
|
+
values() {
|
1095
|
+
const iterator = this.keys();
|
1096
|
+
return {
|
1097
|
+
[iteratorSymbol]: () => this.values(),
|
1098
|
+
next: () => {
|
1099
|
+
const result = iterator.next();
|
1100
|
+
if (result.done)
|
1101
|
+
return result;
|
1102
|
+
const value = this.get(result.value);
|
1103
|
+
return {
|
1104
|
+
done: false,
|
1105
|
+
value,
|
1106
|
+
};
|
1107
|
+
},
|
1108
|
+
};
|
1109
|
+
},
|
1110
|
+
entries() {
|
1111
|
+
const iterator = this.keys();
|
1112
|
+
return {
|
1113
|
+
[iteratorSymbol]: () => this.entries(),
|
1114
|
+
next: () => {
|
1115
|
+
const result = iterator.next();
|
1116
|
+
if (result.done)
|
1117
|
+
return result;
|
1118
|
+
const value = this.get(result.value);
|
1119
|
+
return {
|
1120
|
+
done: false,
|
1121
|
+
value: [result.value, value],
|
1122
|
+
};
|
1123
|
+
},
|
1124
|
+
};
|
1125
|
+
},
|
1126
|
+
[iteratorSymbol]() {
|
1127
|
+
return this.entries();
|
1128
|
+
},
|
1129
|
+
};
|
1130
|
+
const mapHandlerKeys = Reflect.ownKeys(mapHandler);
|
1131
|
+
|
1132
|
+
const getNextIterator = (target, iterator, { isValuesIterator }) => () => {
|
1133
|
+
var _a, _b;
|
1134
|
+
const result = iterator.next();
|
1135
|
+
if (result.done)
|
1136
|
+
return result;
|
1137
|
+
const key = result.value;
|
1138
|
+
let value = target.setMap.get(key);
|
1139
|
+
const currentDraft = getProxyDraft(value);
|
1140
|
+
const mutable = ((_b = (_a = target.options).mark) === null || _b === undefined ? undefined : _b.call(_a, value, dataTypes)) === dataTypes.mutable;
|
1141
|
+
if (target.options.strict) {
|
1142
|
+
checkReadable(key, target.options, mutable);
|
1143
|
+
}
|
1144
|
+
if (!mutable &&
|
1145
|
+
!currentDraft &&
|
1146
|
+
isDraftable(key, target.options) &&
|
1147
|
+
!target.finalized &&
|
1148
|
+
target.original.has(key)) {
|
1149
|
+
// draft a draftable original set item
|
1150
|
+
const proxy = internal.createDraft({
|
1151
|
+
original: key,
|
1152
|
+
parentDraft: target,
|
1153
|
+
key,
|
1154
|
+
finalities: target.finalities,
|
1155
|
+
options: target.options,
|
1156
|
+
});
|
1157
|
+
target.setMap.set(key, proxy);
|
1158
|
+
value = proxy;
|
1159
|
+
}
|
1160
|
+
else if (currentDraft) {
|
1161
|
+
// drafted
|
1162
|
+
value = currentDraft.proxy;
|
1163
|
+
}
|
1164
|
+
return {
|
1165
|
+
done: false,
|
1166
|
+
value: isValuesIterator ? value : [value, value],
|
1167
|
+
};
|
1168
|
+
};
|
1169
|
+
const setHandler = {
|
1170
|
+
get size() {
|
1171
|
+
const target = getProxyDraft(this);
|
1172
|
+
return target.setMap.size;
|
1173
|
+
},
|
1174
|
+
has(value) {
|
1175
|
+
const target = getProxyDraft(this);
|
1176
|
+
// reassigned or non-draftable values
|
1177
|
+
if (target.setMap.has(value))
|
1178
|
+
return true;
|
1179
|
+
ensureShallowCopy(target);
|
1180
|
+
const valueProxyDraft = getProxyDraft(value);
|
1181
|
+
// drafted
|
1182
|
+
if (valueProxyDraft && target.setMap.has(valueProxyDraft.original))
|
1183
|
+
return true;
|
1184
|
+
return false;
|
1185
|
+
},
|
1186
|
+
add(value) {
|
1187
|
+
const target = getProxyDraft(this);
|
1188
|
+
if (!this.has(value)) {
|
1189
|
+
ensureShallowCopy(target);
|
1190
|
+
markChanged(target);
|
1191
|
+
target.assignedMap.set(value, true);
|
1192
|
+
target.setMap.set(value, value);
|
1193
|
+
markFinalization(target, value, value, generatePatches);
|
1194
|
+
}
|
1195
|
+
return this;
|
1196
|
+
},
|
1197
|
+
delete(value) {
|
1198
|
+
if (!this.has(value)) {
|
1199
|
+
return false;
|
1200
|
+
}
|
1201
|
+
const target = getProxyDraft(this);
|
1202
|
+
ensureShallowCopy(target);
|
1203
|
+
markChanged(target);
|
1204
|
+
const valueProxyDraft = getProxyDraft(value);
|
1205
|
+
if (valueProxyDraft && target.setMap.has(valueProxyDraft.original)) {
|
1206
|
+
// delete drafted
|
1207
|
+
target.assignedMap.set(valueProxyDraft.original, false);
|
1208
|
+
return target.setMap.delete(valueProxyDraft.original);
|
1209
|
+
}
|
1210
|
+
if (!valueProxyDraft && target.setMap.has(value)) {
|
1211
|
+
// non-draftable values
|
1212
|
+
target.assignedMap.set(value, false);
|
1213
|
+
}
|
1214
|
+
else {
|
1215
|
+
// reassigned
|
1216
|
+
target.assignedMap.delete(value);
|
1217
|
+
}
|
1218
|
+
// delete reassigned or non-draftable values
|
1219
|
+
return target.setMap.delete(value);
|
1220
|
+
},
|
1221
|
+
clear() {
|
1222
|
+
if (!this.size)
|
1223
|
+
return;
|
1224
|
+
const target = getProxyDraft(this);
|
1225
|
+
ensureShallowCopy(target);
|
1226
|
+
markChanged(target);
|
1227
|
+
for (const value of target.original) {
|
1228
|
+
target.assignedMap.set(value, false);
|
1229
|
+
}
|
1230
|
+
target.setMap.clear();
|
1231
|
+
},
|
1232
|
+
values() {
|
1233
|
+
const target = getProxyDraft(this);
|
1234
|
+
ensureShallowCopy(target);
|
1235
|
+
const iterator = target.setMap.keys();
|
1236
|
+
return {
|
1237
|
+
[Symbol.iterator]: () => this.values(),
|
1238
|
+
next: getNextIterator(target, iterator, { isValuesIterator: true }),
|
1239
|
+
};
|
1240
|
+
},
|
1241
|
+
entries() {
|
1242
|
+
const target = getProxyDraft(this);
|
1243
|
+
ensureShallowCopy(target);
|
1244
|
+
const iterator = target.setMap.keys();
|
1245
|
+
return {
|
1246
|
+
[Symbol.iterator]: () => this.entries(),
|
1247
|
+
next: getNextIterator(target, iterator, {
|
1248
|
+
isValuesIterator: false,
|
1249
|
+
}),
|
1250
|
+
};
|
1251
|
+
},
|
1252
|
+
keys() {
|
1253
|
+
return this.values();
|
1254
|
+
},
|
1255
|
+
[iteratorSymbol]() {
|
1256
|
+
return this.values();
|
1257
|
+
},
|
1258
|
+
forEach(callback, thisArg) {
|
1259
|
+
const iterator = this.values();
|
1260
|
+
let result = iterator.next();
|
1261
|
+
while (!result.done) {
|
1262
|
+
callback.call(thisArg, result.value, result.value, this);
|
1263
|
+
result = iterator.next();
|
1264
|
+
}
|
1265
|
+
},
|
1266
|
+
};
|
1267
|
+
if (Set.prototype.difference) {
|
1268
|
+
// for compatibility with new Set methods
|
1269
|
+
// https://github.com/tc39/proposal-set-methods
|
1270
|
+
// And `https://github.com/tc39/proposal-set-methods/blob/main/details.md#symbolspecies` has some details about the `@@species` symbol.
|
1271
|
+
// So we can't use SubSet instance constructor to get the constructor of the SubSet instance.
|
1272
|
+
Object.assign(setHandler, {
|
1273
|
+
intersection(other) {
|
1274
|
+
return Set.prototype.intersection.call(new Set(this.values()), other);
|
1275
|
+
},
|
1276
|
+
union(other) {
|
1277
|
+
return Set.prototype.union.call(new Set(this.values()), other);
|
1278
|
+
},
|
1279
|
+
difference(other) {
|
1280
|
+
return Set.prototype.difference.call(new Set(this.values()), other);
|
1281
|
+
},
|
1282
|
+
symmetricDifference(other) {
|
1283
|
+
return Set.prototype.symmetricDifference.call(new Set(this.values()), other);
|
1284
|
+
},
|
1285
|
+
isSubsetOf(other) {
|
1286
|
+
return Set.prototype.isSubsetOf.call(new Set(this.values()), other);
|
1287
|
+
},
|
1288
|
+
isSupersetOf(other) {
|
1289
|
+
return Set.prototype.isSupersetOf.call(new Set(this.values()), other);
|
1290
|
+
},
|
1291
|
+
isDisjointFrom(other) {
|
1292
|
+
return Set.prototype.isDisjointFrom.call(new Set(this.values()), other);
|
1293
|
+
},
|
1294
|
+
});
|
1295
|
+
}
|
1296
|
+
const setHandlerKeys = Reflect.ownKeys(setHandler);
|
1297
|
+
|
1298
|
+
const draftsCache = new WeakSet();
|
1299
|
+
const proxyHandler = {
|
1300
|
+
get(target, key, receiver) {
|
1301
|
+
var _a, _b;
|
1302
|
+
const copy = (_a = target.copy) === null || _a === undefined ? undefined : _a[key];
|
1303
|
+
// Improve draft reading performance by caching the draft copy.
|
1304
|
+
if (copy && draftsCache.has(copy)) {
|
1305
|
+
return copy;
|
1306
|
+
}
|
1307
|
+
if (key === PROXY_DRAFT)
|
1308
|
+
return target;
|
1309
|
+
let markResult;
|
1310
|
+
if (target.options.mark) {
|
1311
|
+
// handle `Uncaught TypeError: Method get Map.prototype.size called on incompatible receiver #<Map>`
|
1312
|
+
// or `Uncaught TypeError: Method get Set.prototype.size called on incompatible receiver #<Set>`
|
1313
|
+
const value = key === 'size' &&
|
1314
|
+
(target.original instanceof Map || target.original instanceof Set)
|
1315
|
+
? Reflect.get(target.original, key)
|
1316
|
+
: Reflect.get(target.original, key, receiver);
|
1317
|
+
markResult = target.options.mark(value, dataTypes);
|
1318
|
+
if (markResult === dataTypes.mutable) {
|
1319
|
+
if (target.options.strict) {
|
1320
|
+
checkReadable(value, target.options, true);
|
1321
|
+
}
|
1322
|
+
return value;
|
1323
|
+
}
|
1324
|
+
}
|
1325
|
+
const source = latest(target);
|
1326
|
+
if (source instanceof Map && mapHandlerKeys.includes(key)) {
|
1327
|
+
if (key === 'size') {
|
1328
|
+
return Object.getOwnPropertyDescriptor(mapHandler, 'size').get.call(target.proxy);
|
1329
|
+
}
|
1330
|
+
const handle = mapHandler[key];
|
1331
|
+
if (handle) {
|
1332
|
+
return handle.bind(target.proxy);
|
1333
|
+
}
|
1334
|
+
}
|
1335
|
+
if (source instanceof Set && setHandlerKeys.includes(key)) {
|
1336
|
+
if (key === 'size') {
|
1337
|
+
return Object.getOwnPropertyDescriptor(setHandler, 'size').get.call(target.proxy);
|
1338
|
+
}
|
1339
|
+
const handle = setHandler[key];
|
1340
|
+
if (handle) {
|
1341
|
+
return handle.bind(target.proxy);
|
1342
|
+
}
|
1343
|
+
}
|
1344
|
+
if (!has(source, key)) {
|
1345
|
+
const desc = getDescriptor(source, key);
|
1346
|
+
return desc
|
1347
|
+
? `value` in desc
|
1348
|
+
? desc.value
|
1349
|
+
: // !case: support for getter
|
1350
|
+
(_b = desc.get) === null || _b === undefined ? undefined : _b.call(target.proxy)
|
1351
|
+
: undefined;
|
1352
|
+
}
|
1353
|
+
const value = source[key];
|
1354
|
+
if (target.options.strict) {
|
1355
|
+
checkReadable(value, target.options);
|
1356
|
+
}
|
1357
|
+
if (target.finalized || !isDraftable(value, target.options)) {
|
1358
|
+
return value;
|
1359
|
+
}
|
1360
|
+
// Ensure that the assigned values are not drafted
|
1361
|
+
if (value === peek(target.original, key)) {
|
1362
|
+
ensureShallowCopy(target);
|
1363
|
+
target.copy[key] = createDraft({
|
1364
|
+
original: target.original[key],
|
1365
|
+
parentDraft: target,
|
1366
|
+
key: target.type === 1 /* DraftType.Array */ ? Number(key) : key,
|
1367
|
+
finalities: target.finalities,
|
1368
|
+
options: target.options,
|
1369
|
+
});
|
1370
|
+
// !case: support for custom shallow copy function
|
1371
|
+
if (typeof markResult === 'function') {
|
1372
|
+
const subProxyDraft = getProxyDraft(target.copy[key]);
|
1373
|
+
ensureShallowCopy(subProxyDraft);
|
1374
|
+
// Trigger a custom shallow copy to update to a new copy
|
1375
|
+
markChanged(subProxyDraft);
|
1376
|
+
return subProxyDraft.copy;
|
1377
|
+
}
|
1378
|
+
return target.copy[key];
|
1379
|
+
}
|
1380
|
+
return value;
|
1381
|
+
},
|
1382
|
+
set(target, key, value) {
|
1383
|
+
var _a;
|
1384
|
+
if (target.type === 3 /* DraftType.Set */ || target.type === 2 /* DraftType.Map */) {
|
1385
|
+
throw new Error(`Map/Set draft does not support any property assignment.`);
|
1386
|
+
}
|
1387
|
+
let _key;
|
1388
|
+
if (target.type === 1 /* DraftType.Array */ &&
|
1389
|
+
key !== 'length' &&
|
1390
|
+
!(Number.isInteger((_key = Number(key))) &&
|
1391
|
+
_key >= 0 &&
|
1392
|
+
(key === 0 || _key === 0 || String(_key) === String(key)))) {
|
1393
|
+
throw new Error(`Only supports setting array indices and the 'length' property.`);
|
1394
|
+
}
|
1395
|
+
const desc = getDescriptor(latest(target), key);
|
1396
|
+
if (desc === null || desc === undefined ? undefined : desc.set) {
|
1397
|
+
// !case: cover the case of setter
|
1398
|
+
desc.set.call(target.proxy, value);
|
1399
|
+
return true;
|
1400
|
+
}
|
1401
|
+
const current = peek(latest(target), key);
|
1402
|
+
const currentProxyDraft = getProxyDraft(current);
|
1403
|
+
if (currentProxyDraft && isEqual(currentProxyDraft.original, value)) {
|
1404
|
+
// !case: ignore the case of assigning the original draftable value to a draft
|
1405
|
+
target.copy[key] = value;
|
1406
|
+
target.assignedMap = (_a = target.assignedMap) !== null && _a !== undefined ? _a : new Map();
|
1407
|
+
target.assignedMap.set(key, false);
|
1408
|
+
return true;
|
1409
|
+
}
|
1410
|
+
// !case: handle new props with value 'undefined'
|
1411
|
+
if (isEqual(value, current) &&
|
1412
|
+
(value !== undefined || has(target.original, key)))
|
1413
|
+
return true;
|
1414
|
+
ensureShallowCopy(target);
|
1415
|
+
markChanged(target);
|
1416
|
+
if (has(target.original, key) && isEqual(value, target.original[key])) {
|
1417
|
+
// !case: handle the case of assigning the original non-draftable value to a draft
|
1418
|
+
target.assignedMap.delete(key);
|
1419
|
+
}
|
1420
|
+
else {
|
1421
|
+
target.assignedMap.set(key, true);
|
1422
|
+
}
|
1423
|
+
target.copy[key] = value;
|
1424
|
+
markFinalization(target, key, value, generatePatches);
|
1425
|
+
return true;
|
1426
|
+
},
|
1427
|
+
has(target, key) {
|
1428
|
+
return key in latest(target);
|
1429
|
+
},
|
1430
|
+
ownKeys(target) {
|
1431
|
+
return Reflect.ownKeys(latest(target));
|
1432
|
+
},
|
1433
|
+
getOwnPropertyDescriptor(target, key) {
|
1434
|
+
const source = latest(target);
|
1435
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(source, key);
|
1436
|
+
if (!descriptor)
|
1437
|
+
return descriptor;
|
1438
|
+
return {
|
1439
|
+
writable: true,
|
1440
|
+
configurable: target.type !== 1 /* DraftType.Array */ || key !== 'length',
|
1441
|
+
enumerable: descriptor.enumerable,
|
1442
|
+
value: source[key],
|
1443
|
+
};
|
1444
|
+
},
|
1445
|
+
getPrototypeOf(target) {
|
1446
|
+
return Reflect.getPrototypeOf(target.original);
|
1447
|
+
},
|
1448
|
+
setPrototypeOf() {
|
1449
|
+
throw new Error(`Cannot call 'setPrototypeOf()' on drafts`);
|
1450
|
+
},
|
1451
|
+
defineProperty() {
|
1452
|
+
throw new Error(`Cannot call 'defineProperty()' on drafts`);
|
1453
|
+
},
|
1454
|
+
deleteProperty(target, key) {
|
1455
|
+
var _a;
|
1456
|
+
if (target.type === 1 /* DraftType.Array */) {
|
1457
|
+
return proxyHandler.set.call(this, target, key, undefined, target.proxy);
|
1458
|
+
}
|
1459
|
+
if (peek(target.original, key) !== undefined || key in target.original) {
|
1460
|
+
// !case: delete an existing key
|
1461
|
+
ensureShallowCopy(target);
|
1462
|
+
markChanged(target);
|
1463
|
+
target.assignedMap.set(key, false);
|
1464
|
+
}
|
1465
|
+
else {
|
1466
|
+
target.assignedMap = (_a = target.assignedMap) !== null && _a !== undefined ? _a : new Map();
|
1467
|
+
// The original non-existent key has been deleted
|
1468
|
+
target.assignedMap.delete(key);
|
1469
|
+
}
|
1470
|
+
if (target.copy)
|
1471
|
+
delete target.copy[key];
|
1472
|
+
return true;
|
1473
|
+
},
|
1474
|
+
};
|
1475
|
+
function createDraft(createDraftOptions) {
|
1476
|
+
const { original, parentDraft, key, finalities, options } = createDraftOptions;
|
1477
|
+
const type = getType(original);
|
1478
|
+
const proxyDraft = {
|
1479
|
+
type,
|
1480
|
+
finalized: false,
|
1481
|
+
parent: parentDraft,
|
1482
|
+
original,
|
1483
|
+
copy: null,
|
1484
|
+
proxy: null,
|
1485
|
+
finalities,
|
1486
|
+
options,
|
1487
|
+
// Mapping of draft Set items to their corresponding draft values.
|
1488
|
+
setMap: type === 3 /* DraftType.Set */
|
1489
|
+
? new Map(original.entries())
|
1490
|
+
: undefined,
|
1491
|
+
};
|
1492
|
+
// !case: undefined as a draft map key
|
1493
|
+
if (key || 'key' in createDraftOptions) {
|
1494
|
+
proxyDraft.key = key;
|
1495
|
+
}
|
1496
|
+
const { proxy, revoke } = Proxy.revocable(type === 1 /* DraftType.Array */ ? Object.assign([], proxyDraft) : proxyDraft, proxyHandler);
|
1497
|
+
finalities.revoke.push(revoke);
|
1498
|
+
draftsCache.add(proxy);
|
1499
|
+
proxyDraft.proxy = proxy;
|
1500
|
+
if (parentDraft) {
|
1501
|
+
const target = parentDraft;
|
1502
|
+
target.finalities.draft.push((patches, inversePatches) => {
|
1503
|
+
var _a, _b;
|
1504
|
+
const oldProxyDraft = getProxyDraft(proxy);
|
1505
|
+
// if target is a Set draft, `setMap` is the real Set copies proxy mapping.
|
1506
|
+
let copy = target.type === 3 /* DraftType.Set */ ? target.setMap : target.copy;
|
1507
|
+
const draft = get(copy, key);
|
1508
|
+
const proxyDraft = getProxyDraft(draft);
|
1509
|
+
if (proxyDraft) {
|
1510
|
+
// assign the updated value to the copy object
|
1511
|
+
let updatedValue = proxyDraft.original;
|
1512
|
+
if (proxyDraft.operated) {
|
1513
|
+
updatedValue = getValue(draft);
|
1514
|
+
}
|
1515
|
+
finalizeSetValue(proxyDraft);
|
1516
|
+
finalizePatches(proxyDraft, generatePatches, patches, inversePatches);
|
1517
|
+
if (target.options.enableAutoFreeze) {
|
1518
|
+
target.options.updatedValues =
|
1519
|
+
(_a = target.options.updatedValues) !== null && _a !== undefined ? _a : new WeakMap();
|
1520
|
+
target.options.updatedValues.set(updatedValue, proxyDraft.original);
|
1521
|
+
}
|
1522
|
+
// final update value
|
1523
|
+
set(copy, key, updatedValue);
|
1524
|
+
}
|
1525
|
+
// !case: handle the deleted key
|
1526
|
+
(_b = oldProxyDraft.callbacks) === null || _b === undefined ? undefined : _b.forEach((callback) => {
|
1527
|
+
callback(patches, inversePatches);
|
1528
|
+
});
|
1529
|
+
});
|
1530
|
+
}
|
1531
|
+
else {
|
1532
|
+
// !case: handle the root draft
|
1533
|
+
const target = getProxyDraft(proxy);
|
1534
|
+
target.finalities.draft.push((patches, inversePatches) => {
|
1535
|
+
finalizeSetValue(target);
|
1536
|
+
finalizePatches(target, generatePatches, patches, inversePatches);
|
1537
|
+
});
|
1538
|
+
}
|
1539
|
+
return proxy;
|
1540
|
+
}
|
1541
|
+
internal.createDraft = createDraft;
|
1542
|
+
function finalizeDraft(result, returnedValue, patches, inversePatches, enableAutoFreeze) {
|
1543
|
+
var _a;
|
1544
|
+
const proxyDraft = getProxyDraft(result);
|
1545
|
+
const original = (_a = proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.original) !== null && _a !== undefined ? _a : result;
|
1546
|
+
const hasReturnedValue = !!returnedValue.length;
|
1547
|
+
if (proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.operated) {
|
1548
|
+
while (proxyDraft.finalities.draft.length > 0) {
|
1549
|
+
const finalize = proxyDraft.finalities.draft.pop();
|
1550
|
+
finalize(patches, inversePatches);
|
1551
|
+
}
|
1552
|
+
}
|
1553
|
+
const state = hasReturnedValue
|
1554
|
+
? returnedValue[0]
|
1555
|
+
: proxyDraft
|
1556
|
+
? proxyDraft.operated
|
1557
|
+
? proxyDraft.copy
|
1558
|
+
: proxyDraft.original
|
1559
|
+
: result;
|
1560
|
+
if (proxyDraft)
|
1561
|
+
revokeProxy(proxyDraft);
|
1562
|
+
if (enableAutoFreeze) {
|
1563
|
+
deepFreeze(state, state, proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.options.updatedValues);
|
1564
|
+
}
|
1565
|
+
return [
|
1566
|
+
state,
|
1567
|
+
patches && hasReturnedValue
|
1568
|
+
? [{ op: Operation.Replace, path: [], value: returnedValue[0] }]
|
1569
|
+
: patches,
|
1570
|
+
inversePatches && hasReturnedValue
|
1571
|
+
? [{ op: Operation.Replace, path: [], value: original }]
|
1572
|
+
: inversePatches,
|
1573
|
+
];
|
1574
|
+
}
|
1575
|
+
|
1576
|
+
function draftify(baseState, options) {
|
1577
|
+
var _a;
|
1578
|
+
const finalities = {
|
1579
|
+
draft: [],
|
1580
|
+
revoke: [],
|
1581
|
+
handledSet: new WeakSet(),
|
1582
|
+
};
|
1583
|
+
let patches;
|
1584
|
+
let inversePatches;
|
1585
|
+
if (options.enablePatches) {
|
1586
|
+
patches = [];
|
1587
|
+
inversePatches = [];
|
1588
|
+
}
|
1589
|
+
const isMutable = ((_a = options.mark) === null || _a === undefined ? undefined : _a.call(options, baseState, dataTypes)) === dataTypes.mutable ||
|
1590
|
+
!isDraftable(baseState, options);
|
1591
|
+
const draft = isMutable
|
1592
|
+
? baseState
|
1593
|
+
: createDraft({
|
1594
|
+
original: baseState,
|
1595
|
+
parentDraft: null,
|
1596
|
+
finalities,
|
1597
|
+
options,
|
1598
|
+
});
|
1599
|
+
return [
|
1600
|
+
draft,
|
1601
|
+
(returnedValue = []) => {
|
1602
|
+
const [finalizedState, finalizedPatches, finalizedInversePatches] = finalizeDraft(draft, returnedValue, patches, inversePatches, options.enableAutoFreeze);
|
1603
|
+
return (options.enablePatches
|
1604
|
+
? [finalizedState, finalizedPatches, finalizedInversePatches]
|
1605
|
+
: finalizedState);
|
1606
|
+
},
|
1607
|
+
];
|
1608
|
+
}
|
1609
|
+
|
1610
|
+
function handleReturnValue(options) {
|
1611
|
+
const { rootDraft, value, useRawReturn = false, isRoot = true } = options;
|
1612
|
+
forEach(value, (key, item, source) => {
|
1613
|
+
const proxyDraft = getProxyDraft(item);
|
1614
|
+
// just handle the draft which is created by the same rootDraft
|
1615
|
+
if (proxyDraft &&
|
1616
|
+
rootDraft &&
|
1617
|
+
proxyDraft.finalities === rootDraft.finalities) {
|
1618
|
+
options.isContainDraft = true;
|
1619
|
+
const currentValue = proxyDraft.original;
|
1620
|
+
// final update value, but just handle return value
|
1621
|
+
if (source instanceof Set) {
|
1622
|
+
const arr = Array.from(source);
|
1623
|
+
source.clear();
|
1624
|
+
arr.forEach((_item) => source.add(key === _item ? currentValue : _item));
|
1625
|
+
}
|
1626
|
+
else {
|
1627
|
+
set(source, key, currentValue);
|
1628
|
+
}
|
1629
|
+
}
|
1630
|
+
else if (typeof item === 'object' && item !== null) {
|
1631
|
+
options.value = item;
|
1632
|
+
options.isRoot = false;
|
1633
|
+
handleReturnValue(options);
|
1634
|
+
}
|
1635
|
+
});
|
1636
|
+
if (isRoot) {
|
1637
|
+
if (!options.isContainDraft)
|
1638
|
+
console.warn(`The return value does not contain any draft, please use 'rawReturn()' to wrap the return value to improve performance.`);
|
1639
|
+
if (useRawReturn) {
|
1640
|
+
console.warn(`The return value contains drafts, please don't use 'rawReturn()' to wrap the return value.`);
|
1641
|
+
}
|
1642
|
+
}
|
1643
|
+
}
|
1644
|
+
function getCurrent(target) {
|
1645
|
+
var _a;
|
1646
|
+
const proxyDraft = getProxyDraft(target);
|
1647
|
+
if (!isDraftable(target, proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.options))
|
1648
|
+
return target;
|
1649
|
+
const type = getType(target);
|
1650
|
+
if (proxyDraft && !proxyDraft.operated)
|
1651
|
+
return proxyDraft.original;
|
1652
|
+
let currentValue;
|
1653
|
+
function ensureShallowCopy() {
|
1654
|
+
currentValue =
|
1655
|
+
type === 2 /* DraftType.Map */
|
1656
|
+
? !isBaseMapInstance(target)
|
1657
|
+
? new (Object.getPrototypeOf(target).constructor)(target)
|
1658
|
+
: new Map(target)
|
1659
|
+
: type === 3 /* DraftType.Set */
|
1660
|
+
? Array.from(proxyDraft.setMap.values())
|
1661
|
+
: shallowCopy(target, proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.options);
|
1662
|
+
}
|
1663
|
+
if (proxyDraft) {
|
1664
|
+
// It's a proxy draft, let's create a shallow copy eagerly
|
1665
|
+
proxyDraft.finalized = true;
|
1666
|
+
try {
|
1667
|
+
ensureShallowCopy();
|
1668
|
+
}
|
1669
|
+
finally {
|
1670
|
+
proxyDraft.finalized = false;
|
1671
|
+
}
|
1672
|
+
}
|
1673
|
+
else {
|
1674
|
+
// It's not a proxy draft, let's use the target directly and let's see
|
1675
|
+
// lazily if we need to create a shallow copy
|
1676
|
+
currentValue = target;
|
1677
|
+
}
|
1678
|
+
forEach(currentValue, (key, value) => {
|
1679
|
+
if (proxyDraft && isEqual(get(proxyDraft.original, key), value))
|
1680
|
+
return;
|
1681
|
+
const newValue = getCurrent(value);
|
1682
|
+
if (newValue !== value) {
|
1683
|
+
if (currentValue === target)
|
1684
|
+
ensureShallowCopy();
|
1685
|
+
set(currentValue, key, newValue);
|
1686
|
+
}
|
1687
|
+
});
|
1688
|
+
if (type === 3 /* DraftType.Set */) {
|
1689
|
+
const value = (_a = proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.original) !== null && _a !== undefined ? _a : currentValue;
|
1690
|
+
return !isBaseSetInstance(value)
|
1691
|
+
? new (Object.getPrototypeOf(value).constructor)(currentValue)
|
1692
|
+
: new Set(currentValue);
|
1693
|
+
}
|
1694
|
+
return currentValue;
|
1695
|
+
}
|
1696
|
+
function current(target) {
|
1697
|
+
if (!isDraft(target)) {
|
1698
|
+
throw new Error(`current() is only used for Draft, parameter: ${target}`);
|
1699
|
+
}
|
1700
|
+
return getCurrent(target);
|
1701
|
+
}
|
1702
|
+
|
1703
|
+
/**
|
1704
|
+
* `makeCreator(options)` to make a creator function.
|
1705
|
+
*
|
1706
|
+
* ## Example
|
1707
|
+
*
|
1708
|
+
* ```ts
|
1709
|
+
* import { makeCreator } from '../index';
|
1710
|
+
*
|
1711
|
+
* const baseState = { foo: { bar: 'str' }, arr: [] };
|
1712
|
+
* const create = makeCreator({ enableAutoFreeze: true });
|
1713
|
+
* const state = create(
|
1714
|
+
* baseState,
|
1715
|
+
* (draft) => {
|
1716
|
+
* draft.foo.bar = 'str2';
|
1717
|
+
* },
|
1718
|
+
* );
|
1719
|
+
*
|
1720
|
+
* expect(state).toEqual({ foo: { bar: 'str2' }, arr: [] });
|
1721
|
+
* expect(state).not.toBe(baseState);
|
1722
|
+
* expect(state.foo).not.toBe(baseState.foo);
|
1723
|
+
* expect(state.arr).toBe(baseState.arr);
|
1724
|
+
* expect(Object.isFrozen(state)).toBeTruthy();
|
1725
|
+
* ```
|
1726
|
+
*/
|
1727
|
+
const makeCreator = (arg) => {
|
1728
|
+
return function create(arg0, arg1, arg2) {
|
1729
|
+
var _a, _b, _c;
|
1730
|
+
if (typeof arg0 === 'function' && typeof arg1 !== 'function') {
|
1731
|
+
return function (base, ...args) {
|
1732
|
+
return create(base, (draft) => arg0.call(this, draft, ...args), arg1);
|
1733
|
+
};
|
1734
|
+
}
|
1735
|
+
const base = arg0;
|
1736
|
+
const mutate = arg1;
|
1737
|
+
let options = arg2;
|
1738
|
+
if (typeof arg1 !== 'function') {
|
1739
|
+
options = arg1;
|
1740
|
+
}
|
1741
|
+
if (options !== undefined &&
|
1742
|
+
Object.prototype.toString.call(options) !== '[object Object]') {
|
1743
|
+
throw new Error(`Invalid options: ${options}, 'options' should be an object.`);
|
1744
|
+
}
|
1745
|
+
options = Object.assign(Object.assign({}, arg), options);
|
1746
|
+
const state = isDraft(base) ? current(base) : base;
|
1747
|
+
const mark = Array.isArray(options.mark)
|
1748
|
+
? ((value, types) => {
|
1749
|
+
for (const mark of options.mark) {
|
1750
|
+
if (typeof mark !== 'function') {
|
1751
|
+
throw new Error(`Invalid mark: ${mark}, 'mark' should be a function.`);
|
1752
|
+
}
|
1753
|
+
const result = mark(value, types);
|
1754
|
+
if (result) {
|
1755
|
+
return result;
|
1756
|
+
}
|
1757
|
+
}
|
1758
|
+
return;
|
1759
|
+
})
|
1760
|
+
: options.mark;
|
1761
|
+
const enablePatches = (_a = options.enablePatches) !== null && _a !== undefined ? _a : false;
|
1762
|
+
const strict = (_b = options.strict) !== null && _b !== undefined ? _b : false;
|
1763
|
+
const enableAutoFreeze = (_c = options.enableAutoFreeze) !== null && _c !== undefined ? _c : false;
|
1764
|
+
const _options = {
|
1765
|
+
enableAutoFreeze,
|
1766
|
+
mark,
|
1767
|
+
strict,
|
1768
|
+
enablePatches,
|
1769
|
+
};
|
1770
|
+
if (!isDraftable(state, _options) &&
|
1771
|
+
typeof state === 'object' &&
|
1772
|
+
state !== null) {
|
1773
|
+
throw new Error(`Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.`);
|
1774
|
+
}
|
1775
|
+
const [draft, finalize] = draftify(state, _options);
|
1776
|
+
if (typeof arg1 !== 'function') {
|
1777
|
+
if (!isDraftable(state, _options)) {
|
1778
|
+
throw new Error(`Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.`);
|
1779
|
+
}
|
1780
|
+
return [draft, finalize];
|
1781
|
+
}
|
1782
|
+
let result;
|
1783
|
+
try {
|
1784
|
+
result = mutate(draft);
|
1785
|
+
}
|
1786
|
+
catch (error) {
|
1787
|
+
revokeProxy(getProxyDraft(draft));
|
1788
|
+
throw error;
|
1789
|
+
}
|
1790
|
+
const returnValue = (value) => {
|
1791
|
+
const proxyDraft = getProxyDraft(draft);
|
1792
|
+
if (!isDraft(value)) {
|
1793
|
+
if (value !== undefined &&
|
1794
|
+
!isEqual(value, draft) &&
|
1795
|
+
(proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.operated)) {
|
1796
|
+
throw new Error(`Either the value is returned as a new non-draft value, or only the draft is modified without returning any value.`);
|
1797
|
+
}
|
1798
|
+
const rawReturnValue = value === null || value === undefined ? undefined : value[RAW_RETURN_SYMBOL];
|
1799
|
+
if (rawReturnValue) {
|
1800
|
+
const _value = rawReturnValue[0];
|
1801
|
+
if (_options.strict && typeof value === 'object' && value !== null) {
|
1802
|
+
handleReturnValue({
|
1803
|
+
rootDraft: proxyDraft,
|
1804
|
+
value,
|
1805
|
+
useRawReturn: true,
|
1806
|
+
});
|
1807
|
+
}
|
1808
|
+
return finalize([_value]);
|
1809
|
+
}
|
1810
|
+
if (value !== undefined) {
|
1811
|
+
if (typeof value === 'object' && value !== null) {
|
1812
|
+
handleReturnValue({ rootDraft: proxyDraft, value });
|
1813
|
+
}
|
1814
|
+
return finalize([value]);
|
1815
|
+
}
|
1816
|
+
}
|
1817
|
+
if (value === draft || value === undefined) {
|
1818
|
+
return finalize([]);
|
1819
|
+
}
|
1820
|
+
const returnedProxyDraft = getProxyDraft(value);
|
1821
|
+
if (_options === returnedProxyDraft.options) {
|
1822
|
+
if (returnedProxyDraft.operated) {
|
1823
|
+
throw new Error(`Cannot return a modified child draft.`);
|
1824
|
+
}
|
1825
|
+
return finalize([current(value)]);
|
1826
|
+
}
|
1827
|
+
return finalize([value]);
|
1828
|
+
};
|
1829
|
+
if (result instanceof Promise) {
|
1830
|
+
return result.then(returnValue, (error) => {
|
1831
|
+
revokeProxy(getProxyDraft(draft));
|
1832
|
+
throw error;
|
1833
|
+
});
|
1834
|
+
}
|
1835
|
+
return returnValue(result);
|
1836
|
+
};
|
1837
|
+
};
|
1838
|
+
|
1839
|
+
/**
|
1840
|
+
* `create(baseState, callback, options)` to create the next state
|
1841
|
+
*
|
1842
|
+
* ## Example
|
1843
|
+
*
|
1844
|
+
* ```ts
|
1845
|
+
* import { create } from '../index';
|
1846
|
+
*
|
1847
|
+
* const baseState = { foo: { bar: 'str' }, arr: [] };
|
1848
|
+
* const state = create(
|
1849
|
+
* baseState,
|
1850
|
+
* (draft) => {
|
1851
|
+
* draft.foo.bar = 'str2';
|
1852
|
+
* },
|
1853
|
+
* );
|
1854
|
+
*
|
1855
|
+
* expect(state).toEqual({ foo: { bar: 'str2' }, arr: [] });
|
1856
|
+
* expect(state).not.toBe(baseState);
|
1857
|
+
* expect(state.foo).not.toBe(baseState.foo);
|
1858
|
+
* expect(state.arr).toBe(baseState.arr);
|
1859
|
+
* ```
|
1860
|
+
*/
|
1861
|
+
const create = makeCreator();
|
1862
|
+
|
1863
|
+
Object.prototype.constructor.toString();
|
1864
|
+
|
1865
|
+
class RumiousState {
|
1866
|
+
constructor(value, reactor) {
|
1867
|
+
this.value = value;
|
1868
|
+
this.reactor = reactor ?? new RumiousReactor(this);
|
1869
|
+
}
|
1870
|
+
set(value) {
|
1871
|
+
this.value = value;
|
1872
|
+
this.reactor.emit({
|
1873
|
+
type: "SET",
|
1874
|
+
target: this,
|
1875
|
+
value: value
|
1876
|
+
});
|
1877
|
+
}
|
1878
|
+
get() {
|
1879
|
+
return this.value;
|
1880
|
+
}
|
1881
|
+
increase(count = 1) {
|
1882
|
+
if (typeof this.value === "number") {
|
1883
|
+
this.set(this.value + count);
|
1884
|
+
}
|
1885
|
+
}
|
1886
|
+
produce(callback) {
|
1887
|
+
const [draft, finalize] = create(this.value);
|
1888
|
+
callback(draft);
|
1889
|
+
this.set(finalize());
|
1890
|
+
}
|
1891
|
+
}
|
1892
|
+
function watch(state, fn) {
|
1893
|
+
state.reactor.addBinding(fn);
|
1894
|
+
}
|
1895
|
+
function unwatch(state, fn) {
|
1896
|
+
state.reactor.removeBinding(fn);
|
1897
|
+
}
|
1898
|
+
function createState(value) {
|
1899
|
+
return new RumiousState(value);
|
1900
|
+
}
|
1901
|
+
|
1902
|
+
function eventBindingDirective(context, target, modifier, data) {
|
1903
|
+
if (typeof data === "string") {
|
1904
|
+
data = context.findName(extractName(data));
|
1905
|
+
}
|
1906
|
+
target.addEventListener(modifier, data);
|
1907
|
+
}
|
1908
|
+
function refBindingDirective(context, target, modifier, data) {
|
1909
|
+
if (typeof data === "string") {
|
1910
|
+
data = context.findName(extractName(data));
|
1911
|
+
}
|
1912
|
+
if (data instanceof RumiousElementRef) {
|
1913
|
+
data.target = target;
|
1914
|
+
} else {
|
1915
|
+
throw Error("Rumious: ref directive required RumiousElementRef !");
|
1916
|
+
}
|
1917
|
+
}
|
1918
|
+
function injectDirective(context, target, modifier, data) {
|
1919
|
+
if (typeof data === "string") {
|
1920
|
+
data = context.findName(extractName(data));
|
1921
|
+
}
|
1922
|
+
if (data instanceof RumiousDymanicInjector) {
|
1923
|
+
data.addTarget(target);
|
1924
|
+
data.inject(target);
|
1925
|
+
} else {
|
1926
|
+
throw Error("Rumious: inject directive required RumiousInjector !");
|
1927
|
+
}
|
1928
|
+
}
|
1929
|
+
function bindDirective(context, target, modifier, data) {
|
1930
|
+
if (typeof data === "string") {
|
1931
|
+
data = context.findName(extractName(data));
|
1932
|
+
}
|
1933
|
+
if (data instanceof RumiousState) {
|
1934
|
+
target.setAttribute(modifier, data.value);
|
1935
|
+
data.reactor.addBinding(({}) => {
|
1936
|
+
target.setAttribute(modifier, data.value);
|
1937
|
+
});
|
1938
|
+
} else {
|
1939
|
+
throw Error("Rumious: bind directive required RumiousState !");
|
1940
|
+
}
|
1941
|
+
}
|
1942
|
+
function modelDirective(context, target, modifier, data) {
|
1943
|
+
if (typeof data === "string") {
|
1944
|
+
data = context.findName(extractName(data));
|
1945
|
+
}
|
1946
|
+
if (data instanceof RumiousState && (target instanceof HTMLInputElement || target instanceof HTMLSelectElement || target instanceof HTMLTextAreaElement)) {
|
1947
|
+
const type = target.type;
|
1948
|
+
target.addEventListener("input", () => {
|
1949
|
+
if (target instanceof HTMLInputElement) {
|
1950
|
+
switch (type) {
|
1951
|
+
case "checkbox":
|
1952
|
+
data.set(target.checked);
|
1953
|
+
break;
|
1954
|
+
case "radio":
|
1955
|
+
if (target.checked) data.set(target.value);
|
1956
|
+
break;
|
1957
|
+
default:
|
1958
|
+
data.set(target.value);
|
1959
|
+
}
|
1960
|
+
} else if (target instanceof HTMLSelectElement || target instanceof HTMLTextAreaElement) {
|
1961
|
+
data.set(target.value);
|
1962
|
+
}
|
1963
|
+
});
|
1964
|
+
} else {
|
1965
|
+
throw Error("Rumious: model directive requires RumiousState and a valid form element!");
|
1966
|
+
}
|
1967
|
+
}
|
1968
|
+
const directives = {
|
1969
|
+
"on": eventBindingDirective,
|
1970
|
+
"ref": refBindingDirective,
|
1971
|
+
"inject": injectDirective,
|
1972
|
+
"bind": bindDirective,
|
1973
|
+
"model": modelDirective
|
1974
|
+
};
|
1975
|
+
|
1976
|
+
class RumiousDynamicArrayRenderer {
|
1977
|
+
domMap = new Map();
|
1978
|
+
constructor(state, callback, keyFn) {
|
1979
|
+
this.state = state;
|
1980
|
+
this.callback = callback;
|
1981
|
+
this.keyFn = keyFn ?? ((_, i) => i);
|
1982
|
+
}
|
1983
|
+
prepare(anchor, context) {
|
1984
|
+
this.anchorElement = anchor;
|
1985
|
+
this.context = context;
|
1986
|
+
}
|
1987
|
+
async render() {
|
1988
|
+
await this.reconcile(this.state.value);
|
1989
|
+
this.state.reactor.addBinding(this.onStateChange.bind(this));
|
1990
|
+
}
|
1991
|
+
async onStateChange(commit) {
|
1992
|
+
if (commit.type === "APPEND") {
|
1993
|
+
const key = this.keyFn(commit.item, commit.key);
|
1994
|
+
if (!this.domMap.has(key)) {
|
1995
|
+
const template = await this.callback(commit.item, commit.key);
|
1996
|
+
const frag = document.createDocumentFragment();
|
1997
|
+
this.context.renderHelper(this.context, template, frag);
|
1998
|
+
this.anchorElement.appendChild(frag);
|
1999
|
+
this.domMap.set(key, frag);
|
2000
|
+
}
|
2001
|
+
} else if (commit.type === "SET_BY_KEY") {
|
2002
|
+
const key = this.keyFn(commit.item, commit.key);
|
2003
|
+
const oldNode = this.anchorElement.childNodes[commit.key];
|
2004
|
+
if (oldNode) {
|
2005
|
+
const template = await this.callback(commit.item, commit.key);
|
2006
|
+
const frag = document.createDocumentFragment();
|
2007
|
+
this.context.renderHelper(this.context, template, frag);
|
2008
|
+
this.anchorElement.replaceChild(frag, oldNode);
|
2009
|
+
this.domMap.set(key, frag);
|
2010
|
+
}
|
2011
|
+
} else if (commit.type === "REMOVE_BY_KEY") {
|
2012
|
+
const node = this.anchorElement.childNodes[commit.key];
|
2013
|
+
if (node) {
|
2014
|
+
this.anchorElement.removeChild(node);
|
2015
|
+
const entry = [...this.domMap.entries()].find(([, n]) => n === node);
|
2016
|
+
if (entry) this.domMap.delete(entry[0]);
|
2017
|
+
}
|
2018
|
+
} else if (commit.type === "INSERT_BY_KEY") {
|
2019
|
+
const key = this.keyFn(commit.item, commit.key);
|
2020
|
+
if (!this.domMap.has(key)) {
|
2021
|
+
const template = await this.callback(commit.item, commit.key);
|
2022
|
+
const frag = document.createDocumentFragment();
|
2023
|
+
this.context.renderHelper(this.context, template, frag);
|
2024
|
+
const refNode = this.anchorElement.childNodes[commit.key] ?? null;
|
2025
|
+
this.anchorElement.insertBefore(frag, refNode);
|
2026
|
+
this.domMap.set(key, frag);
|
2027
|
+
}
|
2028
|
+
} else {
|
2029
|
+
await this.reconcile(this.state.value);
|
2030
|
+
}
|
2031
|
+
}
|
2032
|
+
async reconcile(nextItems) {
|
2033
|
+
const oldMap = this.domMap;
|
2034
|
+
const newMap = new Map();
|
2035
|
+
const newNodes = [];
|
2036
|
+
for (let i = 0; i < nextItems.length; i++) {
|
2037
|
+
const item = nextItems[i];
|
2038
|
+
const key = this.keyFn(item, i);
|
2039
|
+
let node = oldMap.get(key);
|
2040
|
+
if (!node) {
|
2041
|
+
const template = await this.callback(item, i);
|
2042
|
+
const frag = document.createDocumentFragment();
|
2043
|
+
this.context.renderHelper(this.context, template, frag);
|
2044
|
+
node = frag;
|
2045
|
+
}
|
2046
|
+
newMap.set(key, node);
|
2047
|
+
newNodes.push(node);
|
2048
|
+
}
|
2049
|
+
this.anchorElement.textContent = "";
|
2050
|
+
for (const node of newNodes) {
|
2051
|
+
this.anchorElement.appendChild(node);
|
2052
|
+
}
|
2053
|
+
this.domMap = newMap;
|
2054
|
+
}
|
2055
|
+
}
|
2056
|
+
function renderDynamicArray(state, callback) {
|
2057
|
+
return new RumiousDynamicArrayRenderer(state, callback);
|
2058
|
+
}
|
2059
|
+
|
2060
|
+
function isPrimitive(value) {
|
2061
|
+
return value !== Object(value);
|
2062
|
+
}
|
2063
|
+
|
2064
|
+
async function dynamicValue$1(target, textNode, value, context) {
|
2065
|
+
const parent = textNode.parentNode;
|
2066
|
+
if (!parent) return;
|
2067
|
+
if (isPrimitive(value)) {
|
2068
|
+
textNode.textContent = String(value);
|
2069
|
+
} else if (value && value instanceof RumiousState) {
|
2070
|
+
textNode.textContent = value.value;
|
2071
|
+
function onValueChange({}) {
|
2072
|
+
if (document.contains(textNode)) {
|
2073
|
+
textNode.textContent = value.value;
|
2074
|
+
} else {
|
2075
|
+
value.reactor.removeBinding(onValueChange);
|
2076
|
+
}
|
2077
|
+
}
|
2078
|
+
value.reactor.addBinding(onValueChange);
|
2079
|
+
} else if (Array.isArray(value)) {
|
2080
|
+
textNode.textContent = value.map(String).join("");
|
2081
|
+
} else if (value instanceof RumiousDynamicArrayRenderer) {
|
2082
|
+
value.prepare(textNode.parentElement, context);
|
2083
|
+
value.render();
|
2084
|
+
} else if (value instanceof HTMLElement) {
|
2085
|
+
textNode.replaceWith(value);
|
2086
|
+
} else if (value instanceof RumiousRenderTemplate) {
|
2087
|
+
let fragment = document.createDocumentFragment();
|
2088
|
+
context.renderHelper?.(context, value, fragment);
|
2089
|
+
textNode.replaceWith(fragment);
|
2090
|
+
} else if (value instanceof NodeList || value instanceof HTMLCollection) {
|
2091
|
+
if (value.length === 0) {
|
2092
|
+
textNode.remove();
|
2093
|
+
return;
|
2094
|
+
}
|
2095
|
+
const fragment = document.createDocumentFragment();
|
2096
|
+
for (const node of Array.from(value)) {
|
2097
|
+
fragment.appendChild(node.cloneNode(true));
|
2098
|
+
}
|
2099
|
+
textNode.replaceWith(fragment);
|
2100
|
+
textNode.remove();
|
2101
|
+
} else if (value && typeof value.toString === "function") {
|
2102
|
+
try {
|
2103
|
+
textNode.textContent = value.toString();
|
2104
|
+
} catch {
|
2105
|
+
textNode.textContent = "";
|
2106
|
+
}
|
2107
|
+
} else {
|
2108
|
+
textNode.textContent = "";
|
2109
|
+
}
|
2110
|
+
}
|
2111
|
+
|
2112
|
+
function template(generator) {
|
2113
|
+
return new RumiousRenderTemplate(generator);
|
2114
|
+
}
|
2115
|
+
function addDirective(element, context, name, modifier = "", data) {
|
2116
|
+
let callback = directives[name];
|
2117
|
+
if (callback) {
|
2118
|
+
callback(context, element, modifier, data);
|
2119
|
+
} else {
|
2120
|
+
throw Error("Rumious: Cannot solve directive !");
|
2121
|
+
}
|
2122
|
+
}
|
2123
|
+
function dynamicValue(target, textNode, value, context) {
|
2124
|
+
dynamicValue$1(target, textNode, value, context);
|
2125
|
+
}
|
2126
|
+
function createComponent(componentConstructor) {
|
2127
|
+
let tagName = componentConstructor.tagName;
|
2128
|
+
if (!window.customElements.get(tagName)) {
|
2129
|
+
window.customElements.define(tagName, class extends RumiousComponentElement {
|
2130
|
+
static tag = tagName;
|
2131
|
+
});
|
2132
|
+
}
|
2133
|
+
return document.createElement(tagName);
|
2134
|
+
}
|
2135
|
+
|
2136
|
+
// This is just to satisfy TypeScript's JSX requirement.
|
2137
|
+
// Rumious doesn't use createElement — we do things differently.
|
2138
|
+
|
2139
|
+
function createElement(...args) {
|
2140
|
+
throw Error("Rumious doesn't use createElement !");
|
2141
|
+
}
|
2142
|
+
window.RUMIOUS_JSX = {
|
2143
|
+
template,
|
2144
|
+
createElement,
|
2145
|
+
addDirective,
|
2146
|
+
dynamicValue,
|
2147
|
+
createComponent
|
2148
|
+
};
|
2149
|
+
|
2150
|
+
var JSX = /*#__PURE__*/Object.freeze({
|
2151
|
+
__proto__: null,
|
2152
|
+
template: template
|
2153
|
+
});
|
2154
|
+
|
2155
|
+
var Render = /*#__PURE__*/Object.freeze({
|
2156
|
+
__proto__: null,
|
2157
|
+
RumiousDymanicInjector: RumiousDymanicInjector,
|
2158
|
+
RumiousRenderTemplate: RumiousRenderTemplate,
|
2159
|
+
createHTMLInjector: createHTMLInjector,
|
2160
|
+
renderDynamicArray: renderDynamicArray
|
2161
|
+
});
|
2162
|
+
|
2163
|
+
class RumiousChildrenRef {
|
2164
|
+
constructor(target) {
|
2165
|
+
this.target = target;
|
2166
|
+
}
|
2167
|
+
list() {
|
2168
|
+
return Array.from(this.target.children);
|
2169
|
+
}
|
2170
|
+
getChild(index) {
|
2171
|
+
return this.list()[index];
|
2172
|
+
}
|
2173
|
+
remove(index) {
|
2174
|
+
this.list()[index]?.remove();
|
2175
|
+
}
|
2176
|
+
add(child) {
|
2177
|
+
this.target.appendChild(child);
|
2178
|
+
}
|
2179
|
+
querySelector(query) {
|
2180
|
+
return this.target.querySelector(query);
|
2181
|
+
}
|
2182
|
+
querySelectorAll(query) {
|
2183
|
+
return this.target.querySelectorAll(query);
|
2184
|
+
}
|
2185
|
+
clear() {
|
2186
|
+
this.target.innerHTML = '';
|
2187
|
+
}
|
2188
|
+
replaceChild(index, newChild) {
|
2189
|
+
const oldChild = this.getChild(index);
|
2190
|
+
if (oldChild) {
|
2191
|
+
this.target.replaceChild(newChild, oldChild);
|
2192
|
+
}
|
2193
|
+
}
|
2194
|
+
insertBefore(newChild, index) {
|
2195
|
+
const referenceChild = this.getChild(index);
|
2196
|
+
if (referenceChild) {
|
2197
|
+
this.target.insertBefore(newChild, referenceChild);
|
2198
|
+
} else {
|
2199
|
+
this.add(newChild);
|
2200
|
+
}
|
2201
|
+
}
|
2202
|
+
prepend(child) {
|
2203
|
+
this.target.prepend(child);
|
2204
|
+
}
|
2205
|
+
getFirstChild() {
|
2206
|
+
return this.list()[0];
|
2207
|
+
}
|
2208
|
+
getLastChild() {
|
2209
|
+
return this.list()[this.list().length - 1];
|
2210
|
+
}
|
2211
|
+
hasChildren() {
|
2212
|
+
return this.target.hasChildNodes();
|
2213
|
+
}
|
2214
|
+
count() {
|
2215
|
+
return this.target.children.length;
|
2216
|
+
}
|
2217
|
+
find(predicate) {
|
2218
|
+
return this.list().find(predicate);
|
2219
|
+
}
|
2220
|
+
forEach(callback) {
|
2221
|
+
this.list().forEach(callback);
|
2222
|
+
}
|
2223
|
+
removeAllMatching(query) {
|
2224
|
+
this.querySelectorAll(query).forEach(element => element.remove());
|
2225
|
+
}
|
2226
|
+
toggleClass(className) {
|
2227
|
+
this.list().forEach(child => child.classList.toggle(className));
|
2228
|
+
}
|
2229
|
+
setAttribute(key, value) {
|
2230
|
+
this.list().forEach(child => child.setAttribute(key, value));
|
2231
|
+
}
|
2232
|
+
}
|
2233
|
+
|
2234
|
+
var Ref = /*#__PURE__*/Object.freeze({
|
2235
|
+
__proto__: null,
|
2236
|
+
RumiousChildrenRef: RumiousChildrenRef,
|
2237
|
+
RumiousElementRef: RumiousElementRef,
|
2238
|
+
createElementRef: createElementRef
|
2239
|
+
});
|
2240
|
+
|
2241
|
+
class RumiousArrayState extends RumiousState {
|
2242
|
+
constructor(value, reactor) {
|
2243
|
+
super(value, reactor);
|
2244
|
+
}
|
2245
|
+
set(arg1, arg2) {
|
2246
|
+
if (typeof arg1 === "number" && arg2 !== undefined) {
|
2247
|
+
const index = arg1;
|
2248
|
+
const newValue = arg2;
|
2249
|
+
this.value[index] = newValue;
|
2250
|
+
this.reactor.emit({
|
2251
|
+
type: "SET_BY_KEY",
|
2252
|
+
value: [...this.value],
|
2253
|
+
target: this,
|
2254
|
+
key: index,
|
2255
|
+
item: newValue
|
2256
|
+
});
|
2257
|
+
} else if (Array.isArray(arg1)) {
|
2258
|
+
super.set(arg1);
|
2259
|
+
} else {
|
2260
|
+
throw new Error("Invalid arguments passed to set()");
|
2261
|
+
}
|
2262
|
+
}
|
2263
|
+
get(arg) {
|
2264
|
+
return typeof arg === "number" ? this.value[arg] : this.value;
|
2265
|
+
}
|
2266
|
+
insert(index, newItem) {
|
2267
|
+
this.value.splice(index, 0, newItem);
|
2268
|
+
this.reactor.emit({
|
2269
|
+
type: "INSERT_BY_KEY",
|
2270
|
+
value: this.value,
|
2271
|
+
target: this,
|
2272
|
+
key: index,
|
2273
|
+
item: newItem
|
2274
|
+
});
|
2275
|
+
return this;
|
2276
|
+
}
|
2277
|
+
remove(index) {
|
2278
|
+
this.value.splice(index, 1);
|
2279
|
+
this.reactor.emit({
|
2280
|
+
type: "REMOVE_BY_KEY",
|
2281
|
+
value: this.value,
|
2282
|
+
target: this,
|
2283
|
+
key: index
|
2284
|
+
});
|
2285
|
+
return this;
|
2286
|
+
}
|
2287
|
+
append(value) {
|
2288
|
+
this.value.push(value);
|
2289
|
+
this.reactor.emit({
|
2290
|
+
type: "APPEND",
|
2291
|
+
value: this.value,
|
2292
|
+
target: this,
|
2293
|
+
item: value
|
2294
|
+
});
|
2295
|
+
return this;
|
2296
|
+
}
|
2297
|
+
clear() {
|
2298
|
+
this.value.length = 0;
|
2299
|
+
this.reactor.emit({
|
2300
|
+
type: "SET",
|
2301
|
+
value: [],
|
2302
|
+
target: this
|
2303
|
+
});
|
2304
|
+
return this;
|
2305
|
+
}
|
2306
|
+
replace(index, newItem) {
|
2307
|
+
if (index < 0 || index >= this.value.length) {
|
2308
|
+
throw new Error("Index out of bounds");
|
2309
|
+
}
|
2310
|
+
this.value[index] = newItem;
|
2311
|
+
this.reactor.emit({
|
2312
|
+
type: "SET_BY_KEY",
|
2313
|
+
value: [...this.value],
|
2314
|
+
target: this,
|
2315
|
+
key: index,
|
2316
|
+
item: newItem
|
2317
|
+
});
|
2318
|
+
return this;
|
2319
|
+
}
|
2320
|
+
filter(callback) {
|
2321
|
+
this.value = this.value.filter(callback);
|
2322
|
+
this.reactor.emit({
|
2323
|
+
type: "SET",
|
2324
|
+
value: this.value,
|
2325
|
+
target: this
|
2326
|
+
});
|
2327
|
+
return this;
|
2328
|
+
}
|
2329
|
+
map(callback) {
|
2330
|
+
this.value = this.value.map(callback);
|
2331
|
+
this.reactor.emit({
|
2332
|
+
type: "SET",
|
2333
|
+
value: this.value,
|
2334
|
+
target: this
|
2335
|
+
});
|
2336
|
+
return this;
|
2337
|
+
}
|
2338
|
+
sort(compareFn) {
|
2339
|
+
this.value.sort(compareFn);
|
2340
|
+
this.reactor.emit({
|
2341
|
+
type: "SET",
|
2342
|
+
value: this.value,
|
2343
|
+
target: this
|
2344
|
+
});
|
2345
|
+
return this;
|
2346
|
+
}
|
2347
|
+
reverse() {
|
2348
|
+
this.value.reverse();
|
2349
|
+
this.reactor.emit({
|
2350
|
+
type: "SET",
|
2351
|
+
value: this.value,
|
2352
|
+
target: this
|
2353
|
+
});
|
2354
|
+
return this;
|
2355
|
+
}
|
2356
|
+
get length() {
|
2357
|
+
return this.value.length;
|
2358
|
+
}
|
2359
|
+
forEach(callback) {
|
2360
|
+
this.value.forEach(callback);
|
2361
|
+
}
|
2362
|
+
}
|
2363
|
+
function createArrayState(value) {
|
2364
|
+
return new RumiousArrayState(value);
|
2365
|
+
}
|
2366
|
+
|
2367
|
+
class RumiousObjectState extends RumiousState {
|
2368
|
+
#locked = false;
|
2369
|
+
constructor(value, reactor) {
|
2370
|
+
super(value, reactor);
|
2371
|
+
}
|
2372
|
+
set(arg1, arg2) {
|
2373
|
+
if (this.#locked) throw new Error("Object is locked");
|
2374
|
+
if (typeof arg1 === "string") {
|
2375
|
+
const key = arg1;
|
2376
|
+
const newValue = arg2;
|
2377
|
+
this.value[key] = newValue;
|
2378
|
+
this.reactor.emit({
|
2379
|
+
type: "SET_BY_KEY",
|
2380
|
+
value: {
|
2381
|
+
...this.value
|
2382
|
+
},
|
2383
|
+
target: this,
|
2384
|
+
key,
|
2385
|
+
item: newValue
|
2386
|
+
});
|
2387
|
+
return this;
|
2388
|
+
} else if (typeof arg1 === "object") {
|
2389
|
+
super.set(arg1);
|
2390
|
+
} else {
|
2391
|
+
throw new Error("Invalid arguments passed to set()");
|
2392
|
+
}
|
2393
|
+
}
|
2394
|
+
remove(key) {
|
2395
|
+
if (this.#locked) throw new Error("Object is locked");
|
2396
|
+
if (key in this.value) {
|
2397
|
+
delete this.value[key];
|
2398
|
+
this.reactor.emit({
|
2399
|
+
type: "REMOVE_BY_KEY",
|
2400
|
+
value: {
|
2401
|
+
...this.value
|
2402
|
+
},
|
2403
|
+
target: this,
|
2404
|
+
key
|
2405
|
+
});
|
2406
|
+
}
|
2407
|
+
return this;
|
2408
|
+
}
|
2409
|
+
merge(partial) {
|
2410
|
+
if (this.#locked) throw new Error("Object is locked");
|
2411
|
+
Object.assign(this.value, partial);
|
2412
|
+
this.reactor.emit({
|
2413
|
+
type: "SET",
|
2414
|
+
value: {
|
2415
|
+
...this.value
|
2416
|
+
},
|
2417
|
+
target: this
|
2418
|
+
});
|
2419
|
+
return this;
|
2420
|
+
}
|
2421
|
+
assign(obj) {
|
2422
|
+
if (this.#locked) throw new Error("Object is locked");
|
2423
|
+
this.value = {
|
2424
|
+
...this.value,
|
2425
|
+
...obj
|
2426
|
+
};
|
2427
|
+
this.reactor.emit({
|
2428
|
+
type: "SET",
|
2429
|
+
value: {
|
2430
|
+
...this.value
|
2431
|
+
},
|
2432
|
+
target: this
|
2433
|
+
});
|
2434
|
+
return this;
|
2435
|
+
}
|
2436
|
+
clear() {
|
2437
|
+
if (this.#locked) throw new Error("Object is locked");
|
2438
|
+
for (const key in this.value) {
|
2439
|
+
delete this.value[key];
|
2440
|
+
}
|
2441
|
+
this.reactor.emit({
|
2442
|
+
type: "SET",
|
2443
|
+
value: {},
|
2444
|
+
target: this
|
2445
|
+
});
|
2446
|
+
return this;
|
2447
|
+
}
|
2448
|
+
get(arg) {
|
2449
|
+
return typeof arg === "string" ? this.value[arg] : this.value;
|
2450
|
+
}
|
2451
|
+
keys() {
|
2452
|
+
return Object.keys(this.value);
|
2453
|
+
}
|
2454
|
+
values() {
|
2455
|
+
return Object.values(this.value);
|
2456
|
+
}
|
2457
|
+
entries() {
|
2458
|
+
return Object.entries(this.value);
|
2459
|
+
}
|
2460
|
+
has(key) {
|
2461
|
+
return key in this.value;
|
2462
|
+
}
|
2463
|
+
get size() {
|
2464
|
+
return Object.keys(this.value).length;
|
2465
|
+
}
|
2466
|
+
forEach(callback) {
|
2467
|
+
Object.entries(this.value).forEach(([key, value]) => {
|
2468
|
+
callback(value, key, this.value);
|
2469
|
+
});
|
2470
|
+
}
|
2471
|
+
map(callback) {
|
2472
|
+
const result = {};
|
2473
|
+
Object.entries(this.value).forEach(([key, value]) => {
|
2474
|
+
result[key] = callback(value, key, this.value);
|
2475
|
+
});
|
2476
|
+
return result;
|
2477
|
+
}
|
2478
|
+
clone() {
|
2479
|
+
return JSON.parse(JSON.stringify(this.value));
|
2480
|
+
}
|
2481
|
+
toJSON() {
|
2482
|
+
return JSON.parse(JSON.stringify(this.value));
|
2483
|
+
}
|
2484
|
+
toObject() {
|
2485
|
+
return this.get();
|
2486
|
+
}
|
2487
|
+
lock() {
|
2488
|
+
this.#locked = true;
|
2489
|
+
return this;
|
2490
|
+
}
|
2491
|
+
unlock() {
|
2492
|
+
this.#locked = false;
|
2493
|
+
return this;
|
2494
|
+
}
|
2495
|
+
get isLocked() {
|
2496
|
+
return this.#locked;
|
2497
|
+
}
|
2498
|
+
freeze() {
|
2499
|
+
Object.freeze(this.value);
|
2500
|
+
return this;
|
2501
|
+
}
|
2502
|
+
unfreeze() {
|
2503
|
+
this.value = JSON.parse(JSON.stringify(this.value));
|
2504
|
+
return this;
|
2505
|
+
}
|
2506
|
+
get isFrozen() {
|
2507
|
+
return Object.isFrozen(this.value);
|
2508
|
+
}
|
2509
|
+
}
|
2510
|
+
function createObjectState(value) {
|
2511
|
+
return new RumiousObjectState(value);
|
2512
|
+
}
|
2513
|
+
|
2514
|
+
var State = /*#__PURE__*/Object.freeze({
|
2515
|
+
__proto__: null,
|
2516
|
+
RumiousArrayState: RumiousArrayState,
|
2517
|
+
RumiousObjectState: RumiousObjectState,
|
2518
|
+
RumiousState: RumiousState,
|
2519
|
+
createArrayState: createArrayState,
|
2520
|
+
createObjectState: createObjectState,
|
2521
|
+
createState: createState,
|
2522
|
+
unwatch: unwatch,
|
2523
|
+
watch: watch
|
2524
|
+
});
|
2525
|
+
|
2526
|
+
window.RUMIOUS_CONTEXTS = {};
|
2527
|
+
class RumiousContext {
|
2528
|
+
constructor(initialValues = {}) {
|
2529
|
+
this.events = {};
|
2530
|
+
this.values = initialValues;
|
2531
|
+
}
|
2532
|
+
has(key) {
|
2533
|
+
return key in this.values;
|
2534
|
+
}
|
2535
|
+
set(key, value) {
|
2536
|
+
this.values[key] = value;
|
2537
|
+
}
|
2538
|
+
get(key) {
|
2539
|
+
return this.values[key];
|
2540
|
+
}
|
2541
|
+
on(event, callback) {
|
2542
|
+
if (!this.events[event]) this.events[event] = [];
|
2543
|
+
this.events[event].push(callback);
|
2544
|
+
}
|
2545
|
+
off(event, callback) {
|
2546
|
+
if (!this.events[event]) return;
|
2547
|
+
this.events[event] = this.events[event].filter(fn => fn !== callback);
|
2548
|
+
}
|
2549
|
+
emit(event, payload) {
|
2550
|
+
if (!this.events[event]) return;
|
2551
|
+
this.events[event].forEach(fn => fn(payload));
|
2552
|
+
}
|
2553
|
+
}
|
2554
|
+
function createContext(values, name = generateName("rctx_")) {
|
2555
|
+
if (window.RUMIOUS_CONTEXTS[name]) return window.RUMIOUS_CONTEXTS[name];else {
|
2556
|
+
let context = new RumiousContext(values);
|
2557
|
+
window.RUMIOUS_CONTEXTS[name] = context;
|
2558
|
+
return context;
|
2559
|
+
}
|
2560
|
+
}
|
2561
|
+
|
2562
|
+
var Context = /*#__PURE__*/Object.freeze({
|
2563
|
+
__proto__: null,
|
2564
|
+
RumiousContext: RumiousContext,
|
2565
|
+
createContext: createContext
|
2566
|
+
});
|
2567
|
+
|
2568
|
+
function tholle(func, limit) {
|
2569
|
+
let lastCall = 0;
|
2570
|
+
return function (...args) {
|
2571
|
+
const now = Date.now();
|
2572
|
+
if (now - lastCall >= limit) {
|
2573
|
+
lastCall = now;
|
2574
|
+
func.apply(this, args);
|
2575
|
+
}
|
2576
|
+
};
|
2577
|
+
}
|
2578
|
+
function denounce(func, delay) {
|
2579
|
+
let timer;
|
2580
|
+
return function (...args) {
|
2581
|
+
clearTimeout(timer);
|
2582
|
+
timer = setTimeout(() => func.apply(this, args), delay);
|
2583
|
+
};
|
2584
|
+
}
|
2585
|
+
function trailingThrottle(func, limit) {
|
2586
|
+
let lastCall = 0;
|
2587
|
+
let lastArgs = null;
|
2588
|
+
let timeout = null;
|
2589
|
+
function invoke() {
|
2590
|
+
lastCall = Date.now();
|
2591
|
+
func.apply(this, lastArgs);
|
2592
|
+
lastArgs = null;
|
2593
|
+
}
|
2594
|
+
return function (...args) {
|
2595
|
+
const now = Date.now();
|
2596
|
+
lastArgs = args;
|
2597
|
+
if (now - lastCall >= limit) {
|
2598
|
+
invoke.call(this);
|
2599
|
+
} else if (!timeout) {
|
2600
|
+
timeout = setTimeout(() => {
|
2601
|
+
timeout = null;
|
2602
|
+
invoke.call(this);
|
2603
|
+
}, limit - (now - lastCall));
|
2604
|
+
}
|
2605
|
+
};
|
2606
|
+
}
|
2607
|
+
function leadingTrailingDebounce(func, delay) {
|
2608
|
+
let timer;
|
2609
|
+
let isFirstCall = true;
|
2610
|
+
return function (...args) {
|
2611
|
+
if (isFirstCall) {
|
2612
|
+
func.apply(this, args);
|
2613
|
+
isFirstCall = false;
|
2614
|
+
}
|
2615
|
+
clearTimeout(timer);
|
2616
|
+
timer = setTimeout(() => {
|
2617
|
+
func.apply(this, args);
|
2618
|
+
isFirstCall = true;
|
2619
|
+
}, delay);
|
2620
|
+
};
|
2621
|
+
}
|
2622
|
+
function rafThrottle(func) {
|
2623
|
+
let ticking = false;
|
2624
|
+
return function (...args) {
|
2625
|
+
if (!ticking) {
|
2626
|
+
ticking = true;
|
2627
|
+
requestAnimationFrame(() => {
|
2628
|
+
func.apply(this, args);
|
2629
|
+
ticking = false;
|
2630
|
+
});
|
2631
|
+
}
|
2632
|
+
};
|
2633
|
+
}
|
2634
|
+
|
2635
|
+
var Performance = /*#__PURE__*/Object.freeze({
|
2636
|
+
__proto__: null,
|
2637
|
+
denounce: denounce,
|
2638
|
+
leadingTrailingDebounce: leadingTrailingDebounce,
|
2639
|
+
rafThrottle: rafThrottle,
|
2640
|
+
tholle: tholle,
|
2641
|
+
trailingThrottle: trailingThrottle
|
2642
|
+
});
|
2643
|
+
|
2644
|
+
const Rumious = {
|
2645
|
+
...Component,
|
2646
|
+
...App,
|
2647
|
+
...JSX,
|
2648
|
+
...Render,
|
2649
|
+
...Ref,
|
2650
|
+
...State,
|
2651
|
+
...Context,
|
2652
|
+
...Performance
|
2653
|
+
};
|
2654
|
+
window.Rumious = Rumious;
|
2655
|
+
|
2656
|
+
return Rumious;
|
2657
|
+
|
2658
|
+
})();
|