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 * @since 9 206 */ 207declare abstract class SubscribedAbstractProperty<T> { 208 /** 209 * Setting Subscribers. 210 * @since 7 211 * @systemapi 212 */ 213 protected subscribers_: Set<number>; 214 215 /** 216 * Private user ID. 217 * @since 7 218 * @systemapi 219 */ 220 private id_; 221 222 /** 223 * Private user information. 224 * @since 7 225 * @systemapi 226 */ 227 private info_?; 228 229 /** 230 * @since 7 231 * @systemapi 232 */ 233 constructor( 234 /** 235 * Subscriber IPropertySubscriber. 236 * @since 7 237 * @systemapi 238 */ 239 subscribeMe?: IPropertySubscriber, 240 /** 241 * Subscriber info. 242 * @since 7 243 * @systemapi 244 */ 245 info?: string, 246 ); 247 248 /** 249 * Called when the subscriber ID is entered. 250 * @since 7 251 * @systemapi 252 */ 253 id(): number; 254 255 /** 256 * Called when a subscriber information description is entered. 257 * @since 7 258 * @systemapi 259 */ 260 info(): string; 261 262 /** 263 * Reads value of the sync'ed AppStorage/LocalStorage property. 264 * `let link : SubscribedAbstractProperty<string> =AppStorage.Link<string>("foo")` 265 * then `link.get()` returns the value of "foo" property in AppStorage. 266 * @returns { T } the value of the sync'ed AppStorage/LocalStorage property. 267 * 268 * @since 9 269 */ 270 abstract get(): T; 271 272 /** 273 * Updates the value of value of the sync'ed AppStorage/LocalStorage property. 274 * Sets new value, must be of type T, and must not be 'undefined' or 'null'. 275 * `let link : SubscribedAbstractProperty<string> =AppStorage.Link<string>("foo")` 276 * then `link.set("Hello")` will set the value of "foo" property in AppStorage. 277 * 278 * @param { T } newValue 279 * @since 9 280 */ 281 abstract set(newValue: T): void; 282 283 /** 284 * Called when a two-way synchronization is created. 285 * @since 7 286 * @systemapi 287 */ 288 createTwoWaySync(subscribeMe?: IPropertySubscriber, info?: string): SyncedPropertyTwoWay<T>; 289 290 /** 291 * Called when a one-way synchronization is created. 292 * @since 7 293 * @systemapi 294 */ 295 createOneWaySync(subscribeMe?: IPropertySubscriber, info?: string): SyncedPropertyOneWay<T>; 296 297 /** 298 * Called when the subscriber is unlinked. 299 * @since 7 300 * @systemapi 301 */ 302 unlinkSuscriber(subscriberId: number): void; 303 304 /** 305 * Called when the notification has changed. 306 * @since 7 307 * @systemapi 308 */ 309 protected notifyHasChanged(newValue: T): void; 310 311 /** 312 * Called when the notification property is read. 313 * @since 7 314 * @systemapi 315 */ 316 protected notifyPropertyRead(): void; 317 318 /** 319 * Called when the number of users is queried. 320 * @since 7 321 * @systemapi 322 */ 323 numberOfSubscrbers(): number; 324} 325 326/** 327 * Provides an interface for attribute subscribers. 328 * @since 7 329 * @systemapi 330 */ 331interface IPropertySubscriber { 332 /** 333 * Called when the ID of the property subscriber is queried. 334 * @since 7 335 * @systemapi 336 */ 337 id(): number; 338 339 /** 340 * Provides a single attribute change user interface. 341 * @since 7 342 * @systemapi 343 */ 344 aboutToBeDeleted(owningView?: IPropertySubscriber): void; 345} 346 347/** 348 * Defines the state value. 349 * @since 7 350 * @systemapi 351 */ 352declare class SyncedPropertyTwoWay<T> extends SubscribedAbstractProperty<T> 353 implements ISinglePropertyChangeSubscriber<T> { 354 /** 355 * Sources of synchronization attributes bidirectionally. 356 * @since 7 357 * @systemapi 358 */ 359 private source_; 360 361 /** 362 * constructor parameters. 363 * @since 7 364 * @systemapi 365 */ 366 constructor(source: SubscribedAbstractProperty<T>, subscribeMe?: IPropertySubscriber, info?: string); 367 368 /** 369 * Called when processing information about to be deleted. 370 * @since 7 371 * @systemapi 372 */ 373 aboutToBeDeleted(unsubscribeMe?: IPropertySubscriber): void; 374 375 /** 376 * Information Changed. 377 * @since 7 378 * @systemapi 379 */ 380 hasChanged(newValue: T): void; 381 382 /** 383 * Called when data is obtained. 384 * @since 7 385 * @systemapi 386 */ 387 get(): T; 388 389 /** 390 * Called when data is created. 391 * @since 7 392 * @systemapi 393 */ 394 set(newValue: T): void; 395} 396 397/** 398* Defines the prop state value. 399* @since 7 400* @systemapi 401*/ 402declare class SyncedPropertyOneWay<T> extends SubscribedAbstractProperty<T> 403 implements ISinglePropertyChangeSubscriber<T> { 404 /** 405 * Pack value for single-item binding. 406 * @since 7 407 * @systemapi 408 */ 409 private wrappedValue_; 410 411 /** 412 * Sources of synchronization attributes bidirectionally. 413 * @since 7 414 * @systemapi 415 */ 416 private source_; 417 418 /** 419 * Constructor parameters. 420 * @since 7 421 * @systemapi 422 */ 423 constructor(source: SubscribedAbstractProperty<T>, subscribeMe?: IPropertySubscriber, info?: string); 424 425 /** 426 * Called when processing information about to be deleted. 427 * @since 7 428 * @systemapi 429 */ 430 aboutToBeDeleted(unsubscribeMe?: IPropertySubscriber): void; 431 432 /** 433 * Information Changed. 434 * @since 7 435 * @systemapi 436 */ 437 hasChanged(newValue: T): void; 438 439 /** 440 * Called when data is obtained. 441 * @since 7 442 * @systemapi 443 */ 444 get(): T; 445 446 /** 447 * Called when data is created. 448 * @since 7 449 * @systemapi 450 */ 451 set(newValue: T): void; 452} 453 454/** 455 * Defines the subscriber. 456 * @since 7 457 * @systemapi 458 */ 459interface ISinglePropertyChangeSubscriber<T> extends IPropertySubscriber { 460 /** 461 * Provides a single attribute change user interface. 462 * @since 7 463 * @systemapi 464 */ 465 hasChanged(newValue: T): void; 466} 467 468/** 469 * Defines the Subscribale base class. 470 * @since 7 471 * @systemapi 472 */ 473declare abstract class SubscribaleAbstract { 474 /** 475 * Returns the ownership attribute set by the. 476 * @since 7 477 * @systemapi 478 */ 479 private owningProperties_: Set<number>; 480 481 /** 482 * Constructor. 483 * @since 7 484 * @systemapi 485 */ 486 constructor(); 487 488 /** 489 * Called when the notification property has changed. 490 * @since 7 491 * @systemapi 492 */ 493 protected notifyPropertyHasChanged(propName: string, newValue: any): void; 494 495 /** 496 * Called when adding an already owned property. 497 * @since 7 498 * @systemapi 499 */ 500 public addOwningProperty(subscriber: IPropertySubscriber): void; 501 502 /** 503 * Called when an already owned property is deleted. 504 * @since 7 505 * @systemapi 506 */ 507 public removeOwningProperty(property: IPropertySubscriber): void; 508 509 /** 510 * Called when an already owned property is deleted by ID 511 * @since 7 512 * @systemapi 513 */ 514 public removeOwningPropertyById(subscriberId: number): void; 515} 516 517/** 518 * Defines the Environment interface. 519 * @since 7 520 */ 521declare class Environment { 522 /** 523 * Constructor. 524 * @since 7 525 * @systemapi 526 */ 527 constructor(); 528 529 /** 530 * Called when a property value is checked. 531 * @since 7 532 */ 533 static EnvProp<S>(key: string, value: S): boolean; 534 535 /** 536 * Called when multiple property values are checked. 537 * @since 7 538 */ 539 static EnvProps( 540 props: { 541 key: string; 542 defaultValue: any; 543 }[], 544 ): void; 545 546 /** 547 * Set the key value. 548 * @since 7 549 */ 550 static Keys(): Array<string>; 551} 552 553/** 554 * Defines the PersistentStorage interface. 555 * @since 7 556 */ 557declare class PersistentStorage { 558 /** 559 * Constructor parameters. 560 * @since 7 561 * @systemapi 562 */ 563 constructor(appStorage: AppStorage, storage: Storage); 564 565 /** 566 * Called when a persistence property is stored. 567 * @since 7 568 */ 569 static PersistProp<T>(key: string, defaultValue: T): void; 570 571 /** 572 * Called when a property is deleted. 573 * @since 7 574 */ 575 static DeleteProp(key: string): void; 576 577 /** 578 * Called when multiple persistence properties are stored. 579 * @since 7 580 */ 581 static PersistProps( 582 properties: { 583 key: string; 584 defaultValue: any; 585 }[], 586 ): void; 587 588 /** 589 * Set the key value. 590 * @since 7 591 */ 592 static Keys(): Array<string>; 593} 594 595/** 596 * Used for ide. 597 * @since 7 598 * @systemapi 599 */ 600declare const appStorage: AppStorage; 601 602/** 603 * 604 * LocalStorage 605 * 606 * Class implements a Map of ObservableObjectBase UI state variables. 607 * Instances can be created to manage UI state within a limited "local" 608 * access, and life cycle as defined by the app. 609 * AppStorage singleton is sub-class of LocalStorage for 610 * UI state of app-wide access and same life cycle as the app. 611 * 612 * @form 613 * @since 9 614 */ 615declare class LocalStorage { 616 /** 617 * Construct new instance of LocalStorage 618 * initialize with all properties and their values that Object.keys(params) returns 619 * Property values must not be undefined. 620 * @param initializingProperties Object containing keys and values. see set() for valid values 621 * 622 * @form 623 * @since 9 624 */ 625 constructor(initializingProperties?: Object); 626 627 /** 628 * Get current LocalStorage shared from stage. 629 * @StageModelOnly 630 * @form 631 * @since 9 632 */ 633 static GetShared(): LocalStorage; 634 635 /** 636 * Check if LocalStorage has a property with given name 637 * return true if property with given name exists 638 * same as ES6 Map.prototype.has() 639 * @param propName searched property 640 * @returns true if property with such name exists in LocalStorage 641 * 642 * @form 643 * @since 9 644 */ 645 has(propName: string): boolean; 646 647 /** 648 * Provide names of all properties in LocalStorage 649 * same as ES6 Map.prototype.keys() 650 * @returns return a Map Iterator 651 * 652 * @form 653 * @since 9 654 */ 655 keys(): IterableIterator<string>; 656 657 /** 658 * Returns number of properties in LocalStorage 659 * same as Map.prototype.size() 660 * @returns return number of properties 661 * 662 * @form 663 * @since 9 664 */ 665 size(): number; 666 667 /** 668 * Returns value of given property 669 * return undefined if no property with this name 670 * @param propName 671 * @returns property value if found or undefined 672 * 673 * @form 674 * @since 9 675 */ 676 get<T>(propName: string): T | undefined; 677 678 /** 679 * Set value of given property in LocalStorage 680 * Method sets nothing and returns false if property with this name does not exist 681 * or if newValue is `undefined` or `null` (`undefined`, `null` value are not allowed for state variables). 682 * @param propName 683 * @param newValue must be of type T and must not be undefined or null 684 * @returns true on success, i.e. when above conditions are satisfied, otherwise false 685 * 686 * @form 687 * @since 9 688 */ 689 set<T>(propName: string, newValue: T): boolean; 690 691 /** 692 * Set value of given property, if it exists, see set() . 693 * Add property if no property with given name and initialize with given value. 694 * Do nothing and return false if newValue is undefined or null 695 * (undefined, null value is not allowed for state variables) 696 * @param propName 697 * @param newValue must be of type T and must not be undefined or null 698 * @returns true on success, i.e. when above conditions are satisfied, otherwise false 699 * 700 * @form 701 * @since 9 702 */ 703 setOrCreate<T>(propName: string, newValue: T): boolean; 704 705 /** 706 * Create and return a two-way sync "(link") to named property 707 * @param propName name of source property in LocalStorage 708 * @returns instance of SubscribedAbstractProperty<T> 709 * return undefined if named property does not already exist in LocalStorage 710 * Apps can use SDK functions of base class SubscribedPropertyAbstract<T> 711 * 712 * @form 713 * @since 9 714 */ 715 link<T>(propName: string): SubscribedAbstractProperty<T>; 716 717 /** 718 * Like see link(), but will create and initialize a new source property in LocalStorage if missing 719 * @param propName name of source property in LocalStorage 720 * @param defaultValue value to be used for initializing if new creating new property in LocalStorage 721 * default value must be of type T, must not be undefined or null. 722 * @returns instance of SubscribedAbstractProperty<T> 723 * Apps can use SDK functions of base class SubscribedAbstractProperty<T> 724 * 725 * @form 726 * @since 9 727 */ 728 setAndLink<T>(propName: string, defaultValue: T): SubscribedAbstractProperty<T>; 729 730 /** 731 * Create and return a one-way sync ('prop') to named property 732 * @param { string } propName name of source property in LocalStorage 733 * @returns { SubscribedAbstractProperty<S> } instance of SubscribedAbstractProperty<S> 734 * return undefined if named property does not already exist in LocalStorage 735 * Apps can use SDK functions of base class SubscribedAbstractProperty<S> 736 * 737 * @form 738 * @since 9 739 */ 740 prop<S>(propName: string): SubscribedAbstractProperty<S>; 741 742 /** 743 * Like see prop(), will create and initialize a new source property in LocalStorage if missing 744 * @param { string } propName name of source property in LocalStorage 745 * @param { S } defaultValue value to be used for initializing if new creating new property in LocalStorage. 746 * Default value must be of type T, must not be undefined or null. 747 * @returns { SubscribedAbstractProperty<S> } instance of SubscribedAbstractProperty<S> 748 * Apps can use SDK functions of base class SubscribedAbstractProperty<S> 749 * 750 * @form 751 * @since 9 752 */ 753 setAndProp<S>(propName: string, defaultValue: S): SubscribedAbstractProperty<S>; 754 755 /** 756 * Delete property from StorageBase 757 * Use with caution: 758 * Before deleting a prop from LocalStorage all its subscribers need to 759 * unsubscribe from the property. 760 * This method fails and returns false if given property still has subscribers 761 * Another reason for failing is unknown property. 762 * 763 * Developer advise: 764 * Subscribers are created with see link(), see prop() 765 * and also via @LocalStorageLink and @LocalStorageProp state variable decorators. 766 * That means as long as their is a @Component instance that uses such decorated variable 767 * or a sync relationship with a SubscribedAbstractProperty variable the property can nit 768 * (and also should not!) be deleted from LocalStorage. 769 * 770 * @param propName 771 * @returns false if method failed 772 * 773 * @form 774 * @since 9 775 */ 776 delete(propName: string): boolean; 777 778 /** 779 * Delete all properties from the LocalStorage instance 780 * Precondition is that there are no subscribers. 781 * method returns false and deletes no properties if there is any property 782 * that still has subscribers 783 * 784 * @form 785 * @since 9 786 */ 787 clear(): boolean; 788} 789 790declare module "StateManagement" { 791 module "StateManagement" { 792 // @ts-ignore 793 export { LocalStorage }; 794 } 795} 796