-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(signals): add a way to set symbol as key (#4665)
* feat(signals): add a way to set symbol as key * chore: use weakset * chore: update readme * chore: address feedback * chore: update docs * chore: add optional * chore: address feedback * chore: fix missing renames * chore: fix isomorphic test
- Loading branch information
Showing
14 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/@lwc/integration-karma/test/signal/protocol/x/signal/signal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/@lwc/integration-karma/test/signal/reactivity/x/signal/signal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
import { vi } from 'vitest'; | ||
|
||
describe('signals', () => { | ||
let setTrustedSignalSet: (signals: WeakSet<object>) => void; | ||
let addTrustedSignal: (signal: object) => void; | ||
let isTrustedSignal: (target: object) => boolean; | ||
|
||
beforeEach(async () => { | ||
vi.resetModules(); | ||
const signalsModule = await import('../signals'); | ||
setTrustedSignalSet = signalsModule.setTrustedSignalSet; | ||
addTrustedSignal = signalsModule.addTrustedSignal; | ||
isTrustedSignal = signalsModule.isTrustedSignal; | ||
}); | ||
|
||
describe('setTrustedSignalSet', () => { | ||
it('should throw an error if trustedSignals is already set', () => { | ||
setTrustedSignalSet(new WeakSet()); | ||
expect(() => setTrustedSignalSet(new WeakSet())).toThrow( | ||
'Trusted Signal Set is already set!' | ||
); | ||
}); | ||
}); | ||
|
||
describe('addTrustedSignal', () => { | ||
it('should add a signal to the trustedSignals set', () => { | ||
const mockWeakSet = new WeakSet(); | ||
setTrustedSignalSet(mockWeakSet); | ||
const signal = {}; | ||
addTrustedSignal(signal); | ||
expect(isTrustedSignal(signal)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('isTrustedSignal', () => { | ||
it('should return true for a trusted signal', () => { | ||
const mockWeakSet = new WeakSet(); | ||
setTrustedSignalSet(mockWeakSet); | ||
const signal = {}; | ||
addTrustedSignal(signal); | ||
expect(isTrustedSignal(signal)).toBe(true); | ||
}); | ||
|
||
it('should return false for an untrusted signal', () => { | ||
const mockWeakSet = new WeakSet(); | ||
setTrustedSignalSet(mockWeakSet); | ||
expect(isTrustedSignal({})).toBe(false); | ||
}); | ||
|
||
it('should return true for all calls when trustedSignals is not set', () => { | ||
expect(isTrustedSignal({})).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
import { isFalse } from './assert'; | ||
|
||
let trustedSignals: WeakSet<object>; | ||
|
||
export function setTrustedSignalSet(signals: WeakSet<object>) { | ||
isFalse(trustedSignals, 'Trusted Signal Set is already set!'); | ||
|
||
trustedSignals = signals; | ||
} | ||
|
||
export function addTrustedSignal(signal: object) { | ||
// This should be a no-op when the trustedSignals set isn't set by runtime | ||
trustedSignals?.add(signal); | ||
} | ||
|
||
export function isTrustedSignal(target: object): boolean { | ||
if (!trustedSignals) { | ||
// The runtime didn't set a trustedSignals set | ||
// this check should only be performed for runtimes that care about filtering signals to track | ||
// our default behavior should be to track all signals | ||
return true; | ||
} | ||
return trustedSignals.has(target); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,5 +40,8 @@ | |
] | ||
} | ||
} | ||
}, | ||
"devDependencies": { | ||
"@lwc/shared": "8.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters