@wordpress/preferences-persistence 2.57.0 → 3.0.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.
Files changed (132) hide show
  1. package/README.md +7 -7
  2. package/build/create/debounce-async.cjs +2 -1
  3. package/build/create/debounce-async.cjs.map +3 -3
  4. package/build/create/index.cjs +11 -21
  5. package/build/create/index.cjs.map +4 -4
  6. package/build/get-newest-data.cjs +53 -0
  7. package/build/get-newest-data.cjs.map +7 -0
  8. package/build/index.cjs +8 -16
  9. package/build/index.cjs.map +4 -4
  10. package/build/is-preferences-data.cjs +29 -0
  11. package/build/is-preferences-data.cjs.map +7 -0
  12. package/build/local-storage.cjs +46 -0
  13. package/build/local-storage.cjs.map +7 -0
  14. package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.cjs +2 -1
  15. package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.cjs.map +3 -3
  16. package/build/migrations/legacy-local-storage-data/index.cjs +7 -5
  17. package/build/migrations/legacy-local-storage-data/index.cjs.map +4 -4
  18. package/build/migrations/legacy-local-storage-data/move-feature-preferences.cjs +2 -1
  19. package/build/migrations/legacy-local-storage-data/move-feature-preferences.cjs.map +3 -3
  20. package/build/migrations/legacy-local-storage-data/move-individual-preference.cjs +2 -1
  21. package/build/migrations/legacy-local-storage-data/move-individual-preference.cjs.map +3 -3
  22. package/build/migrations/legacy-local-storage-data/move-interface-enable-items.cjs +2 -1
  23. package/build/migrations/legacy-local-storage-data/move-interface-enable-items.cjs.map +3 -3
  24. package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.cjs +2 -1
  25. package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.cjs.map +3 -3
  26. package/build/migrations/legacy-local-storage-data/types.cjs +19 -0
  27. package/build/migrations/legacy-local-storage-data/types.cjs.map +7 -0
  28. package/build/migrations/preferences-package-data/convert-complementary-areas.cjs +15 -11
  29. package/build/migrations/preferences-package-data/convert-complementary-areas.cjs.map +3 -3
  30. package/build/migrations/preferences-package-data/convert-editor-settings.cjs +30 -14
  31. package/build/migrations/preferences-package-data/convert-editor-settings.cjs.map +4 -4
  32. package/build/migrations/preferences-package-data/index.cjs +2 -1
  33. package/build/migrations/preferences-package-data/index.cjs.map +3 -3
  34. package/build/types.cjs +19 -0
  35. package/build/types.cjs.map +7 -0
  36. package/build-module/create/debounce-async.mjs +1 -1
  37. package/build-module/create/debounce-async.mjs.map +3 -3
  38. package/build-module/create/index.mjs +10 -21
  39. package/build-module/create/index.mjs.map +3 -3
  40. package/build-module/get-newest-data.mjs +22 -0
  41. package/build-module/get-newest-data.mjs.map +7 -0
  42. package/build-module/index.mjs +7 -16
  43. package/build-module/index.mjs.map +3 -3
  44. package/build-module/is-preferences-data.mjs +8 -0
  45. package/build-module/is-preferences-data.mjs.map +7 -0
  46. package/build-module/local-storage.mjs +20 -0
  47. package/build-module/local-storage.mjs.map +7 -0
  48. package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.mjs +1 -1
  49. package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.mjs.map +3 -3
  50. package/build-module/migrations/legacy-local-storage-data/index.mjs +6 -5
  51. package/build-module/migrations/legacy-local-storage-data/index.mjs.map +3 -3
  52. package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.mjs +1 -1
  53. package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.mjs.map +3 -3
  54. package/build-module/migrations/legacy-local-storage-data/move-individual-preference.mjs +1 -1
  55. package/build-module/migrations/legacy-local-storage-data/move-individual-preference.mjs.map +3 -3
  56. package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.mjs +1 -1
  57. package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.mjs.map +3 -3
  58. package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.mjs +1 -1
  59. package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.mjs.map +3 -3
  60. package/build-module/migrations/legacy-local-storage-data/types.mjs +1 -0
  61. package/build-module/migrations/legacy-local-storage-data/types.mjs.map +7 -0
  62. package/build-module/migrations/preferences-package-data/convert-complementary-areas.mjs +14 -11
  63. package/build-module/migrations/preferences-package-data/convert-complementary-areas.mjs.map +3 -3
  64. package/build-module/migrations/preferences-package-data/convert-editor-settings.mjs +19 -14
  65. package/build-module/migrations/preferences-package-data/convert-editor-settings.mjs.map +3 -3
  66. package/build-module/migrations/preferences-package-data/index.mjs +1 -1
  67. package/build-module/migrations/preferences-package-data/index.mjs.map +3 -3
  68. package/build-module/types.mjs +1 -0
  69. package/build-module/types.mjs.map +7 -0
  70. package/build-types/create/debounce-async.d.ts +19 -0
  71. package/build-types/create/debounce-async.d.ts.map +1 -0
  72. package/build-types/create/index.d.ts +20 -0
  73. package/build-types/create/index.d.ts.map +1 -0
  74. package/build-types/get-newest-data.d.ts +11 -0
  75. package/build-types/get-newest-data.d.ts.map +1 -0
  76. package/build-types/index.d.ts +20 -0
  77. package/build-types/index.d.ts.map +1 -0
  78. package/build-types/is-preferences-data.d.ts +11 -0
  79. package/build-types/is-preferences-data.d.ts.map +1 -0
  80. package/build-types/local-storage.d.ts +19 -0
  81. package/build-types/local-storage.d.ts.map +1 -0
  82. package/build-types/migrations/legacy-local-storage-data/convert-edit-post-panels.d.ts +35 -0
  83. package/build-types/migrations/legacy-local-storage-data/convert-edit-post-panels.d.ts.map +1 -0
  84. package/build-types/migrations/legacy-local-storage-data/index.d.ts +22 -0
  85. package/build-types/migrations/legacy-local-storage-data/index.d.ts.map +1 -0
  86. package/build-types/migrations/legacy-local-storage-data/move-feature-preferences.d.ts +60 -0
  87. package/build-types/migrations/legacy-local-storage-data/move-feature-preferences.d.ts.map +1 -0
  88. package/build-types/migrations/legacy-local-storage-data/move-individual-preference.d.ts +42 -0
  89. package/build-types/migrations/legacy-local-storage-data/move-individual-preference.d.ts.map +1 -0
  90. package/build-types/migrations/legacy-local-storage-data/move-interface-enable-items.d.ts +50 -0
  91. package/build-types/migrations/legacy-local-storage-data/move-interface-enable-items.d.ts.map +1 -0
  92. package/build-types/migrations/legacy-local-storage-data/move-third-party-feature-preferences.d.ts +44 -0
  93. package/build-types/migrations/legacy-local-storage-data/move-third-party-feature-preferences.d.ts.map +1 -0
  94. package/build-types/migrations/legacy-local-storage-data/types.d.ts +60 -0
  95. package/build-types/migrations/legacy-local-storage-data/types.d.ts.map +1 -0
  96. package/build-types/migrations/preferences-package-data/convert-complementary-areas.d.ts +5 -0
  97. package/build-types/migrations/preferences-package-data/convert-complementary-areas.d.ts.map +1 -0
  98. package/build-types/migrations/preferences-package-data/convert-editor-settings.d.ts +5 -0
  99. package/build-types/migrations/preferences-package-data/convert-editor-settings.d.ts.map +1 -0
  100. package/build-types/migrations/preferences-package-data/index.d.ts +5 -0
  101. package/build-types/migrations/preferences-package-data/index.d.ts.map +1 -0
  102. package/build-types/types.d.ts +40 -0
  103. package/build-types/types.d.ts.map +1 -0
  104. package/package.json +6 -6
  105. package/src/create/{debounce-async.js → debounce-async.ts} +13 -10
  106. package/src/create/index.ts +95 -0
  107. package/src/create/test/index.jsdom.test.js +45 -0
  108. package/src/get-newest-data.ts +50 -0
  109. package/src/index.ts +40 -0
  110. package/src/is-preferences-data.ts +15 -0
  111. package/src/local-storage.ts +30 -0
  112. package/src/migrations/legacy-local-storage-data/{convert-edit-post-panels.js → convert-edit-post-panels.ts} +13 -5
  113. package/src/migrations/legacy-local-storage-data/{index.js → index.ts} +22 -15
  114. package/src/migrations/legacy-local-storage-data/{move-feature-preferences.js → move-feature-preferences.ts} +13 -7
  115. package/src/migrations/legacy-local-storage-data/{move-individual-preference.js → move-individual-preference.ts} +15 -12
  116. package/src/migrations/legacy-local-storage-data/{move-interface-enable-items.js → move-interface-enable-items.ts} +6 -2
  117. package/src/migrations/legacy-local-storage-data/{move-third-party-feature-preferences.js → move-third-party-feature-preferences.ts} +8 -4
  118. package/src/migrations/legacy-local-storage-data/test/index.js +6 -0
  119. package/src/migrations/legacy-local-storage-data/types.ts +66 -0
  120. package/src/migrations/preferences-package-data/convert-complementary-areas.ts +21 -0
  121. package/src/migrations/preferences-package-data/convert-editor-settings.ts +58 -0
  122. package/src/migrations/preferences-package-data/{index.js → index.ts} +4 -1
  123. package/src/migrations/preferences-package-data/test/convert-complementary-areas.js +10 -0
  124. package/src/migrations/preferences-package-data/test/convert-editor-settings.js +35 -0
  125. package/src/test/index.jsdom.test.js +95 -0
  126. package/src/test/local-storage.jsdom.test.js +50 -0
  127. package/src/types.ts +43 -0
  128. package/CHANGELOG.md +0 -228
  129. package/src/create/index.js +0 -105
  130. package/src/index.js +0 -47
  131. package/src/migrations/preferences-package-data/convert-complementary-areas.js +0 -16
  132. package/src/migrations/preferences-package-data/convert-editor-settings.js +0 -48
