@psytask/core 1.1.1 → 1.1.3

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.d.ts CHANGED
@@ -69,13 +69,19 @@ declare const createTimer: (
69
69
  * Return true to stop timer
70
70
  *
71
71
  * @param time Current frame time
72
- * @param records History frame times, the first frame means the first frame
73
- * that triggers VSync
72
+ * @param records History frame times, whose elements represent the start time
73
+ * of each frame that triggers VSync
74
74
  */
75
75
  shouldStop: (time: number, records: TimerRecords) => boolean) => Timer;
76
76
  type NodeLike = string | Node;
77
77
  type BuiltinData = {
78
+ /**
79
+ * History frame times, whose elements represent the start time of each frame
80
+ * that triggers VSync
81
+ */
78
82
  frame_times: TimerRecords;
83
+ /** Real show duration */
84
+ duration: number;
79
85
  };
80
86
  type ForbiddenData = {
81
87
  [K in keyof BuiltinData]?: never;
@@ -210,8 +216,8 @@ declare class Scene<T extends MaybeGenericComponent> extends EventEmitter<SceneE
210
216
  * @param options - {@link SceneOptions}
211
217
  */
212
218
  constructor(component: T, options: SceneOptions<T>);
213
- /** Add a microtask to close the scene. It is useful when close in 'show' */
214
- close(): Promise<void>;
219
+ /** Add close task as Microtask if is caled in the frame else next rAF. */
220
+ close(): void;
215
221
  }
216
222
 
217
223
  export { EventEmitter, Scene, createComponentAdapter, createTimer, generic, getCurrentScene };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @psytask/core v1.1.1 cubxx MIT */
1
+ /** @psytask/core v1.1.3 cubxx MIT */
2
2
  const symbol = Symbol.dispose ?? /* @__PURE__ */ Symbol.for("Symbol.dispose");
