1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/** 17 * AppStorage singleton is sub-class of see LocalStorage for 18 * UI state of app-wide access and same life cycle as the app. 19 * @since 7 20 */ 21declare class AppStorage { 22 /** 23 * Called when a link is set. 24 * @since 7 25 */ 26 static Link(propName: string): any; 27 28 /** 29 * Like see Link(), but will create and initialize a new source property in AppStorage if missing 30 * 31 * Same as see LocalStorage.setAndLink() 32 * 33 * @param { string } propName name of source property in AppStorage 34 * @param { T } defaultValue value to be used for initializing if new creating new property in AppStorage 35 * default value must be of type T, must not be 'undefined' or 'null'. 36 * @returns { SubscribedAbstractProperty<T> } instance of SubscribedAbstractProperty<T> 37 * 38 * @since 7 39 */ 40 static SetAndLink<T>(propName: string, defaultValue: T): SubscribedAbstractProperty<T>; 41 42 /** 43 * Called when a property is set. 44 * @since 7 45 */ 46 static Prop(propName: string): any; 47 48 /** 49 * Like see prop(), will create and initialize a new source property in AppStorage if missing 50 * 51 * Same as see LocalStorage.setAndProp() 52 * 53 * @param { string } propName name of source property in AppStorage 54 * @param { S } defaultValue value to be used for initializing if new creating new property in AppStorage. 55 * default value must be of type T, must not be undefined or null. 56 * @returns { SubscribedAbstractProperty<S> } instance of SubscribedAbstractProperty<S> 57 * return undefined if named property does not already exist in AppStorage. 58 * 59 * @since 7 60 */ 61 static SetAndProp<S>(propName: string, defaultValue: S): SubscribedAbstractProperty<S>; 62 63 /** 64 * Checks if AppStorage has a property with given name 65 * returns true if property with given name exists 66 * same as ES6 Map.prototype.has() 67 * 68 * Same as see LocalStorage.has() 69 * 70 * @param { string } propName searched property 71 * @returns { boolean } true if property with such name exists in AppStorage 72 * 73 * @since 7 74 */ 75 static Has(propName: string): boolean; 76 77 /** 78 * Same as see LocalStorage.get() 79 * 80 * Obtain the value of property with given name, returns undefined if the property does not exist in AppStorage. 81 * 82 * @param { string } propName 83 * @returns { T | undefined } property value of type T if found or undefined 84 * 85 * @since 7 86 */ 87 static Get<T>(propName: string): T | undefined; 88 89 /** 90 * Set value of given property in AppStorage 91 * Method sets nothing and returns false if property with this name does not exist 92 * or if newValue is `undefined` or `null`. 93 * 94 * Same as see LocalStorage.set 95 * 96 * @param { string } propName 97 * @param { T } newValue must be of type T and must not be undefined or null 98 * @returns { boolean } true on success, i.e. when above conditions are satisfied, otherwise false 99 * 100 * @since 7 101 */ 102 static Set<T>(propName: string, newValue: T): boolean; 103 104 /** 105 * Set value of given property, if it exists, see set() . 106 * Add property if no property with given name in AppStorage,. yet, and initialize with given value. 107 * Do nothing and return false if newValue is undefined or null 108 * 109 * see LocalStorage.setOrCreate() 110 * 111 * @param { string } propName 112 * @param { T } newValue must be of type T and must not be undefined or null 113 * 114 * @since 7 115 */ 116 static SetOrCreate<T>(propName: string, newValue: T): void; 117 118 /** 119 * Delete property with given name from AppStorage 120 * Use with caution: 121 * Before deleting a prop from AppStorage all its subscribers need to 122 * unsubscribe from the property. 123 * This method fails and returns false if given property still has subscribers 124 * Another reason for failing is unknown property name. 125 * 126 * Developer advise: 127 * Subscribers to a property in AppStorage are created with see link(), see prop() 128 * and also via @StorageLink and @StorageProp state variable decorators. 129 * That means as long as their is a @Component instance that uses such decorated variable 130 * or a sync relationship with a SubscribedAbstractProperty variable the property can not 131 * (and also should not!) be deleted from AppStorage. 132 * 133 * Same as see LocalStorage.delete() 134 * 135 * @param { string } propName 136 * @returns { boolean } false if method failed 137 * 138 * @since 7 139 */ 140 static Delete(propName: string): boolean; 141 142 /** 143 * Provide names of all properties in AppStorage 144 * same as ES6 Map.prototype.keys() 145 * 146 * Same as see LocalStorage.keys() 147 * 148 * @returns { IterableIterator<string> } return a Map Iterator 149 * 150 * @since 7 151 */ 152 static Keys(): IterableIterator<string>; 153 154 /** 155 * Called when a cleanup occurs. 156 * @since 7 157 * @deprecated since 9 158 * @useinstead AppStorage.Clear 159 */ 160 static staticClear(): boolean; 161 162 /** 163 * Delete all properties from the AppStorage. 164 * 165 * Precondition is that there are no subscribers, see Delete(). 166 * @returns { boolean } false and deletes no properties if there is any property 167 * that still has subscribers. 168 * 169 * @since 9 170 */ 171 static Clear(): boolean; 172 173 /** 174 * Called when the data can be changed. 175 * @since 7 176 */ 177 static IsMutable(propName: string): boolean; 178 179 /** 180 * Method returns the number of properties currently in AppStorage 181 * 182 * @returns { number } Returns the number of properties currently in AppStorage 183 * @since 7 184 */ 185 static Size(): number; 186} 187 188/** 189 * Defines the subscribed abstract property. 190 * @since 7 191 * @systemapi 192 */ 193/** 194 * SubscribedAbstractProperty<T> is the return value of 195 * - AppStorage static functions Link(), Prop(), SetAndLink(), and SetAndProp() 196 * - LocalStorage member methods link(), prop(), setAndLink(), and setAndProp() 197 * 'T' can be boolean, string, number or custom class. 198 * 199 * Main functions 200 * see get() reads the linked AppStorage/LocalStorage property value, 201 * see set(newValue) write a new value to the synched AppStorage/LocalStorage property 202 * see aboutToBeDeleted() ends the sync relationship with the AppStorage/LocalStorage property 203 * The app must call this function before the SubscribedAbstractProperty<T> object 204 * goes out of scope. 205 * @form 206 * @since 9 207 */ 208declare abstract class SubscribedAbstractProperty<T> { 209 /** 210 * Setting Subscribers. 211 * @since 7 212 * @systemapi 213 */ 214 protected subscribers_: Set<number>; 215 216 /** 217 * Private user ID. 218 * @since 7 219 * @systemapi 220 */ 221 private id_; 222 223 /** 224 * Private user information. 225 * @since 7 226 * @systemapi 227 */ 228 private info_?; 229 230 /** 231 * @since 7 232 * @systemapi 233 */ 234 constructor( 235 /** 236 * Subscriber IPropertySubscriber. 237 * @since 7 238 * @systemapi 239 */ 240 subscribeMe?: IPropertySubscriber, 241 /** 242 * Subscriber info. 243 * @since 7 244 * @systemapi 245 */ 246 info?: string, 247 ); 248 249 /** 250 * Called when the subscriber ID is entered. 251 * @since 7 252 * @systemapi 253 */ 254 id(): number; 255 256 /** 257 * Called when a subscriber information description is entered. 258 * @since 7 259 * @systemapi 260 */ 261 info(): string; 262 263 /** 264 * Reads value of the sync'ed AppStorage/LocalStorage property. 265 * `let link : SubscribedAbstractProperty<string> =AppStorage.Link<string>("foo")` 266 * then `link.get()` returns the value of "foo" property in AppStorage. 267 * @returns { T } the value of the sync'ed AppStorage/LocalStorage property. 268 * 269 * @form 270 * @since 9 271 */ 272 abstract get(): T; 273 274 /** 275 * Updates the value of value of the sync'ed AppStorage/LocalStorage property. 276 * Sets new value, must be of type T, and must not be 'undefined' or 'null'. 277 * `let link : SubscribedAbstractProperty<string> =AppStorage.Link<string>("foo")` 278 * then `link.set("Hello")` will set the value of "foo" property in AppStorage. 279 * 280 * @param { T } newValue 281 * @form 282 * @since 9 283 */ 284 abstract set(newValue: T): void; 285 286 /** 287 * Called when a two-way synchronization is created. 288 * @since 7 289 * @systemapi 290 */ 291 createTwoWaySync(subscribeMe?: IPropertySubscriber, info?: string): SyncedPropertyTwoWay<T>; 292 293 /** 294 * Called when a one-way synchronization is created. 295 * @since 7 296 * @systemapi 297 */ 298 createOneWaySync(subscribeMe?: IPropertySubscriber, info?: string): SyncedPropertyOneWay<T>; 299 300 /** 301 * Called when the subscriber is unlinked. 302 * @since 7 303 * @systemapi 304 */ 305 unlinkSuscriber(subscriberId: number): void; 306 307 /** 308 * Called when the notification has changed. 309 * @since 7 310 * @systemapi 311 */ 312 protected notifyHasChanged(newValue: T): void; 313 314 /** 315 * Called when the notification property is read. 316 * @since 7 317 * @systemapi 318 */ 319 protected notifyPropertyRead(): void; 320 321 /** 322 * Called when the number of users is queried. 323 * @since 7 324 * @systemapi 325 */ 326 numberOfSubscrbers(): number; 327} 328 329/** 330 * Provides an interface for attribute subscribers. 331 * @since 7 332 * @systemapi 333 */ 334interface IPropertySubscriber { 335 /** 336 * Called when the ID of the property subscriber is queried. 337 * @since 7 338 * @systemapi 339 */ 340 id(): number; 341 342 /** 343 * Provides a single attribute change user interface. 344 * @since 7 345 * @systemapi 346 */ 347 aboutToBeDeleted(owningView?: IPropertySubscriber): void; 348} 349 350/** 351 * Defines the state value. 352 * @since 7 353 * @systemapi 354 */ 355declare class SyncedPropertyTwoWay<T> extends SubscribedAbstractProperty<T> 356 implements ISinglePropertyChangeSubscriber<T> { 357 /** 358 * Sources of synchronization attributes bidirectionally. 359 * @since 7 360 * @systemapi 361 */ 362 private source_; 363 364 /** 365 * constructor parameters. 366 * @since 7 367 * @systemapi 368 */ 369 constructor(source: SubscribedAbstractProperty<T>, subscribeMe?: IPropertySubscriber, info?: string); 370 371 /** 372 * Called when processing information about to be deleted. 373 * @since 7 374 * @systemapi 375 */ 376 aboutToBeDeleted(unsubscribeMe?: IPropertySubscriber): void; 377 378 /** 379 * Information Changed. 380 * @since 7 381 * @systemapi 382 */ 383 hasChanged(newValue: T): void; 384 385 /** 386 * Called when data is obtained. 387 * @since 7 388 * @systemapi 389 */ 390 get(): T; 391 392 /** 393 * Called when data is created. 394 * @since 7 395 * @systemapi 396 */ 397 set(newValue: T): void; 398} 399 400/** 401* Defines the prop state value. 402* @since 7 403* @systemapi 404*/ 405declare class SyncedPropertyOneWay<T> extends SubscribedAbstractProperty<T> 406 implements ISinglePropertyChangeSubscriber<T> { 407 /** 408 * Pack value for single-item binding. 409 * @since 7 410 * @systemapi 411 */ 412 private wrappedValue_; 413 414 /** 415 * Sources of synchronization attributes bidirectionally. 416 * @since 7 417 * @systemapi 418 */ 419 private source_; 420 421 /** 422 * Constructor parameters. 423 * @since 7 424 * @systemapi 425 */ 426 constructor(source: SubscribedAbstractProperty<T>, subscribeMe?: IPropertySubscriber, info?: string); 427 428 /** 429 * Called when processing information about to be deleted. 430 * @since 7 431 * @systemapi 432 */ 433 aboutToBeDeleted(unsubscribeMe?: IPropertySubscriber): void; 434 435 /** 436 * Information Changed. 437 * @since 7 438 * @systemapi 439 */ 440 hasChanged(newValue: T): void; 441 442 /** 443 * Called when data is obtained. 444 * @since 7 445 * @systemapi 446 */ 447 get(): T; 448 449 /** 450 * Called when data is created. 451 * @since 7 452 * @systemapi 453 */ 454 set(newValue: T): void; 455} 456 457/** 458 * Defines the subscriber. 459 * @since 7 460 * @systemapi 461 */ 462interface ISinglePropertyChangeSubscriber<T> extends IPropertySubscriber { 463 /** 464 * Provides a single attribute change user interface. 465 * @since 7 466 * @systemapi 467 */ 468 hasChanged(newValue: T): void; 469} 470 471/** 472 * Defines the Subscribale base class. 473 * @since 7 474 * @systemapi 475 */ 476declare abstract class SubscribaleAbstract { 477 /** 478 * Returns the ownership attribute set by the. 479 * @since 7 480 * @systemapi 481 */ 482 private owningProperties_: Set<number>; 483 484 /** 485 * Constructor. 486 * @since 7 487 * @systemapi 488 */ 489 constructor(); 490 491 /** 492 * Called when the notification property has changed. 493 * @since 7 494 * @systemapi 495 */ 496 protected notifyPropertyHasChanged(propName: string, newValue: any): void; 497 498 /** 499 * Called when adding an already owned property. 500 * @since 7 501 * @systemapi 502 */ 503 public addOwningProperty(subscriber: IPropertySubscriber): void; 504 505 /** 506 * Called when an already owned property is deleted. 507 * @since 7 508 * @systemapi 509 */ 510 public removeOwningProperty(property: IPropertySubscriber): void; 511 512 /** 513 * Called when an already owned property is deleted by ID 514 * @since 7 515 * @systemapi 516 */ 517 public removeOwningPropertyById(subscriberId: number): void; 518} 519 520/** 521 * Defines the Environment interface. 522 * @since 7 523 */ 524declare class Environment { 525 /** 526 * Constructor. 527 * @since 7 528 * @systemapi 529 */ 530 constructor(); 531 532 /** 533 * Called when a property value is checked. 534 * @since 7 535 */ 536 static EnvProp<S>(key: string, value: S): boolean; 537 538 /** 539 * Called when multiple property values are checked. 540 * @since 7 541 */ 542 static EnvProps( 543 props: { 544 key: string; 545 defaultValue: any; 546 }[], 547 ): void; 548 549 /** 550 * Set the key value. 551 * @since 7 552 */ 553 static Keys(): Array<string>; 554} 555 556/** 557 * Defines the PersistentStorage interface. 558 * @since 7 559 */ 560declare class PersistentStorage { 561 /** 562 * Constructor parameters. 563 * @since 7 564 * @systemapi 565 */ 566 constructor(appStorage: AppStorage, storage: Storage); 567 568 /** 569 * Called when a persistence property is stored. 570 * @since 7 571 */ 572 static PersistProp<T>(key: string, defaultValue: T): void; 573 574 /** 575 * Called when a property is deleted. 576 * @since 7 577 */ 578 static DeleteProp(key: string): void; 579 580 /** 581 * Called when multiple persistence properties are stored. 582 * @since 7 583 */ 584 static PersistProps( 585 properties: { 586 key: string; 587 defaultValue: any; 588 }[], 589 ): void; 590 591 /** 592 * Set the key value. 593 * @since 7 594 */ 595 static Keys(): Array<string>; 596} 597 598/** 599 * Used for ide. 600 * @since 7 601 * @systemapi 602 */ 603declare const appStorage: AppStorage; 604 605/** 606 * 607 * LocalStorage 608 * 609 * Class implements a Map of ObservableObjectBase UI state variables. 610 * Instances can be created to manage UI state within a limited "local" 611 * access, and life cycle as defined by the app. 612 * AppStorage singleton is sub-class of LocalStorage for 613 * UI state of app-wide access and same life cycle as the app. 614 * 615 * @form 616 * @since 9 617 */ 618declare class LocalStorage { 619 /** 620 * Construct new instance of LocalStorage 621 * initialize with all properties and their values that Object.keys(params) returns 622 * Property values must not be undefined. 623 * @param initializingProperties Object containing keys and values. see set() for valid values 624 * 625 * @form 626 * @since 9 627 */ 628 constructor(initializingProperties?: Object); 629 630 /** 631 * Get current LocalStorage shared from stage. 632 * @StageModelOnly 633 * @form 634 * @since 9 635 */ 636 static GetShared(): LocalStorage; 637 638 /** 639 * Check if LocalStorage has a property with given name 640 * return true if property with given name exists 641 * same as ES6 Map.prototype.has() 642 * @param propName searched property 643 * @returns true if property with such name exists in LocalStorage 644 * 645 * @form 646 * @since 9 647 */ 648 has(propName: string): boolean; 649 650 /** 651 * Provide names of all properties in LocalStorage 652 * same as ES6 Map.prototype.keys() 653 * @returns return a Map Iterator 654 * 655 * @form 656 * @since 9 657 */ 658 keys(): IterableIterator<string>; 659 660 /** 661 * Returns number of properties in LocalStorage 662 * same as Map.prototype.size() 663 * @returns return number of properties 664 * 665 * @form 666 * @since 9 667 */ 668 size(): number; 669 670 /** 671 * Returns value of given property 672 * return undefined if no property with this name 673 * @param propName 674 * @returns property value if found or undefined 675 * 676 * @form 677 * @since 9 678 */ 679 get<T>(propName: string): T | undefined; 680 681 /** 682 * Set value of given property in LocalStorage 683 * Method sets nothing and returns false if property with this name does not exist 684 * or if newValue is `undefined` or `null` (`undefined`, `null` value are not allowed for state variables). 685 * @param propName 686 * @param newValue must be of type T and must not be undefined or null 687 * @returns true on success, i.e. when above conditions are satisfied, otherwise false 688 * 689 * @form 690 * @since 9 691 */ 692 set<T>(propName: string, newValue: T): boolean; 693 694 /** 695 * Set value of given property, if it exists, see set() . 696 * Add property if no property with given name and initialize with given value. 697 * Do nothing and return false if newValue is undefined or null 698 * (undefined, null value is not allowed for state variables) 699 * @param propName 700 * @param newValue must be of type T and must not be undefined or null 701 * @returns true on success, i.e. when above conditions are satisfied, otherwise false 702 * 703 * @form 704 * @since 9 705 */ 706 setOrCreate<T>(propName: string, newValue: T): boolean; 707 708 /** 709 * Create and return a two-way sync "(link") to named property 710 * @param propName name of source property in LocalStorage 711 * @returns instance of SubscribedAbstractProperty<T> 712 * return undefined if named property does not already exist in LocalStorage 713 * Apps can use SDK functions of base class SubscribedPropertyAbstract<T> 714 * 715 * @form 716 * @since 9 717 */ 718 link<T>(propName: string): SubscribedAbstractProperty<T>; 719 720 /** 721 * Like see link(), but will create and initialize a new source property in LocalStorage if missing 722 * @param propName name of source property in LocalStorage 723 * @param defaultValue value to be used for initializing if new creating new property in LocalStorage 724 * default value must be of type T, must not be undefined or null. 725 * @returns instance of SubscribedAbstractProperty<T> 726 * Apps can use SDK functions of base class SubscribedAbstractProperty<T> 727 * 728 * @form 729 * @since 9 730 */ 731 setAndLink<T>(propName: string, defaultValue: T): SubscribedAbstractProperty<T>; 732 733 /** 734 * Create and return a one-way sync ('prop') to named property 735 * @param { string } propName name of source property in LocalStorage 736 * @returns { SubscribedAbstractProperty<S> } instance of SubscribedAbstractProperty<S> 737 * return undefined if named property does not already exist in LocalStorage 738 * Apps can use SDK functions of base class SubscribedAbstractProperty<S> 739 * 740 * @form 741 * @since 9 742 */ 743 prop<S>(propName: string): SubscribedAbstractProperty<S>; 744 745 /** 746 * Like see prop(), will create and initialize a new source property in LocalStorage if missing 747 * @param { string } propName name of source property in LocalStorage 748 * @param { S } defaultValue value to be used for initializing if new creating new property in LocalStorage. 749 * Default value must be of type T, must not be undefined or null. 750 * @returns { SubscribedAbstractProperty<S> } instance of SubscribedAbstractProperty<S> 751 * Apps can use SDK functions of base class SubscribedAbstractProperty<S> 752 * 753 * @form 754 * @since 9 755 */ 756 setAndProp<S>(propName: string, defaultValue: S): SubscribedAbstractProperty<S>; 757 758 /** 759 * Delete property from StorageBase 760 * Use with caution: 761 * Before deleting a prop from LocalStorage all its subscribers need to 762 * unsubscribe from the property. 763 * This method fails and returns false if given property still has subscribers 764 * Another reason for failing is unknown property. 765 * 766 * Developer advise: 767 * Subscribers are created with see link(), see prop() 768 * and also via @LocalStorageLink and @LocalStorageProp state variable decorators. 769 * That means as long as their is a @Component instance that uses such decorated variable 770 * or a sync relationship with a SubscribedAbstractProperty variable the property can nit 771 * (and also should not!) be deleted from LocalStorage. 772 * 773 * @param propName 774 * @returns false if method failed 775 * 776 * @form 777 * @since 9 778 */ 779 delete(propName: string): boolean; 780 781 /** 782 * Delete all properties from the LocalStorage instance 783 * Precondition is that there are no subscribers. 784 * method returns false and deletes no properties if there is any property 785 * that still has subscribers 786 * 787 * @form 788 * @since 9 789 */ 790 clear(): boolean; 791} 792 793declare module "StateManagement" { 794 module "StateManagement" { 795 // @ts-ignore 796 export { LocalStorage }; 797 } 798} 799