@converse/skeletor 3.0.1 → 3.1.0
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/README.md +330 -38
- package/dist/skeletor.d.ts +193 -56
- package/dist/skeletor.esm.js +739 -457
- package/dist/skeletor.esm.js.map +1 -1
- package/dist/skeletor.js +740 -456
- package/dist/skeletor.js.map +1 -1
- package/dist/skeletor.min.js +6 -19
- package/dist/skeletor.node.js +12494 -0
- package/dist/skeletor.node.mjs +12481 -0
- package/package.json +24 -7
- package/src/autosync.ts +156 -0
- package/src/collection.ts +140 -35
- package/src/drivers/helpers.ts +61 -0
- package/src/drivers/nodeSQLiteStorage.ts +162 -0
- package/src/drivers/sessionStorage.ts +16 -22
- package/src/drivers/types.ts +25 -0
- package/src/eventemitter.ts +9 -0
- package/src/helpers.ts +57 -10
- package/src/index.node.ts +8 -0
- package/src/index.ts +11 -3
- package/src/model.ts +254 -13
- package/src/storage.ts +78 -38
- package/src/types.ts +34 -9
- package/src/utils/events.ts +6 -6
package/dist/skeletor.d.ts
CHANGED
|
@@ -1,38 +1,10 @@
|
|
|
1
1
|
import localForage from 'localforage';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
+
* @deprecated Use `PersistentStorage` instead.
|
|
4
5
|
* @public
|
|
5
6
|
*/
|
|
6
|
-
export declare
|
|
7
|
-
storeInitialized: Promise<void>;
|
|
8
|
-
store: LocalForageWithExtensions;
|
|
9
|
-
name: string;
|
|
10
|
-
static sessionStorageInitialized: Promise<void>;
|
|
11
|
-
static localForage: typeof localForage;
|
|
12
|
-
constructor(id: string, type: 'local' | 'session' | 'indexed' | 'in_memory' | LocalForageWithExtensions, batchedWrites?: boolean);
|
|
13
|
-
/**
|
|
14
|
-
* @param type - The storage type: 'local', 'session', 'indexed', or 'in_memory'
|
|
15
|
-
* @param batchedWrites - Whether to enable batched writes
|
|
16
|
-
*/
|
|
17
|
-
initStore(type: 'local' | 'session' | 'indexed' | 'in_memory', batchedWrites: boolean): Promise<void>;
|
|
18
|
-
flush(): void;
|
|
19
|
-
clear(): Promise<void>;
|
|
20
|
-
sync(): {
|
|
21
|
-
(method: SyncOperation, model: Model, options: SyncOptions): Promise<void>;
|
|
22
|
-
__name__: string;
|
|
23
|
-
};
|
|
24
|
-
removeCollectionReference(model: Model, collection: Collection | undefined): Promise<any> | undefined;
|
|
25
|
-
addCollectionReference(model: Model, collection: Collection | undefined): Promise<any> | undefined;
|
|
26
|
-
getCollectionReferenceData(model: Model): Record<string, string[]>;
|
|
27
|
-
save(model: Model): Promise<any>;
|
|
28
|
-
create(model: Model, options: SyncOptions): Promise<any>;
|
|
29
|
-
update(model: Model): Promise<any>;
|
|
30
|
-
find(model: Model): Promise<any>;
|
|
31
|
-
findAll(): Promise<any[]>;
|
|
32
|
-
destroy(model: Model, collection: Collection | undefined): Promise<Model>;
|
|
33
|
-
getStorageSize(): Promise<number>;
|
|
34
|
-
getItemName(id: string | number): string;
|
|
35
|
-
}
|
|
7
|
+
export declare const BrowserStorage: typeof PersistentStorage;
|
|
36
8
|
|
|
37
9
|
/**
|
|
38
10
|
* @public
|
|
@@ -49,10 +21,12 @@ export declare type ClassConstructor<T = any> = new (...args: any[]) => T;
|
|
|
49
21
|
* indexes of their models, both in order, and for lookup by `id`.
|
|
50
22
|
*/
|
|
51
23
|
export declare class Collection<T extends Model = Model> extends EventEmitterObject {
|
|
24
|
+
#private;
|
|
52
25
|
[key: symbol]: () => CollectionIterator<T>;
|
|
53
|
-
|
|
26
|
+
_storage?: PersistentStorage;
|
|
54
27
|
_comparator?: Comparator<T>;
|
|
55
28
|
_url: string;
|
|
29
|
+
initialized?: Promise<void>;
|
|
56
30
|
models: T[];
|
|
57
31
|
protected _byId: Record<string, T>;
|
|
58
32
|
protected _model?: new (attributes?: Partial<ModelAttributes>, options?: ModelOptions) => T;
|
|
@@ -64,8 +38,29 @@ export declare class Collection<T extends Model = Model> extends EventEmitterObj
|
|
|
64
38
|
constructor(models?: T[] | ModelAttributes[] | T | ModelAttributes, options?: CollectionOptions<T>);
|
|
65
39
|
get comparator(): Comparator<T>;
|
|
66
40
|
set comparator(c: Comparator<T>);
|
|
67
|
-
|
|
68
|
-
|
|
41
|
+
/**
|
|
42
|
+
* The canonical storage accessor. Set to a `PersistentStorage` instance
|
|
43
|
+
* to enable local persistence for this collection.
|
|
44
|
+
*/
|
|
45
|
+
get storage(): PersistentStorage | undefined;
|
|
46
|
+
set storage(s: PersistentStorage);
|
|
47
|
+
/**
|
|
48
|
+
* @deprecated Use `storage` instead.
|
|
49
|
+
*/
|
|
50
|
+
get browserStorage(): PersistentStorage | undefined;
|
|
51
|
+
set browserStorage(s: PersistentStorage);
|
|
52
|
+
/**
|
|
53
|
+
* Override to return `true` to automatically load the collection from
|
|
54
|
+
* storage on construction. Await `initialized` to know when hydration
|
|
55
|
+
* is complete.
|
|
56
|
+
*
|
|
57
|
+
* Note: collection `autoSync` is hydrate-only. Unlike `Model`, it does
|
|
58
|
+
* **not** schedule auto-saves when the collection changes (`add`/`remove`/
|
|
59
|
+
* `reset`). Persisting members is each model's responsibility — enable
|
|
60
|
+
* `autoSync` on the contained model class and every model writes its own
|
|
61
|
+
* record. A collection-level write would be redundant with that.
|
|
62
|
+
*/
|
|
63
|
+
get autoSync(): boolean;
|
|
69
64
|
/**
|
|
70
65
|
* The default model for a collection is just a **Model**.
|
|
71
66
|
* This should be overridden in most cases.
|
|
@@ -207,6 +202,17 @@ export declare class Collection<T extends Model = Model> extends EventEmitterObj
|
|
|
207
202
|
* wait for the server to agree.
|
|
208
203
|
*/
|
|
209
204
|
create(model: T | ModelAttributes, options?: FetchOrCreateOptions): Promise<T> | T;
|
|
205
|
+
/**
|
|
206
|
+
* Subscribe to collection changes (add, remove, reset, sort, update).
|
|
207
|
+
* Returns an unsubscribe function.
|
|
208
|
+
*
|
|
209
|
+
* `collection.subscribe(callback)` — fires once per operation on add/remove/reset/sort (via `update`, `reset`, `sort` events).
|
|
210
|
+
* `collection.subscribe(event, callback)` — subscribe to a specific event (base EventEmitter form).
|
|
211
|
+
*
|
|
212
|
+
* Compatible with React's `useSyncExternalStore` and other store-style APIs.
|
|
213
|
+
*/
|
|
214
|
+
subscribe(event: string, callback: EventCallback, context?: unknown): () => void;
|
|
215
|
+
subscribe(callback: (collection: this) => void): () => void;
|
|
210
216
|
/**
|
|
211
217
|
* **parse** converts a response into a list of models to be added to the
|
|
212
218
|
* collection. The default implementation is just to pass it through.
|
|
@@ -296,6 +302,19 @@ export declare type CollectionOptions<T extends Model = Model> = Options & {
|
|
|
296
302
|
*/
|
|
297
303
|
export declare type Comparator<T extends Model = Model> = string | boolean | ((a: T, b: T) => number) | ((a?: T) => string);
|
|
298
304
|
|
|
305
|
+
/**
|
|
306
|
+
* @public
|
|
307
|
+
*/
|
|
308
|
+
export declare type ComputedProperties<M> = Record<string, ComputedProperty<M>>;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* @public
|
|
312
|
+
*/
|
|
313
|
+
export declare type ComputedProperty<M> = {
|
|
314
|
+
deps: string[];
|
|
315
|
+
fn: (model: M) => any;
|
|
316
|
+
};
|
|
317
|
+
|
|
299
318
|
/**
|
|
300
319
|
* @public
|
|
301
320
|
*/
|
|
@@ -362,6 +381,11 @@ export declare function EventEmitter<T extends ClassConstructor>(Base: T): {
|
|
|
362
381
|
* receive the true name of the event as the first argument).
|
|
363
382
|
*/
|
|
364
383
|
trigger(name: string, ...args: any[]): /*elided*/ any;
|
|
384
|
+
/**
|
|
385
|
+
* Subscribe to an event. Returns an unsubscribe function.
|
|
386
|
+
* Compatible with `useSyncExternalStore` and other store-style APIs.
|
|
387
|
+
*/
|
|
388
|
+
subscribe(event: string, callback: EventCallback, context?: EventContext): () => void;
|
|
365
389
|
};
|
|
366
390
|
} & T;
|
|
367
391
|
|
|
@@ -416,6 +440,11 @@ export declare const EventEmitterObject: {
|
|
|
416
440
|
* receive the true name of the event as the first argument).
|
|
417
441
|
*/
|
|
418
442
|
trigger(name: string, ...args: any[]): /*elided*/ any;
|
|
443
|
+
/**
|
|
444
|
+
* Subscribe to an event. Returns an unsubscribe function.
|
|
445
|
+
* Compatible with `useSyncExternalStore` and other store-style APIs.
|
|
446
|
+
*/
|
|
447
|
+
subscribe(event: string, callback: EventCallback, context?: EventContext): () => void;
|
|
419
448
|
};
|
|
420
449
|
} & ObjectConstructor;
|
|
421
450
|
|
|
@@ -442,11 +471,22 @@ export declare interface EventHandlersMap {
|
|
|
442
471
|
*/
|
|
443
472
|
export declare type EventListenerMap = Record<string, ListeningType>;
|
|
444
473
|
|
|
474
|
+
/**
|
|
475
|
+
* @public
|
|
476
|
+
*/
|
|
445
477
|
export declare type FetchOrCreateOptions = Options & {
|
|
446
478
|
promise?: boolean;
|
|
447
479
|
success?: (m: Model, resp: any, callbackOpts: Options) => void;
|
|
448
480
|
};
|
|
449
481
|
|
|
482
|
+
/**
|
|
483
|
+
* Return the PersistentStorage instance configured on `obj`, checking both
|
|
484
|
+
* the canonical `storage` accessor and the deprecated `browserStorage` alias
|
|
485
|
+
* so that subclasses that still override `get browserStorage()` continue to work.
|
|
486
|
+
* @public
|
|
487
|
+
*/
|
|
488
|
+
export declare function getStorage(obj: Model<any> | Collection<any> | null | undefined): PersistentStorage | undefined;
|
|
489
|
+
|
|
450
490
|
/**
|
|
451
491
|
* @public
|
|
452
492
|
*/
|
|
@@ -484,25 +524,6 @@ export declare interface ListeningType {
|
|
|
484
524
|
stop(name: string | EventCallbackMap, callback: EventCallback): void;
|
|
485
525
|
}
|
|
486
526
|
|
|
487
|
-
/**
|
|
488
|
-
* @public
|
|
489
|
-
*/
|
|
490
|
-
export declare interface LocalForageWithExtensions {
|
|
491
|
-
setItem(key: string, value: any): Promise<any>;
|
|
492
|
-
getItem(key: string): Promise<any>;
|
|
493
|
-
removeItem(key: string): Promise<void>;
|
|
494
|
-
clear(): Promise<void>;
|
|
495
|
-
length(): Promise<number>;
|
|
496
|
-
key(keyIndex: number): Promise<string>;
|
|
497
|
-
keys(): Promise<string[]>;
|
|
498
|
-
setItems?(items: Record<string, any>): Promise<void>;
|
|
499
|
-
getItems?(keys: string[]): Promise<Record<string, any>>;
|
|
500
|
-
debouncedSetItems?: {
|
|
501
|
-
(items: Record<string, any>): Promise<void>;
|
|
502
|
-
flush?: () => void;
|
|
503
|
-
};
|
|
504
|
-
}
|
|
505
|
-
|
|
506
527
|
/**
|
|
507
528
|
* @public
|
|
508
529
|
* **Models** are the basic data object in the framework --
|
|
@@ -511,25 +532,47 @@ export declare interface LocalForageWithExtensions {
|
|
|
511
532
|
* performing computations and transformations on that data.
|
|
512
533
|
*/
|
|
513
534
|
export declare class Model<T extends ModelAttributes = ModelAttributes> extends EventEmitterObject {
|
|
514
|
-
|
|
535
|
+
#private;
|
|
536
|
+
_storage?: PersistentStorage;
|
|
515
537
|
_changing: boolean;
|
|
516
538
|
_pending: boolean | ModelOptions;
|
|
517
539
|
_previousAttributes?: T;
|
|
518
540
|
_url: string;
|
|
519
541
|
_urlRoot: string;
|
|
542
|
+
attrs: T;
|
|
520
543
|
attributes: T;
|
|
521
544
|
changed: Partial<T>;
|
|
522
545
|
cid: string;
|
|
523
546
|
collection?: Collection;
|
|
524
547
|
id: string | number;
|
|
548
|
+
initialized?: Promise<void>;
|
|
525
549
|
validationError: string | number | null;
|
|
526
550
|
/**
|
|
527
551
|
* Create a new model with the specified attributes. A client id (`cid`)
|
|
528
552
|
* is automatically generated and assigned for you.
|
|
529
553
|
*/
|
|
530
554
|
constructor(attributes?: Partial<T>, options?: ModelOptions);
|
|
531
|
-
|
|
532
|
-
|
|
555
|
+
/**
|
|
556
|
+
* The canonical storage accessor. Set to a `PersistentStorage` instance
|
|
557
|
+
* to enable local persistence for this model.
|
|
558
|
+
*/
|
|
559
|
+
get storage(): PersistentStorage | undefined;
|
|
560
|
+
set storage(s: PersistentStorage);
|
|
561
|
+
/**
|
|
562
|
+
* @deprecated Use `storage` instead.
|
|
563
|
+
*/
|
|
564
|
+
get browserStorage(): PersistentStorage | undefined;
|
|
565
|
+
set browserStorage(s: PersistentStorage);
|
|
566
|
+
/**
|
|
567
|
+
* Override to enable automatic persistence. When true, any `set()` call
|
|
568
|
+
* that changes attributes will schedule a debounced save to `storage`, and
|
|
569
|
+
* the model will auto-hydrate from storage on construction.
|
|
570
|
+
*/
|
|
571
|
+
get autoSync(): boolean;
|
|
572
|
+
/**
|
|
573
|
+
* Debounce delay (ms) for auto-save writes. Override to tune.
|
|
574
|
+
*/
|
|
575
|
+
get autoSyncDelay(): number;
|
|
533
576
|
/**
|
|
534
577
|
* The default name for the JSON `id` attribute is `"id"`. MongoDB and
|
|
535
578
|
* CouchDB users may want to set this to `"_id"` (by overriding this getter
|
|
@@ -556,6 +599,15 @@ export declare class Model<T extends ModelAttributes = ModelAttributes> extends
|
|
|
556
599
|
* Return a hash of defaults for the model's attributes.
|
|
557
600
|
*/
|
|
558
601
|
defaults(): Partial<T>;
|
|
602
|
+
/**
|
|
603
|
+
* Declare computed properties. Override in subclasses to define properties
|
|
604
|
+
* that derive their value from other attributes. Each entry specifies the
|
|
605
|
+
* attribute keys it depends on (`deps`) and a function (`fn`) that receives
|
|
606
|
+
* the model and returns the computed value. The value is cached and
|
|
607
|
+
* recalculated automatically when any dep changes, firing a `change:key`
|
|
608
|
+
* event.
|
|
609
|
+
*/
|
|
610
|
+
get computed(): ComputedProperties<this>;
|
|
559
611
|
/**
|
|
560
612
|
* Return a copy of the model's `attributes` object.
|
|
561
613
|
*/
|
|
@@ -565,7 +617,7 @@ export declare class Model<T extends ModelAttributes = ModelAttributes> extends
|
|
|
565
617
|
*/
|
|
566
618
|
sync(method: SyncOperation, model: Model<any>, options: Options): any;
|
|
567
619
|
/**
|
|
568
|
-
* Get the value of an attribute.
|
|
620
|
+
* Get the value of an attribute, or a computed property.
|
|
569
621
|
*/
|
|
570
622
|
get<K extends keyof T>(attr: K): T[K];
|
|
571
623
|
keys(): string[];
|
|
@@ -663,6 +715,16 @@ export declare class Model<T extends ModelAttributes = ModelAttributes> extends
|
|
|
663
715
|
* Check if the model is currently in a valid state.
|
|
664
716
|
*/
|
|
665
717
|
isValid(options?: ModelOptions): boolean;
|
|
718
|
+
/**
|
|
719
|
+
* Subscribe to model changes. Returns an unsubscribe function.
|
|
720
|
+
*
|
|
721
|
+
* `model.subscribe(callback)` — fires on any attribute change, receives `(model, changed)`.
|
|
722
|
+
* `model.subscribe(event, callback)` — subscribe to a specific event (base EventEmitter form).
|
|
723
|
+
*
|
|
724
|
+
* Compatible with React's `useSyncExternalStore` and other store-style APIs.
|
|
725
|
+
*/
|
|
726
|
+
subscribe(event: string, callback: EventCallback, context?: unknown): () => void;
|
|
727
|
+
subscribe(callback: (model: this, changed: Partial<T>) => void): () => void;
|
|
666
728
|
/**
|
|
667
729
|
* Run validation against the next complete set of model attributes,
|
|
668
730
|
* returning `true` if all is well. Otherwise, fire an `"invalid"` event.
|
|
@@ -686,6 +748,10 @@ export declare type ModelOptions = Options & {
|
|
|
686
748
|
unset?: boolean;
|
|
687
749
|
silent?: boolean;
|
|
688
750
|
validate?: boolean;
|
|
751
|
+
/** Suppress the auto-save triggered by set() on autoSync models. */
|
|
752
|
+
noAutoSave?: boolean;
|
|
753
|
+
/** Mark a set() call as originating from a storage read; suppresses auto-save. */
|
|
754
|
+
fromStorage?: boolean;
|
|
689
755
|
};
|
|
690
756
|
|
|
691
757
|
/**
|
|
@@ -712,6 +778,50 @@ export declare type ObjectWithId = Record<string, any> & {
|
|
|
712
778
|
*/
|
|
713
779
|
export declare type Options = Record<string, any>;
|
|
714
780
|
|
|
781
|
+
/**
|
|
782
|
+
* @public
|
|
783
|
+
*/
|
|
784
|
+
export declare class PersistentStorage {
|
|
785
|
+
storeInitialized: Promise<void>;
|
|
786
|
+
storeReady: boolean;
|
|
787
|
+
store: StorageDriver;
|
|
788
|
+
name: string;
|
|
789
|
+
static sessionStorageInitialized: Promise<void>;
|
|
790
|
+
static localForage: typeof localForage;
|
|
791
|
+
/** Register a storage instance with a weak reference. @public */
|
|
792
|
+
static register(instance: PersistentStorage): void;
|
|
793
|
+
/**
|
|
794
|
+
* Flush the debounced write buffer of every registered PersistentStorage
|
|
795
|
+
* instance. Called automatically on pagehide/visibilitychange when autoSync
|
|
796
|
+
* is in use; can also be called manually.
|
|
797
|
+
* @public
|
|
798
|
+
*/
|
|
799
|
+
static flushAll(): void;
|
|
800
|
+
constructor(id: string, type: StoreType | StorageDriver, batchedWrites?: boolean);
|
|
801
|
+
/**
|
|
802
|
+
* @param type - The storage type: 'local', 'session', 'indexed', 'node', or 'in_memory'
|
|
803
|
+
* @param batchedWrites - Whether to enable batched writes
|
|
804
|
+
*/
|
|
805
|
+
initStore(type: StoreType, batchedWrites: boolean): Promise<void>;
|
|
806
|
+
flush(): void;
|
|
807
|
+
clear(): Promise<void>;
|
|
808
|
+
sync(): {
|
|
809
|
+
(method: SyncOperation, model: Model, options: SyncOptions): Promise<void>;
|
|
810
|
+
__name__: string;
|
|
811
|
+
};
|
|
812
|
+
removeCollectionReference(model: Model, collection: Collection | undefined): Promise<any> | undefined;
|
|
813
|
+
addCollectionReference(model: Model, collection: Collection | undefined): Promise<any> | undefined;
|
|
814
|
+
getCollectionReferenceData(model: Model): Record<string, string[]>;
|
|
815
|
+
save(model: Model): Promise<any>;
|
|
816
|
+
create(model: Model, options: SyncOptions): Promise<any>;
|
|
817
|
+
update(model: Model): Promise<any>;
|
|
818
|
+
find(model: Model): Promise<any>;
|
|
819
|
+
findAll(): Promise<any[]>;
|
|
820
|
+
destroy(model: Model, collection: Collection | undefined): Promise<Model>;
|
|
821
|
+
getStorageSize(): Promise<number>;
|
|
822
|
+
getItemName(id: string | number): string;
|
|
823
|
+
}
|
|
824
|
+
|
|
715
825
|
/**
|
|
716
826
|
* @public
|
|
717
827
|
*/
|
|
@@ -730,6 +840,32 @@ export declare interface SkeletorType {
|
|
|
730
840
|
noConflict?: () => SkeletorType;
|
|
731
841
|
}
|
|
732
842
|
|
|
843
|
+
/**
|
|
844
|
+
* Storage driver interface for key-value persistence.
|
|
845
|
+
* @public
|
|
846
|
+
*/
|
|
847
|
+
export declare interface StorageDriver {
|
|
848
|
+
ready(): Promise<void>;
|
|
849
|
+
setItem(key: string, value: any): Promise<any>;
|
|
850
|
+
getItem(key: string): Promise<any>;
|
|
851
|
+
removeItem(key: string): Promise<void>;
|
|
852
|
+
clear(): Promise<void>;
|
|
853
|
+
length(): Promise<number>;
|
|
854
|
+
key(keyIndex: number): Promise<string | null>;
|
|
855
|
+
keys(): Promise<string[]>;
|
|
856
|
+
setItems?(items: Record<string, any>): Promise<void>;
|
|
857
|
+
getItems?(keys: string[]): Promise<Record<string, any>>;
|
|
858
|
+
debouncedSetItems?: {
|
|
859
|
+
(items: Record<string, any>): Promise<void>;
|
|
860
|
+
flush?: () => void;
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
/**
|
|
865
|
+
* @public
|
|
866
|
+
*/
|
|
867
|
+
export declare type StoreType = 'local' | 'session' | 'indexed' | 'in_memory' | 'node';
|
|
868
|
+
|
|
733
869
|
/**
|
|
734
870
|
* Override this function to change the manner in which Backbone persists
|
|
735
871
|
* models to the server. You will be passed the type of request, and the
|
|
@@ -763,6 +899,7 @@ export declare interface SyncOptions {
|
|
|
763
899
|
error?: (error: any) => void;
|
|
764
900
|
xhr?: any;
|
|
765
901
|
wait?: boolean;
|
|
902
|
+
collection?: Collection;
|
|
766
903
|
}
|
|
767
904
|
|
|
768
905
|
export { }
|