LoadingReactiveVar.js
2 years ago
ReactiveHook.js
2 years ago
ReactiveSet.js
2 years ago
ReactiveVar.js
2 years ago
ReactiveVar.js
115 lines
| 1 | import { isEmpty } from '../functions'; |
| 2 | |
| 3 | function ReactiveVar( value = null ) { |
| 4 | this.current = value; |
| 5 | this.signals = []; |
| 6 | this.sanitizers = []; |
| 7 | this.isDebug = false; |
| 8 | this.isSilence = false; |
| 9 | this.isMaked = false; |
| 10 | } |
| 11 | |
| 12 | ReactiveVar.prototype = { |
| 13 | watchOnce: function ( callable ) { |
| 14 | if ( 'function' !== typeof callable ) { |
| 15 | return; |
| 16 | } |
| 17 | const clearWatcher = this.watch( () => { |
| 18 | clearWatcher(); |
| 19 | callable(); |
| 20 | } ); |
| 21 | }, |
| 22 | watch: function ( callable ) { |
| 23 | if ( 'function' !== typeof callable ) { |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | if ( this.signals.some( ( { signal } ) => signal === callable ) ) { |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | this.signals.push( { |
| 32 | signal: callable, |
| 33 | trace: new Error().stack, |
| 34 | } ); |
| 35 | |
| 36 | const index = this.signals.length - 1; |
| 37 | |
| 38 | return () => this.signals.splice( index, 1 ); |
| 39 | }, |
| 40 | sanitize: function ( callable ) { |
| 41 | if ( 'function' !== typeof callable ) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | if ( -1 !== this.sanitizers.indexOf( callable ) ) { |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | this.sanitizers.push( callable ); |
| 50 | |
| 51 | const index = this.sanitizers.length - 1; |
| 52 | |
| 53 | return () => this.sanitizers.splice( index, 1 ); |
| 54 | }, |
| 55 | make: function () { |
| 56 | if ( this.isMaked ) { |
| 57 | return; |
| 58 | } |
| 59 | this.isMaked = true; |
| 60 | let current = this.current; |
| 61 | let prevValue = null; |
| 62 | const self = this; |
| 63 | |
| 64 | Object.defineProperty( this, 'current', { |
| 65 | get() { |
| 66 | return current; |
| 67 | }, |
| 68 | set( newVal ) { |
| 69 | if ( current === newVal ) { |
| 70 | return; |
| 71 | } |
| 72 | prevValue = current; |
| 73 | if ( self.isDebug ) { |
| 74 | console.group( 'ReactiveVar has changed' ); |
| 75 | console.log( 'current:', current ); |
| 76 | console.log( 'newVal:', newVal ); |
| 77 | console.groupEnd(); |
| 78 | debugger; |
| 79 | } |
| 80 | current = self.applySanitizers( newVal ); |
| 81 | |
| 82 | if ( self.isSilence ) { |
| 83 | return; |
| 84 | } |
| 85 | self.notify( prevValue ); |
| 86 | }, |
| 87 | } ); |
| 88 | }, |
| 89 | notify: function ( prevValue = null ) { |
| 90 | this.signals.forEach( |
| 91 | ( { signal } ) => signal.call( this, prevValue ) ); |
| 92 | }, |
| 93 | applySanitizers: function ( value ) { |
| 94 | for ( const sanitizer of this.sanitizers ) { |
| 95 | value = sanitizer.call( this, value ); |
| 96 | } |
| 97 | |
| 98 | return value; |
| 99 | }, |
| 100 | setIfEmpty( newValue ) { |
| 101 | if ( !isEmpty( this.current ) ) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | this.current = newValue; |
| 106 | }, |
| 107 | debug() { |
| 108 | this.isDebug = !this.isDebug; |
| 109 | }, |
| 110 | silence() { |
| 111 | this.isSilence = !this.isSilence; |
| 112 | }, |
| 113 | }; |
| 114 | |
| 115 | export default ReactiveVar; |