@@ -0,0 +1,22 @@
1
+ import type { ScopedPreferences } from '../../types';
2
+ import type { LegacyData } from './types';
3
+ /**
4
+ * Converts data from the old `@wordpress/data` package format.
5
+ *
6
+ * @param data The legacy data in its original format.
7
+ *
8
+ * @return The converted data or `undefined` if there was
9
+ * nothing to convert.
10
+ */
11
+ export declare function convertLegacyData(data: LegacyData | null | undefined): ScopedPreferences | undefined;
12
+ /**
13
+ * Gets the legacy local storage data for the given user and returns the
14
+ * data converted to the new format.
15
+ *
16
+ * @param userId The user id.
17
+ *
18
+ * @return The converted data or undefined if no local
19
+ * storage data could be found.
20
+ */
21
+ export default function convertLegacyLocalStorageData(userId: string | number): ScopedPreferences | undefined;
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/migrations/legacy-local-storage-data/index.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAa1C;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAChC,IAAI,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,GACjC,iBAAiB,GAAG,SAAS,CA0D/B;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,6BAA6B,CACpD,MAAM,EAAE,MAAM,GAAG,MAAM,GACrB,iBAAiB,GAAG,SAAS,CAG/B"}
@@ -0,0 +1,60 @@
1
+ import type { LegacyData } from './types';
2
+ /**
3
+ * Move the 'features' object in local storage from the sourceStoreName to the
4
+ * preferences store data structure.
5
+ *
6
+ * Previously, editors used a data structure like this for feature preferences:
7
+ * ```js
8
+ * {
9
+ * 'core/edit-post': {
10
+ * preferences: {
11
+ * features; {
12
+ * topToolbar: true,
13
+ * // ... other boolean 'feature' preferences
14
+ * },
15
+ * },
16
+ * },
17
+ * }
18
+ * ```
19
+ *
20
+ * And for a while these feature preferences lived in the interface package:
21
+ * ```js
22
+ * {
23
+ * 'core/interface': {
24
+ * preferences: {
25
+ * features: {
26
+ * 'core/edit-post': {
27
+ * topToolbar: true
28
+ * }
29
+ * }
30
+ * }
31
+ * }
32
+ * }
33
+ * ```
34
+ *
35
+ * In the preferences store, 'features' aren't considered special, they're
36
+ * merged to the root level of the scope along with other preferences:
37
+ * ```js
38
+ * {
39
+ * 'core/preferences': {
40
+ * preferences: {
41
+ * 'core/edit-post': {
42
+ * topToolbar: true,
43
+ * // ... any other preferences.
44
+ * }
45
+ * }
46
+ * }
47
+ * }
48
+ * ```
49
+ *
50
+ * This function handles moving from either the source store or the interface
51
+ * store to the preferences data structure.
52
+ *
53
+ * @param state The state before migration.
54
+ * @param sourceStoreName The name of the store that has persisted
55
+ * preferences to migrate to the preferences
56
+ * package.
57
+ * @return The migrated state
58
+ */
59
+ export default function moveFeaturePreferences(state: LegacyData, sourceStoreName: string): LegacyData;
60
+ //# sourceMappingURL=move-feature-preferences.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"move-feature-preferences.d.ts","sourceRoot":"","sources":["../../../src/migrations/legacy-local-storage-data/move-feature-preferences.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAsB,UAAU,EAAE,MAAM,SAAS,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAC7C,KAAK,EAAE,UAAU,EACjB,eAAe,EAAE,MAAM,GACrB,UAAU,CA8EZ"}
@@ -0,0 +1,42 @@
1
+ import type { ScopePreferences } from '../../types';
2
+ import type { LegacyData } from './types';
3
+ /**
4
+ * Migrates an individual item inside the `preferences` object for a package's store.
5
+ *
6
+ * Previously, some packages had individual 'preferences' of any data type, and many used
7
+ * complex nested data structures. For example:
8
+ * ```js
9
+ * {
10
+ * 'core/edit-post': {
11
+ * preferences: {
12
+ * panels: {
13
+ * publish: {
14
+ * opened: true,
15
+ * enabled: true,
16
+ * }
17
+ * },
18
+ * // ...other preferences.
19
+ * },
20
+ * },
21
+ * }
22
+ *
23
+ * This function supports moving an individual preference like 'panels' above into the
24
+ * preferences package data structure.
25
+ *
26
+ * It supports moving a preference to a particular scope in the preferences store and
27
+ * optionally converting the data using a `convert` function.
28
+ *
29
+ * ```
30
+ *
31
+ * @param state The original state.
32
+ * @param migrate An options object that contains details of the migration.
33
+ * @param migrate.from The name of the store to migrate from.
34
+ * @param migrate.to The scope in the preferences store to migrate to.
35
+ * @param key The key in the preferences object to migrate.
36
+ * @param convert A function that converts preferences from one format to another.
37
+ */
38
+ export default function moveIndividualPreferenceToPreferences(state: LegacyData, { from: sourceStoreName, to: scope }: {
39
+ from: string;
40
+ to: string;
41
+ }, key: string, convert?: (preferences: ScopePreferences) => object): LegacyData;
42
+ //# sourceMappingURL=move-individual-preference.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"move-individual-preference.d.ts","sourceRoot":"","sources":["../../../src/migrations/legacy-local-storage-data/move-individual-preference.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAI1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,CAAC,OAAO,UAAU,qCAAqC,CAC5D,KAAK,EAAE,UAAU,EACjB,EAAE,IAAI,EAAE,eAAe,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,EAClE,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,CAAE,WAAW,EAAE,gBAAgB,KAAM,MAAiB,GAC7D,UAAU,CA+CZ"}
@@ -0,0 +1,50 @@
1
+ import type { LegacyData } from './types';
2
+ /**
3
+ * Migrates interface 'enableItems' data to the preferences store.
4
+ *
5
+ * The interface package stores this data in this format:
6
+ * ```js
7
+ * {
8
+ * enableItems: {
9
+ * singleEnableItems: {
10
+ * complementaryArea: {
11
+ * 'core/edit-post': 'edit-post/document',
12
+ * 'core/edit-site': 'edit-site/global-styles',
13
+ * }
14
+ * },
15
+ * multipleEnableItems: {
16
+ * pinnedItems: {
17
+ * 'core/edit-post': {
18
+ * 'plugin-1': true,
19
+ * },
20
+ * 'core/edit-site': {
21
+ * 'plugin-2': true,
22
+ * },
23
+ * },
24
+ * }
25
+ * }
26
+ * }
27
+ * ```
28
+ *
29
+ * and it should be converted it to:
30
+ * ```js
31
+ * {
32
+ * 'core/edit-post': {
33
+ * complementaryArea: 'edit-post/document',
34
+ * pinnedItems: {
35
+ * 'plugin-1': true,
36
+ * },
37
+ * },
38
+ * 'core/edit-site': {
39
+ * complementaryArea: 'edit-site/global-styles',
40
+ * pinnedItems: {
41
+ * 'plugin-2': true,
42
+ * },
43
+ * },
44
+ * }
45
+ * ```
46
+ *
47
+ * @param state The local storage state.
48
+ */
49
+ export default function moveInterfaceEnableItems(state: LegacyData): LegacyData;
50
+ //# sourceMappingURL=move-interface-enable-items.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"move-interface-enable-items.d.ts","sourceRoot":"","sources":["../../../src/migrations/legacy-local-storage-data/move-interface-enable-items.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,MAAM,CAAC,OAAO,UAAU,wBAAwB,CAC/C,KAAK,EAAE,UAAU,GACf,UAAU,CAwEZ"}
@@ -0,0 +1,44 @@
1
+ import type { LegacyData } from './types';
2
+ /**
3
+ * The interface package previously had a public API that could be used by
4
+ * plugins to set persisted boolean 'feature' preferences.
5
+ *
6
+ * While usage was likely non-existent or very small, this function ensures
7
+ * those are migrated to the preferences data structure. The interface
8
+ * package's APIs have now been deprecated and use the preferences store.
9
+ *
10
+ * This will convert data that looks like this:
11
+ * ```js
12
+ * {
13
+ * 'core/interface': {
14
+ * preferences: {
15
+ * features: {
16
+ * 'my-plugin': {
17
+ * myPluginFeature: true
18
+ * }
19
+ * }
20
+ * }
21
+ * }
22
+ * }
23
+ * ```
24
+ *
25
+ * To this:
26
+ * ```js
27
+ * * {
28
+ * 'core/preferences': {
29
+ * preferences: {
30
+ * 'my-plugin': {
31
+ * myPluginFeature: true
32
+ * }
33
+ * }
34
+ * }
35
+ * }
36
+ * ```
37
+ *
38
+ * @param state The local storage state
39
+ *
40
+ * @return The state with third party preferences moved to the
41
+ * preferences data structure.
42
+ */
43
+ export default function moveThirdPartyFeaturePreferencesToPreferences(state: LegacyData): LegacyData;
44
+ //# sourceMappingURL=move-third-party-feature-preferences.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"move-third-party-feature-preferences.d.ts","sourceRoot":"","sources":["../../../src/migrations/legacy-local-storage-data/move-third-party-feature-preferences.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,CAAC,OAAO,UAAU,6CAA6C,CACpE,KAAK,EAAE,UAAU,GACf,UAAU,CAwDZ"}
@@ -0,0 +1,60 @@
1
+ import type { ScopedPreferences } from '../../types';
2
+ /**
3
+ * Boolean 'feature' preferences, keyed by feature name.
4
+ */
5
+ export type FeaturePreferences = Record<string, boolean | undefined>;
6
+ /**
7
+ * Persisted state of a package's store, such as `core/edit-post`.
8
+ */
9
+ export interface LegacyStoreState {
10
+ preferences?: Record<string, unknown>;
11
+ [key: string]: unknown;
12
+ }
13
+ /**
14
+ * Persisted state of the `core/interface` store.
15
+ */
16
+ export interface LegacyInterfaceStoreState {
17
+ preferences?: {
18
+ features?: Record<string, FeaturePreferences | undefined>;
19
+ [key: string]: unknown;
20
+ };
21
+ enableItems?: {
22
+ singleEnableItems?: {
23
+ complementaryArea?: Record<string, string>;
24
+ };
25
+ multipleEnableItems?: {
26
+ pinnedItems?: Record<string, Record<string, boolean>>;
27
+ };
28
+ };
29
+ [key: string]: unknown;
30
+ }
31
+ /**
32
+ * Persisted state of the `core/preferences` store.
33
+ */
34
+ export interface LegacyPreferencesStoreState {
35
+ preferences?: ScopedPreferences;
36
+ [key: string]: unknown;
37
+ }
38
+ /**
39
+ * Local storage data in the legacy `@wordpress/data` format, keyed by store name.
40
+ */
41
+ export interface LegacyData {
42
+ 'core/interface'?: LegacyInterfaceStoreState;
43
+ 'core/preferences'?: LegacyPreferencesStoreState;
44
+ [storeName: string]: LegacyStoreState | undefined;
45
+ }
46
+ /**
47
+ * The post editor's panels state in the legacy format.
48
+ */
49
+ export interface LegacyEditPostPanel {
50
+ enabled?: boolean;
51
+ opened?: boolean;
52
+ }
53
+ /**
54
+ * The post editor's panels state in the preferences format.
55
+ */
56
+ export interface EditPostPanels {
57
+ inactivePanels: string[];
58
+ openPanels: string[];
59
+ }
60
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/migrations/legacy-local-storage-data/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAE,MAAM,EAAE,OAAO,GAAG,SAAS,CAAE,CAAC;AAEvE;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC,WAAW,CAAC,EAAE,MAAM,CAAE,MAAM,EAAE,OAAO,CAAE,CAAC;IACxC,CAAE,GAAG,EAAE,MAAM,GAAI,OAAO,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACzC,WAAW,CAAC,EAAE;QACb,QAAQ,CAAC,EAAE,MAAM,CAAE,MAAM,EAAE,kBAAkB,GAAG,SAAS,CAAE,CAAC;QAC5D,CAAE,GAAG,EAAE,MAAM,GAAI,OAAO,CAAC;KACzB,CAAC;IACF,WAAW,CAAC,EAAE;QACb,iBAAiB,CAAC,EAAE;YACnB,iBAAiB,CAAC,EAAE,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,CAAC;SAC7C,CAAC;QACF,mBAAmB,CAAC,EAAE;YACrB,WAAW,CAAC,EAAE,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,MAAM,EAAE,OAAO,CAAE,CAAE,CAAC;SAC1D,CAAC;KACF,CAAC;IACF,CAAE,GAAG,EAAE,MAAM,GAAI,OAAO,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC3C,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAChC,CAAE,GAAG,EAAE,MAAM,GAAI,OAAO,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IAC1B,gBAAgB,CAAC,EAAE,yBAAyB,CAAC;IAC7C,kBAAkB,CAAC,EAAE,2BAA2B,CAAC;IACjD,CAAE,SAAS,EAAE,MAAM,GAAI,gBAAgB,GAAG,SAAS,CAAC;CACpD;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IACnC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC9B,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,UAAU,EAAE,MAAM,EAAE,CAAC;CACrB"}
@@ -0,0 +1,5 @@
1
+ import type { ScopedPreferences } from '../../types';
2
+ export default function convertComplementaryAreas(state: ScopedPreferences): {
3
+ [x: string]: import("../..").ScopePreferences | undefined;
4
+ };
5
+ //# sourceMappingURL=convert-complementary-areas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert-complementary-areas.d.ts","sourceRoot":"","sources":["../../../src/migrations/preferences-package-data/convert-complementary-areas.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAE,KAAK,EAAE,iBAAiB;;EAkB1E"}
@@ -0,0 +1,5 @@
1
+ import type { ScopedPreferences } from '../../types';
2
+ export default function convertEditorSettings(data: ScopedPreferences): {
3
+ [x: string]: import("../..").ScopePreferences | undefined;
4
+ };
5
+ //# sourceMappingURL=convert-editor-settings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert-editor-settings.d.ts","sourceRoot":"","sources":["../../../src/migrations/preferences-package-data/convert-editor-settings.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAKrD,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAE,IAAI,EAAE,iBAAiB;;EAmDrE"}
@@ -0,0 +1,5 @@
1
+ import type { ScopedPreferences } from '../../types';
2
+ export default function convertPreferencesPackageData(data: ScopedPreferences): {
3
+ [x: string]: import("../..").ScopePreferences | undefined;
4
+ };
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/migrations/preferences-package-data/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD,MAAM,CAAC,OAAO,UAAU,6BAA6B,CACpD,IAAI,EAAE,iBAAiB;;EAKvB"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Preferences of a single scope, such as `core/edit-post`.
3
+ */
4
+ export type ScopePreferences = Record<string, unknown>;
5
+ /**
6
+ * Preferences store data, keyed by scope.
7
+ */
8
+ export type ScopedPreferences = Record<string, ScopePreferences | undefined>;
9
+ /**
10
+ * Persisted preferences data, keyed by scope.
11
+ */
12
+ export interface PreferencesData {
13
+ /**
14
+ * When the data was last modified, as an ISO 8601 date string.
15
+ */
16
+ _modified?: string;
17
+ [scope: string]: unknown;
18
+ }
19
+ export interface CreateOptions {
20
+ /**
21
+ * Any persisted preferences data that should be preloaded.
22
+ * When set, the persistence layer will avoid fetching data from the REST API.
23
+ */
24
+ preloadedData?: PreferencesData | null;
25
+ /**
26
+ * The key to use for restoring the localStorage backup, used when the
27
+ * persistence layer calls `localStorage.getItem` or `localStorage.setItem`.
28
+ */
29
+ localStorageRestoreKey?: string;
30
+ /**
31
+ * Debounce requests to the API so that they only occur at minimum every
32
+ * `requestDebounceMS` milliseconds, and don't swamp the server. Defaults to 2500ms.
33
+ */
34
+ requestDebounceMS?: number;
35
+ }
36
+ export interface PersistenceLayer {
37
+ get: () => Promise<PreferencesData>;
38
+ set: (newData: PreferencesData) => void;
39
+ }
40
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAE,MAAM,EAAE,OAAO,CAAE,CAAC;AAEzD;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAE,MAAM,EAAE,gBAAgB,GAAG,SAAS,CAAE,CAAC;AAE/E;;GAEG;AACH,MAAM,WAAW,eAAe;IAC/B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,CAAE,KAAK,EAAE,MAAM,GAAI,OAAO,CAAC;CAC3B;AAED,MAAM,WAAW,aAAa;IAC7B;;;OAGG;IACH,aAAa,CAAC,EAAE,eAAe,GAAG,IAAI,CAAC;IACvC;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,gBAAgB;IAChC,GAAG,EAAE,MAAM,OAAO,CAAE,eAAe,CAAE,CAAC;IACtC,GAAG,EAAE,CAAE,OAAO,EAAE,eAAe,KAAM,IAAI,CAAC;CAC1C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/preferences-persistence",
3
- "version": "2.57.0",
3
+ "version": "3.0.0",
4
4
  "description": "Persistence utilities for `wordpress/preferences`.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -20,14 +20,13 @@
