watch-state 3.4.4 → 3.4.5
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/index.es6.js +1 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/utils/index.d.ts +1 -0
- package/utils/index.es6.js +1 -0
- package/utils/index.js +2 -0
- package/utils/unwatch/index.d.ts +1 -0
- package/utils/unwatch/index.es6.js +1 -0
- package/utils/unwatch/index.js +9 -0
- package/utils/unwatch/unwatch.d.ts +15 -0
- package/utils/unwatch/unwatch.es6.js +25 -0
- package/utils/unwatch/unwatch.js +29 -0
- package/watch-state.min.js +1 -1
package/index.es6.js
CHANGED
|
@@ -4,4 +4,5 @@ export { Cache } from './Cache/Cache.es6.js';
|
|
|
4
4
|
export { Event, globalEvent } from './Event/Event.es6.js';
|
|
5
5
|
export { onDestroy } from './utils/onDestroy/onDestroy.es6.js';
|
|
6
6
|
export { createEvent } from './utils/createEvent/createEvent.es6.js';
|
|
7
|
+
export { unwatch } from './utils/unwatch/unwatch.es6.js';
|
|
7
8
|
export { scope } from './constants.es6.js';
|
package/index.js
CHANGED
|
@@ -8,6 +8,7 @@ var Cache = require('./Cache/Cache.js');
|
|
|
8
8
|
var Event = require('./Event/Event.js');
|
|
9
9
|
var onDestroy = require('./utils/onDestroy/onDestroy.js');
|
|
10
10
|
var createEvent = require('./utils/createEvent/createEvent.js');
|
|
11
|
+
var unwatch = require('./utils/unwatch/unwatch.js');
|
|
11
12
|
var constants = require('./constants.js');
|
|
12
13
|
|
|
13
14
|
|
|
@@ -19,4 +20,5 @@ exports.Event = Event.Event;
|
|
|
19
20
|
exports.globalEvent = Event.globalEvent;
|
|
20
21
|
exports.onDestroy = onDestroy.onDestroy;
|
|
21
22
|
exports.createEvent = createEvent.createEvent;
|
|
23
|
+
exports.unwatch = unwatch.unwatch;
|
|
22
24
|
exports.scope = constants.scope;
|
package/package.json
CHANGED
package/utils/index.d.ts
CHANGED
package/utils/index.es6.js
CHANGED
package/utils/index.js
CHANGED
|
@@ -4,8 +4,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var onDestroy = require('./onDestroy/onDestroy.js');
|
|
6
6
|
var createEvent = require('./createEvent/createEvent.js');
|
|
7
|
+
var unwatch = require('./unwatch/unwatch.js');
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
exports.onDestroy = onDestroy.onDestroy;
|
|
11
12
|
exports.createEvent = createEvent.createEvent;
|
|
13
|
+
exports.unwatch = unwatch.unwatch;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './unwatch';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { unwatch } from './unwatch.es6.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* You can stop watching a piece of code
|
|
3
|
+
* ```typescript
|
|
4
|
+
* import { State, Watch, unwatch } from '../..'
|
|
5
|
+
*
|
|
6
|
+
* const count = new State(0)
|
|
7
|
+
*
|
|
8
|
+
* new Watch(() => {
|
|
9
|
+
* console.log(unwatch(() => count.value++))
|
|
10
|
+
* })
|
|
11
|
+
*
|
|
12
|
+
* count.value++
|
|
13
|
+
* ```
|
|
14
|
+
* */
|
|
15
|
+
export declare function unwatch<T>(fn: () => T): T;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { scope } from '../../constants.es6.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* You can stop watching a piece of code
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { State, Watch, unwatch } from '../..'
|
|
7
|
+
*
|
|
8
|
+
* const count = new State(0)
|
|
9
|
+
*
|
|
10
|
+
* new Watch(() => {
|
|
11
|
+
* console.log(unwatch(() => count.value++))
|
|
12
|
+
* })
|
|
13
|
+
*
|
|
14
|
+
* count.value++
|
|
15
|
+
* ```
|
|
16
|
+
* */
|
|
17
|
+
function unwatch(fn) {
|
|
18
|
+
const { activeWatcher } = scope;
|
|
19
|
+
scope.activeWatcher = undefined;
|
|
20
|
+
const result = fn();
|
|
21
|
+
scope.activeWatcher = activeWatcher;
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { unwatch };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var constants = require('../../constants.js');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* You can stop watching a piece of code
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { State, Watch, unwatch } from '../..'
|
|
11
|
+
*
|
|
12
|
+
* const count = new State(0)
|
|
13
|
+
*
|
|
14
|
+
* new Watch(() => {
|
|
15
|
+
* console.log(unwatch(() => count.value++))
|
|
16
|
+
* })
|
|
17
|
+
*
|
|
18
|
+
* count.value++
|
|
19
|
+
* ```
|
|
20
|
+
* */
|
|
21
|
+
function unwatch(fn) {
|
|
22
|
+
const { activeWatcher } = constants.scope;
|
|
23
|
+
constants.scope.activeWatcher = undefined;
|
|
24
|
+
const result = fn();
|
|
25
|
+
constants.scope.activeWatcher = activeWatcher;
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.unwatch = unwatch;
|
package/watch-state.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var watchState=function(t){"use strict";const e={activeWatcher:void 0,activeEvent:void 0,activeEventDeep:0};function s(t){e.activeWatcher&&e.activeWatcher.onClear(t)}class a{add(t){let{watchers:e}=this;if(e){if(e.has(t))return;e.add(t)}else e=this.watchers=new Set([t]);t.onClear((()=>e.delete(t)))}start(){e.activeEvent||(this.activeWatcher=e.activeWatcher,e.activeWatcher=void 0,e.activeEvent=this),e.activeEventDeep++}end(){--e.activeEventDeep||e.activeEvent!==this||(e.activeEvent=void 0,this.update(),e.activeWatcher=this.activeWatcher)}forceUpdate(){const{watchers:t}=this;this.watchers=void 0;for(const e of t)e.update()}update(){var t;(null===(t=this.watchers)||void 0===t?void 0:t.size)&&(this===i?this.forceUpdate():(i.start(),this.forceUpdate(),i.end()))}}const i=new a;class r{constructor(t,e,a){this.watcher=t,this.ran=!1,e||s((()=>this.destroy())),a||this.watchRun()}run(){const{ran:t}=this;return this.ran=!0,this.watcher(t)}watchRun(){const t=e.activeWatcher;e.activeWatcher=this,this.run(),e.activeWatcher=t}forceUpdate(){this.destroy(),this.watchRun()}update(){this.destroy(),e.activeEvent?e.activeEvent.add(this):this.watchRun()}destroy(){const{destructors:t}=this;t&&(this.destructors=void 0,t.forEach((t=>t())))}onClear(t){return this.destructors?this.destructors.push(t):this.destructors=[t],this}onDestroy(t){return this.onClear(t)}}class
|
|
1
|
+
var watchState=function(t){"use strict";const e={activeWatcher:void 0,activeEvent:void 0,activeEventDeep:0};function s(t){e.activeWatcher&&e.activeWatcher.onClear(t)}class a{add(t){let{watchers:e}=this;if(e){if(e.has(t))return;e.add(t)}else e=this.watchers=new Set([t]);t.onClear((()=>e.delete(t)))}start(){e.activeEvent||(this.activeWatcher=e.activeWatcher,e.activeWatcher=void 0,e.activeEvent=this),e.activeEventDeep++}end(){--e.activeEventDeep||e.activeEvent!==this||(e.activeEvent=void 0,this.update(),e.activeWatcher=this.activeWatcher)}forceUpdate(){const{watchers:t}=this;this.watchers=void 0;for(const e of t)e.update()}update(){var t;(null===(t=this.watchers)||void 0===t?void 0:t.size)&&(this===i?this.forceUpdate():(i.start(),this.forceUpdate(),i.end()))}}const i=new a;class r{constructor(t,e,a){this.watcher=t,this.ran=!1,e||s((()=>this.destroy())),a||this.watchRun()}run(){const{ran:t}=this;return this.ran=!0,this.watcher(t)}watchRun(){const t=e.activeWatcher;e.activeWatcher=this,this.run(),e.activeWatcher=t}forceUpdate(){this.destroy(),this.watchRun()}update(){this.destroy(),e.activeEvent?e.activeEvent.add(this):this.watchRun()}destroy(){const{destructors:t}=this;t&&(this.destructors=void 0,t.forEach((t=>t())))}onClear(t){return this.destructors?this.destructors.push(t):this.destructors=[t],this}onDestroy(t){return this.onClear(t)}}class c extends a{constructor(t){super(),this.state=t}get value(){return e.activeWatcher&&this.add(e.activeWatcher),this.state}set value(t){t!==this.state&&(this.state=t,this.update())}}class h extends r{constructor(t,e,s){super(t,e,!s)}destroy(){return super.destroy()}run(){this.updated=!0,this.value=super.run()}get hasWatcher(){if(this.updated&&this.size)for(const t of this._state.watchers)if(!(t instanceof h)||t.hasWatcher)return!0}get size(){var t,e;return null===(e=null===(t=this._state)||void 0===t?void 0:t.watchers)||void 0===e?void 0:e.size}deepUpdate(){if(this.updated=!1,this.destroy(),this.size)for(const t of this._state.watchers)t.deepUpdate()}update(){this.updated&&(this.hasWatcher?this.forceUpdate():this.deepUpdate())}get state(){return this._state||(this._state=new c)}get value(){return this.updated||this.forceUpdate(),this.state.value}set value(t){this.state.value=t}}return t.Cache=h,t.Event=a,t.State=c,t.Watch=r,t.createEvent=function(t){return function(){i.start();const e=t.apply(this,arguments);return i.end(),e}},t.globalEvent=i,t.onDestroy=s,t.scope=e,t.unwatch=function(t){const{activeWatcher:s}=e;e.activeWatcher=void 0;const a=t();return e.activeWatcher=s,a},Object.defineProperty(t,"__esModule",{value:!0}),t}({});
|