From bd9ba413cee207338de17cd7d2141debff527074 Mon Sep 17 00:00:00 2001 From: David Arenas Date: Wed, 7 Aug 2024 10:27:06 +0200 Subject: [PATCH] Interactivity API: Refactor internal proxy and signals system (#62734) * WIP * More WIP * Restore previous packages * Fix current tests * Add computation tests * Fix getter call * Fix for..in on new properties * Refactor code a little * Add tests for getters with scope * Move namespaces to properties * Fix a problem with object references * Add store handlers * Add signals-core dependency * Rename Property to PropSignal * Move withScope inside PropSignal logic * Create proxies folder * Attempt to make the context work * Reorganize code * Make peek() return only the value inside signalValue * A bit of refactoring * Return the computed value with `peek()` * Rename DEFAULT_SCOPE to NO_SCOPE * Remove deepsignal * Handle functions inside state * Fix withScope in this PR * Fix context proxification * Move store root logic to store proxy handlers * Move store initialization inside init * Add TODO comment inside `peek()` * Fix store root assignments * Fix lint error * Enable skipped test for non-initialized getters * Rename get(State|Store)Proxy to proxify(State|Store) * Make `getContext` throw when there is no scope * Fix `proxifyState` types * Rename some variables in tests * Add test for object reference keeping * Rename test suite for state proxy * Add tests for getters and functions with scope * Add tests for prop subscription inside functions * Allow functions to use `this` * Minor fixes * Add tests to store proxies * Throw an error when an object cannot be proxified * Change peek implementation * Add tests for peek and unsupported structures * Test peeking getters that access scope or other namespaces * Ignore well-known symbols * Minor comment format fix * Move namespace arg to first position in proxify functions * chore: Update jest.config.js to stop ignoring deepsignal because we removed it as dependency * Add comments to proxy registry * Remove namespace from PropSignal * Remove unused `peekValueSignal` method * Simplify PropSignal methods * Add TSDocs to PropSignal * Disable unused vars lint rule in state-proxy tests * Remove descriptor alias * Expand `getProxyNs` docs * Add more comments in `state` * Refactor state functions and add tsdocs * Fix some grammar issues * Fix default namespace for setters * Replace `isNotRoot` with `isRoot` * Update comments in store.ts * Move `isPlainObject` to utils * Remove remaining deepSignal references * Delete unnecessary @ts-ignore-next-line * Use `isPlainObject` from utils inside store * Move scopes and namespaces to separate files * Call `init` outside `DOMContentLoaded` * Replace `signals-core` imports with `signals` * Rename `proxiedStore` to `proxifiedStore` Co-authored-by: Luis Herranz * Remove unnecessary `peek()` call * Throw more descriptive errors from getContext and getElement * Use more descriptive name for proxy functions * Use destructured `get` inside `setGetter` call * Add comment to explain `objToIterable` signals subscription Co-authored-by: Luis Herranz * Change line comment to block comment * Replace `?` with `!` operator * Wrap Interactivity API tests with appropriate `describe` * Remove unnecessary `setNamespace` calls in tests * Remove duplicated test * Fix typo * Add extra tests for getter modification * Test the right namespace is used inside getters * Fix test name * Check if length's PropSignal exists before updating its value * Add deepMerge tests and prevent server overwritting * Make sure context inheritance is shallow and server props don't overwrite * Refactor wp-each to use new proxifyContext structure --------- Co-authored-by: DAreRodz Co-authored-by: michalczaplinski Co-authored-by: luisherranz Co-authored-by: gziolo Co-authored-by: otakupahp Co-authored-by: sirreal --- package-lock.json | 32 - .../directive-each/render.php | 4 +- .../interactive-blocks/directive-each/view.js | 10 +- .../directive-priorities/view.js | 10 +- packages/interactivity-router/src/index.ts | 8 +- packages/interactivity/package.json | 1 - packages/interactivity/src/directives.tsx | 75 +- packages/interactivity/src/hooks.tsx | 93 +- packages/interactivity/src/index.ts | 21 +- packages/interactivity/src/namespaces.ts | 10 + packages/interactivity/src/proxies/index.ts | 5 + .../interactivity/src/proxies/registry.ts | 82 ++ packages/interactivity/src/proxies/signals.ts | 143 ++ packages/interactivity/src/proxies/state.ts | 250 ++++ packages/interactivity/src/proxies/store.ts | 79 + .../src/proxies/test/state-proxy.ts | 1269 +++++++++++++++++ .../src/proxies/test/store-proxy.ts | 123 ++ packages/interactivity/src/scopes.ts | 98 ++ packages/interactivity/src/store.ts | 192 +-- packages/interactivity/src/test/utils.ts | 337 ++++- packages/interactivity/src/utils.ts | 67 +- .../interactivity/deferred-store.spec.ts | 4 +- .../interactivity/directive-context.spec.ts | 50 +- .../interactivity/directive-each.spec.ts | 9 +- .../interactivity/router-navigate.spec.ts | 31 +- test/unit/jest.config.js | 2 +- 26 files changed, 2571 insertions(+), 434 deletions(-) create mode 100644 packages/interactivity/src/namespaces.ts create mode 100644 packages/interactivity/src/proxies/index.ts create mode 100644 packages/interactivity/src/proxies/registry.ts create mode 100644 packages/interactivity/src/proxies/signals.ts create mode 100644 packages/interactivity/src/proxies/state.ts create mode 100644 packages/interactivity/src/proxies/store.ts create mode 100644 packages/interactivity/src/proxies/test/state-proxy.ts create mode 100644 packages/interactivity/src/proxies/test/store-proxy.ts create mode 100644 packages/interactivity/src/scopes.ts diff --git a/package-lock.json b/package-lock.json index 23f504d7cbf46..e2cab9e63002d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53739,7 +53739,6 @@ "license": "GPL-2.0-or-later", "dependencies": { "@preact/signals": "^1.2.2", - "deepsignal": "^1.4.0", "preact": "^10.19.3" }, "engines": { @@ -53774,31 +53773,6 @@ "preact": "10.x" } }, - "packages/interactivity/node_modules/deepsignal": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/deepsignal/-/deepsignal-1.4.0.tgz", - "integrity": "sha512-x0XUMT48s+xQRLc2fPFfxnYLCJ46vffw47OQ5NcHFzacOjfW5eA0NrEmI0bhQHL6MgUHkBVT4TIiWTVwzTEwpg==", - "peerDependencies": { - "@preact/signals": "^1.1.4", - "@preact/signals-core": "^1.5.1", - "@preact/signals-react": "^1.3.8 || ^2.0.0", - "preact": "^10.16.0" - }, - "peerDependenciesMeta": { - "@preact/signals": { - "optional": true - }, - "@preact/signals-core": { - "optional": true - }, - "@preact/signals-react": { - "optional": true - }, - "preact": { - "optional": true - } - } - }, "packages/interactivity/node_modules/preact": { "version": "10.19.3", "resolved": "https://registry.npmjs.org/preact/-/preact-10.19.3.tgz", @@ -68293,7 +68267,6 @@ "version": "file:packages/interactivity", "requires": { "@preact/signals": "^1.2.2", - "deepsignal": "^1.4.0", "preact": "^10.19.3" }, "dependencies": { @@ -68305,11 +68278,6 @@ "@preact/signals-core": "^1.4.0" } }, - "deepsignal": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/deepsignal/-/deepsignal-1.4.0.tgz", - "integrity": "sha512-x0XUMT48s+xQRLc2fPFfxnYLCJ46vffw47OQ5NcHFzacOjfW5eA0NrEmI0bhQHL6MgUHkBVT4TIiWTVwzTEwpg==" - }, "preact": { "version": "10.19.3", "resolved": "https://registry.npmjs.org/preact/-/preact-10.19.3.tgz", diff --git a/packages/e2e-tests/plugins/interactive-blocks/directive-each/render.php b/packages/e2e-tests/plugins/interactive-blocks/directive-each/render.php index 27fd6c7d17293..47eb351d837e7 100644 --- a/packages/e2e-tests/plugins/interactive-blocks/directive-each/render.php +++ b/packages/e2e-tests/plugins/interactive-blocks/directive-each/render.php @@ -220,14 +220,14 @@
-