├── .gitignore ├── .npmignore ├── License.md ├── README.md ├── index.d.ts ├── index.js ├── package-lock.json ├── package.json └── src ├── simple-timer.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .vscode 3 | lib 4 | node_modules 5 | npm-debug.log 6 | typings 7 | package-lock.json 8 | ng2-simple-timer-*.tgz -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .git 3 | .gitignore 4 | .vscode 5 | examples 6 | node_modules 7 | npm-debug.log 8 | src 9 | tsconfig.json 10 | typings 11 | typings.json 12 | package-lock.json 13 | ng2-simple-timer-*.tgz -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2018 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Simple Timer [![Paypal donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/donate/?business=HZF49NM9D35SJ&no_recurring=0¤cy_code=CAD) 2 | 3 | A simple timer service for Angular, base on RxJS. 4 | 5 | Name/ID(string) base API. RxJS object not exposed. 6 | 7 | > To enable faster update, ng2-simple-timer switched to Angular CLI starting 8.2.0 and use new repository https://github.com/J-Siu/ng2-simple-timer-lib/ 8 | > 9 | > Project contains both library and example. 10 | > 11 | > All version < 8.2.0 are in old repository https://github.com/J-Siu/ng2-simple-timer/ 12 | 13 | ### Table Of Content 14 | 15 | 16 | - [Version](#version) 17 | - [Install](#install) 18 | - [Usage](#usage) 19 | - ["noImplicitAny": false](#noimplicitany-false) 20 | - [Import into Angular2 RC5+ application typescript](#import-into-angular2-rc5-application-typescript) 21 | - [API](#api) 22 | - [newTimer](#newtimer) 23 | - [delTimer](#deltimer) 24 | - [getTimer](#gettimer) 25 | - [getSubscription](#getsubscription) 26 | - [subscribe](#subscribe) 27 | - [unsubscribe](#unsubscribe) 28 | - [Example](#example) 29 | - [Contributors](#contributors) 30 | - [Changelog](#changelog) 31 | - [License](#license) 32 | 33 | 34 | 35 | ### Version 36 | 37 | - For Angular 2.x.x, please use 1.3.1. 38 | - For Angular 4.x.x, please use 1.3.5. 39 | - For Angular 6.x.x, please use 6.0.0. 40 | 41 | ### Install 42 | 43 | ``` 44 | npm install ng2-simple-timer 45 | ``` 46 | 47 | ### Usage 48 | 49 | #### "noImplicitAny": false 50 | 51 | Must set `"noImplicitAny": false` in application __tsconfig.json__. Else following error may occur at build time: 52 | 53 | error TS7006: Parameter 'any' implicitly has an 'any' type 54 | 55 | #### Import into Angular2 RC5+ application (typescript) 56 | 57 | `ng2-simple-timer` is implemented as Angular injectable service name __SimpleTimer__. 58 | 59 | __For module using SimpleTimer__ 60 | 61 | Add `SimpleTimer` into module providers (eg. [app.module.ts](https://github.com/J-Siu/ng2-simple-timer-example/blob/master/app/app.module.ts)). 62 | 63 | ```javascript 64 | import { SimpleTimer } from 'ng2-simple-timer'; 65 | 66 | @NgModule({ 67 | providers: [SimpleTimer] 68 | }) 69 | ``` 70 | 71 | __For each child component using SimpleTimer__ 72 | 73 | ```javascript 74 | import {SimpleTimer} from 'ng2-simple-timer'; 75 | 76 | export class ChildComponent { 77 | 78 | constructor(private st: SimpleTimer) { } 79 | 80 | } 81 | ``` 82 | 83 | #### API 84 | 85 | ###### newTimer 86 | 87 | `newTimer(name: string, sec: number, delay: boolean = false): boolean` 88 | 89 | `newTimer` will create timer `name` and tick every 'number' of seconds. Creating timer with the same name multiple times has no side effect. 90 | 91 | `delay`: If set to true will delay the 1st tick till the end of the first interval. 92 | 93 | Return `false` if timer `name` exist. 94 | 95 | ```javascript 96 | this.st.newTimer('5sec', 5); 97 | this.st.newTimer('5sec', 5, true); 98 | ``` 99 | 100 | ###### delTimer 101 | 102 | `delTimer(name: string): boolean` 103 | 104 | `delTimer` will delete timer `name` 105 | 106 | Return `false` if timer `name` does not exist. 107 | 108 | ```javascript 109 | this.st.delTimer('5sec'); 110 | ``` 111 | 112 | ###### getTimer 113 | 114 | `getTimer(): string[]` 115 | 116 | `getTimer` will return all timer name in string array. 117 | ```javascript 118 | let t: string[] = this.st.getTimer(); 119 | ``` 120 | 121 | ###### getSubscription 122 | 123 | `getSubscription(): string[]` 124 | 125 | `getSubscription` will return all subscription id in string array. 126 | ```javascript 127 | let ids: string[] = this.st.getSubscription(); 128 | ``` 129 | 130 | ###### subscribe 131 | 132 | `subscribe(name: string, callback: () => void): string` 133 | 134 | `subscribe` will link `callback` function to timer `name`. Whenever timer `name` tick, `callback` will be invoked. 135 | 136 | Return subscription id(string). 137 | 138 | Return empty string if timer `name` does not exist. 139 | 140 | Either use Lambda(fat arrow) in typescript to pass in callback or bind `this` to another variable in javascript, else `this` scope will be lost. 141 | 142 | __Lambda(fat arrow)__ 143 | ```javascript 144 | counter: number = 0; 145 | timerId: string; 146 | 147 | ngOnInit() { 148 | // lazy mode 149 | this.timerId = this.st.subscribe('5sec', () => this.callback()); 150 | } 151 | 152 | callback() { 153 | this.counter++; 154 | } 155 | ``` 156 | 157 | ###### unsubscribe 158 | 159 | `unsubscribe(id: string): boolean` 160 | 161 | `unsubscribe` will cancel subscription using `id`. 162 | 163 | `unsubscribe` will return false if `id` is undefined or `id` is not found in subscription list. 164 | 165 | ```javascript 166 | timerId: string; 167 | 168 | this.st.unsubscribe(this.timerId); 169 | ``` 170 | 171 | ### Example 172 | 173 | GitHub: [ng2-simple-timer-example](https://github.com/J-Siu/ng2-simple-timer-example) 174 | Plunker: [Angular2 Simple Timer Example](http://embed.plnkr.co/HaTd8q/) 175 | 176 | ### Contributors 177 | 178 | * [John Sing Dao Siu](https://github.com/J-Siu) 179 | 180 | 181 | ### Changelog 182 | 183 | * 0.2.0 184 | - Angular 2 RC4 185 | * 0.2.2 186 | - API change 187 | - newTimer() return boolean 188 | - subscribe() - lazy mode removed 189 | - API new 190 | - delTimer() 191 | * 0.2.3 192 | - Support Angular 2 RC5 193 | * 0.2.4 194 | - Remove module, export `SimpleTimer` only 195 | * 1.2.4 196 | - Support Angular 2.0.0 197 | - Clean up package 198 | * 1.2.5 199 | - Add Plunker example 200 | * 1.2.7 201 | - Support Angular 2.4.* 202 | - Replace node-uuid with uuid 203 | * 1.2.8 204 | - Change uuid as dependency 205 | * 1.2.9 206 | - Replace uuid with angular2-uuid 207 | * 1.3.0 208 | - Add instruction for `"noImplicitAny": false` 209 | - Clean up package 210 | * 1.3.1 211 | - Due to the rapid release cycle of Angular, to minimize update purely due to `peerDependencies`, it is modified as follow: 212 | - `"peerDependencies": { "@angular/core": ">=2.4.0" }` 213 | * 1.3.2 214 | - Update package.json for Angular 4.3.1. For Angular 2.x.x, please use 1.3.1 or earlier. 215 | - Fix readme example code syntax error. 216 | - API change: 217 | - `subscribe(name: string, callback: (any) => void): string` change to `subscribe(name: string, callback: () => void): string` 218 | * 1.3.3 219 | - Fix readme example code for `subscribe` 220 | * 1.3.4 221 | - Add `delay` option for `newTimer` 222 | * 1.3.5 223 | - Bugfix for `newTimer` 224 | * 6.0.0 225 | - Update for Angular 6.x, which include moving from RxJS 5.1 to 6.0 226 | - Update version to match major version of Angular 227 | 228 | ### License 229 | 230 | The MIT License 231 | 232 | Copyright (c) 2018 233 | 234 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 235 | 236 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 237 | 238 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 239 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export * from './lib/simple-timer'; 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | exports.SimpleTimer = require('./lib/simple-timer').SimpleTimer; 2 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ng2-simple-timer", 3 | "version": "6.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@angular/core": { 8 | "version": "6.1.0", 9 | "resolved": "https://registry.npmjs.org/@angular/core/-/core-6.1.0.tgz", 10 | "integrity": "sha512-gWu9Q7q2+fhFC5dl/BvGW7Ha7NUJtK9wQLYQlfIMim4lKTOiM1/S0MYBVMrEq58ldMr9DnA35f5jGno3x6/v+g==", 11 | "dev": true, 12 | "requires": { 13 | "tslib": "^1.9.0" 14 | } 15 | }, 16 | "@types/node": { 17 | "version": "8.9.5", 18 | "resolved": "https://registry.npmjs.org/@types/node/-/node-8.9.5.tgz", 19 | "integrity": "sha512-jRHfWsvyMtXdbhnz5CVHxaBgnV6duZnPlQuRSo/dm/GnmikNcmZhxIES4E9OZjUmQ8C+HCl4KJux+cXN/ErGDQ==", 20 | "dev": true 21 | }, 22 | "@types/rx": { 23 | "version": "4.1.1", 24 | "resolved": "https://registry.npmjs.org/@types/rx/-/rx-4.1.1.tgz", 25 | "integrity": "sha1-WY/JSla67ZdfGUV04PVy/Y5iekg=", 26 | "requires": { 27 | "@types/rx-core": "*", 28 | "@types/rx-core-binding": "*", 29 | "@types/rx-lite": "*", 30 | "@types/rx-lite-aggregates": "*", 31 | "@types/rx-lite-async": "*", 32 | "@types/rx-lite-backpressure": "*", 33 | "@types/rx-lite-coincidence": "*", 34 | "@types/rx-lite-experimental": "*", 35 | "@types/rx-lite-joinpatterns": "*", 36 | "@types/rx-lite-testing": "*", 37 | "@types/rx-lite-time": "*", 38 | "@types/rx-lite-virtualtime": "*" 39 | } 40 | }, 41 | "@types/rx-core": { 42 | "version": "4.0.3", 43 | "resolved": "https://registry.npmjs.org/@types/rx-core/-/rx-core-4.0.3.tgz", 44 | "integrity": "sha1-CzNUsSOM7b4rdPYybxOdvHpZHWA=" 45 | }, 46 | "@types/rx-core-binding": { 47 | "version": "4.0.4", 48 | "resolved": "https://registry.npmjs.org/@types/rx-core-binding/-/rx-core-binding-4.0.4.tgz", 49 | "integrity": "sha512-5pkfxnC4w810LqBPUwP5bg7SFR/USwhMSaAeZQQbEHeBp57pjKXRlXmqpMrLJB4y1oglR/c2502853uN0I+DAQ==", 50 | "requires": { 51 | "@types/rx-core": "*" 52 | } 53 | }, 54 | "@types/rx-lite": { 55 | "version": "4.0.5", 56 | "resolved": "https://registry.npmjs.org/@types/rx-lite/-/rx-lite-4.0.5.tgz", 57 | "integrity": "sha512-KZk5XTR1dm/kNgBx8iVpjno6fRYtAUQWBOmj+O8j724+nk097sz4fOoHJNpCkOJUtHUurZlJC7QvSFCZHbkC+w==", 58 | "requires": { 59 | "@types/rx-core": "*", 60 | "@types/rx-core-binding": "*" 61 | } 62 | }, 63 | "@types/rx-lite-aggregates": { 64 | "version": "4.0.3", 65 | "resolved": "https://registry.npmjs.org/@types/rx-lite-aggregates/-/rx-lite-aggregates-4.0.3.tgz", 66 | "integrity": "sha512-MAGDAHy8cRatm94FDduhJF+iNS5//jrZ/PIfm+QYw9OCeDgbymFHChM8YVIvN2zArwsRftKgE33QfRWvQk4DPg==", 67 | "requires": { 68 | "@types/rx-lite": "*" 69 | } 70 | }, 71 | "@types/rx-lite-async": { 72 | "version": "4.0.2", 73 | "resolved": "https://registry.npmjs.org/@types/rx-lite-async/-/rx-lite-async-4.0.2.tgz", 74 | "integrity": "sha512-vTEv5o8l6702ZwfAM5aOeVDfUwBSDOs+ARoGmWAKQ6LOInQ8J4/zjM7ov12fuTpktUKdMQjkeCp07Vd73mPkxw==", 75 | "requires": { 76 | "@types/rx-lite": "*" 77 | } 78 | }, 79 | "@types/rx-lite-backpressure": { 80 | "version": "4.0.3", 81 | "resolved": "https://registry.npmjs.org/@types/rx-lite-backpressure/-/rx-lite-backpressure-4.0.3.tgz", 82 | "integrity": "sha512-Y6aIeQCtNban5XSAF4B8dffhIKu6aAy/TXFlScHzSxh6ivfQBQw6UjxyEJxIOt3IT49YkS+siuayM2H/Q0cmgA==", 83 | "requires": { 84 | "@types/rx-lite": "*" 85 | } 86 | }, 87 | "@types/rx-lite-coincidence": { 88 | "version": "4.0.3", 89 | "resolved": "https://registry.npmjs.org/@types/rx-lite-coincidence/-/rx-lite-coincidence-4.0.3.tgz", 90 | "integrity": "sha512-1VNJqzE9gALUyMGypDXZZXzR0Tt7LC9DdAZQ3Ou/Q0MubNU35agVUNXKGHKpNTba+fr8GdIdkC26bRDqtCQBeQ==", 91 | "requires": { 92 | "@types/rx-lite": "*" 93 | } 94 | }, 95 | "@types/rx-lite-experimental": { 96 | "version": "4.0.1", 97 | "resolved": "https://registry.npmjs.org/@types/rx-lite-experimental/-/rx-lite-experimental-4.0.1.tgz", 98 | "integrity": "sha1-xTL1y98/LBXaFt7Ykw0bKYQCPL0=", 99 | "requires": { 100 | "@types/rx-lite": "*" 101 | } 102 | }, 103 | "@types/rx-lite-joinpatterns": { 104 | "version": "4.0.1", 105 | "resolved": "https://registry.npmjs.org/@types/rx-lite-joinpatterns/-/rx-lite-joinpatterns-4.0.1.tgz", 106 | "integrity": "sha1-9w/jcFGKhDLykVjMkv+1a05K/D4=", 107 | "requires": { 108 | "@types/rx-lite": "*" 109 | } 110 | }, 111 | "@types/rx-lite-testing": { 112 | "version": "4.0.1", 113 | "resolved": "https://registry.npmjs.org/@types/rx-lite-testing/-/rx-lite-testing-4.0.1.tgz", 114 | "integrity": "sha1-IbGdEfTf1v/vWp0WSOnIh5v+Iek=", 115 | "requires": { 116 | "@types/rx-lite-virtualtime": "*" 117 | } 118 | }, 119 | "@types/rx-lite-time": { 120 | "version": "4.0.3", 121 | "resolved": "https://registry.npmjs.org/@types/rx-lite-time/-/rx-lite-time-4.0.3.tgz", 122 | "integrity": "sha512-ukO5sPKDRwCGWRZRqPlaAU0SKVxmWwSjiOrLhoQDoWxZWg6vyB9XLEZViKOzIO6LnTIQBlk4UylYV0rnhJLxQw==", 123 | "requires": { 124 | "@types/rx-lite": "*" 125 | } 126 | }, 127 | "@types/rx-lite-virtualtime": { 128 | "version": "4.0.3", 129 | "resolved": "https://registry.npmjs.org/@types/rx-lite-virtualtime/-/rx-lite-virtualtime-4.0.3.tgz", 130 | "integrity": "sha512-3uC6sGmjpOKatZSVHI2xB1+dedgml669ZRvqxy+WqmGJDVusOdyxcKfyzjW0P3/GrCiN4nmRkLVMhPwHCc5QLg==", 131 | "requires": { 132 | "@types/rx-lite": "*" 133 | } 134 | }, 135 | "angular2-uuid": { 136 | "version": "1.1.1", 137 | "resolved": "https://registry.npmjs.org/angular2-uuid/-/angular2-uuid-1.1.1.tgz", 138 | "integrity": "sha1-cvA81TK39AAy6x7PufhFc4S+lW4=" 139 | }, 140 | "core-js": { 141 | "version": "2.5.7", 142 | "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.5.7.tgz", 143 | "integrity": "sha512-RszJCAxg/PP6uzXVXL6BsxSXx/B05oJAQ2vkJRjyjrEcNVycaqOmNb5OTxZPE3xa5gwZduqza6L9JOCenh/Ecw==", 144 | "dev": true 145 | }, 146 | "rxjs": { 147 | "version": "6.2.2", 148 | "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.2.2.tgz", 149 | "integrity": "sha512-0MI8+mkKAXZUF9vMrEoPnaoHkfzBPP4IGwUYRJhIRJF6/w3uByO1e91bEHn8zd43RdkTMKiooYKmwz7RH6zfOQ==", 150 | "dev": true, 151 | "requires": { 152 | "tslib": "^1.9.0" 153 | } 154 | }, 155 | "tslib": { 156 | "version": "1.9.0", 157 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.0.tgz", 158 | "integrity": "sha512-f/qGG2tUkrISBlQZEjEqoZ3B2+npJjIf04H1wuAv9iA8i04Icp+61KRXxFdha22670NJopsZCIjhC3SnjPRKrQ==", 159 | "dev": true 160 | }, 161 | "typescript": { 162 | "version": "2.7.2", 163 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.7.2.tgz", 164 | "integrity": "sha512-p5TCYZDAO0m4G344hD+wx/LATebLWZNkkh2asWUFqSsD2OrDNhbAHuSjobrmsUmdzjJjEeZVU9g1h3O6vpstnw==", 165 | "dev": true 166 | }, 167 | "zone.js": { 168 | "version": "0.8.26", 169 | "resolved": "https://registry.npmjs.org/zone.js/-/zone.js-0.8.26.tgz", 170 | "integrity": "sha512-W9Nj+UmBJG251wkCacIkETgra4QgBo/vgoEkb4a2uoLzpQG7qF9nzwoLXWU5xj3Fg2mxGvEDh47mg24vXccYjA==", 171 | "dev": true 172 | } 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": { 3 | "name": "John, Sing Dao, Siu", 4 | "url": "https://github.com/J-Siu" 5 | }, 6 | "bugs": { 7 | "url": "https://github.com/J-Siu/ng2-simple-timer/issues" 8 | }, 9 | "dependencies": { 10 | "angular2-uuid": "*" 11 | }, 12 | "description": "Angular 2 simple timer service", 13 | "devDependencies": { 14 | "@angular/core": "^6.1.0", 15 | "@types/node": "~8.9.4", 16 | "@types/rx": "^4.1.1", 17 | "core-js": "^2.5.4", 18 | "rxjs": "^6.0.0", 19 | "typescript": "~2.7.2", 20 | "zone.js": "~0.8.26" 21 | }, 22 | "keywords": [ 23 | "angular", 24 | "ng", 25 | "component", 26 | "timer", 27 | "example", 28 | "simple" 29 | ], 30 | "license": "MIT", 31 | "name": "ng2-simple-timer", 32 | "peerDependencies": { 33 | "@angular/common": "^6.0.0-rc.0 || ^6.0.0", 34 | "@angular/core": "^6.0.0-rc.0 || ^6.0.0" 35 | }, 36 | "repository": { 37 | "type": "git", 38 | "url": "git+https://github.com/J-Siu/ng2-simple-timer.git" 39 | }, 40 | "scripts": { 41 | "build": "tsc -p src/" 42 | }, 43 | "version": "6.0.0" 44 | } 45 | -------------------------------------------------------------------------------- /src/simple-timer.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { Observable, Subscription, timer } from 'rxjs'; 3 | import { UUID } from 'angular2-uuid'; 4 | 5 | interface TimerList { 6 | [name: string]: { 7 | second: number, 8 | t: Observable 9 | }; 10 | } 11 | 12 | interface SubscriptionList { 13 | [id: string]: { 14 | name: string, 15 | subscription: Subscription 16 | }; 17 | } 18 | 19 | @Injectable({ providedIn: 'root' }) 20 | export class SimpleTimer { 21 | 22 | private timers: TimerList = {}; 23 | private subscription: SubscriptionList = {}; 24 | 25 | getTimer(): string[] { 26 | return Object.keys(this.timers); 27 | } 28 | getSubscription(): string[] { 29 | return Object.keys(this.subscription); 30 | } 31 | newTimer(name: string, sec: number, delay: boolean = false): boolean { 32 | if (name === undefined || sec === undefined || this.timers[name]) { 33 | return false; 34 | } 35 | let t: Observable 36 | if (delay) { 37 | t = timer(sec * 1000, sec * 1000); 38 | } 39 | else { 40 | t = timer(0, sec * 1000); 41 | } 42 | this.timers[name] = { second: sec, t: t }; 43 | return true; 44 | } 45 | delTimer(name: string): boolean { 46 | if (name === undefined || !this.timers[name]) { 47 | return false; 48 | } 49 | let s = this.getSubscription(); 50 | // unsubscribe all subscription for queue 'name' 51 | s.forEach(i => { 52 | if (this.subscription[i].name === name) { 53 | this.unsubscribe(i); 54 | } 55 | }); 56 | // delete queue 'name' subject and observable 57 | delete this.timers[name].t; 58 | delete this.timers[name]; 59 | } 60 | /** 61 | * 62 | * @param name 63 | * @param callback 64 | */ 65 | subscribe(name: string, callback: () => void): string { 66 | if (!this.timers[name]) { 67 | return ''; 68 | } 69 | let id = name + '-' + UUID.UUID(); 70 | this.subscription[id] = { 71 | name: name, 72 | subscription: this.timers[name].t.subscribe(callback) 73 | } 74 | return id; 75 | } 76 | /** 77 | * 78 | * @param id 79 | */ 80 | unsubscribe(id: string): boolean { 81 | if (!id || !this.subscription[id]) { 82 | return false; 83 | } 84 | this.subscription[id].subscription.unsubscribe(); 85 | delete this.subscription[id]; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "emitDecoratorMetadata": true, 4 | "experimentalDecorators": true, 5 | "target": "es5", 6 | "module": "commonjs", 7 | "moduleResolution": "node", 8 | "removeComments": true, 9 | "sourceMap": true, 10 | "outDir": "../lib", 11 | "declaration": true, 12 | "lib": [ "es2015", "dom" ] 13 | } 14 | } 15 | --------------------------------------------------------------------------------