@storybook/instrumenter 7.0.0-beta.2 → 7.0.0-beta.4

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/dist/index.js CHANGED
@@ -1 +1 @@
1
- var U=Object.create;var I=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var Y=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var F=(f,s)=>{for(var t in s)I(f,t,{get:s[t],enumerable:!0})},E=(f,s,t,d)=>{if(s&&typeof s=="object"||typeof s=="function")for(let r of M(s))!G.call(f,r)&&r!==t&&I(f,r,{get:()=>s[r],enumerable:!(d=B(s,r))||d.enumerable});return f};var K=(f,s,t)=>(t=f!=null?U(Y(f)):{},E(s||!f||!f.__esModule?I(t,"default",{value:f,enumerable:!0}):t,f)),V=f=>E(I({},"__esModule",{value:!0}),f);var X={};F(X,{CallStates:()=>N,EVENTS:()=>S,instrument:()=>L});module.exports=V(X);var k=require("@storybook/preview-api"),b=require("@storybook/client-logger"),m=require("@storybook/core-events"),_=K(require("global"));var N=(r=>(r.DONE="done",r.ERROR="error",r.ACTIVE="active",r.WAITING="waiting",r))(N||{});var S={CALL:"storybook/instrumenter/call",SYNC:"storybook/instrumenter/sync",START:"storybook/instrumenter/start",BACK:"storybook/instrumenter/back",GOTO:"storybook/instrumenter/goto",NEXT:"storybook/instrumenter/next",END:"storybook/instrumenter/end"},D,P=((D=_.default.FEATURES)==null?void 0:D.interactionsDebugger)!==!0,w={debugger:!P,start:!1,back:!1,goto:!1,next:!1,end:!1},A=new Error("This function ran after the play function completed. Did you forget to `await` it?"),x=f=>Object.prototype.toString.call(f)==="[object Object]",W=f=>Object.prototype.toString.call(f)==="[object Module]",z=f=>{if(!x(f)&&!W(f))return!1;if(f.constructor===void 0)return!0;let s=f.constructor.prototype;return!(!x(s)||Object.prototype.hasOwnProperty.call(s,"isPrototypeOf")===!1)},H=f=>{try{return new f.constructor}catch{return{}}},T=()=>({renderPhase:void 0,isDebugging:!1,isPlaying:!1,isLocked:!1,cursor:0,calls:[],shadowCalls:[],callRefsByResult:new Map,chainedCallIds:new Set,ancestors:[],playUntil:void 0,resolvers:{},syncTimeout:void 0}),j=(f,s=!1)=>{let t=(s?f.shadowCalls:f.calls).filter(r=>r.retain);if(!t.length)return;let d=new Map(Array.from(f.callRefsByResult.entries()).filter(([,r])=>r.retain));return{cursor:t.length,calls:t,callRefsByResult:d}},C=class{constructor(){this.initialized=!1;this.channel=k.addons.getChannel(),this.state=_.default.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__||{};let s=({storyId:n,isPlaying:o=!0,isDebugging:e=!1})=>{let c=this.getState(n);this.setState(n,{...T(),...j(c,e),shadowCalls:e?c.shadowCalls:[],chainedCallIds:e?c.chainedCallIds:new Set,playUntil:e?c.playUntil:void 0,isPlaying:o,isDebugging:e}),this.sync(n)};this.channel.on(m.FORCE_REMOUNT,s),this.channel.on(m.STORY_RENDER_PHASE_CHANGED,({storyId:n,newPhase:o})=>{let{isDebugging:e}=this.getState(n);this.setState(n,{renderPhase:o}),o==="preparing"&&e&&s({storyId:n}),o==="playing"&&s({storyId:n,isDebugging:e}),o==="played"&&this.setState(n,{isLocked:!1,isPlaying:!1,isDebugging:!1}),o==="errored"&&this.setState(n,{isLocked:!1,isPlaying:!1})}),this.channel.on(m.SET_CURRENT_STORY,()=>{this.initialized?this.cleanup():this.initialized=!0});let t=({storyId:n,playUntil:o})=>{this.getState(n).isDebugging||this.setState(n,({calls:c})=>({calls:[],shadowCalls:c.map(u=>({...u,status:"waiting"})),isDebugging:!0}));let e=this.getLog(n);this.setState(n,({shadowCalls:c})=>{var a;if(o||!e.length)return{playUntil:o};let u=c.findIndex(h=>h.id===e[0].callId);return{playUntil:(a=c.slice(0,u).filter(h=>h.interceptable&&!h.ancestors.length).slice(-1)[0])==null?void 0:a.id}}),this.channel.emit(m.FORCE_REMOUNT,{storyId:n,isDebugging:!0})},d=({storyId:n})=>{var c;let o=this.getLog(n).filter(u=>!u.ancestors.length),e=o.reduceRight((u,a,h)=>u>=0||a.status==="waiting"?u:h,-1);t({storyId:n,playUntil:(c=o[e-1])==null?void 0:c.callId})},r=({storyId:n,callId:o})=>{var p;let{calls:e,shadowCalls:c,resolvers:u}=this.getState(n),a=e.find(({id:g})=>g===o),h=c.find(({id:g})=>g===o);if(!a&&h&&Object.values(u).length>0){let g=(p=this.getLog(n).find(y=>y.status==="waiting"))==null?void 0:p.callId;h.id!==g&&this.setState(n,{playUntil:h.id}),Object.values(u).forEach(y=>y())}else t({storyId:n,playUntil:o})},i=({storyId:n})=>{var e;let{resolvers:o}=this.getState(n);if(Object.values(o).length>0)Object.values(o).forEach(c=>c());else{let c=(e=this.getLog(n).find(u=>u.status==="waiting"))==null?void 0:e.callId;c?t({storyId:n,playUntil:c}):l({storyId:n})}},l=({storyId:n})=>{this.setState(n,{playUntil:void 0,isDebugging:!1}),Object.values(this.getState(n).resolvers).forEach(o=>o())};this.channel.on(S.START,t),this.channel.on(S.BACK,d),this.channel.on(S.GOTO,r),this.channel.on(S.NEXT,i),this.channel.on(S.END,l)}getState(s){return this.state[s]||T()}setState(s,t){let d=this.getState(s),r=typeof t=="function"?t(d):t;this.state={...this.state,[s]:{...d,...r}},_.default.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__=this.state}cleanup(){this.state=Object.entries(this.state).reduce((t,[d,r])=>{let i=j(r);return i&&(t[d]=Object.assign(T(),i)),t},{});let s={controlStates:w,logItems:[]};this.channel.emit(S.SYNC,s),_.default.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__=this.state}getLog(s){let{calls:t,shadowCalls:d}=this.getState(s),r=[...d];t.forEach((l,n)=>{r[n]=l});let i=new Set;return r.reduceRight((l,n)=>(n.args.forEach(o=>{o!=null&&o.__callId__&&i.add(o.__callId__)}),n.path.forEach(o=>{o.__callId__&&i.add(o.__callId__)}),(n.interceptable||n.exception)&&!i.has(n.id)&&(l.unshift({callId:n.id,status:n.status,ancestors:n.ancestors}),i.add(n.id)),l),[])}instrument(s,t){if(!z(s))return s;let{mutate:d=!1,path:r=[]}=t;return Object.keys(s).reduce((i,l)=>{let n=s[l];return typeof n!="function"?(i[l]=this.instrument(n,{...t,path:r.concat(l)}),i):typeof n.__originalFn__=="function"?(i[l]=n,i):(i[l]=(...o)=>this.track(l,n,o,t),i[l].__originalFn__=n,Object.defineProperty(i[l],"name",{value:l,writable:!1}),Object.keys(n).length>0&&Object.assign(i[l],this.instrument({...n},{...t,path:r.concat(l)})),i)},d?s:H(s))}track(s,t,d,r){var y;let i=((y=d==null?void 0:d[0])==null?void 0:y.__storyId__)||_.default.window.__STORYBOOK_PREVIEW__.selectionStore.selection.storyId,{cursor:l,ancestors:n}=this.getState(i);this.setState(i,{cursor:l+1});let o=`${n.slice(-1)[0]||i} [${l}] ${s}`,{path:e=[],intercept:c=!1,retain:u=!1}=r,a=typeof c=="function"?c(s,e):c,h={id:o,cursor:l,storyId:i,ancestors:n,path:e,method:s,args:d,interceptable:a,retain:u},g=(a&&!n.length?this.intercept:this.invoke).call(this,t,h,r);return this.instrument(g,{...r,mutate:!0,path:[{__callId__:h.id}]})}intercept(s,t,d){let{chainedCallIds:r,isDebugging:i,playUntil:l}=this.getState(t.storyId),n=r.has(t.id);return!i||n||l?(l===t.id&&this.setState(t.storyId,{playUntil:void 0}),this.invoke(s,t,d)):new Promise(o=>{this.setState(t.storyId,({resolvers:e})=>({isLocked:!1,resolvers:{...e,[t.id]:o}}))}).then(()=>(this.setState(t.storyId,o=>{let{[t.id]:e,...c}=o.resolvers;return{isLocked:!0,resolvers:c}}),this.invoke(s,t,d)))}invoke(s,t,d){let{callRefsByResult:r,renderPhase:i}=this.getState(t.storyId),l=e=>{var c,u;if(r.has(e))return r.get(e);if(e instanceof Array)return e.map(l);if(e instanceof Date)return{__date__:{value:e.toISOString()}};if(e instanceof Error){let{name:a,message:h,stack:p}=e;return{__error__:{name:a,message:h,stack:p}}}if(e instanceof RegExp){let{flags:a,source:h}=e;return{__regexp__:{flags:a,source:h}}}if(e instanceof _.default.window.HTMLElement){let{prefix:a,localName:h,id:p,classList:g,innerText:y}=e,O=Array.from(g);return{__element__:{prefix:a,localName:h,id:p,classNames:O,innerText:y}}}return typeof e=="function"?{__function__:{name:e.name}}:typeof e=="symbol"?{__symbol__:{description:e.description}}:typeof e=="object"&&((c=e==null?void 0:e.constructor)==null?void 0:c.name)&&((u=e==null?void 0:e.constructor)==null?void 0:u.name)!=="Object"?{__class__:{name:e.constructor.name}}:Object.prototype.toString.call(e)==="[object Object]"?Object.fromEntries(Object.entries(e).map(([a,h])=>[a,l(h)])):e},n={...t,args:t.args.map(l)};t.path.forEach(e=>{e!=null&&e.__callId__&&this.setState(t.storyId,({chainedCallIds:c})=>({chainedCallIds:new Set(Array.from(c).concat(e.__callId__))}))});let o=e=>{if(e instanceof Error){let{name:c,message:u,stack:a,callId:h=t.id}=e,p={name:c,message:u,stack:a,callId:h};if(this.update({...n,status:"error",exception:p}),this.setState(t.storyId,g=>({callRefsByResult:new Map([...Array.from(g.callRefsByResult.entries()),[e,{__callId__:t.id,retain:t.retain}]])})),t.ancestors.length)throw Object.prototype.hasOwnProperty.call(e,"callId")||Object.defineProperty(e,"callId",{value:t.id}),e;if(e!==A)throw b.logger.warn(e),m.IGNORED_EXCEPTION}throw e};try{if(i==="played"&&!t.retain)throw A;let c=(d.getArgs?d.getArgs(t,this.getState(t.storyId)):t.args).map(a=>typeof a!="function"||Object.keys(a).length?a:(...h)=>{let{cursor:p,ancestors:g}=this.getState(t.storyId);this.setState(t.storyId,{cursor:0,ancestors:[...g,t.id]});let y=()=>this.setState(t.storyId,{cursor:p,ancestors:g}),O=!1;try{let R=a(...h);return R instanceof Promise?(O=!0,R.finally(y)):R}finally{O||y()}}),u=s(...c);return u&&["object","function","symbol"].includes(typeof u)&&this.setState(t.storyId,a=>({callRefsByResult:new Map([...Array.from(a.callRefsByResult.entries()),[u,{__callId__:t.id,retain:t.retain}]])})),this.update({...n,status:u instanceof Promise?"active":"done"}),u instanceof Promise?u.then(a=>(this.update({...n,status:"done"}),a),o):u}catch(e){return o(e)}}update(s){this.channel.emit(S.CALL,s),this.setState(s.storyId,({calls:t})=>{let d=t.concat(s).reduce((r,i)=>Object.assign(r,{[i.id]:i}),{});return{calls:Object.values(d).sort((r,i)=>r.id.localeCompare(i.id,void 0,{numeric:!0}))}}),this.sync(s.storyId)}sync(s){let t=()=>{var u;let{isLocked:d,isPlaying:r}=this.getState(s),i=this.getLog(s),l=(u=i.filter(({ancestors:a})=>!a.length).find(a=>a.status==="waiting"))==null?void 0:u.callId,n=i.some(a=>a.status==="active");if(P||d||n||i.length===0){let a={controlStates:w,logItems:i};this.channel.emit(S.SYNC,a);return}let o=i.some(a=>["done","error"].includes(a.status)),c={controlStates:{debugger:!0,start:o,back:o,goto:!0,next:r,end:r},logItems:i,pausedAt:l};this.channel.emit(S.SYNC,c)};this.setState(s,({syncTimeout:d})=>(clearTimeout(d),{syncTimeout:setTimeout(t,0)}))}};function L(f,s={}){var t,d,r,i;try{let l=!1,n=!1;return(d=(t=_.default.window.location)==null?void 0:t.search)!=null&&d.includes("instrument=true")?l=!0:(i=(r=_.default.window.location)==null?void 0:r.search)!=null&&i.includes("instrument=false")&&(n=!0),_.default.window.parent===_.default.window&&!l||n?f:(_.default.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__||(_.default.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__=new C),_.default.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__.instrument(f,s))}catch(l){return b.once.warn(l),f}}0&&(module.exports={CallStates,EVENTS,instrument});
1
+ var B=Object.create;var b=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var G=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var K=(f,s)=>{for(var t in s)b(f,t,{get:s[t],enumerable:!0})},N=(f,s,t,d)=>{if(s&&typeof s=="object"||typeof s=="function")for(let r of Y(s))!F.call(f,r)&&r!==t&&b(f,r,{get:()=>s[r],enumerable:!(d=M(s,r))||d.enumerable});return f};var V=(f,s,t)=>(t=f!=null?B(G(f)):{},N(s||!f||!f.__esModule?b(t,"default",{value:f,enumerable:!0}):t,f)),W=f=>N(b({},"__esModule",{value:!0}),f);var $={};K($,{CallStates:()=>w,EVENTS:()=>S,instrument:()=>U});module.exports=W($);var P=require("@storybook/preview-api"),R=require("@storybook/client-logger"),m=require("@storybook/core-events"),_=V(require("global"));var w=(r=>(r.DONE="done",r.ERROR="error",r.ACTIVE="active",r.WAITING="waiting",r))(w||{});var S={CALL:"storybook/instrumenter/call",SYNC:"storybook/instrumenter/sync",START:"storybook/instrumenter/start",BACK:"storybook/instrumenter/back",GOTO:"storybook/instrumenter/goto",NEXT:"storybook/instrumenter/next",END:"storybook/instrumenter/end"},k,L=((k=_.default.FEATURES)==null?void 0:k.interactionsDebugger)!==!0,A={debugger:!L,start:!1,back:!1,goto:!1,next:!1,end:!1},x=new Error("This function ran after the play function completed. Did you forget to `await` it?"),j=f=>Object.prototype.toString.call(f)==="[object Object]",z=f=>Object.prototype.toString.call(f)==="[object Module]",H=f=>{if(!j(f)&&!z(f))return!1;if(f.constructor===void 0)return!0;let s=f.constructor.prototype;return!(!j(s)||Object.prototype.hasOwnProperty.call(s,"isPrototypeOf")===!1)},X=f=>{try{return new f.constructor}catch{return{}}},T=()=>({renderPhase:void 0,isDebugging:!1,isPlaying:!1,isLocked:!1,cursor:0,calls:[],shadowCalls:[],callRefsByResult:new Map,chainedCallIds:new Set,ancestors:[],playUntil:void 0,resolvers:{},syncTimeout:void 0}),D=(f,s=!1)=>{let t=(s?f.shadowCalls:f.calls).filter(r=>r.retain);if(!t.length)return;let d=new Map(Array.from(f.callRefsByResult.entries()).filter(([,r])=>r.retain));return{cursor:t.length,calls:t,callRefsByResult:d}},C=class{constructor(){this.initialized=!1;this.channel=P.addons.getChannel(),this.state=_.default.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__||{};let s=({storyId:n,isPlaying:o=!0,isDebugging:e=!1})=>{let c=this.getState(n);this.setState(n,{...T(),...D(c,e),shadowCalls:e?c.shadowCalls:[],chainedCallIds:e?c.chainedCallIds:new Set,playUntil:e?c.playUntil:void 0,isPlaying:o,isDebugging:e}),this.sync(n)};this.channel.on(m.FORCE_REMOUNT,s),this.channel.on(m.STORY_RENDER_PHASE_CHANGED,({storyId:n,newPhase:o})=>{let{isDebugging:e}=this.getState(n);this.setState(n,{renderPhase:o}),o==="preparing"&&e&&s({storyId:n}),o==="playing"&&s({storyId:n,isDebugging:e}),o==="played"&&this.setState(n,{isLocked:!1,isPlaying:!1,isDebugging:!1}),o==="errored"&&this.setState(n,{isLocked:!1,isPlaying:!1})}),this.channel.on(m.SET_CURRENT_STORY,()=>{this.initialized?this.cleanup():this.initialized=!0});let t=({storyId:n,playUntil:o})=>{this.getState(n).isDebugging||this.setState(n,({calls:c})=>({calls:[],shadowCalls:c.map(u=>({...u,status:"waiting"})),isDebugging:!0}));let e=this.getLog(n);this.setState(n,({shadowCalls:c})=>{var a;if(o||!e.length)return{playUntil:o};let u=c.findIndex(h=>h.id===e[0].callId);return{playUntil:(a=c.slice(0,u).filter(h=>h.interceptable&&!h.ancestors.length).slice(-1)[0])==null?void 0:a.id}}),this.channel.emit(m.FORCE_REMOUNT,{storyId:n,isDebugging:!0})},d=({storyId:n})=>{var c;let o=this.getLog(n).filter(u=>!u.ancestors.length),e=o.reduceRight((u,a,h)=>u>=0||a.status==="waiting"?u:h,-1);t({storyId:n,playUntil:(c=o[e-1])==null?void 0:c.callId})},r=({storyId:n,callId:o})=>{var p;let{calls:e,shadowCalls:c,resolvers:u}=this.getState(n),a=e.find(({id:g})=>g===o),h=c.find(({id:g})=>g===o);if(!a&&h&&Object.values(u).length>0){let g=(p=this.getLog(n).find(y=>y.status==="waiting"))==null?void 0:p.callId;h.id!==g&&this.setState(n,{playUntil:h.id}),Object.values(u).forEach(y=>y())}else t({storyId:n,playUntil:o})},i=({storyId:n})=>{var e;let{resolvers:o}=this.getState(n);if(Object.values(o).length>0)Object.values(o).forEach(c=>c());else{let c=(e=this.getLog(n).find(u=>u.status==="waiting"))==null?void 0:e.callId;c?t({storyId:n,playUntil:c}):l({storyId:n})}},l=({storyId:n})=>{this.setState(n,{playUntil:void 0,isDebugging:!1}),Object.values(this.getState(n).resolvers).forEach(o=>o())};this.channel.on(S.START,t),this.channel.on(S.BACK,d),this.channel.on(S.GOTO,r),this.channel.on(S.NEXT,i),this.channel.on(S.END,l)}getState(s){return this.state[s]||T()}setState(s,t){let d=this.getState(s),r=typeof t=="function"?t(d):t;this.state={...this.state,[s]:{...d,...r}},_.default.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__=this.state}cleanup(){this.state=Object.entries(this.state).reduce((t,[d,r])=>{let i=D(r);return i&&(t[d]=Object.assign(T(),i)),t},{});let s={controlStates:A,logItems:[]};this.channel.emit(S.SYNC,s),_.default.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__=this.state}getLog(s){let{calls:t,shadowCalls:d}=this.getState(s),r=[...d];t.forEach((l,n)=>{r[n]=l});let i=new Set;return r.reduceRight((l,n)=>(n.args.forEach(o=>{o!=null&&o.__callId__&&i.add(o.__callId__)}),n.path.forEach(o=>{o.__callId__&&i.add(o.__callId__)}),(n.interceptable||n.exception)&&!i.has(n.id)&&(l.unshift({callId:n.id,status:n.status,ancestors:n.ancestors}),i.add(n.id)),l),[])}instrument(s,t){if(!H(s))return s;let{mutate:d=!1,path:r=[]}=t;return Object.keys(s).reduce((i,l)=>{let n=s[l];return typeof n!="function"?(i[l]=this.instrument(n,{...t,path:r.concat(l)}),i):typeof n.__originalFn__=="function"?(i[l]=n,i):(i[l]=(...o)=>this.track(l,n,o,t),i[l].__originalFn__=n,Object.defineProperty(i[l],"name",{value:l,writable:!1}),Object.keys(n).length>0&&Object.assign(i[l],this.instrument({...n},{...t,path:r.concat(l)})),i)},d?s:X(s))}track(s,t,d,r){var y,O,I,E;let i=((y=d==null?void 0:d[0])==null?void 0:y.__storyId__)||((E=(I=(O=_.default.window.__STORYBOOK_PREVIEW__)==null?void 0:O.selectionStore)==null?void 0:I.selection)==null?void 0:E.storyId),{cursor:l,ancestors:n}=this.getState(i);this.setState(i,{cursor:l+1});let o=`${n.slice(-1)[0]||i} [${l}] ${s}`,{path:e=[],intercept:c=!1,retain:u=!1}=r,a=typeof c=="function"?c(s,e):c,h={id:o,cursor:l,storyId:i,ancestors:n,path:e,method:s,args:d,interceptable:a,retain:u},g=(a&&!n.length?this.intercept:this.invoke).call(this,t,h,r);return this.instrument(g,{...r,mutate:!0,path:[{__callId__:h.id}]})}intercept(s,t,d){let{chainedCallIds:r,isDebugging:i,playUntil:l}=this.getState(t.storyId),n=r.has(t.id);return!i||n||l?(l===t.id&&this.setState(t.storyId,{playUntil:void 0}),this.invoke(s,t,d)):new Promise(o=>{this.setState(t.storyId,({resolvers:e})=>({isLocked:!1,resolvers:{...e,[t.id]:o}}))}).then(()=>(this.setState(t.storyId,o=>{let{[t.id]:e,...c}=o.resolvers;return{isLocked:!0,resolvers:c}}),this.invoke(s,t,d)))}invoke(s,t,d){let{callRefsByResult:r,renderPhase:i}=this.getState(t.storyId),l=e=>{var c,u;if(r.has(e))return r.get(e);if(e instanceof Array)return e.map(l);if(e instanceof Date)return{__date__:{value:e.toISOString()}};if(e instanceof Error){let{name:a,message:h,stack:p}=e;return{__error__:{name:a,message:h,stack:p}}}if(e instanceof RegExp){let{flags:a,source:h}=e;return{__regexp__:{flags:a,source:h}}}if(e instanceof _.default.window.HTMLElement){let{prefix:a,localName:h,id:p,classList:g,innerText:y}=e,O=Array.from(g);return{__element__:{prefix:a,localName:h,id:p,classNames:O,innerText:y}}}return typeof e=="function"?{__function__:{name:e.name}}:typeof e=="symbol"?{__symbol__:{description:e.description}}:typeof e=="object"&&((c=e==null?void 0:e.constructor)==null?void 0:c.name)&&((u=e==null?void 0:e.constructor)==null?void 0:u.name)!=="Object"?{__class__:{name:e.constructor.name}}:Object.prototype.toString.call(e)==="[object Object]"?Object.fromEntries(Object.entries(e).map(([a,h])=>[a,l(h)])):e},n={...t,args:t.args.map(l)};t.path.forEach(e=>{e!=null&&e.__callId__&&this.setState(t.storyId,({chainedCallIds:c})=>({chainedCallIds:new Set(Array.from(c).concat(e.__callId__))}))});let o=e=>{if(e instanceof Error){let{name:c,message:u,stack:a,callId:h=t.id}=e,p={name:c,message:u,stack:a,callId:h};if(this.update({...n,status:"error",exception:p}),this.setState(t.storyId,g=>({callRefsByResult:new Map([...Array.from(g.callRefsByResult.entries()),[e,{__callId__:t.id,retain:t.retain}]])})),t.ancestors.length)throw Object.prototype.hasOwnProperty.call(e,"callId")||Object.defineProperty(e,"callId",{value:t.id}),e;if(e!==x)throw R.logger.warn(e),m.IGNORED_EXCEPTION}throw e};try{if(i==="played"&&!t.retain)throw x;let c=(d.getArgs?d.getArgs(t,this.getState(t.storyId)):t.args).map(a=>typeof a!="function"||Object.keys(a).length?a:(...h)=>{let{cursor:p,ancestors:g}=this.getState(t.storyId);this.setState(t.storyId,{cursor:0,ancestors:[...g,t.id]});let y=()=>this.setState(t.storyId,{cursor:p,ancestors:g}),O=!1;try{let I=a(...h);return I instanceof Promise?(O=!0,I.finally(y)):I}finally{O||y()}}),u=s(...c);return u&&["object","function","symbol"].includes(typeof u)&&this.setState(t.storyId,a=>({callRefsByResult:new Map([...Array.from(a.callRefsByResult.entries()),[u,{__callId__:t.id,retain:t.retain}]])})),this.update({...n,status:u instanceof Promise?"active":"done"}),u instanceof Promise?u.then(a=>(this.update({...n,status:"done"}),a),o):u}catch(e){return o(e)}}update(s){this.channel.emit(S.CALL,s),this.setState(s.storyId,({calls:t})=>{let d=t.concat(s).reduce((r,i)=>Object.assign(r,{[i.id]:i}),{});return{calls:Object.values(d).sort((r,i)=>r.id.localeCompare(i.id,void 0,{numeric:!0}))}}),this.sync(s.storyId)}sync(s){let t=()=>{var u;let{isLocked:d,isPlaying:r}=this.getState(s),i=this.getLog(s),l=(u=i.filter(({ancestors:a})=>!a.length).find(a=>a.status==="waiting"))==null?void 0:u.callId,n=i.some(a=>a.status==="active");if(L||d||n||i.length===0){let a={controlStates:A,logItems:i};this.channel.emit(S.SYNC,a);return}let o=i.some(a=>["done","error"].includes(a.status)),c={controlStates:{debugger:!0,start:o,back:o,goto:!0,next:r,end:r},logItems:i,pausedAt:l};this.channel.emit(S.SYNC,c)};this.setState(s,({syncTimeout:d})=>(clearTimeout(d),{syncTimeout:setTimeout(t,0)}))}};function U(f,s={}){var t,d,r,i;try{let l=!1,n=!1;return(d=(t=_.default.window.location)==null?void 0:t.search)!=null&&d.includes("instrument=true")?l=!0:(i=(r=_.default.window.location)==null?void 0:r.search)!=null&&i.includes("instrument=false")&&(n=!0),_.default.window.parent===_.default.window&&!l||n?f:(_.default.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__||(_.default.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__=new C),_.default.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__.instrument(f,s))}catch(l){return R.once.warn(l),f}}0&&(module.exports={CallStates,EVENTS,instrument});
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{addons}from"@storybook/preview-api";import{once,logger}from"@storybook/client-logger";import{FORCE_REMOUNT,IGNORED_EXCEPTION,SET_CURRENT_STORY,STORY_RENDER_PHASE_CHANGED}from"@storybook/core-events";import global from"global";var CallStates=(CallStates2=>(CallStates2.DONE="done",CallStates2.ERROR="error",CallStates2.ACTIVE="active",CallStates2.WAITING="waiting",CallStates2))(CallStates||{});var EVENTS={CALL:"storybook/instrumenter/call",SYNC:"storybook/instrumenter/sync",START:"storybook/instrumenter/start",BACK:"storybook/instrumenter/back",GOTO:"storybook/instrumenter/goto",NEXT:"storybook/instrumenter/next",END:"storybook/instrumenter/end"},debuggerDisabled=global.FEATURES?.interactionsDebugger!==!0,controlsDisabled={debugger:!debuggerDisabled,start:!1,back:!1,goto:!1,next:!1,end:!1},alreadyCompletedException=new Error("This function ran after the play function completed. Did you forget to `await` it?"),isObject=o=>Object.prototype.toString.call(o)==="[object Object]",isModule=o=>Object.prototype.toString.call(o)==="[object Module]",isInstrumentable=o=>{if(!isObject(o)&&!isModule(o))return!1;if(o.constructor===void 0)return!0;let proto=o.constructor.prototype;return!(!isObject(proto)||Object.prototype.hasOwnProperty.call(proto,"isPrototypeOf")===!1)},construct=obj=>{try{return new obj.constructor}catch{return{}}},getInitialState=()=>({renderPhase:void 0,isDebugging:!1,isPlaying:!1,isLocked:!1,cursor:0,calls:[],shadowCalls:[],callRefsByResult:new Map,chainedCallIds:new Set,ancestors:[],playUntil:void 0,resolvers:{},syncTimeout:void 0}),getRetainedState=(state,isDebugging=!1)=>{let calls=(isDebugging?state.shadowCalls:state.calls).filter(call=>call.retain);if(!calls.length)return;let callRefsByResult=new Map(Array.from(state.callRefsByResult.entries()).filter(([,ref])=>ref.retain));return{cursor:calls.length,calls,callRefsByResult}},Instrumenter=class{constructor(){this.initialized=!1;this.channel=addons.getChannel(),this.state=global.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__||{};let resetState=({storyId,isPlaying=!0,isDebugging=!1})=>{let state=this.getState(storyId);this.setState(storyId,{...getInitialState(),...getRetainedState(state,isDebugging),shadowCalls:isDebugging?state.shadowCalls:[],chainedCallIds:isDebugging?state.chainedCallIds:new Set,playUntil:isDebugging?state.playUntil:void 0,isPlaying,isDebugging}),this.sync(storyId)};this.channel.on(FORCE_REMOUNT,resetState),this.channel.on(STORY_RENDER_PHASE_CHANGED,({storyId,newPhase})=>{let{isDebugging}=this.getState(storyId);this.setState(storyId,{renderPhase:newPhase}),newPhase==="preparing"&&isDebugging&&resetState({storyId}),newPhase==="playing"&&resetState({storyId,isDebugging}),newPhase==="played"&&this.setState(storyId,{isLocked:!1,isPlaying:!1,isDebugging:!1}),newPhase==="errored"&&this.setState(storyId,{isLocked:!1,isPlaying:!1})}),this.channel.on(SET_CURRENT_STORY,()=>{this.initialized?this.cleanup():this.initialized=!0});let start=({storyId,playUntil})=>{this.getState(storyId).isDebugging||this.setState(storyId,({calls})=>({calls:[],shadowCalls:calls.map(call=>({...call,status:"waiting"})),isDebugging:!0}));let log=this.getLog(storyId);this.setState(storyId,({shadowCalls})=>{if(playUntil||!log.length)return{playUntil};let firstRowIndex=shadowCalls.findIndex(call=>call.id===log[0].callId);return{playUntil:shadowCalls.slice(0,firstRowIndex).filter(call=>call.interceptable&&!call.ancestors.length).slice(-1)[0]?.id}}),this.channel.emit(FORCE_REMOUNT,{storyId,isDebugging:!0})},back=({storyId})=>{let log=this.getLog(storyId).filter(call=>!call.ancestors.length),last=log.reduceRight((res,item,index)=>res>=0||item.status==="waiting"?res:index,-1);start({storyId,playUntil:log[last-1]?.callId})},goto=({storyId,callId})=>{let{calls,shadowCalls,resolvers}=this.getState(storyId),call=calls.find(({id})=>id===callId),shadowCall=shadowCalls.find(({id})=>id===callId);if(!call&&shadowCall&&Object.values(resolvers).length>0){let nextId=this.getLog(storyId).find(c=>c.status==="waiting")?.callId;shadowCall.id!==nextId&&this.setState(storyId,{playUntil:shadowCall.id}),Object.values(resolvers).forEach(resolve=>resolve())}else start({storyId,playUntil:callId})},next=({storyId})=>{let{resolvers}=this.getState(storyId);if(Object.values(resolvers).length>0)Object.values(resolvers).forEach(resolve=>resolve());else{let nextId=this.getLog(storyId).find(c=>c.status==="waiting")?.callId;nextId?start({storyId,playUntil:nextId}):end({storyId})}},end=({storyId})=>{this.setState(storyId,{playUntil:void 0,isDebugging:!1}),Object.values(this.getState(storyId).resolvers).forEach(resolve=>resolve())};this.channel.on(EVENTS.START,start),this.channel.on(EVENTS.BACK,back),this.channel.on(EVENTS.GOTO,goto),this.channel.on(EVENTS.NEXT,next),this.channel.on(EVENTS.END,end)}getState(storyId){return this.state[storyId]||getInitialState()}setState(storyId,update){let state=this.getState(storyId),patch=typeof update=="function"?update(state):update;this.state={...this.state,[storyId]:{...state,...patch}},global.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__=this.state}cleanup(){this.state=Object.entries(this.state).reduce((acc,[storyId,state])=>{let retainedState=getRetainedState(state);return retainedState&&(acc[storyId]=Object.assign(getInitialState(),retainedState)),acc},{});let payload={controlStates:controlsDisabled,logItems:[]};this.channel.emit(EVENTS.SYNC,payload),global.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__=this.state}getLog(storyId){let{calls,shadowCalls}=this.getState(storyId),merged=[...shadowCalls];calls.forEach((call,index)=>{merged[index]=call});let seen=new Set;return merged.reduceRight((acc,call)=>(call.args.forEach(arg=>{arg?.__callId__&&seen.add(arg.__callId__)}),call.path.forEach(node=>{node.__callId__&&seen.add(node.__callId__)}),(call.interceptable||call.exception)&&!seen.has(call.id)&&(acc.unshift({callId:call.id,status:call.status,ancestors:call.ancestors}),seen.add(call.id)),acc),[])}instrument(obj,options){if(!isInstrumentable(obj))return obj;let{mutate=!1,path=[]}=options;return Object.keys(obj).reduce((acc,key)=>{let value=obj[key];return typeof value!="function"?(acc[key]=this.instrument(value,{...options,path:path.concat(key)}),acc):typeof value.__originalFn__=="function"?(acc[key]=value,acc):(acc[key]=(...args)=>this.track(key,value,args,options),acc[key].__originalFn__=value,Object.defineProperty(acc[key],"name",{value:key,writable:!1}),Object.keys(value).length>0&&Object.assign(acc[key],this.instrument({...value},{...options,path:path.concat(key)})),acc)},mutate?obj:construct(obj))}track(method,fn,args,options){let storyId=args?.[0]?.__storyId__||global.window.__STORYBOOK_PREVIEW__.selectionStore.selection.storyId,{cursor,ancestors}=this.getState(storyId);this.setState(storyId,{cursor:cursor+1});let id=`${ancestors.slice(-1)[0]||storyId} [${cursor}] ${method}`,{path=[],intercept=!1,retain=!1}=options,interceptable=typeof intercept=="function"?intercept(method,path):intercept,call={id,cursor,storyId,ancestors,path,method,args,interceptable,retain},result=(interceptable&&!ancestors.length?this.intercept:this.invoke).call(this,fn,call,options);return this.instrument(result,{...options,mutate:!0,path:[{__callId__:call.id}]})}intercept(fn,call,options){let{chainedCallIds,isDebugging,playUntil}=this.getState(call.storyId),isChainedUpon=chainedCallIds.has(call.id);return!isDebugging||isChainedUpon||playUntil?(playUntil===call.id&&this.setState(call.storyId,{playUntil:void 0}),this.invoke(fn,call,options)):new Promise(resolve=>{this.setState(call.storyId,({resolvers})=>({isLocked:!1,resolvers:{...resolvers,[call.id]:resolve}}))}).then(()=>(this.setState(call.storyId,state=>{let{[call.id]:_,...resolvers}=state.resolvers;return{isLocked:!0,resolvers}}),this.invoke(fn,call,options)))}invoke(fn,call,options){let{callRefsByResult,renderPhase}=this.getState(call.storyId),serializeValues=value=>{if(callRefsByResult.has(value))return callRefsByResult.get(value);if(value instanceof Array)return value.map(serializeValues);if(value instanceof Date)return{__date__:{value:value.toISOString()}};if(value instanceof Error){let{name,message,stack}=value;return{__error__:{name,message,stack}}}if(value instanceof RegExp){let{flags,source}=value;return{__regexp__:{flags,source}}}if(value instanceof global.window.HTMLElement){let{prefix,localName,id,classList,innerText}=value,classNames=Array.from(classList);return{__element__:{prefix,localName,id,classNames,innerText}}}return typeof value=="function"?{__function__:{name:value.name}}:typeof value=="symbol"?{__symbol__:{description:value.description}}:typeof value=="object"&&value?.constructor?.name&&value?.constructor?.name!=="Object"?{__class__:{name:value.constructor.name}}:Object.prototype.toString.call(value)==="[object Object]"?Object.fromEntries(Object.entries(value).map(([key,val])=>[key,serializeValues(val)])):value},info={...call,args:call.args.map(serializeValues)};call.path.forEach(ref=>{ref?.__callId__&&this.setState(call.storyId,({chainedCallIds})=>({chainedCallIds:new Set(Array.from(chainedCallIds).concat(ref.__callId__))}))});let handleException=e=>{if(e instanceof Error){let{name,message,stack,callId=call.id}=e,exception={name,message,stack,callId};if(this.update({...info,status:"error",exception}),this.setState(call.storyId,state=>({callRefsByResult:new Map([...Array.from(state.callRefsByResult.entries()),[e,{__callId__:call.id,retain:call.retain}]])})),call.ancestors.length)throw Object.prototype.hasOwnProperty.call(e,"callId")||Object.defineProperty(e,"callId",{value:call.id}),e;if(e!==alreadyCompletedException)throw logger.warn(e),IGNORED_EXCEPTION}throw e};try{if(renderPhase==="played"&&!call.retain)throw alreadyCompletedException;let finalArgs=(options.getArgs?options.getArgs(call,this.getState(call.storyId)):call.args).map(arg=>typeof arg!="function"||Object.keys(arg).length?arg:(...args)=>{let{cursor,ancestors}=this.getState(call.storyId);this.setState(call.storyId,{cursor:0,ancestors:[...ancestors,call.id]});let restore=()=>this.setState(call.storyId,{cursor,ancestors}),willRestore=!1;try{let res=arg(...args);return res instanceof Promise?(willRestore=!0,res.finally(restore)):res}finally{willRestore||restore()}}),result=fn(...finalArgs);return result&&["object","function","symbol"].includes(typeof result)&&this.setState(call.storyId,state=>({callRefsByResult:new Map([...Array.from(state.callRefsByResult.entries()),[result,{__callId__:call.id,retain:call.retain}]])})),this.update({...info,status:result instanceof Promise?"active":"done"}),result instanceof Promise?result.then(value=>(this.update({...info,status:"done"}),value),handleException):result}catch(e){return handleException(e)}}update(call){this.channel.emit(EVENTS.CALL,call),this.setState(call.storyId,({calls})=>{let callsById=calls.concat(call).reduce((a,c)=>Object.assign(a,{[c.id]:c}),{});return{calls:Object.values(callsById).sort((a,b)=>a.id.localeCompare(b.id,void 0,{numeric:!0}))}}),this.sync(call.storyId)}sync(storyId){let synchronize=()=>{let{isLocked,isPlaying}=this.getState(storyId),logItems=this.getLog(storyId),pausedAt=logItems.filter(({ancestors})=>!ancestors.length).find(item=>item.status==="waiting")?.callId,hasActive=logItems.some(item=>item.status==="active");if(debuggerDisabled||isLocked||hasActive||logItems.length===0){let payload2={controlStates:controlsDisabled,logItems};this.channel.emit(EVENTS.SYNC,payload2);return}let hasPrevious=logItems.some(item=>["done","error"].includes(item.status)),payload={controlStates:{debugger:!0,start:hasPrevious,back:hasPrevious,goto:!0,next:isPlaying,end:isPlaying},logItems,pausedAt};this.channel.emit(EVENTS.SYNC,payload)};this.setState(storyId,({syncTimeout})=>(clearTimeout(syncTimeout),{syncTimeout:setTimeout(synchronize,0)}))}};function instrument(obj,options={}){try{let forceInstrument=!1,skipInstrument=!1;return global.window.location?.search?.includes("instrument=true")?forceInstrument=!0:global.window.location?.search?.includes("instrument=false")&&(skipInstrument=!0),global.window.parent===global.window&&!forceInstrument||skipInstrument?obj:(global.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__||(global.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__=new Instrumenter),global.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__.instrument(obj,options))}catch(e){return once.warn(e),obj}}export{CallStates,EVENTS,instrument};
1
+ import{addons}from"@storybook/preview-api";import{once,logger}from"@storybook/client-logger";import{FORCE_REMOUNT,IGNORED_EXCEPTION,SET_CURRENT_STORY,STORY_RENDER_PHASE_CHANGED}from"@storybook/core-events";import global from"global";var CallStates=(CallStates2=>(CallStates2.DONE="done",CallStates2.ERROR="error",CallStates2.ACTIVE="active",CallStates2.WAITING="waiting",CallStates2))(CallStates||{});var EVENTS={CALL:"storybook/instrumenter/call",SYNC:"storybook/instrumenter/sync",START:"storybook/instrumenter/start",BACK:"storybook/instrumenter/back",GOTO:"storybook/instrumenter/goto",NEXT:"storybook/instrumenter/next",END:"storybook/instrumenter/end"},debuggerDisabled=global.FEATURES?.interactionsDebugger!==!0,controlsDisabled={debugger:!debuggerDisabled,start:!1,back:!1,goto:!1,next:!1,end:!1},alreadyCompletedException=new Error("This function ran after the play function completed. Did you forget to `await` it?"),isObject=o=>Object.prototype.toString.call(o)==="[object Object]",isModule=o=>Object.prototype.toString.call(o)==="[object Module]",isInstrumentable=o=>{if(!isObject(o)&&!isModule(o))return!1;if(o.constructor===void 0)return!0;let proto=o.constructor.prototype;return!(!isObject(proto)||Object.prototype.hasOwnProperty.call(proto,"isPrototypeOf")===!1)},construct=obj=>{try{return new obj.constructor}catch{return{}}},getInitialState=()=>({renderPhase:void 0,isDebugging:!1,isPlaying:!1,isLocked:!1,cursor:0,calls:[],shadowCalls:[],callRefsByResult:new Map,chainedCallIds:new Set,ancestors:[],playUntil:void 0,resolvers:{},syncTimeout:void 0}),getRetainedState=(state,isDebugging=!1)=>{let calls=(isDebugging?state.shadowCalls:state.calls).filter(call=>call.retain);if(!calls.length)return;let callRefsByResult=new Map(Array.from(state.callRefsByResult.entries()).filter(([,ref])=>ref.retain));return{cursor:calls.length,calls,callRefsByResult}},Instrumenter=class{constructor(){this.initialized=!1;this.channel=addons.getChannel(),this.state=global.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__||{};let resetState=({storyId,isPlaying=!0,isDebugging=!1})=>{let state=this.getState(storyId);this.setState(storyId,{...getInitialState(),...getRetainedState(state,isDebugging),shadowCalls:isDebugging?state.shadowCalls:[],chainedCallIds:isDebugging?state.chainedCallIds:new Set,playUntil:isDebugging?state.playUntil:void 0,isPlaying,isDebugging}),this.sync(storyId)};this.channel.on(FORCE_REMOUNT,resetState),this.channel.on(STORY_RENDER_PHASE_CHANGED,({storyId,newPhase})=>{let{isDebugging}=this.getState(storyId);this.setState(storyId,{renderPhase:newPhase}),newPhase==="preparing"&&isDebugging&&resetState({storyId}),newPhase==="playing"&&resetState({storyId,isDebugging}),newPhase==="played"&&this.setState(storyId,{isLocked:!1,isPlaying:!1,isDebugging:!1}),newPhase==="errored"&&this.setState(storyId,{isLocked:!1,isPlaying:!1})}),this.channel.on(SET_CURRENT_STORY,()=>{this.initialized?this.cleanup():this.initialized=!0});let start=({storyId,playUntil})=>{this.getState(storyId).isDebugging||this.setState(storyId,({calls})=>({calls:[],shadowCalls:calls.map(call=>({...call,status:"waiting"})),isDebugging:!0}));let log=this.getLog(storyId);this.setState(storyId,({shadowCalls})=>{if(playUntil||!log.length)return{playUntil};let firstRowIndex=shadowCalls.findIndex(call=>call.id===log[0].callId);return{playUntil:shadowCalls.slice(0,firstRowIndex).filter(call=>call.interceptable&&!call.ancestors.length).slice(-1)[0]?.id}}),this.channel.emit(FORCE_REMOUNT,{storyId,isDebugging:!0})},back=({storyId})=>{let log=this.getLog(storyId).filter(call=>!call.ancestors.length),last=log.reduceRight((res,item,index)=>res>=0||item.status==="waiting"?res:index,-1);start({storyId,playUntil:log[last-1]?.callId})},goto=({storyId,callId})=>{let{calls,shadowCalls,resolvers}=this.getState(storyId),call=calls.find(({id})=>id===callId),shadowCall=shadowCalls.find(({id})=>id===callId);if(!call&&shadowCall&&Object.values(resolvers).length>0){let nextId=this.getLog(storyId).find(c=>c.status==="waiting")?.callId;shadowCall.id!==nextId&&this.setState(storyId,{playUntil:shadowCall.id}),Object.values(resolvers).forEach(resolve=>resolve())}else start({storyId,playUntil:callId})},next=({storyId})=>{let{resolvers}=this.getState(storyId);if(Object.values(resolvers).length>0)Object.values(resolvers).forEach(resolve=>resolve());else{let nextId=this.getLog(storyId).find(c=>c.status==="waiting")?.callId;nextId?start({storyId,playUntil:nextId}):end({storyId})}},end=({storyId})=>{this.setState(storyId,{playUntil:void 0,isDebugging:!1}),Object.values(this.getState(storyId).resolvers).forEach(resolve=>resolve())};this.channel.on(EVENTS.START,start),this.channel.on(EVENTS.BACK,back),this.channel.on(EVENTS.GOTO,goto),this.channel.on(EVENTS.NEXT,next),this.channel.on(EVENTS.END,end)}getState(storyId){return this.state[storyId]||getInitialState()}setState(storyId,update){let state=this.getState(storyId),patch=typeof update=="function"?update(state):update;this.state={...this.state,[storyId]:{...state,...patch}},global.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__=this.state}cleanup(){this.state=Object.entries(this.state).reduce((acc,[storyId,state])=>{let retainedState=getRetainedState(state);return retainedState&&(acc[storyId]=Object.assign(getInitialState(),retainedState)),acc},{});let payload={controlStates:controlsDisabled,logItems:[]};this.channel.emit(EVENTS.SYNC,payload),global.window.parent.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER_STATE__=this.state}getLog(storyId){let{calls,shadowCalls}=this.getState(storyId),merged=[...shadowCalls];calls.forEach((call,index)=>{merged[index]=call});let seen=new Set;return merged.reduceRight((acc,call)=>(call.args.forEach(arg=>{arg?.__callId__&&seen.add(arg.__callId__)}),call.path.forEach(node=>{node.__callId__&&seen.add(node.__callId__)}),(call.interceptable||call.exception)&&!seen.has(call.id)&&(acc.unshift({callId:call.id,status:call.status,ancestors:call.ancestors}),seen.add(call.id)),acc),[])}instrument(obj,options){if(!isInstrumentable(obj))return obj;let{mutate=!1,path=[]}=options;return Object.keys(obj).reduce((acc,key)=>{let value=obj[key];return typeof value!="function"?(acc[key]=this.instrument(value,{...options,path:path.concat(key)}),acc):typeof value.__originalFn__=="function"?(acc[key]=value,acc):(acc[key]=(...args)=>this.track(key,value,args,options),acc[key].__originalFn__=value,Object.defineProperty(acc[key],"name",{value:key,writable:!1}),Object.keys(value).length>0&&Object.assign(acc[key],this.instrument({...value},{...options,path:path.concat(key)})),acc)},mutate?obj:construct(obj))}track(method,fn,args,options){let storyId=args?.[0]?.__storyId__||global.window.__STORYBOOK_PREVIEW__?.selectionStore?.selection?.storyId,{cursor,ancestors}=this.getState(storyId);this.setState(storyId,{cursor:cursor+1});let id=`${ancestors.slice(-1)[0]||storyId} [${cursor}] ${method}`,{path=[],intercept=!1,retain=!1}=options,interceptable=typeof intercept=="function"?intercept(method,path):intercept,call={id,cursor,storyId,ancestors,path,method,args,interceptable,retain},result=(interceptable&&!ancestors.length?this.intercept:this.invoke).call(this,fn,call,options);return this.instrument(result,{...options,mutate:!0,path:[{__callId__:call.id}]})}intercept(fn,call,options){let{chainedCallIds,isDebugging,playUntil}=this.getState(call.storyId),isChainedUpon=chainedCallIds.has(call.id);return!isDebugging||isChainedUpon||playUntil?(playUntil===call.id&&this.setState(call.storyId,{playUntil:void 0}),this.invoke(fn,call,options)):new Promise(resolve=>{this.setState(call.storyId,({resolvers})=>({isLocked:!1,resolvers:{...resolvers,[call.id]:resolve}}))}).then(()=>(this.setState(call.storyId,state=>{let{[call.id]:_,...resolvers}=state.resolvers;return{isLocked:!0,resolvers}}),this.invoke(fn,call,options)))}invoke(fn,call,options){let{callRefsByResult,renderPhase}=this.getState(call.storyId),serializeValues=value=>{if(callRefsByResult.has(value))return callRefsByResult.get(value);if(value instanceof Array)return value.map(serializeValues);if(value instanceof Date)return{__date__:{value:value.toISOString()}};if(value instanceof Error){let{name,message,stack}=value;return{__error__:{name,message,stack}}}if(value instanceof RegExp){let{flags,source}=value;return{__regexp__:{flags,source}}}if(value instanceof global.window.HTMLElement){let{prefix,localName,id,classList,innerText}=value,classNames=Array.from(classList);return{__element__:{prefix,localName,id,classNames,innerText}}}return typeof value=="function"?{__function__:{name:value.name}}:typeof value=="symbol"?{__symbol__:{description:value.description}}:typeof value=="object"&&value?.constructor?.name&&value?.constructor?.name!=="Object"?{__class__:{name:value.constructor.name}}:Object.prototype.toString.call(value)==="[object Object]"?Object.fromEntries(Object.entries(value).map(([key,val])=>[key,serializeValues(val)])):value},info={...call,args:call.args.map(serializeValues)};call.path.forEach(ref=>{ref?.__callId__&&this.setState(call.storyId,({chainedCallIds})=>({chainedCallIds:new Set(Array.from(chainedCallIds).concat(ref.__callId__))}))});let handleException=e=>{if(e instanceof Error){let{name,message,stack,callId=call.id}=e,exception={name,message,stack,callId};if(this.update({...info,status:"error",exception}),this.setState(call.storyId,state=>({callRefsByResult:new Map([...Array.from(state.callRefsByResult.entries()),[e,{__callId__:call.id,retain:call.retain}]])})),call.ancestors.length)throw Object.prototype.hasOwnProperty.call(e,"callId")||Object.defineProperty(e,"callId",{value:call.id}),e;if(e!==alreadyCompletedException)throw logger.warn(e),IGNORED_EXCEPTION}throw e};try{if(renderPhase==="played"&&!call.retain)throw alreadyCompletedException;let finalArgs=(options.getArgs?options.getArgs(call,this.getState(call.storyId)):call.args).map(arg=>typeof arg!="function"||Object.keys(arg).length?arg:(...args)=>{let{cursor,ancestors}=this.getState(call.storyId);this.setState(call.storyId,{cursor:0,ancestors:[...ancestors,call.id]});let restore=()=>this.setState(call.storyId,{cursor,ancestors}),willRestore=!1;try{let res=arg(...args);return res instanceof Promise?(willRestore=!0,res.finally(restore)):res}finally{willRestore||restore()}}),result=fn(...finalArgs);return result&&["object","function","symbol"].includes(typeof result)&&this.setState(call.storyId,state=>({callRefsByResult:new Map([...Array.from(state.callRefsByResult.entries()),[result,{__callId__:call.id,retain:call.retain}]])})),this.update({...info,status:result instanceof Promise?"active":"done"}),result instanceof Promise?result.then(value=>(this.update({...info,status:"done"}),value),handleException):result}catch(e){return handleException(e)}}update(call){this.channel.emit(EVENTS.CALL,call),this.setState(call.storyId,({calls})=>{let callsById=calls.concat(call).reduce((a,c)=>Object.assign(a,{[c.id]:c}),{});return{calls:Object.values(callsById).sort((a,b)=>a.id.localeCompare(b.id,void 0,{numeric:!0}))}}),this.sync(call.storyId)}sync(storyId){let synchronize=()=>{let{isLocked,isPlaying}=this.getState(storyId),logItems=this.getLog(storyId),pausedAt=logItems.filter(({ancestors})=>!ancestors.length).find(item=>item.status==="waiting")?.callId,hasActive=logItems.some(item=>item.status==="active");if(debuggerDisabled||isLocked||hasActive||logItems.length===0){let payload2={controlStates:controlsDisabled,logItems};this.channel.emit(EVENTS.SYNC,payload2);return}let hasPrevious=logItems.some(item=>["done","error"].includes(item.status)),payload={controlStates:{debugger:!0,start:hasPrevious,back:hasPrevious,goto:!0,next:isPlaying,end:isPlaying},logItems,pausedAt};this.channel.emit(EVENTS.SYNC,payload)};this.setState(storyId,({syncTimeout})=>(clearTimeout(syncTimeout),{syncTimeout:setTimeout(synchronize,0)}))}};function instrument(obj,options={}){try{let forceInstrument=!1,skipInstrument=!1;return global.window.location?.search?.includes("instrument=true")?forceInstrument=!0:global.window.location?.search?.includes("instrument=false")&&(skipInstrument=!0),global.window.parent===global.window&&!forceInstrument||skipInstrument?obj:(global.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__||(global.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__=new Instrumenter),global.window.__STORYBOOK_ADDON_INTERACTIONS_INSTRUMENTER__.instrument(obj,options))}catch(e){return once.warn(e),obj}}export{CallStates,EVENTS,instrument};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/instrumenter",
3
- "version": "7.0.0-beta.2",
3
+ "version": "7.0.0-beta.4",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "storybook"
@@ -42,10 +42,10 @@
42
42
  "prep": "../../../scripts/prepare/bundle.ts"
43
43
  },
44
44
  "dependencies": {
45
- "@storybook/channels": "7.0.0-beta.2",
46
- "@storybook/client-logger": "7.0.0-beta.2",
47
- "@storybook/core-events": "7.0.0-beta.2",
48
- "@storybook/preview-api": "7.0.0-beta.2",
45
+ "@storybook/channels": "7.0.0-beta.4",
46
+ "@storybook/client-logger": "7.0.0-beta.4",
47
+ "@storybook/core-events": "7.0.0-beta.4",
48
+ "@storybook/preview-api": "7.0.0-beta.4",
49
49
  "core-js": "^3.8.2",
50
50
  "global": "^4.4.0"
51
51
  },
@@ -60,5 +60,5 @@
60
60
  "./src/index.ts"
61
61
  ]
62
62
  },
63
- "gitHead": "66a0d588184add900c48d49f3a62743fa9cc5185"
63
+ "gitHead": "4d869ed6f7166e0d06f2bef5a1b872fa77a0cf2d"
64
64
  }