11import React from 'react' ;
22
3- import type { Splitter , TypedLines , TypistProps } from './types/TypistProps' ;
4- import { emptyFunc } from './utils/defaultFuncs' ;
3+ import type { Splitter , TypedLines , TypistProps , Delay } from './types/TypistProps' ;
4+ import { defaultSplitter , emptyFunc } from './utils/defaultFuncs' ;
55import getActions from './utils/getActions' ;
66
7- type T = Omit < TypistProps , 'cursor' | 'disabled' | 'restartKey' > ;
8- type CoreProps = Required < T > ;
7+ type CoreProps = Omit < TypistProps , 'cursor' | 'disabled' | 'restartKey' > ;
98type SetTypedLines = React . Dispatch < React . SetStateAction < TypedLines > > ;
109
1110export default class TypistCore {
1211 #children: React . ReactNode ;
13- #typingDelay! : number | ( ( ) => number ) ;
14- #backspaceDelay! : number | ( ( ) => number ) ;
12+ #typingDelay! : Delay ;
13+ #backspaceDelay! : Delay ;
1514 #loop! : boolean ;
1615 #pause! : boolean ;
1716 #startDelay! : number ;
@@ -24,7 +23,7 @@ export default class TypistCore {
2423 #setTypedLines: SetTypedLines ;
2524
2625 constructor ( props : CoreProps , setTypedLines : SetTypedLines ) {
27- this . setUpProps ( props ) ;
26+ this . updateProps ( props ) ;
2827 this . #setTypedLines = setTypedLines ;
2928 }
3029
@@ -36,16 +35,16 @@ export default class TypistCore {
3635 } ;
3736 } ;
3837
39- setUpProps = ( {
38+ updateProps = ( {
4039 children,
41- typingDelay,
42- backspaceDelay,
43- loop,
44- pause,
45- startDelay,
46- finishDelay,
47- onTypingDone,
48- splitter,
40+ typingDelay = 75 ,
41+ backspaceDelay = 75 ,
42+ loop = false ,
43+ pause = false ,
44+ startDelay = 0 ,
45+ finishDelay = 0 ,
46+ onTypingDone = emptyFunc ,
47+ splitter = defaultSplitter ,
4948 } : CoreProps ) => {
5049 this . #children = children ;
5150 this . #typingDelay = typingDelay ;
@@ -82,9 +81,10 @@ export default class TypistCore {
8281 }
8382 } ;
8483
85- #timeoutPromise = ( delay : number ) => {
84+ #timeoutPromise = ( delay : Delay ) => {
8685 return new Promise < void > ( ( resolve , reject ) => {
87- const timeoutId = setTimeout ( resolve , delay ) ;
86+ const ms = typeof delay === 'number' ? delay : delay ( ) ;
87+ const timeoutId = setTimeout ( resolve , ms ) ;
8888 this . #clearTimer = ( ) => {
8989 clearTimeout ( timeoutId ) ;
9090 reject ( ) ;
@@ -123,14 +123,13 @@ export default class TypistCore {
123123 } ;
124124
125125 /**
126- * Each async token should be executed after the pause promise resolve.
126+ * Each token should be executed after the pause promise resolve.
127127 * @param callback
128- * @param timeoutDelay
128+ * @param delay
129129 */
130- #executeAsyncToken = async ( callback : ( ) => void , timeoutDelay : number | ( ( ) => number ) ) => {
130+ #executeToken = async ( callback : ( ) => void , delay : Delay ) => {
131131 await this . #pausePromise( ) ;
132132 callback ( ) ;
133- const delay = typeof timeoutDelay === 'number' ? timeoutDelay : timeoutDelay ( ) ;
134133 await this . #timeoutPromise( delay ) ;
135134 } ;
136135
@@ -147,7 +146,7 @@ export default class TypistCore {
147146 const splittedLine = this . #splitter( line ) ;
148147 const lastIdx = this . #typedLines. length ;
149148 for ( let charIdx = 1 ; charIdx <= splittedLine . length ; charIdx ++ ) {
150- await this . #executeAsyncToken ( ( ) => {
149+ await this . #executeToken ( ( ) => {
151150 const newLine = splittedLine . slice ( 0 , charIdx ) . join ( '' ) ;
152151 const newTypedLines = [ ...this . #typedLines] ;
153152 newTypedLines [ lastIdx ] = newLine ;
@@ -157,14 +156,14 @@ export default class TypistCore {
157156 } ;
158157
159158 #typeElement = async ( el : React . ReactElement ) => {
160- await this . #executeAsyncToken ( ( ) => {
159+ await this . #executeToken ( ( ) => {
161160 this . #updateTypedLines( [ ...this . #typedLines, el ] ) ;
162161 } , this . #typingDelay) ;
163162 } ;
164163
165164 #backspace = async ( amount : number ) => {
166165 while ( amount -- ) {
167- await this . #executeAsyncToken ( ( ) => {
166+ await this . #executeToken ( ( ) => {
168167 const typedLines = [ ...this . #typedLines] ;
169168 let lineIndex = typedLines . length - 1 ;
170169 let line = typedLines [ lineIndex ] ;
0 commit comments