20
20
  "url": "https://github.com/WordPress/gutenberg/issues"
21
21
  },
22
22
  "engines": {
23
- "node": ">=18.12.0"
23
+ "node": ">=22.13.0"
24
24
  },
25
25
  "files": [
26
26
  "src",
27
27
  "build",
28
28
  "build-module",
29
- "build-types",
30
- "*.md"
29
+ "build-types"
31
30
  ],
32
31
  "main": "build/index.cjs",
33
32
  "module": "build-module/index.mjs",
@@ -40,9 +39,10 @@
40
39
  "./package.json": "./package.json"
41
40
  },
42
41
  "wpScript": true,
42
+ "types": "build-types",
43
43
  "sideEffects": false,
44
44
  "dependencies": {
45
- "@wordpress/api-fetch": "^7.57.0"
45
+ "@wordpress/api-fetch": "^8.0.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "vitest": "^5.0.3"
@@ -50,5 +50,5 @@
50
50
  "publishConfig": {
51
51
  "access": "public"
52
52
  },
53
- "gitHead": "12441e2520c78b2254a792a8cba40c34110be060"
53
+ "gitHead": "8d30f92cbbde3ddf781c8799443c649befbbcb88"
54
54
  }
@@ -9,21 +9,24 @@
9
9
  * This is distinct from `{ debounce } from @wordpress/compose` in that it
10
10
  * waits for promise resolution.
11
11
  *
12
- * @param {Function} func A function that returns a promise.
13
- * @param {number} delayMS A delay in milliseconds.
12
+ * @param func A function that returns a promise.
13
+ * @param delayMS A delay in milliseconds.
14
14
  *
15
- * @return {Function} A function that debounce whatever function is passed
16
- * to it.
15
+ * @return A function that debounce whatever function is passed
16
+ * to it.
17
17
  */
18
- export default function debounceAsync( func, delayMS ) {
19
- let timeoutId;
20
- let activePromise;
18
+ export default function debounceAsync< Args extends unknown[], Result >(
19
+ func: ( ...args: Args ) => Promise< Result >,
20
+ delayMS: number
21
+ ): ( ...args: Args ) => Promise< Result > {
22
+ let timeoutId: ReturnType< typeof setTimeout > | null | undefined;
23
+ let activePromise: Promise< void > | null | undefined;
21
24
 
22
- return async function debounced( ...args ) {
25
+ return async function debounced( ...args: Args ) {
23
26
  // This is a leading edge debounce. If there's no promise or timeout
24
27
  // in progress, call the debounced function immediately.
25
28
  if ( ! activePromise && ! timeoutId ) {
26
- return new Promise( ( resolve, reject ) => {
29
+ return new Promise< Result >( ( resolve, reject ) => {
27
30
  // Keep a reference to the promise.
28
31
  activePromise = func( ...args )
29
32
  .then( ( ...thenArgs ) => {
@@ -53,7 +56,7 @@ export default function debounceAsync( func, delayMS ) {
53
56
  }
54
57
 
55
58
  // Trigger any trailing edge calls to the function.
56
- return new Promise( ( resolve, reject ) => {
59
+ return new Promise< Result >( ( resolve, reject ) => {
57
60
  // Schedule the next request but with a delay.
58
61
  timeoutId = setTimeout( () => {
59
62
  activePromise = func( ...args )
@@ -0,0 +1,95 @@
1
+ import apiFetch from '@wordpress/api-fetch';
2
+ import debounceAsync from './debounce-async';
3
+ import getNewestData from '../get-newest-data';
4
+ import isPreferencesData from '../is-preferences-data';
5
+ import { writeStoredJSON } from '../local-storage';
6
+ import type {
7
+ CreateOptions,
8
+ PersistenceLayer,
9
+ PreferencesData,
10
+ } from '../types';
11
+
12
+ /**
13
+ * Creates a persistence layer that stores data in WordPress user meta via the
14
+ * REST API.
15
+ *
16
+ * @param {CreateOptions} [options]
17
+ * @param {PreferencesData|null} [options.preloadedData] Any persisted preferences data that should be preloaded.
18
+ * When set, the persistence layer will avoid fetching data
19
+ * from the REST API.
20
+ * @param {string} [options.localStorageRestoreKey] The key to use for restoring the localStorage backup, used
21
+ * when the persistence layer calls `localStorage.getItem` or
22
+ * `localStorage.setItem`.
23
+ * @param {number} [options.requestDebounceMS] Debounce requests to the API so that they only occur at
24
+ * minimum every `requestDebounceMS` milliseconds, and don't
25
+ * swamp the server. Defaults to 2500ms.
26
+ *
27
+ * @return A persistence layer for WordPress user meta.
28
+ */
29
+ export default function create( {
30
+ preloadedData,
31
+ localStorageRestoreKey = 'WP_PREFERENCES_RESTORE_DATA',
32
+ requestDebounceMS = 2500,
33
+ }: CreateOptions = {} ): PersistenceLayer {
34
+ let cache = isPreferencesData( preloadedData ) ? preloadedData : undefined;
35
+ const debouncedApiFetch = debounceAsync( apiFetch, requestDebounceMS );
36
+
37
+ async function get() {
38
+ if ( cache ) {
39
+ return cache;
40
+ }
41
+
42
+ const user = await apiFetch< {
43
+ meta?: { persisted_preferences?: unknown };
44
+ } >( {
45
+ path: '/wp/v2/users/me?context=edit',
46
+ } );
47
+
48
+ cache =
49
+ getNewestData(
50
+ user?.meta?.persisted_preferences,
51
+ localStorageRestoreKey
52
+ ) ?? {};
53
+
54
+ return cache;
55
+ }
56
+
57
+ function set( newData: PreferencesData ) {
58
+ const dataWithTimestamp = {
59
+ ...newData,
60
+ _modified: new Date().toISOString(),
61
+ };
62
+ cache = dataWithTimestamp;
63
+
64
+ // Store data in local storage as a fallback. If for some reason the
65
+ // api request does not complete or becomes unavailable, this data
66
+ // can be used to restore preferences.
67
+ writeStoredJSON( localStorageRestoreKey, dataWithTimestamp );
68
+
69
+ // The user meta endpoint seems susceptible to errors when consecutive
70
+ // requests are made in quick succession. Ensure there's a gap between
71
+ // any consecutive requests.
72
+ //
73
+ // Catch and do nothing with errors from the REST API.
74
+ debouncedApiFetch( {
75
+ path: '/wp/v2/users/me',
76
+ method: 'PUT',
77
+ // `keepalive` will still send the request in the background,
78
+ // even when a browser unload event might interrupt it.
79
+ // This should hopefully make things more resilient.
80
+ // This does have a size limit of 64kb, but the data is usually
81
+ // much less.
82
+ keepalive: true,
83
+ data: {
84
+ meta: {
85
+ persisted_preferences: dataWithTimestamp,
86
+ },
87
+ },
88
+ } ).catch( () => {} );
89
+ }
90
+
91
+ return {
92
+ get,
93
+ set,
94
+ };
95
+ }
@@ -12,6 +12,7 @@ describe( 'create', () => {
12
12
  afterEach( () => {
13
13
  mockedApiFetch.mockReset();
14
14
  vi.restoreAllMocks();
15
+ window.localStorage.clear();
15
16
  } );
16
17
 
17
18
  describe( 'set', () => {
@@ -161,5 +162,49 @@ describe( 'create', () => {
161
162
  const { get } = create();
162
163
  expect( await get() ).toEqual( {} );
163
164
  } );
165
+
166
+ it( 'returns a new empty object for each persistence layer', async () => {
167
+ mockedApiFetch.mockResolvedValue( {
168
+ meta: { persisted_preferences: null },
169
+ } );
170
+
171
+ const first = await create().get();
172
+ first.core = { fixedToolbar: true };
173
+ expect( await create().get() ).toEqual( {} );
174
+ } );
175
+
176
+ it( 'fetches when the preloaded data is not an object', async () => {
177
+ mockedApiFetch.mockResolvedValueOnce( {
178
+ meta: {
179
+ persisted_preferences: { core: { fixedToolbar: true } },
180
+ },
181
+ } );
182
+
183
+ const { get } = create( { preloadedData: [] } );
184
+ expect( await get() ).toEqual( { core: { fixedToolbar: true } } );
185
+ expect( mockedApiFetch ).toHaveBeenCalled();
186
+ } );
187
+
188
+ it( 'migrates data from the REST API', async () => {
189
+ mockedApiFetch.mockResolvedValueOnce( {
190
+ meta: {
191
+ persisted_preferences: {
192
+ 'core/edit-post': { fixedToolbar: true },
193
+ },
194
+ },
195
+ } );
196
+
197
+ const { get } = create();
198
+ expect( await get() ).toEqual( { core: { fixedToolbar: true } } );
199
+ } );
200
+
201
+ it( 'ignores an empty array from the REST API', async () => {
202
+ mockedApiFetch.mockResolvedValueOnce( {
203
+ meta: { persisted_preferences: [] },
204
+ } );
205
+
206
+ const { get } = create();
207
+ expect( await get() ).toEqual( {} );
208
+ } );
164
209
  } );
165
210
  } );