3
3
  class EventEmitter {
4
4
  listeners = {};
@@ -62,13 +62,18 @@ const createTimer = (shouldStop) => {
62
62
  const timer = {
63
63
  start: (cb) => new Promise((resolve) => {
64
64
  const records = [];
65
- const record = (time) => (records.push(time), cb?.(time));
66
- currentFrameTime && record(currentFrameTime);
65
+ currentFrameTime && (records.push(currentFrameTime), cb?.(currentFrameTime));
67
66
  const frame = (time) => {
68
- shouldStop(time, records) ? timer.stop() : (handle = rAF(frame), record(time));
67
+ const is_stoped = shouldStop(time, records);
68
+ records.push(time);
69
+ is_stoped ? timer.stop() : (cb?.(time), handle = rAF(frame));
69
70
  };
70
71
  let handle = rAF(frame);
71
- timer.stop = () => (cancelAnimationFrame(handle), resolve(records));
72
+ timer.stop = () => {
73
+ records.pop();
74
+ cancelAnimationFrame(handle);
75
+ resolve(records);
76
+ };
72
77
  }),
73
78
  stop() {
74
79
  }
@@ -133,10 +138,10 @@ class Scene extends EventEmitter {
133
138
  */
134
139
  //@ts-expect-error impl generic component
135
140
  show = this.#show;
136
- /** Add a microtask to close the scene. It is useful when close in 'show' */
137
- async close() {
138
- await 0;
139
- this.#timer.stop();
141
+ /** Add close task as Microtask if is caled in the frame else next rAF. */
142
+ close() {
143
+ const t = this.#timer;
144
+ (currentFrameTime ? queueMicrotask : rAF)(() => t.stop());
140
145
  }
141
146
  async #show(patchProps) {
142
147
  const { root, defaultProps } = this.options;
@@ -150,7 +155,8 @@ class Scene extends EventEmitter {
150
155
  root.style.scale = "0";
151
156
  return {
152
157
  ...this.emit("close").data?.(),
153
- frame_times: records
158
+ frame_times: records,
159
+ duration: currentFrameTime - records[0]
154
160
  };
155
161
  }
156
162
  }
package/dist/index.min.js CHANGED
@@ -1 +1 @@
1
- const f=Symbol.dispose??Symbol.for("Symbol.dispose");class m{listeners={};[f](){this.emit("dispose")}on(t,s){return(this.listeners[t]??=new Set).add(s),this}off(t,s){const e=this.listeners[t];return e&&(e.delete(s),e.size===0&&delete this.listeners[t]),this}once(t,s){const e=r=>{try{s(r)}finally{this.off(t,e)}};return this.on(t,e),this}emit(t,...[s]){const e=this.listeners[t];if(e)for(const r of[...e])r(s);return this}}const y=o=>{throw Error(o)},h=requestAnimationFrame,w=Object,b=(o,t)=>w.assign(o,t),S=document;new Proxy({},{get:(o,t)=>s=>b(S.createElement(t),s)});const g=o=>Array.isArray(o)?o:[o];let d;const p=o=>(d=o,setTimeout(()=>d=0),h(p)),A=o=>{d??p(0);const t={start:s=>new Promise(e=>{const r=[],n=c=>(r.push(c),s?.(c));d&&n(d);const i=c=>{o(c,r)?t.stop():(a=h(i),n(c))};let a=h(i);t.stop=()=>(cancelAnimationFrame(a),e(r))}),stop(){}};return t},E=o=>o,F=o=>({wrap:t=>(s=>t(o(s))),render:(t,s,e)=>{const r=o(s);e&&l.push(e);const n=t(r);e&&l.pop();let i;const a=g(typeof n!="string"&&"node"in n?(i=n.data,n.node):n);return{props:r,nodes:a,data:i}}}),l=[],P=()=>l[l.length-1]??y("Not found current scene");class O extends m{constructor(t,s){super(),this.options=s;const{root:e,adapter:r,defaultProps:n,timer:i}=s;(this.root=e).tabIndex=-1,e.style.scale="0";const{props:a,nodes:c,data:u}=r.render(t,{...n},this.on("dispose",()=>e.remove()));this.#t=i(),this.#s=a,this.data=u,e.append(...c)}root;#t;#s;data;show=this.#e;async close(){await 0,this.#t.stop()}async#e(t){const{root:s,defaultProps:e}=this.options,r={...e,...t};for(const i of Object.keys({...this.#s,...r}))this.#s[i]=r[i];this.emit("show"),s.style.scale="1",s.focus();const n=await this.#t.start(i=>this.emit("frame",i));return s.style.scale="0",{...this.emit("close").data?.(),frame_times:n}}}export{m as EventEmitter,O as Scene,F as createComponentAdapter,A as createTimer,E as generic,P as getCurrentScene};
1
+ const f=Symbol.dispose??Symbol.for("Symbol.dispose");class p{listeners={};[f](){this.emit("dispose")}on(t,s){return(this.listeners[t]??=new Set).add(s),this}off(t,s){const e=this.listeners[t];return e&&(e.delete(s),e.size===0&&delete this.listeners[t]),this}once(t,s){const e=r=>{try{s(r)}finally{this.off(t,e)}};return this.on(t,e),this}emit(t,...[s]){const e=this.listeners[t];if(e)for(const r of[...e])r(s);return this}}const y=o=>{throw Error(o)},d=requestAnimationFrame,w=Object,b=(o,t)=>w.assign(o,t),S=document;new Proxy({},{get:(o,t)=>s=>b(S.createElement(t),s)});const g=o=>Array.isArray(o)?o:[o];let c;const m=o=>(c=o,setTimeout(()=>c=0),d(m)),A=o=>{c??m(0);const t={start:s=>new Promise(e=>{const r=[];c&&(r.push(c),s?.(c));const n=a=>{const h=o(a,r);r.push(a),h?t.stop():(s?.(a),i=d(n))};let i=d(n);t.stop=()=>{r.pop(),cancelAnimationFrame(i),e(r)}}),stop(){}};return t},E=o=>o,F=o=>({wrap:t=>(s=>t(o(s))),render:(t,s,e)=>{const r=o(s);e&&l.push(e);const n=t(r);e&&l.pop();let i;const a=g(typeof n!="string"&&"node"in n?(i=n.data,n.node):n);return{props:r,nodes:a,data:i}}}),l=[],P=()=>l[l.length-1]??y("Not found current scene");class k extends p{constructor(t,s){super(),this.options=s;const{root:e,adapter:r,defaultProps:n,timer:i}=s;(this.root=e).tabIndex=-1,e.style.scale="0";const{props:a,nodes:h,data:u}=r.render(t,{...n},this.on("dispose",()=>e.remove()));this.#t=i(),this.#s=a,this.data=u,e.append(...h)}root;#t;#s;data;show=this.#e;close(){const t=this.#t;(c?queueMicrotask:d)(()=>t.stop())}async#e(t){const{root:s,defaultProps:e}=this.options,r={...e,...t};for(const i of Object.keys({...this.#s,...r}))this.#s[i]=r[i];this.emit("show"),s.style.scale="1",s.focus();const n=await this.#t.start(i=>this.emit("frame",i));return s.style.scale="0",{...this.emit("close").data?.(),frame_times:n,duration:c-n[0]}}}export{p as EventEmitter,k as Scene,F as createComponentAdapter,A as createTimer,E as generic,P as getCurrentScene};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@psytask/core",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "type": "module",
5
5
  "author": "cubxx",
6
6
  "license": "MIT",